//
//  ZCEvaluateDetailViewController.m
//  UniversalApp
//
//  Created by 凯朱 on 2019/6/9.
//  Copyright © 2019 徐阳. All rights reserved.
//

#import "ZCEvaluateDetailViewController.h"
#import "ZCEvaluateDetailCell.h"
#import "ZCTextAreaCell.h"
#import "ZCMineService.h"
static  NSString *const kCellId = @"ZCEvaluateDetailCell";
static  NSString *const kTextAreaCellId = @"ZCTextAreaCell";
@interface ZCEvaluateDetailViewController ()<UITableViewDelegate,UITableViewDataSource,ZCTextAreaCellDelegate>
{
    NSString *_replayStr;
}
/**列表*/
@property (nonatomic, strong)UITableView *myTableView;

@end

@implementation ZCEvaluateDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}

-(void)setUpUI{
    [self setTitle:@"评价详情" fontSize:20];
    [self.view addSubview:self.myTableView];
}

/**
 获取数据
 */
-(void)loadData{
    NSDictionary *params = @{@"thought_id":_model.thought_id};
    [ZCMineService postMerchantRecordDetailWithParams:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        [ZCTool hideHudOnKeyWindow];
        kShowToast(message);
        if (isSuccessed == NO){
            return;
        }
        
        _model = [ZCEvaluateListModel modelWithDictionary:result[@"data"]];
        [_myTableView reloadData];
        [self refreshFootView];
        if (self.delegate && [self.delegate respondsToSelector:@selector(zcEvaluateDetailViewControllerReplaySuccess)]) {
            [self.delegate zcEvaluateDetailViewControllerReplaySuccess];
        }
    }];
}

/**
 输入文字
 */
-(void)zcTextAreaCellDidChange:(UITextView *)textView indexPath:(NSIndexPath *)indexPath{
    _replayStr = textView.text;
}

/**
 提交回复
 */
-(void)submitAction{
    
    if (_replayStr.length == 0) {
        [ZCTool showToastOnKeyWindowWith:@"请输入回复内容"];
        return;
    }
    
    NSDictionary *params = @{@"thought_id":_model.thought_id,
                             @"merchant_repay":kSTRING(_replayStr)};
    [ZCTool showHudOnKeyWindow];
    [ZCMineService postRecordReplayWithParams:params finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        kShowToast(message);
        if (isSuccessed == NO){
            [ZCTool hideHudOnKeyWindow];
            return;
        }
        
        //回复完成回调
        [self loadData];
    }];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCEvaluateDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    [cell paddingWithModel:_model];
    return cell;
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ZCEvaluateDetailCell cellHeightWithModel:_model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

/**
 header实现
 */
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, KScreenWidth, 0.1);
        headview.backgroundColor = KWhiteColor;
    }
    return headview;
}

/**
 footer实现
 */
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, KScreenWidth, 10);
        footerView.contentView.backgroundColor = KBgColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,KScreenWidth , KScreenHeight-SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.backgroundColor = KBgColor;
        
        [_myTableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        [self refreshFootView];
    }
    return _myTableView;
}

/**
 底部视图
 */
-(void)refreshFootView{
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)];
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 100, 23)];
    titleLbl.text = @"回复";
    titleLbl.textColor = KColor_1;
    titleLbl.font = kFontMedium(16);
    [footView addSubview:titleLbl];
    footView.backgroundColor = KWhiteColor;
    
    //已回复
    if ([_model.is_message isEqualToString:@"2"]) {
        
        UILabel *descLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 35+9, KScreenWidth-40, 0)];
        descLbl.text = _model.merchant_repay;
        descLbl.numberOfLines = 0;
        [descLbl sizeToFit];
        descLbl.textColor = KColor_1;
        descLbl.font = kFont(13);
        [footView addSubview:descLbl];
        footView.height = MAX(44+descLbl.height+20, 96);
    }else{
        ZCTextAreaCell *textArea = [ZCTextAreaCell viewFromXIB];
        textArea.delegate = self;
        ZCPublicCellModel *model = [[ZCPublicCellModel alloc] init];
        model.limit = @"200";
        model.toast = @"请输入回复内容";
        model.placeholder = @"快来给你商品的评价添加回复吧!";
        textArea.frame = CGRectMake(0,35+9, KScreenWidth, 111);
        [textArea paddingDataWithPublicCellModel:model];
        [footView addSubview:textArea];
        UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        submitBtn.frame = CGRectMake(20, 170,kScreenWidth-40, 46);
        submitBtn.backgroundColor = KColor_O;
        [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        submitBtn.titleLabel.font = kFont(15);
        submitBtn.layer.cornerRadius = 23;
        submitBtn.layer.masksToBounds = YES;
        [submitBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        [submitBtn addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:submitBtn];
        
        footView.height = 216+40;
    }
    _myTableView.tableFooterView = footView;
}

@end