//
//  TaskPointViewController.m
//  Macalline
//
//  Created by JianjiYuan on 14-12-4.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "SATaskDetailViewController.h"
#import "SATaskCheckViewController.h"
#import "CMPopTipView.h"
#import "SATaskReviewViewController.h"
#import "TaskListView.h"
#import "ListPartView.h"

@interface SATaskDetailViewController ()< CMPopTipViewDelegate,UIGestureRecognizerDelegate >
{
    CMPopTipView *popTipView;
    UIButton* btnSheng;
    UIButton* btnShi;
    UILabel *lblStatusName;
    
    UILabel* lblTaskName;
    UILabel* lblAreaName;
    UILabel* lblendTime;
    UILabel* lblStatus;
    UILabel* lblSupportPeople;
}
@property (nonatomic,strong) UIButton* btnCheckTask;
@property (nonatomic,strong) UIButton* btnCheckList;
@property (nonatomic, strong) UIView *descView;
@property (nonatomic, strong) NSString *checkPointStatusType;
@property (nonatomic, strong) NSMutableArray *partContentArr;
@property(nonatomic,strong) TaskListView* view1;
@property(nonatomic,strong) ListPartView* view2;

@property (nonatomic, strong) NSMutableArray *arrTaskArr;
@property (nonatomic, strong) NSMutableArray *arrTaskHistoryArr;

@property(nonatomic,strong) NSDictionary* resultDic;

@property (nonatomic, strong) NSString *taskStatus;

@property (nonatomic, strong) NSString *taskIsFinesh;
@end

@interface SATaskPointDetailCell : UITableViewCell

@property (nonatomic, strong) UILabel *lblPoint;
@property (nonatomic, strong) UILabel *lblQy;
@property (nonatomic, strong) UILabel *lblCanSelecte;
@property (nonatomic, strong) UILabel *lblStatus;
@property (nonatomic, strong) UIImageView *imageVArrow;
@property (nonatomic, strong) UIImageView *imageVUser;

@end

@implementation SATaskDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *areaName = nil;
    switch ([self.areaId intValue]) {
        case 1: areaName = @"外广场"; break;
        case 2: areaName = @"商场大堂"; break;
        case 3: areaName = @"中庭"; break;
        case 4: areaName = @"过道"; break;
        case 5: areaName = @"其他"; break;
        default:
            break;
    }
    self.navigationItem.title = @"任务详情";
    self.taskIsFinesh = @"";
    _checkPointStatusType = @"0";
    
    _descView = [[UIView alloc] init];
    _descView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_descView];
    [_descView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [_descView autoSetDimension:ALDimensionHeight toSize:95];
    

    
    UIView* areaNameView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 95)];
    areaNameView.backgroundColor = [UIColor colorWithRed:0.1569 green:0.6235 blue:0.7216 alpha:1];
    [_descView addSubview:areaNameView];
    
    lblTaskName = [[UILabel alloc] initWithFrame:CGRectMake(10, 4, 300, 20)];
    lblTaskName.font = [UIFont systemFontOfSize:15];
    lblTaskName.textColor = [UIColor whiteColor];
    lblTaskName.text = @"2015年6月第二季会员季";
    [areaNameView addSubview:lblTaskName];
    
    UIImageView* locationImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 31, 12, 18)];
    locationImage.image = [UIImage imageNamed:@"Task/icon_site_white.png"];
    [areaNameView addSubview:locationImage];
    
    lblAreaName = [[UILabel alloc] initWithFrame:CGRectMake(26, 30, 300, 20)];
    lblAreaName.font = [UIFont systemFontOfSize:13];
    lblAreaName.textColor = [UIColor whiteColor];
    lblAreaName.text = @"无锡锡山商场";
    [areaNameView addSubview:lblAreaName];
    
    lblendTime = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 130, 20)];
    lblendTime.font = [UIFont systemFontOfSize:13];
    lblendTime.textColor = [UIColor whiteColor];
    lblendTime.text = @"2015-06-05 活动结束";
    [areaNameView addSubview:lblendTime];
    
    lblStatus = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width-(self.view.bounds.size.width/3.5), 55, self.view.bounds.size.width/3.5, 20)];
    lblStatus.backgroundColor = RGBColor(92, 202, 201, 1);
    lblStatus.font = [UIFont systemFontOfSize:12];
    lblStatus.textColor = [UIColor whiteColor];
    lblStatus.textAlignment = NSTextAlignmentCenter;
    lblStatus.text = @"任务状态:审核通过";
    [areaNameView addSubview:lblStatus];
    
    lblSupportPeople = [[UILabel alloc] initWithFrame:CGRectMake(150, 55, 100, 20)];
    lblSupportPeople.font = [UIFont systemFontOfSize:12];
    lblSupportPeople.textColor = [UIColor whiteColor];
    lblSupportPeople.text = @"上 报 人:王林";
    [areaNameView addSubview:lblSupportPeople];
    
    self.tableVContent.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setViewLayoutConstraint];
    
    UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnHome setBackgroundImage:[UIImage imageNamed:@"Task/icon_filter1.png"] forState:UIControlStateNormal];
    [btnHome setFrame:CGRectMake(0, 0, 25, 25)];
    [btnHome addTarget:self action:@selector(btnHomeClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationController navbarRightItemCustomized:@[btnHome] onwer:self.navigationItem];
    
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setBackgroundImage:[UIImage imageNamed:@"NavBar/btn_nav_back.png"] forState:UIControlStateNormal];
    [btnBack sizeToFit];
    [btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationController navbarLeftItemCustomized:@[btnBack] onwer:self.navigationItem];
    
    //UIButton*
