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

#import "SATaskReviewViewController.h"
#import "TaskCheckViewController.h"
#import "SATaskDetailViewController.h"
#import <UIKit/UITextView.h>

@interface SATaskReviewViewController ()
{
    UILabel* lblCheckPoint;
    UILabel* lblTaskName;
    UILabel* lblTaskAddress;
    UILabel* lblCheckEndTime;
    UILabel* lblCheckPeopleName;
    UITextView* textView ;
    UILabel *lblCheckRemarkPlaceholder;
    UILabel* lblTaskEndTime;
    
}
@property (nonatomic, strong) UIView *descView;
@property (nonatomic, strong) NSDictionary *resultDic;
@end

@implementation SATaskReviewViewController

- (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 = @"任务审核";
    
    _descView = [[UIView alloc] init];
    _descView.backgroundColor = [UIColor colorWithRed:0.1569 green:0.6235 blue:0.7216 alpha:1];
    [self.view addSubview:_descView];
    [_descView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [_descView autoSetDimension:ALDimensionHeight toSize:65];
    
    lblTaskName = [[UILabel alloc] init];
    lblTaskName.textColor = [UIColor whiteColor];
    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)];
    
    lblTaskAddress = [[UILabel alloc] init];
    lblTaskAddress.textColor = [UIColor whiteColor];
    lblTaskAddress.font = [UIFont systemFontOfSize:13];
    lblTaskAddress.text = @"无锡锡山商城";
    [_descView addSubview:lblTaskAddress];
    [lblTaskAddress autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:locationImage withOffset:2];
    [lblTaskAddress autoAlignAxis:ALAxisHorizontal toSameAxisOfView:locationImage];
    
    lblTaskEndTime = [[UILabel alloc] init];
    lblTaskEndTime.textColor = [UIColor whiteColor];
    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(24, 149, 173, 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 whiteColor];
    lblCheckPeoplePre.font = [UIFont systemFontOfSize:13];
    lblCheckPeoplePre.text = @"上  报  人:";
    [checkDescView addSubview:lblCheckPeoplePre];
    [lblCheckPeoplePre autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [lblCheckPeoplePre autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
    
    lblCheckPeopleName = [[UILabel alloc] init];
    lblCheckPeopleName.textColor = [UIColor whiteColor];
    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 whiteColor];
    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];
    
    lblCheckEndTime = [[UILabel alloc] init];
    lblCheckEndTime.textColor = [UIColor whiteColor];
    lblCheckEndTime.font = [UIFont systemFontOfSize:13];
    lblCheckEndTime.text = @"2015-02-02";
    [checkDescView addSubview:lblCheckEndTime];
    [lblCheckEndTime autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblCheckTimePre withOffset:2];
    [lblCheckEndTime autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblCheckTimePre];
    
    UILabel* lblCheckPointPre = [[UILabel alloc] init];
    lblCheckPointPre.textColor = [UIColor whiteColor];
    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];
    
    lblCheckPoint = [[UILabel alloc] init];
    lblCheckPoint.textColor = [UIColor whiteColor];
    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];
    
    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];
    
    lblCheckRemarkPlaceholder=[[UILabel alloc] init];
//    lblCheckRemarkPlaceholder.text=@"此处填写备注信息";
    lblCheckRemarkPlaceholder.text=@"";
    
    [textView addSubview:lblCheckRemarkPlaceholder];
    
    [lblCheckRemarkPlaceholder autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:textView];
    [lblCheckRemarkPlaceholder autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:textView];
    
    [lblCheckRemarkPlaceholder autoSetDimensionsToSize:CGSizeMake(200, 21)];
    
    
    lblCheckRemarkPlaceholder.textAlignment=NSTextAlignmentLeft;
    lblCheckRemarkPlaceholder.font=[UIFont systemFontOfSize:15];
    
    lblCheckRemarkPlaceholder.enabled=NO;
    
    
    
    lblCheckRemarkPlaceholder.hidden = textView.text.length > 0?YES:NO;
    
    
    
    
    
    UIButton* btnPass = [[UIButton alloc] init];
    btnPass.tag = 99;
    [btnPass setBackgroundColor:RGBColor(143, 187, 93, 1)];
    [btnPass setTitle:@"通 过" forState:UIControlStateNormal];
    [btnPass setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnPass addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    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.tag = 100;
    [btnUnPass setBackgroundColor:RGBColor(181, 70, 84, 1)];
    [btnUnPass setTitle:@"不通过" forState:UIControlStateNormal];
    [btnUnPass setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnUnPass addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    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];
    
    [self requestDatas];
}



#pragma textview delegate-========

- (void)textViewDidChange:(UITextView *)textViews
{
    if ([textViews.text isEqualToString:@""]) {
        lblCheckRemarkPlaceholder.hidden = NO;
    }else {
        lblCheckRemarkPlaceholder.hidden = YES;
    }
}

- (void)textViewDidEndEditing:(UITextView *)textViews
{
    if ([textViews.text isEqualToString:@""]) {
        lblCheckRemarkPlaceholder.hidden = NO;
    }else {
        lblCheckRemarkPlaceholder.hidden = YES;
    }
    
}

- (BOOL)textView:(UITextView *)textViews shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textViews resignFirstResponder];
        return NO;
    }
    return YES;
}


- (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:120];
}

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

-(void)btnClick:(UIButton*)sender
{
    if (sender.tag == 99) {
        [self reviewTask:@"1"];
    }
    if (sender.tag == 100) {
        [self reviewTask:@"0"];
    }
}
#pragma ---------------------   请求数据   -----------------------
- (void)requestDatas
{
    NSDictionary *dicParam = @{@"taskId":self.taskId};
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryTaskDetailsForReviewEX";
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}


#pragma ---------------------   审核任务   -----------------------
-(void)reviewTask:(NSString*)ispass
{
    
    NSDictionary *dicParam = dicParam = @{@"taskId":self.taskId,@"content":textView.text,@"isPass":ispass};
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"reviewTaskEX";
    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:@"queryTaskDetailsForReviewEX"])
    {
        _resultDic = [result[@"taskDetails"] validDataOfClass:[NSDictionary class]];
        lblTaskName.text = _resultDic[@"activityName"];
        lblTaskAddress.text = _resultDic[@"shopName"];
        lblCheckEndTime.text = _resultDic[@"endDate"];
        lblCheckPeopleName.text = _resultDic[@"reportUserName"];
        lblCheckEndTime.text = _resultDic[@"reportDate"];
        lblTaskEndTime.text = _resultDic[@"endDate"];
        NSDictionary* lastHistory = [result[@"latestTaskReview"] validDataOfClass:[NSDictionary class]];
        textView.text = lastHistory[@"content"];
        
        lblCheckPoint.text = [NSString stringWithFormat:@"必选(%@)  可选(%@)  上报(%@)",result[@"requiredNumber"],result[@"optionalNumber"],result[@"reportNumber"]];
    }

    if ([operation.userInfo[@"interface"] isEqualToString:@"reviewTaskEX"])
    {
        [UIApplication saveObject:@YES type:Number key:kShouldRequestDataWhenBack];
        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:NSClassFromString(@"SATaskPointDetailViewController")]) {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
}

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

#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);
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textView resignFirstResponder];
}

@end
