//
//  CommonWorkVC.m
//  smartRhino
//
//  Created by niuzhen on 2020/4/28.
//  Copyright © 2020 tederen. All rights reserved.
//

#import "CommonWorkVC.h"
#import "WorkFLowModel.h"
#import "WorkFLowCell.h"
#import "myApprovalPageVC.h"
#import "WorkFlowTabbarController.h"
#import "MyWorkWordVC.h"
#import "CountDataVC.h"
#import "CommonToolMoveVC.h"

@interface CommonWorkVC ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIButton *setBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

/// 协同工作
@property (nonatomic, strong) NSMutableArray<WorkFLowModel*> *dataArray;

/// 数据报表
@property (nonatomic, strong) NSMutableArray <WorkFLowModel*>*dataReportArray;


@property (nonatomic, strong) WorkNumberModel *numberDic;
@property (nonatomic,assign) NSInteger loadFlag;
@end

@implementation CommonWorkVC
+(CommonWorkVC *)initCommonWorkVC{
    CommonWorkVC *controller = [StoryboardManager.shared.Common instantiateViewControllerWithIdentifier:@"CommonWorkVC"];
    return controller;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.view setBackgroundColor:UIColorHex(0xf7f7f7)];
    [self createCollectionView];
    self.loadFlag = 1;
    WS(weakSelf);
    [self.setBtn setAction:^{
        CommonToolMoveVC * vc = [CommonToolMoveVC initCommonToolMoveVC];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

#pragma mark - 数据加载
- (void)getData{
    [self.dataArray removeAllObjects];
    [self.dataReportArray removeAllObjects];
    [self testData];
    self.loadFlag = 1;
    [[HttpManager sharedHttpManager] GETUrl: [NSString stringWithFormat:@"%@%@",BaseUrl,WorkFlowHomeData_Post]parameters:@{} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        NSArray *array = responseObject;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary  *dic in array) {
            WorkFLowModel *mode = [[WorkFLowModel alloc] initWithDictionary:dic error:nil];
            [self.dataArray addObject:mode];
        }
        [self.dataArray addObjectsFromArray:arr];
        self.loadFlag += 1;
        if (self.loadFlag == 4) {
            REMOVESHOW
            [self.collectionView reloadData];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
    
    //获取我审批和我发起以及抄送我的未读数
    [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,@"/api/app/user/user-uread-count"] parameters:@{} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.numberDic = [[WorkNumberModel alloc] initWithDictionary:dic error:nil];
        self.navigationController.tabBarItem.badgeValue = [self.numberDic allNumber];
        /// 我的审批未读数
        self.dataArray.firstObject.workNumber = [dic[@"FlowAuditUReadCount"] integerValue];
        for (WorkFLowModel *model in self.dataArray) {
            if ([model.Name isEqualToString:@"我发起的"]) {
                model.workNumber = [dic[@"FlowSendUReadCount"] integerValue];
            }
            if ([model.Name isEqualToString:@"抄送我的"]) {
                model.workNumber = [dic[@"FlowCcUReadCount"] integerValue];
            }
        }
        self.loadFlag += 1;
        if (self.loadFlag == 4) {
            REMOVESHOW
            [self.collectionView reloadData];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
    [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,WorkFlowHomeData2_Post] parameters:@{} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        NSArray *array = responseObject;
        NSLog(@"%@",responseObject);
        for (NSDictionary  *dic in array) {
            WorkFLowModel *mode = [[WorkFLowModel alloc]initWithDictionary:dic error:nil];
            [self.dataReportArray addObject:mode];
        }
        self.loadFlag += 1;
        if (self.loadFlag == 4) {
            REMOVESHOW
            [self.collectionView reloadData];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)testData{
    WorkFLowModel *mode1 = [[WorkFLowModel alloc] init];
    mode1.Name = @"我的审批";
    mode1.iconImage = IMG(@"work_flow_approvalme");
    mode1.workNumber = 0;
    [self.dataArray addObject:mode1];
    
    WorkFLowModel *mode2 = [[WorkFLowModel alloc] init];
    mode2.Name = @"我发起的";
    mode2.iconImage = IMG(@"work_flow_fromme");
    mode2.workNumber = 0;
    [self.dataArray addObject:mode2];
    
    WorkFLowModel *mode3 = [[WorkFLowModel alloc] init];
    mode3.Name = @"抄送我的";
    mode3.iconImage = IMG(@"work_flow_forme");
    mode3.workNumber = 0;
    [self.dataArray addObject:mode3];
    
 
 

    
//
//    WorkFLowModel *mode13 = [[WorkFLowModel alloc] init];
//    mode13.Name = @"财务数据";
//    mode13.iconImage = IMG(@"work_flow_money_data");
//    mode13.workNumber = 0;
//    [self.dataReportArray addObject:mode13];
//
//    WorkFLowModel *mode14 = [[WorkFLowModel alloc] init];
//    mode14.Name = @"出版数据";
//    mode14.iconImage = IMG(@"work_flow_version_data");
//    mode14.workNumber = 0;
//    [self.dataReportArray addObject:mode14];
//
//    WorkFLowModel *mode15 = [[WorkFLowModel alloc] init];
//    mode15.Name = @"人事数据";
//    mode15.iconImage = IMG(@"work_flow_hr_data");
//    mode15.workNumber = 0;
//    [self.dataReportArray addObject:mode15];
    

    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.dataArray.count;
    }else{
        return self.dataReportArray.count;
    }
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//       return CGSizeMake(kGXScreenWidth-14, 39);
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//    return CGSizeMake(kGXScreenWidth-14, 20);
//}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

       return CGSizeMake((kGXScreenWidth -14)/3 ,(kGXScreenWidth -14)/3);

}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

        return UIEdgeInsetsMake(0, 0, 0, 0);

}

//func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
//    return UIEdgeInsetsZero
//}
//
//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
//    return 1
//}
//
//func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
//    return 5
//}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WorkFLowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WorkFLowCellID" forIndexPath:indexPath];
    WorkFLowModel *model = (indexPath.section == 0) ? self.dataArray[indexPath.item]:self.dataReportArray[indexPath.item];
    if (model.workNumber > 0) {
        NSString * count = [NSString stringWithFormat:@"%ld",(long)model.workNumber];
        UILabel * label = [UILabel new];
        label.font = [UIFont systemFontOfSize:12];
        label.text = count;
        CGFloat width = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH, 16)].width;
        cell.workNumberLab.layer.cornerRadius = 8;
        cell.workNumberLab.layer.masksToBounds = YES;
        if ((width + 5) < 16) {
            cell.width.constant = 16;
        }else{
            cell.width.constant = width + 8;
        }
        cell.workNumberLab.text = count;
    }
    if (model.workNumber == 0 ) {
        cell.workNumberLab.hidden = YES;
        cell.workNumberLab.layer.cornerRadius = 10;
        cell.workNumberLab.layer.masksToBounds = YES;
    }else{
        cell.workNumberLab.hidden = NO;
    }
    cell.titleLabel.text = model.Name;
    if (indexPath.item == 0 || indexPath.item == 1 || indexPath.item == 2) {
        cell.iconImageView.image = model.iconImage;
    }else{
        [cell.iconImageView setImageWithURL:[NSURL URLWithString:model.IconUrl] placeholder:IMG(@"work_flow_approvalme")];
    }
    cell.leftLine.hidden = NO;
    cell.rightLine.hidden = NO;
    cell.bottomLine.hidden= NO;
    cell.topLine.hidden = NO;
    if (indexPath.section == 1) {
        cell.titleLabel.text = model.Title;
        [cell.iconImageView setImageWithURL:[NSURL URLWithString:model.IocUrl] placeholder:IMG(@"work_flow_approvalme")];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WorkFLowModel *model = (indexPath.section == 0) ? self.dataArray[indexPath.item]:self.dataReportArray[indexPath.item];
    
    if (indexPath.section == 0) {
        if (indexPath.item == 0 || indexPath.item == 1 || indexPath.item == 2) {
               MyApprovalPageVC *myApprovalPageVC = [[MyApprovalPageVC alloc]init];
               myApprovalPageVC.title =   model.Name;
            if (indexPath.item == 0) {
                myApprovalPageVC.pageType = Type_ONEA;
            }
            if (indexPath.item == 1) {
                myApprovalPageVC.pageType = Type_ONEB;
            }
            if (indexPath.item == 2) {
                myApprovalPageVC.pageType = Type_ONEC;
            }
            myApprovalPageVC.numberDic = self.numberDic;
            [self.navigationController pushViewController:myApprovalPageVC animated:YES];
        }else{
            if ([model.Name isEqualToString:@"会议"]) {
                [self.navigationController pushViewController:[[WorkFlowTabbarController alloc] init] animated:YES];
//            }else if ([model.Name isEqualToString:@"车辆"]){
//                MyWorkCarVC *vc = [[MyWorkCarVC alloc]init];
//                vc.title = model.Name;
//                vc.indexID = model.Id;
//                [self.navigationController pushViewController:vc animated:YES];
            } else {
                MyWorkWordVC *workWordVC = [[MyWorkWordVC alloc]init];
                workWordVC.title = model.Name;
                workWordVC.pageType = Type_GongWen;
                workWordVC.indexID = model.Id;
                [self.navigationController pushViewController:workWordVC animated:YES];
            }
        }
    }
    if (indexPath.section == 1) {
                WorkFLowCell *cell = (WorkFLowCell *)[collectionView cellForItemAtIndexPath:indexPath];
                if ([cell.titleLabel.text isEqualToString:@"财务统计"]) {
                    CountDataVC *vc = [CountDataVC initCountDataVC];
                    vc.index = 0;
                    vc.titleStr = @"财务统计";
                    [self.navigationController pushViewController:vc animated:YES];
                }
                if ([cell.titleLabel.text isEqualToString:@"人事统计"]) {
                    CountDataVC *vc = [CountDataVC initCountDataVC];
                    vc.index = 1;
                    vc.titleStr = @"人事统计";
                    [self.navigationController pushViewController:vc animated:YES];
                }
                if ([cell.titleLabel.text isEqualToString:@"出版统计"]) {
                    CountDataVC *vc = [CountDataVC initCountDataVC];
                    vc.index = 2;
                    vc.titleStr = @"出版统计";
                    [self.navigationController pushViewController:vc animated:YES];
                }
//        WorkFLowCell *cell = (WorkFLowCell *)[collectionView cellForItemAtIndexPath:indexPath];
//        if ([cell.titleLabel.text isEqualToString:@"财务统计"]) {
//            VersionDataVC *vc = [VersionDataVC initVersionDataVC];
//            vc.pageType = 1;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//        if ([cell.titleLabel.text isEqualToString:@"人事统计"]) {
//            VersionDataVC *vc = [VersionDataVC initVersionData3VC];
//            vc.pageType = 3;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//        if ([cell.titleLabel.text isEqualToString:@"出版统计"]) {
//            VersionDataVC *vc = [VersionDataVC initVersionData2VC];
//            vc.pageType = 2;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
    }
}
#pragma mark - Setter

- (NSMutableArray<WorkFLowModel*> *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}
- (NSMutableArray<WorkFLowModel *> *)dataReportArray{
    if (!_dataReportArray) {
        _dataReportArray = [NSMutableArray new];
    }
    return _dataReportArray;
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.layer.cornerRadius = 2;
    self.collectionView.layer.masksToBounds = YES;
    self.collectionView.backgroundColor = UIColorHex(F7F7F7);
    [self.collectionView registerNib:[UINib nibWithNibName:@"WorkFLowCell" bundle:nil] forCellWithReuseIdentifier:@"WorkFLowCellID"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"MyChanelCollectionReusableViewTop" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyChanelCollectionReusableViewTop"];
     [self.collectionView registerNib:[UINib nibWithNibName:@"FootCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FootCollectionReusableView"];
    self.collectionView.backgroundColor = [UIColor whiteColor];

    [self.collectionView setShowsVerticalScrollIndicator:NO];
    [self.collectionView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:self.collectionView];
    
     self.collectionView.backgroundColor = UIColorHex(F7F7F7);
   
}

@end