//
//  WorkFlowSearchDetailVC.m
//  smartRhino
//
//  Created by tederen on 2019/11/5.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "WorkFlowSearchDetailVC.h"
#import "InfoSearchView.h"
#import "MyApprovalModel.h"
#import "MyApprovalBCell.h"
#import "MyApprovalPageDetail.h"
@interface WorkFlowSearchDetailVC ()<InfoSearchViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) InfoSearchView *topNavSearch;
@property (nonatomic, strong) UIView *myBackImageView;
@property (nonatomic, strong) TDTableView *mytableView;
@property (nonatomic, strong) NSMutableArray <MyApprovalModel*>*waitApprovalDataArray;
@property (nonatomic, strong) NSMutableArray <MyApprovalModel*>*approvalDataArray;
@property (nonatomic, assign) NSInteger isfreshFlag;
@end

@implementation WorkFlowSearchDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isfreshFlag = 1;
    [self.view addSubview:self.myBackImageView];
    [self.view addSubview:self.mytableView];
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = UIColorHex(EEEEEE);
    [self.view addSubview:self.topNavSearch];
    [self.mytableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(kNavigationHeight);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    
    self.apprvovalType = WaitApprovalType;
    [self addNoDataMessageToViewCenter:self.mytableView message:@"暂无审批内容"];
}
#pragma mark -  数据加载
- (void)goDetailController:(NSString *)search{
    [self.topNavSearch setNowSearchData:search];
    SHOWLOADING
    [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,WorkFlowAddApproval_Post] parameters:@{@"Keyword":search,@"PerPage":@(10),@"Page":@(1),@"Type":self.typeString} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary *dic = responseObject;
        id result = dic[@"Items"];
        NSMutableArray *array = [NSMutableArray array];
        if (result == [NSNull null]) {
        }else {
            NSArray *arr = result;
            for (NSDictionary *dic2 in arr) {
                MyApprovalModel *model = [[MyApprovalModel alloc]initWithDictionary:dic2 error:nil];
                [array addObject:model];
            }
        }
        switch (self.apprvovalType) {
            case WaitApprovalType:{
                if (self.isfreshFlag == 1){
                    self.waitApprovalDataArray = array;
                }else{
                    if (array.count == 0){
                    }else{
                        [self.waitApprovalDataArray addObjectsFromArray:array];
                    }
                }
            }break;
            case ApprovalType:{
                if (self.isfreshFlag == 1){
                    self.approvalDataArray = array;
                }else{
                    if (array.count == 0){
                    }else{
                        [self.approvalDataArray addObjectsFromArray:array];
                    }
                }
            }break;
        }
        [self changeMessageStatusWithNumber:array.count];
        [self.mytableView.mj_header endRefreshing];
        [self.mytableView.mj_footer endRefreshing];
        [self.mytableView reloadData];
        REMOVESHOW
    } failure:^(NSError * _Nonnull error) {
        SHOWERROR([ZYCTool handerResultData:error])
    }];
}

#pragma mark - InfoSearchViewDelegate
- (void)searchButtonAction:(UISearchBar *)searchBar {
    [self goDetailController:searchBar.text];
}
- (ApprovalPageSelectType)apprvovalType{
    return 0;
}
#pragma mark - setter
- (InfoSearchView *)topNavSearch {
    if (!_topNavSearch) {
        _topNavSearch = [[InfoSearchView alloc] initWithFrame:CGRectMake(0, (IS_IPHONEX?40:20), kGXScreenWidth, 45) type:InfoSearchViewTypeCancel];
        _topNavSearch.delegate = self;
        WEAKSELF
        _topNavSearch.backBlock = ^{
            STRONGSELF
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
        [_topNavSearch showLineView];
               
        _topNavSearch.cancelBlock = ^{
            STRONGSELF
            [strongSelf.navigationController popViewControllerAnimated:YES];
           
        };
        
    }
    return _topNavSearch;
}
- (UIView *)myBackImageView{
    if (!_myBackImageView) {
        _myBackImageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kGXScreenHeigh, kNavigationHeight)];
        _myBackImageView.backgroundColor = [UIColor whiteColor];
    }
    return _myBackImageView;
}
#pragma mark - setter
- (TDTableView *)mytableView{
    if (!_mytableView) {
        _mytableView = [[TDTableView alloc]init];
        _mytableView.backgroundColor = UIColorHex(0xeeeeee);
        _mytableView.delegate = self;
        _mytableView.dataSource = self;
        [_mytableView registerNib:[UINib nibWithNibName:@"MyApprovalBCell" bundle:nil] forCellReuseIdentifier:@"MyApprovalBCell"];
    }
    return _mytableView;
}
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (self.apprvovalType) {
        case WaitApprovalType:
            return self.waitApprovalDataArray.count;
            break;
        case ApprovalType:
            return self.approvalDataArray.count;
            break;
        default:
            break;
    }
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyApprovalModel *model;
    switch (self.apprvovalType) {
        case WaitApprovalType:
            model = self.waitApprovalDataArray[indexPath.row];
            break;
        case ApprovalType:
            model = self.approvalDataArray[indexPath.row];
            break;
    }
    
    NSString *cellIdentifier = @"MyApprovalBCell";
    MyApprovalBCell *cell = (MyApprovalBCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    [cell loadApprovalData:model];
    cell.falgLab.hidden = YES;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 159.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyApprovalModel *model;
    switch (self.apprvovalType) {
        case WaitApprovalType:
            model = self.waitApprovalDataArray[indexPath.row];
            break;
        case ApprovalType:
            model = self.approvalDataArray[indexPath.row];
            break;
    }
    if (model.Id > 0) {
        MyApprovalPageDetail * myApprovalPageDetail = [[MyApprovalPageDetail alloc]init];
        myApprovalPageDetail.pageType = self.pageType;
        myApprovalPageDetail.indexId = model.Id;
        myApprovalPageDetail.title = model.Title;
        myApprovalPageDetail.TodoId = model.TodoId;
        myApprovalPageDetail.userId = model.UserId;
        [self.navigationController pushViewController:myApprovalPageDetail animated:YES];
    }
}
- (NSMutableArray <MyApprovalModel *>*)approvalDataArray{
    if (!_approvalDataArray) {
        _approvalDataArray = [NSMutableArray array];
    }
    return _approvalDataArray;
    
}
- (NSMutableArray <MyApprovalModel *>*)waitApprovalDataArray{
    if (!_waitApprovalDataArray) {
        _waitApprovalDataArray  = [NSMutableArray array];
    }
    return _waitApprovalDataArray;
}
@end