//    _btnCheckTask = [[UIButton alloc] init];
//    [_btnCheckTask setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [_btnCheckTask setTitle:@"审核任务" forState:UIControlStateNormal];
//    [_btnCheckTask setBackgroundColor:RGBColor(0, 171, 202, 1)];
//    [_btnCheckTask addTarget:self action:@selector(taskReviewClick) forControlEvents:UIControlEventTouchUpInside];
//    _btnCheckTask.titleLabel.font = [UIFont systemFontOfSize:16];
//    _btnCheckTask.layer.cornerRadius = 3;
//    [self.view addSubview:_btnCheckTask];
//    [_btnCheckTask autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view withOffset:-80];
//    [_btnCheckTask autoSetDimensionsToSize:CGSizeMake(140, 35)];
//    [_btnCheckTask autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:7];
    
    
    _btnCheckList = [[UIButton alloc] init];
    _btnCheckList.hidden = YES;
    [_btnCheckList setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btnCheckList setTitle:@"审核记录" forState:UIControlStateNormal];
    [_btnCheckList setBackgroundColor:RGBColor(0, 171, 202, 1)];
    [_btnCheckList addTarget:self action:@selector(selectTaskHistoryList) forControlEvents:UIControlEventTouchUpInside];
    _btnCheckList.titleLabel.font = [UIFont systemFontOfSize:18];
    _btnCheckList.layer.cornerRadius = 5;
    [self.view addSubview:_btnCheckList];
    //[_btnCheckList autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view withOffset:80];
    [_btnCheckList autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view withOffset:40];
    [_btnCheckList autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view withOffset:-40];
    [_btnCheckList autoSetDimensionsToSize:CGSizeMake(240, 35)];
    [_btnCheckList autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:7];
    
    //_btnCheckTask.hidden=NO;
    
    [self requestDatas];
    
}

