//
//  ApplyStateQueryCell.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2020/4/16.
//  Copyright © 2020 雷俊博. All rights reserved.
//

#import "ApplyStateQueryCell.h"
#import "ApplyStateLineTableViewCell.h"
#import "ApplyRefuseCell.h"

@interface ApplyStateQueryCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)NSArray* dataArray;
@property (weak, nonatomic) IBOutlet UILabel *lb_name;
@property (weak, nonatomic) IBOutlet UILabel *lb_Id;

@property (weak, nonatomic) IBOutlet UILabel *lb_state;




@end

@implementation ApplyStateQueryCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.innerView.backgroundColor = K_MainColor;
    self.mainView.backgroundColor = K_MainColor;
    self.tableView.backgroundColor = K_MainColor;
    self.innerView.layer.cornerRadius = 8 * WIDTHRADIU;
    
    self.innerView.layer.masksToBounds = YES;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ApplyStateLineTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ApplyRefuseCell" bundle:nil] forCellReuseIdentifier:@"ApplyRefuseCell"];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        ApplayDateModel*model = self.dataArray.lastObject;
        if (![Tools isBlankString:model.examineRemark]) {
            return self.dataArray.count + 1;
        }
    }
    
    return self.dataArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (self.dataArray.count > 0) {
        ApplayDateModel*model = self.dataArray.lastObject;
        if (![Tools isBlankString:model.examineRemark]) {
            if (indexPath.row < self.dataArray.count) {
                ApplyStateLineTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                
                if (indexPath.row == 0) {
                    cell.topLine.hidden = YES;
                    cell.bottomLine.hidden = NO;
                }else if (indexPath.row == (self.dataArray.count - 1)){
                    cell.topLine.hidden = NO;
                    cell.bottomLine.hidden = YES;
                }else{
                    cell.topLine.hidden = NO;
                    cell.bottomLine.hidden = NO;
                }
                
                ApplayDateModel*model = self.dataArray[indexPath.row];
                cell.lb_content.text = [NSString stringWithFormat:@"%@    %@",[Tools ValidblankString:model.role],[Tools ValidblankString:model.examineState]];
                cell.lb_date.text = [Tools ValidblankString:model.examineTime];
                
                
                return cell;
            }else{
                ApplyRefuseCell*cell = [tableView dequeueReusableCellWithIdentifier:@"ApplyRefuseCell"];
                
                cell.lb_content.text = model.examineRemark;
                
                cell.lb_content.textColor = [UIColor colorWithHex:0xe9e53c];
                
                return cell;
            }
        }
    }
    
    
    ApplyStateLineTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (indexPath.row == 0) {
        cell.topLine.hidden = YES;
        cell.bottomLine.hidden = NO;
    }else if (indexPath.row == (self.dataArray.count - 1)){
        cell.topLine.hidden = NO;
        cell.bottomLine.hidden = YES;
    }else{
        cell.topLine.hidden = NO;
        cell.bottomLine.hidden = NO;
    }
    
    ApplayDateModel*model = self.dataArray[indexPath.row];
    cell.lb_content.text = [NSString stringWithFormat:@"%@    %@",[Tools ValidblankString:model.role],[Tools ValidblankString:model.examineState]];
    cell.lb_date.text = [Tools ValidblankString:model.examineTime];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.dataArray.count > 0) {
        ApplayDateModel*model = self.dataArray.lastObject;
        if (![Tools isBlankString:model.examineRemark] && indexPath.row == self.dataArray.count) {
            return 40;
        }
    }
    
    return 60;
}


- (void)setModel:(ApplayStateModel *)model
{
    self.dataArray = model.progresses;
    NSString*idStr = [EnercryTool AES256Decrypt:[Tools ValidblankString:model.identityNumber] key:MZAESKEY];
    self.lb_Id.text = [Tools isBlankString:model.identityNumber] ?@"" :[NSString stringWithFormat:@"%@****%@",[idStr substringToIndex:4],[idStr substringFromIndex:idStr.length - 4]];
    self.lb_name.text = [EnercryTool AES256Decrypt:[Tools ValidblankString:model.personName] key:MZAESKEY];
    self.lb_state.text = [Tools ValidblankString:model.dataState];
    
    [self.tableView reloadData];
}

#pragma mark - event response
- (IBAction)btnClick:(UIButton *)sender {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(btnClickWithCell:)]) {
        [self.delegate btnClickWithCell:self];
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
