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

#import "BATaskReviewViewController.h"
#import "TaskCheckViewController.h"
#import "SATaskDetailViewController.h"

@interface BATaskReviewViewController ()
{
    
}
@property (nonatomic, strong) UIView *descView;
@end

@interface BATaskReviewCell : 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 BATaskReviewViewController

- (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 = @"任务审核";
    
    if ([[DataMgr getInstance].roleId isEqualToString:@"2"]) {
        if (self.pointType == TaskPointTypeChecked) {
            self.navigationItem.title = @"已上报";
        }else if (self.pointType == TaskPointTypeUnchecked) {
            self.navigationItem.title = @"未上报";
        }
    }else {
        if (self.pointType == TaskPointTypeChecked) {
            self.navigationItem.title = @"已检查";
        }else if (self.pointType == TaskPointTypeUnchecked) {
            self.navigationItem.title = @"已上报";
        }
    }
    
    _descView = [[UIView alloc] init];
    _descView.backgroundColor = RGBColor(0, 99, 159, 1);
    [self.view addSubview:_descView];
    [_descView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [_descView autoSetDimension:ALDimensionHeight toSize:65];
    
    UILabel* lblTaskName = [[UILabel alloc] init];
    lblTaskName.textColor = [UIColor blackColor];
    lblTaskName.font = [UIFont systemFontOfSize:14];
    lblTaskName.text = @"2015年5月第二季会员";
    [_descView addSubview:lblTaskName];
    [lblTaskName autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [lblTaskName autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
    
    UIImageView* locationImage = [[UIImageView  alloc] init];
    locationImage.image = [UIImage imageNamed:@"Task/icon_site_white.png"];
    [_descView addSubview:locationImage];
    [locationImage autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblTaskName];
    [locationImage autoPinEdge:ALEdgeTop  toEdge:ALEdgeBottom ofView:lblTaskName withOffset:1];
    [locationImage autoSetDimensionsToSize:CGSizeMake(12, 18)];
    
    UILabel* lblTaskAddress = [[UILabel alloc] init];
    lblTaskAddress.textColor = [UIColor blackColor];
    lblTaskAddress.font = [UIFont systemFontOfSize:13];
    lblTaskAddress.text = @"无锡锡山商城";
    [_descView addSubview:lblTaskAddress];
    [lblTaskAddress autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:locationImage withOffset:2];
    [lblTaskAddress autoAlignAxis:ALAxisHorizontal toSameAxisOfView:locationImage];
    
    UILabel* lblTaskEndTime = [[UILabel alloc] init];
    lblTaskEndTime.textColor = [UIColor blackColor];
    lblTaskEndTime.font = [UIFont systemFontOfSize:12];
    lblTaskEndTime.text = @"2015-06-05到期";
    [_descView addSubview:lblTaskEndTime];
    [lblTaskEndTime autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblTaskName];
    [lblTaskEndTime autoPinEdge:ALEdgeTop  toEdge:ALEdgeBottom ofView:lblTaskAddress withOffset:3];
    
    
    
    UIView* checkDescView = [[UIView alloc] init];
    checkDescView.backgroundColor = RGBColor(0, 90, 147, 1);
    [self.view addSubview:checkDescView];
    [checkDescView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [checkDescView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [checkDescView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_descView];
    [checkDescView autoSetDimension:ALDimensionHeight toSize:63];
    
    UILabel* lblCheckPeoplePre = [[UILabel alloc] init];
    lblCheckPeoplePre.textColor = [UIColor blackColor];
    lblCheckPeoplePre.font = [UIFont systemFontOfSize:13];
    lblCheckPeoplePre.text = @"上  报  人:";
    [checkDescView addSubview:lblCheckPeoplePre];
    [lblCheckPeoplePre autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [lblCheckPeoplePre autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
    
    UILabel* lblCheckPeopleName = [[UILabel alloc] init];
    lblCheckPeopleName.textColor = [UIColor blackColor];
    lblCheckPeopleName.font = [UIFont systemFontOfSize:13];
    lblCheckPeopleName.text = @"朱超";
    [checkDescView addSubview:lblCheckPeopleName];
    [lblCheckPeopleName autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblCheckPeoplePre withOffset:2];
    [lblCheckPeopleName autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblCheckPeoplePre];
    
    UILabel* lblCheckTimePre = [[UILabel alloc] init];
    lblCheckTimePre.textColor = [UIColor blackColor];
    lblCheckTimePre.font = [UIFont systemFontOfSize:13];
    lblCheckTimePre.text = @"整改上报:";
    [checkDescView addSubview:lblCheckTimePre];
    [lblCheckTimePre autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblCheckPeoplePre];
    [lblCheckTimePre autoPinEdge:ALEdgeTop  toEdge:ALEdgeBottom ofView:lblCheckPeoplePre withOffset:1];
    
    UILabel* lblCheckTime = [[UILabel alloc] init];
    lblCheckTime.textColor = [UIColor blackColor];
    lblCheckTime.font = [UIFont systemFontOfSize:13];
    lblCheckTime.text = @"2015-02-02";
    [checkDescView addSubview:lblCheckTime];
    [lblCheckTime autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblCheckTimePre withOffset:2];
    [lblCheckTime autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblCheckTimePre];
    
    UILabel* lblCheckPointPre = [[UILabel alloc] init];
    lblCheckPointPre.textColor = [UIColor blackColor];
    lblCheckPointPre.font = [UIFont systemFontOfSize:13];
    lblCheckPointPre.text = @"检  查  点:";
    [checkDescView addSubview:lblCheckPointPre];
    [lblCheckPointPre autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblCheckPeoplePre];
    [lblCheckPointPre autoPinEdge:ALEdgeTop  toEdge:ALEdgeBottom ofView:lblCheckTimePre withOffset:1];
    
    UILabel* lblCheckPoint = [[UILabel alloc] init];
    lblCheckPoint.textColor = [UIColor blackColor];
    lblCheckPoint.font = [UIFont systemFontOfSize:13];
    lblCheckPoint.text = @"必选(1)  可选(2)  上报(3)";
    [checkDescView addSubview:lblCheckPoint];
    [lblCheckPoint autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblCheckPointPre withOffset:2];
    [lblCheckPoint autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblCheckPointPre];
    
    UILabel* lblRemark = [[UILabel alloc] init];
    lblRemark.text  = @"备注:";
    lblRemark.textColor = [UIColor blackColor];
    lblRemark.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:lblRemark];
    [lblRemark autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:checkDescView withOffset:20];
    [lblRemark autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    
    UITextView* textView = [[UITextView alloc] init];
    textView.layer.cornerRadius = 5;
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = [UIColor colorWithRed:0.9333 green:0.9333 blue:0.9333 alpha:1].CGColor;
    [self.view addSubview:textView];
    [textView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblRemark withOffset:5];
    [textView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblRemark];
    [textView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [textView autoSetDimension:ALDimensionHeight toSize:100];
    
    UIButton* btnPass = [[UIButton alloc] init];
    [btnPass setBackgroundColor:RGBColor(143, 187, 93, 1)];
    [btnPass setTitle:@"通 过" forState:UIControlStateNormal];
    [btnPass setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnPass.layer.cornerRadius = 3;
    [self.view addSubview:btnPass];
    [btnPass autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:textView withOffset:20];
    [btnPass autoSetDimensionsToSize:CGSizeMake(120, 30)];
    [btnPass autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view withOffset:-70];
    
    UIButton* btnUnPass = [[UIButton alloc] init];
    [btnUnPass setBackgroundColor:RGBColor(181, 70, 84, 1)];
    [btnUnPass setTitle:@"不通过" forState:UIControlStateNormal];
    [btnUnPass setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnUnPass.layer.cornerRadius = 3;
    [self.view addSubview:btnUnPass];
    [btnUnPass autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:textView withOffset:20];
    [btnUnPass autoSetDimensionsToSize:CGSizeMake(120, 30)];
    [btnUnPass autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view withOffset:70];
    
    self.tableVContent.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setViewLayoutConstraint];
    
    UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnHome setBackgroundImage:[UIImage imageNamed:@"NavBar/btn_nav_home.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];
    
    [self requestDatas];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
        [self requestDatas];
    }
}

- (void)setViewLayoutConstraint
{
    [self.tableVContent autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [self.tableVContent autoSetDimension:ALDimensionHeight toSize:44*3];
}

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

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



- (void)requestDatas
{
    NSDictionary *dicParam = nil;
    
    if (self.pointType == TaskPointTypeUnchecked) {
        dicParam = @{@"taskId":self.taskId?:@"",@"status":@"01"};
    }else if (self.pointType == TaskPointTypeChecked) {
        dicParam = @{@"taskId":self.taskId?:@"",@"status":@"02"};
    }else if (self.pointType == TaskPointTypeArea) {
        dicParam = @{@"taskId":self.taskId?:@"621",@"areaId":self.areaId?:@"1"};
    }
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryCheckPointList";
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    self.arrContents = [result[@"checkPointList"] validDataOfClass:[NSArray class]];
    [self.tableVContent reloadData];
    
    if (self.arrContents.count == 0) {
        self.imageVNoResult.hidden = NO;
    }
}

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

- (void)configureCell:(BATaskReviewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    cell.lblPoint.text = self.arrContents[indexPath.row][@"name"];
    cell.lblQy.text = @"已上报";
    cell.lblCanSelecte.text = @"必选";
    NSString *roleId = [DataMgr getInstance].roleId;
    NSString *status = self.arrContents[indexPath.row][@"status"];
    if ([status isEqualToString:@"1"]) {
        cell.lblStatus.text = [roleId isEqualToString:@"2"]?@"未上报":@"待上报";
        //cell.lblStatus.textColor = RGBColor(221, 31, 31, 1);
    }else if ([status isEqualToString:@"2"]) {
        cell.lblStatus.text = [roleId isEqualToString:@"2"]?@"已上报":@"已上报";
        //cell.lblStatus.textColor = RGBColor(3, 134, 92, 1);
    }else if ([status isEqualToString:@"3"]) {
        cell.lblStatus.text = [roleId isEqualToString:@"2"]?@"已上报":@"已检查";
        //cell.lblStatus.textColor = RGBColor(3, 134, 92, 1);
    }else if ([status isEqualToString:@"4"]) {
        cell.lblStatus.text = [roleId isEqualToString:@"2"]?@"已上报":@"已评分";
        //cell.lblStatus.textColor = RGBColor(3, 134, 92, 1);
    }
    
    if ([[DataMgr getInstance].roleId isEqualToString:@"2"]) {
        cell.imageVUser.image = [UIImage imageNamed:@"icon_jiancha.png"];
    }else {
        cell.imageVUser.image = [UIImage imageNamed:@"icon_hecha.png"];
    }
    
}

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

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.arrContents.count;
}

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

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == 500||tableView.tag == 501) {
        
        return nil;
    }
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
    //viewHeader.backgroundColor = [UIColor colorWithRed:0.2431 green:0.7137 blue:0.9373 alpha:1];
    viewHeader.backgroundColor = [UIColor lightGrayColor];
    
    UILabel *lblReviewTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 20)];
    lblReviewTime.textColor = [UIColor whiteColor];
    lblReviewTime.backgroundColor = [UIColor clearColor];
    lblReviewTime.font = [UIFont boldSystemFontOfSize:10];
    lblReviewTime.textAlignment = NSTextAlignmentCenter;
    lblReviewTime.text = @"审核日期";
    [viewHeader addSubview:lblReviewTime];
    
    UILabel *lblReviewPeople = [[UILabel alloc] initWithFrame:CGRectMake(64, 0, 64, 20)];
    lblReviewPeople.textColor = [UIColor whiteColor];
    lblReviewPeople.backgroundColor = [UIColor clearColor];
    lblReviewPeople.font = [UIFont boldSystemFontOfSize:10];
    lblReviewPeople.textAlignment = NSTextAlignmentCenter;
    lblReviewPeople.text = @"审核人";
    [viewHeader addSubview:lblReviewPeople];
    
    UILabel *lblCheckTime = [[UILabel alloc] initWithFrame:CGRectMake(64*2, 0, 64, 20)];
    lblCheckTime.textColor = [UIColor whiteColor];
    lblCheckTime.backgroundColor = [UIColor clearColor];
    lblCheckTime.font = [UIFont boldSystemFontOfSize:10];
    lblCheckTime.textAlignment = NSTextAlignmentCenter;
    lblCheckTime.text = @"上报日期";
    [viewHeader addSubview:lblCheckTime];
    
    UILabel *lblCheckPeople = [[UILabel alloc] initWithFrame:CGRectMake(64*3, 0, 64, 20)];
    lblCheckPeople.textColor = [UIColor whiteColor];
    lblCheckPeople.backgroundColor = [UIColor clearColor];
    lblCheckPeople.font = [UIFont boldSystemFontOfSize:10];
    lblCheckPeople.textAlignment = NSTextAlignmentCenter;
    lblCheckPeople.text = @"上报人";
    [viewHeader addSubview:lblCheckPeople];
    
    UILabel *lblIsPass = [[UILabel alloc] initWithFrame:CGRectMake(64*4, 0, 64, 20)];
    lblIsPass.textColor = [UIColor whiteColor];
    lblIsPass.backgroundColor = [UIColor clearColor];
    lblIsPass.font = [UIFont boldSystemFontOfSize:10];
    lblIsPass.textAlignment = NSTextAlignmentCenter;
    lblIsPass.text = @"状态";
    [viewHeader addSubview:lblIsPass];
    
    return viewHeader;
}
-(void)searchClick
{
    NSLog(@"SearchClick");
}
#pragma mark =============  UITableView Delegate  =============

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    DLog(@"You Click At Section: %d Row: %d", (int)indexPath.section, (int)indexPath.row);
    SATaskDetailViewController* vc = [[SATaskDetailViewController alloc] init];
    //vc.pointType = TaskPointTypeArea;
    [self.navigationController pushViewController:vc animated:YES];
    //    TaskCheckViewController *detailVC = [[TaskCheckViewController alloc] initWithNibName:nil bundle:nil];
    //    detailVC.checkPointId = self.arrContents[indexPath.row][@"id"];
    //    detailVC.checkPointName = self.arrContents[indexPath.row][@"name"];
    //    detailVC.isShowHistory = self.isShowHistory;
    //    [self.navigationController pushViewController:detailVC animated:YES];
}

@end

@implementation BATaskReviewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.lblPoint = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 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(180, 0, 90, 44)];
        _lblQy.textColor = [UIColor lightGrayColor];
        _lblQy.backgroundColor = [UIColor clearColor];
        _lblQy.font = [UIFont systemFontOfSize:13];
        _lblQy.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblQy];
        
        self.imageVArrow = [[UIImageView alloc] initWithFrame:CGRectMake(297, 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