-(void)taskReviewClick
{
    bool isUnSupport = YES;
    for (int i = 0; i < self.arrContents.count; i++) {
        NSString *status = self.arrContents[i][@"reviewStatus"];
        NSString *isRequire = self.arrContents[i][@"isRequire"];
        if ([isRequire isEqualToString:@"1"]) {
            if ([status isEqualToString:@"14001"]||[status isEqualToString:@"14004"]) {
                isUnSupport = NO;
                break;
            }
        }
    }
    if (isUnSupport) {
        if ([self.taskStatus isEqualToString:kShenhetongguocode]||[self.taskStatus isEqualToString:kZhenggaishenhetongguocode]) {
            [UIApplication showToastViewMessage:@"无法对已审核通过的任务再次审核"];
        }
        else
        {
            SATaskReviewViewController* vc = [[SATaskReviewViewController alloc] init];
            vc.taskStatus = _taskStatus;
            vc.pointType = TaskPointTypeArea;
            vc.taskId = self.taskId;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else
    {
        [UIApplication showToastViewMessage:@"有必选任务未上报，无法审核"];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
        [self requestDatas];
        [UIApplication saveObject:@NO type:Number key:kShouldRequestDataWhenBack];
    }
}

- (void)setViewLayoutConstraint
{
    [self.tableVContent autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 10, 0) excludingEdge:ALEdgeTop];
    [self.tableVContent autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_descView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)btnBack
{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:NSClassFromString(@"SATaskPointDetailViewController")]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

- (void)btnHomeClick
{
//    for (UIViewController *controller in self.navigationController.viewControllers) {
//        if ([controller isKindOfClass:NSClassFromString(@"TabBarViewController")]) {
//            [self.navigationController popToViewController:controller animated:YES];
//        }
//    }
    [self selectTaskListData];
}
-(void)selectTaskListData
{
    NSDictionary *dicParam = nil;
    
    dicParam = @{@"type":@"-1",@"status":@"-1",@"shopId":self.shopId};
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryTaskListEX";
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];

}

#pragma ---------------------   请求数据   -----------------------
-(void)selectTaskHistoryList
{
    NSDictionary *dicParam = dicParam = @{@"taskId":self.taskId};
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryReviewHistoryEX";
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
    
}
-(void)addTaskPartView
{
    NSArray* arr = @[@"未提交",@"未审核",@"审核通过",@"待整改",@"已整改",@"已整改审核通过"];
    _view2 = [[ListPartView alloc] initWithFrame:self.navigationController.view.bounds andArray:arr andType:@""];
    __weak typeof(SATaskDetailViewController) *weakSelf = self;
    _view2.BtnClickBlock = ^(int tag) {
        [weakSelf listBtnClickAtRow:tag];
    };
    UITapGestureRecognizer* tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
    tapGesture1.delegate = self;
    [_view2 addGestureRecognizer:tapGesture1];
    [self.navigationController.view addSubview:_view2];
}
-(void)listBtnClickAtRow:(int)rowNum
{
    self.partContentArr = [[NSMutableArray alloc] init];
    if (rowNum == 0) {
        _checkPointStatusType = @"0";
    }
    
    if (rowNum == 1) {
        _checkPointStatusType = @"1";
        for (int i = 0; i < self.arrContents.count; i++) {
            NSString* str = self.arrContents[i][@"reviewStatus"];
            if ([str isEqualToString:@"14001"]) {
                [self.partContentArr addObject:self.arrContents[i]];
            }
        }
    }
    if (rowNum == 2) {
        _checkPointStatusType = @"2";
        for (int i = 0; i < self.arrContents.count; i++) {
            NSString* str = self.arrContents[i][@"reviewStatus"];
            if ([str isEqualToString:@"14002"]) {
                [self.partContentArr addObject:self.arrContents[i]];
            }
        }
    }
    if (rowNum == 3) {
        _checkPointStatusType = @"3";
        for (int i = 0; i < self.arrContents.count; i++) {
            NSString* str = self.arrContents[i][@"reviewStatus"];
            if ([str isEqualToString:@"14003"]) {
                [self.partContentArr addObject:self.arrContents[i]];
            }
        }
    }
    if (rowNum == 4) {
        _checkPointStatusType = @"4";
        for (int i = 0; i < self.arrContents.count; i++) {
            NSString* str = self.arrContents[i][@"reviewStatus"];
            if ([str isEqualToString:@"14004"]) {
                [self.partContentArr addObject:self.arrContents[i]];
            }
        }
    }
    if (rowNum == 5) {
        _checkPointStatusType = @"4";
        for (int i = 0; i < self.arrContents.count; i++) {
            NSString* str = self.arrContents[i][@"reviewStatus"];
            if ([str isEqualToString:@"14005"]) {
                [self.partContentArr addObject:self.arrContents[i]];
            }
        }
    }
    if (rowNum == 6) {
        _checkPointStatusType = @"4";
        for (int i = 0; i < self.arrContents.count; i++) {
            NSString* str = self.arrContents[i][@"reviewStatus"];
            if ([str isEqualToString:@"14006"]) {
                [self.partContentArr addObject:self.arrContents[i]];
            }
        }
    }
    [_view2 removeFromSuperview];
    [self.tableVContent reloadData];
}


-(void)addTaskListView
{
    _view1 = [[TaskListView alloc] initWithFrame:self.navigationController.view.bounds andArray:self.arrTaskArr];
    __weak typeof(SATaskDetailViewController) *weakSelf = self;
        _view1.BtnClickBlock = ^(NSString* tag) {
            [weakSelf listBtnClick:tag];
        };
    UITapGestureRecognizer* tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
    
    tapGesture1.delegate = self;
    [_view1 addGestureRecognizer:tapGesture1];
    [self.navigationController.view addSubview:_view1];
}

-(void)addTaskHistoryListView
{
    _view1 = [[TaskListView alloc] initWithFrame:self.navigationController.view.bounds andHistoryArray1:self.arrTaskHistoryArr];
    
    UITapGestureRecognizer* tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
    
    tapGesture1.delegate = self;
    [_view1 addGestureRecognizer:tapGesture1];
    [self.navigationController.view addSubview:_view1];
}





-(void)Actiondo:(UITapGestureRecognizer*)sender
{
    [_view2 removeFromSuperview];
    
    [_view1 removeFromSuperview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view == _view1 || touch.view == _view2) {
        return YES;
        
    }
    return NO;
}

-(void)listBtnClick:(NSString*)Name
{
    self.taskId = Name;
    [self requestDatas];
    
    [_view1 removeFromSuperview];
}

- (void)requestDatas
{
    NSDictionary *dicParam = nil;
    
    dicParam = @{@"taskId":self.taskId,@"status":@"-1"};
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryCheckPointListEX";
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"queryCheckPointListEX"])
    {
         _resultDic = result;
        
        lblTaskName.text = result[@"activityName"];
        lblAreaName.text = result[@"shopName"];
        lblendTime.text = [NSString stringWithFormat:@"%@活动结束",result[@"activityEndDate"]];
        
        lblSupportPeople.text = [NSString stringWithFormat:@"%@ 上报",result[@"reportUser"]];
        
        _taskStatus = result[@"taskReviewStatus"];
        NSString* status = result[@"taskReviewStatus"];
        
        if ([self.taskIsFinesh isEqualToString:@""]) {
            if([status isEqualToString:kWeitijiaocode]||[status isEqualToString:kWeishenhecode]||[status isEqualToString:kDaizhenggaicode]||[status isEqualToString:kYizhenggaicode])
            {
                self.taskIsFinesh = @"NO";
            }
            else
            {
                self.taskIsFinesh = @"YES";
            }
        }
        else
        {
            if ([self.taskIsFinesh isEqualToString:@"YES"]) {
                if([status isEqualToString:kWeitijiaocode]||[status isEqualToString:kWeishenhecode]||[status isEqualToString:kDaizhenggaicode]||[status isEqualToString:kYizhenggaicode])
                {
                    [UIApplication saveObject:@YES type:Number key:kShouldRequestDataWhenBack];
                    self.taskIsFinesh = @"NO";
                }
                else
                {
                    self.taskIsFinesh = @"YES";
                }
            }
            else
            {
                if([status isEqualToString:kWeitijiaocode]||[status isEqualToString:kWeishenhecode]||[status isEqualToString:kDaizhenggaicode]||[status isEqualToString:kYizhenggaicode])
                {
                    self.taskIsFinesh = @"NO";
                }
                else
                {
                    [UIApplication saveObject:@YES type:Number key:kShouldRequestDataWhenBack];
                    self.taskIsFinesh = @"YES";
                }
            }
        }
        
        if ([status isEqualToString:kWeitijiaocode]) {
            lblStatus.text = [NSString stringWithFormat:@"%@",@"未提交"];
        }
        if ([status isEqualToString:kWeishenhecode]) {
            lblStatus.text = [NSString stringWithFormat:@"%@",@"未审核"];
        }
        if ([status isEqualToString:kShenhetongguocode]) {
            lblStatus.text = [NSString stringWithFormat:@"%@",@"审核通过"];
        }
        if ([status isEqualToString:kDaizhenggaicode]) {
            lblStatus.text = [NSString stringWithFormat:@"%@",@"待整改"];
        }
        if ([status isEqualToString:kYizhenggaicode]) {
            lblStatus.text = [NSString stringWithFormat:@"%@",@"已整改"];
        }
        if ([status isEqualToString:kZhenggaishenhetongguocode]) {
            lblStatus.text = [NSString stringWithFormat:@"%@",@"整改审核通过"];
        }
        
        self.arrContents = [result[@"checkPointList"] validDataOfClass:[NSArray class]];
        [self.tableVContent reloadData];
        
        if (self.arrContents.count == 0) {
            self.imageVNoResult.hidden = NO;
//            self.btnCheckTask.hidden=YES;
        }
    }
    
    if ([operation.userInfo[@"interface"] isEqualToString:@"queryTaskListEX"])
    {
        self.arrTaskArr = [result[@"taskList"] validDataOfClass:[NSArray class]];
        [self addTaskListView];
    }
    
    if ([operation.userInfo[@"interface"] isEqualToString:@"queryReviewHistoryEX"])
    {
        self.arrTaskHistoryArr = [result[@"historyList"] validDataOfClass:[NSArray class]];
        if (self.arrTaskHistoryArr.count==0) {
            [UIApplication showToastViewMessage:@"该任务没有审核记录"];
        }
        else
        {
            [self addTaskHistoryListView];
        }
        
    }
}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    
}

- (void)configureCell:(SATaskPointDetailCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    if ([_checkPointStatusType isEqualToString:@"0"])
    {
        cell.lblPoint.text = self.arrContents[indexPath.row][@"name"];
        
        cell.lblQy.text = self.arrContents[indexPath.row][@"typeDesc"];
        
        
        NSString *isRequire = self.arrContents[indexPath.row][@"isRequire"];
        if ([isRequire isEqualToString:@"0"]) {
            cell.lblCanSelecte.text = @"可选";
        }
        else
        {
            cell.lblCanSelecte.text = @"必选";
        }
        
        cell.lblStatus.text=self.arrContents[indexPath.row][@"reviewStatusDesc"];


        NSString *isEnough = self.arrContents[indexPath.row][@"isNumEnough"];
        NSString *status = self.arrContents[indexPath.row][@"reviewStatus"];
        if ([isEnough isEqualToString:@"0"]) {
            cell.lblStatus.textColor = RGBColor(253, 177, 84, 1);
        }
        else
        {
            if ([status isEqualToString:kWeishenhecode]||[status isEqualToString:kYizhenggaicode]) {
                cell.lblStatus.textColor = RGBColor(253, 177, 84, 1);
            }
            else
            {
                cell.lblStatus.textColor = [UIColor lightGrayColor];
            }
        }
        
    }
    else
    {
        cell.lblPoint.text = self.partContentArr[indexPath.row][@"name"];
        
        cell.lblQy.text = self.arrContents[indexPath.row][@"typeDesc"];
        
        
        NSString *isRequire = self.partContentArr[indexPath.row][@"isRequire"];
        if ([isRequire isEqualToString:@"0"]) {
            cell.lblCanSelecte.text = @"可选";
        }
        else
        {
            cell.lblCanSelecte.text = @"必选";
        }
        
        cell.lblStatus.text=self.partContentArr[indexPath.row][@"reviewStatusDesc"];
        NSString *isEnough = self.partContentArr[indexPath.row][@"isNumEnough"];
        NSString *status = self.partContentArr[indexPath.row][@"reviewStatus"];
        if ([isEnough isEqualToString:@"0"]) {
            cell.lblStatus.textColor = RGBColor(253, 177, 84, 1);
        }
        else
        {
            if ([status isEqualToString:kDaizhenggaicode]||[status isEqualToString:kYizhenggaicode]) {
                cell.lblStatus.textColor = RGBColor(253, 177, 84, 1);
            }
            else
            {
                cell.lblStatus.textColor = [UIColor lightGrayColor];
            }
        }
    }
}

#pragma mark =============  UITableView DataSource  =============

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"TaskListCell";
    SATaskPointDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[SATaskPointDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    [self configureCell:cell forIndexPath:indexPath];
    
    return cell;}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_checkPointStatusType isEqualToString:@"0"]) {
        return self.arrContents.count;
    }
    else
    {
        return self.partContentArr.count;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 35)];
    //viewHeader.backgroundColor = [UIColor colorWithRed:0.2431 green:0.7137 blue:0.9373 alpha:1];
    viewHeader.backgroundColor = [UIColor lightGrayColor];
    
    UILabel *lblPoint = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 115, 35)];
    lblPoint.textColor = [UIColor whiteColor];
    lblPoint.backgroundColor = [UIColor clearColor];
    lblPoint.font = [UIFont boldSystemFontOfSize:13];
    lblPoint.textAlignment = NSTextAlignmentCenter;
    lblPoint.text = @"检查点";
    [viewHeader addSubview:lblPoint];
    
    UILabel *lblArea = [[UILabel alloc] initWithFrame:CGRectMake(115, 0, 50, 35)];
    lblArea.textColor = [UIColor whiteColor];
    lblArea.backgroundColor = [UIColor clearColor];
    lblArea.font = [UIFont boldSystemFontOfSize:13];
    lblArea.textAlignment = NSTextAlignmentCenter;
    lblArea.text = @"区域";
    [viewHeader addSubview:lblArea];
    
    UILabel *lblStatus1 = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 60, 35)];
    lblStatus1.textColor = [UIColor whiteColor];
    lblStatus1.backgroundColor = [UIColor clearColor];
    lblStatus1.font = [UIFont boldSystemFontOfSize:13];
    lblStatus1.textAlignment = NSTextAlignmentCenter;
    lblStatus1.text = @"可选/必选";
    [viewHeader addSubview:lblStatus1];
    
    UIButton *btnShaiXuan = [[UIButton alloc] initWithFrame:CGRectMake(235, 0, self.view.bounds.size.width-235, 35)];
    [btnShaiXuan addTarget:self action:@selector(shaiXuanClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnShaiXuan setBackgroundColor:[UIColor colorWithRed:0.2431 green:0.7137 blue:0.9373 alpha:1]];
    [viewHeader addSubview:btnShaiXuan];
    
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 7.5, 20, 20)];
    
    [btnShaiXuan addSubview:imageView];
    
    lblStatusName = [[UILabel alloc] initWithFrame:CGRectMake(28, 7.5, self.view.bounds.size.width-255, 20)];
    lblStatusName.textColor = [UIColor whiteColor];
    lblStatusName.backgroundColor = [UIColor clearColor];
    lblStatusName.font = [UIFont boldSystemFontOfSize:13];
    lblStatusName.textAlignment = NSTextAlignmentLeft;
    if ([_checkPointStatusType isEqualToString:@"0"]) {
        lblStatusName.text = @"状态筛选";
        imageView.image = [UIImage imageNamed:@"Task/icon_filter.png"];
    }
    if ([_checkPointStatusType isEqualToString:@"1"]) {
        lblStatusName.text = @"状态筛选";
        imageView.image = [UIImage imageNamed:@"Task/icon_loudou_unfull.png"];
    }
    if ([_checkPointStatusType isEqualToString:@"2"]) {
        lblStatusName.text = @"状态筛选";
        imageView.image = [UIImage imageNamed:@"Task/icon_loudou_unfull.png"];
    }
    if ([_checkPointStatusType isEqualToString:@"3"]) {
        lblStatusName.text = @"状态筛选";
        imageView.image = [UIImage imageNamed:@"Task/icon_loudou_unfull.png"];
    }
    if ([_checkPointStatusType isEqualToString:@"4"]) {
        lblStatusName.text = @"状态筛选";
        imageView.image = [UIImage imageNamed:@"Task/icon_loudou_unfull.png"];
    }

    [btnShaiXuan addSubview:lblStatusName];
    
    return viewHeader;
}
-(void)shaiXuanClick:(UIButton*)sender
{
    [self addTaskPartView];
}
#pragma mark =============  UITableView Delegate  =============

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 500) {
        self.partContentArr = [[NSMutableArray alloc] init];
        if (indexPath.row == 0) {
            _checkPointStatusType = @"0";
            lblStatusName.text = @"全部";
        }
        if (indexPath.row == 1) {
            _checkPointStatusType = @"1";
            lblStatusName.text = @"未上报";
            for (int i = 0; i < self.arrContents.count; i++) {
                NSString* str = self.arrContents[i][@"status"];
                if ([str isEqualToString:@"1"]) {
                    [self.partContentArr addObject:self.arrContents[i]];
                }
            }
        }
        if (indexPath.row == 2) {
            _checkPointStatusType = @"2";
            lblStatusName.text = @"已上报";
            for (int i = 0; i < self.arrContents.count; i++) {
                NSString* str = self.arrContents[i][@"status"];
                if ([str isEqualToString:@"2"]) {
                    [self.partContentArr addObject:self.arrContents[i]];
                }
            }
        }
        if (indexPath.row == 3) {
            _checkPointStatusType = @"3";
            lblStatusName.text = @"未评分";
            for (int i = 0; i < self.arrContents.count; i++) {
                NSString* str = self.arrContents[i][@"status"];
                if ([str isEqualToString:@"3"]) {
                    [self.partContentArr addObject:self.arrContents[i]];
                }
            }
        }
        if (indexPath.row == 4) {
            _checkPointStatusType = @"4";
            lblStatusName.text = @"已评分";
            for (int i = 0; i < self.arrContents.count; i++) {
                NSString* str = self.arrContents[i][@"status"];
                if ([str isEqualToString:@"4"]) {
                    [self.partContentArr addObject:self.arrContents[i]];
                }
            }
        }
        [self.tableVContent reloadData];
    }
    else
    {
        NSMutableArray* idArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < self.arrContents.count; i++) {
            [idArr addObject:self.arrContents[i][@"id"]];
        }
        
        NSMutableArray* isEnuoghArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < self.arrContents.count; i++) {
            [isEnuoghArr addObject:self.arrContents[i][@"isNumEnough"]];
        }
        
        NSMutableArray* statusArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < self.arrContents.count; i++) {
            [statusArr addObject:self.arrContents[i][@"reviewStatus"]];
        }
        if ([_checkPointStatusType isEqualToString:@"0"])
        {
//            NSString* status = self.arrContents[indexPath.row][@"status"];
//            if ([status isEqualToString:@"1"]) {
//                [UIApplication showToastViewMessage:@"检查点未上报，无法查看"];
//            }
//            else
//            {
                self.selectedIndexPath = indexPath;
                DLog(@"You Click At Section: %d Row: %d", (int)indexPath.section, (int)indexPath.row);
                SATaskCheckViewController *detailVC = [[SATaskCheckViewController alloc] initWithNibName:nil bundle:nil];
                detailVC.isEnuogh = self.arrContents[indexPath.row][@"isNumEnough"];
                detailVC.checkPointRecordId = self.arrContents[indexPath.row][@"id"];
                detailVC.checkPointIdArr = idArr;
                detailVC.checkPointIsEnoughArr = isEnuoghArr;
                detailVC.statusArr = statusArr;
                detailVC.taskStatus = _taskStatus;
                detailVC.isRequire =self.arrContents[indexPath.row][@"isRequire"];
                [self.navigationController pushViewController:detailVC animated:YES];
//            }
        }
        else
        {
//            NSString* status = self.partContentArr[indexPath.row][@"status"];
//            if ([status isEqualToString:@"1"]) {
//                [UIApplication showToastViewMessage:@"检查点未上报，无法查看"];
//            }
//            else
//            {
                self.selectedIndexPath = indexPath;
                DLog(@"You Click At Section: %d Row: %d", (int)indexPath.section, (int)indexPath.row);
                SATaskCheckViewController *detailVC = [[SATaskCheckViewController alloc] initWithNibName:nil bundle:nil];
                detailVC.isEnuogh = self.arrContents[indexPath.row][@"isNumEnough"];
                detailVC.checkPointRecordId = self.partContentArr[indexPath.row][@"id"];
                detailVC.checkPointIdArr = idArr;
                detailVC.checkPointIsEnoughArr = isEnuoghArr;
                detailVC.statusArr = statusArr;
                detailVC.taskStatus =_taskStatus;
                detailVC.isRequire =self.arrContents[indexPath.row][@"isRequire"];
                [self.navigationController pushViewController:detailVC animated:YES];
//            }
        }
        
    }
    
}
#pragma mark =============  CMPopTipView Delegate  =============
- (void)popTipViewWasDismissedByUser:(CMPopTipView *)popTipView
{
    
}
@end

#pragma ---------------------   自定义cell   -----------------------

@implementation SATaskPointDetailCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.lblPoint = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 115, 44)];
        _lblPoint.textColor = [UIColor colorWithRed:0.2431 green:0.7137 blue:0.9373 alpha:1];
        _lblPoint.backgroundColor = [UIColor clearColor];
        _lblPoint.font = [UIFont systemFontOfSize:13];
        _lblPoint.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblPoint];
        
        self.lblQy = [[UILabel alloc] initWithFrame:CGRectMake(115, 0, 50, 44)];
        _lblQy.textColor = [UIColor lightGrayColor];
        _lblQy.backgroundColor = [UIColor clearColor];
        _lblQy.font = [UIFont systemFontOfSize:11];
        _lblQy.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblQy];
        
        self.lblCanSelecte = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 60, 44)];
        _lblCanSelecte.textColor = [UIColor lightGrayColor];
        _lblCanSelecte.backgroundColor = [UIColor clearColor];
        _lblCanSelecte.font = [UIFont systemFontOfSize:13];
        _lblCanSelecte.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblCanSelecte];
        
        self.lblStatus = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 60, 44)];
        _lblStatus.textColor = [UIColor lightGrayColor];
        _lblStatus.numberOfLines = 2;
        _lblStatus.backgroundColor = [UIColor clearColor];
        _lblStatus.font = [UIFont systemFontOfSize:13];
        _lblStatus.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_lblStatus];
        
        self.imageVArrow = [[UIImageView alloc] initWithFrame:CGRectMake(305, 15.5, 8, 13)];
        _imageVArrow.image = [UIImage imageNamed:@"Task_Point/image_point_arrow.png"];
        [self.contentView addSubview:_imageVArrow];
        
        //        self.imageVUser = [[UIImageView alloc] initWithFrame:CGRectMake(10, 14, 15, 16)];
        //        [self.contentView addSubview:_imageVUser];
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 0.5)];
        viewLine.backgroundColor = [UIColor colorWithRed:0.7333 green:0.7333 blue:0.7333 alpha:1];
        [self.contentView addSubview:viewLine];
        
    }
    return self;
}

@end