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

#import "TaskDetailViewController.h"
#import "TaskSeatViewController.h"
#import "RDVTabBarController.h"
#import "TaskAreaViewController.h"

@interface TaskDetailViewController ()
{
    UIView *viewTask;
    UIView *viewLocation;
}

@property (nonatomic, strong) UIImageView *imageVLocation;
@property (nonatomic, strong) UILabel *lblLocation;

@property (nonatomic, strong) UIImageView *imageVFlag;
@property (nonatomic, strong) UILabel *lblTaskTitle;
@property (nonatomic, strong) UITextView *textVTaskContent;
@property (nonatomic, strong) UIButton *btnStartTask;

@property (nonatomic, strong) TaskSeatViewController *vcTaskSeat;


@end

@implementation TaskDetailViewController

- (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.
    
    [self setNavTitle:self.activityName andSubTitle:@"企划部.促销"];

    [self addTaskSeatView];
    
    {
        viewLocation = [UIView newAutoLayoutView];
        [self.view addSubview:viewLocation];
        UIView *viewLine = [UIView newAutoLayoutView];
        viewLine.backgroundColor = RGBColor(0, 120, 193, 1);
        [viewLocation addSubview:viewLine];
        [viewLocation addSubview:self.imageVLocation];
        [viewLocation addSubview:self.lblLocation];
        
        [self.imageVLocation autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
        [self.imageVLocation autoSetDimensionsToSize:CGSizeMake(8, 13)];
        [self.imageVLocation autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
        [self.lblLocation autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.imageVLocation withOffset:4];
        [self.lblLocation autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:8];
        [self.lblLocation autoSetDimension:ALDimensionHeight toSize:25];
        [self.lblLocation autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.imageVLocation];
        [viewLine autoSetDimension:ALDimensionHeight toSize:0.5];
        [viewLine autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];

    }
    
    {
        viewTask = [UIView newAutoLayoutView];
        [self.view addSubview:viewTask];
        [viewTask addSubview:self.imageVFlag];
        [viewTask addSubview:self.lblTaskTitle];
        [viewTask addSubview:self.textVTaskContent];
        [self.imageVFlag autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
        [self.imageVFlag autoSetDimensionsToSize:CGSizeMake(9, 10)];
        [self.imageVFlag autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:8];
        
        [self.lblTaskTitle autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.imageVFlag withOffset:4];
        [self.lblTaskTitle autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:8];
        [self.lblTaskTitle autoSetDimension:ALDimensionHeight toSize:21];
        [self.lblTaskTitle autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.imageVFlag];
        
        [self.textVTaskContent autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblTaskTitle];
        [self.textVTaskContent autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:8];
        [self.textVTaskContent autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:8];
        [self.textVTaskContent autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:8];
    }
    
    [self setViewLayoutConstraint];
    
    [self requestDatas];
    
}

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

- (void)setViewLayoutConstraint
{
    [viewLocation autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [viewLocation autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [viewLocation autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_vcTaskSeat.view];
    [viewLocation autoSetDimension:ALDimensionHeight toSize:28];
    
    [viewTask autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [viewTask autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [viewTask autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:viewLocation];
    [viewTask autoSetDimension:ALDimensionHeight toSize:75 relation:NSLayoutRelationGreaterThanOrEqual];
    
    [self.btnStartTask autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:viewTask];
    [self.btnStartTask autoSetDimensionsToSize:CGSizeMake(280, 30)];
    [self.btnStartTask autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    [self.btnStartTask autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10 relation:NSLayoutRelationEqual];
}

- (void)setNavTitle:(NSString *)title andSubTitle:(NSString *)subTitle
{
    UIView *viewTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 44)];
    viewTitle.backgroundColor = [UIColor clearColor];
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 180, 20)];
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.font = [UIFont boldSystemFontOfSize:16];
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.text = title;
    [viewTitle addSubview:lblTitle];
    
    UILabel *lblSubTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 180, 20)];
    lblSubTitle.textColor = [UIColor whiteColor];
    lblSubTitle.backgroundColor = [UIColor clearColor];
    lblSubTitle.font = [UIFont systemFontOfSize:13];
    lblSubTitle.text = subTitle;
    lblSubTitle.textAlignment = NSTextAlignmentCenter;
    [viewTitle addSubview:lblSubTitle];
    self.navigationItem.titleView = viewTitle;
}

- (void)addTaskSeatView
{
    self.vcTaskSeat = [[TaskSeatViewController alloc] initWithNibName:@"TaskSeatViewController" bundle:nil];
    [_vcTaskSeat.view setFrame:CGRectMake(0, 0, CGRectGetWidth(_vcTaskSeat.view.frame), CGRectGetHeight(_vcTaskSeat.view.frame))];
    [self.view addSubview:_vcTaskSeat.view];
}

- (void)requestDatas
{
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryTaskDetails";
    NSDictionary *dicParam = @{@"taskId":self.taskId?:@""};
    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
{
    _vcTaskSeat.dictProgress = result[@"progress"];
    NSDictionary *dictTaskDetail = result[@"taskDetails"];
    
    if ([[DataMgr getInstance].roleId isEqualToString:@"2"]) {//检查员
        _vcTaskSeat.strStartDate = dictTaskDetail[@"reportStartDate"];
        _vcTaskSeat.strEndDate = dictTaskDetail[@"reportEndDate"];
        
    }else {//考核员
        _vcTaskSeat.strStartDate = dictTaskDetail[@"checkStartDate"];
        _vcTaskSeat.strEndDate = dictTaskDetail[@"checkEndDate"];
    }

    [_vcTaskSeat reloadDatas];
    
    self.textVTaskContent.text = dictTaskDetail[@"details"];
    self.lblLocation.text = [NSString stringWithFormat:@"地点：%@", dictTaskDetail[@"shopName"]];
}

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

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

- (void)btnStartTaskClick:(id)sender
{
    TaskAreaViewController *detailVC = [[TaskAreaViewController alloc] initWithNibName:nil bundle:nil];
    detailVC.taskId = self.taskId;
    detailVC.activityName = self.activityName;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (UIImageView *)imageVLocation
{
    if (!_imageVLocation) {
        self.imageVLocation = [UIImageView newAutoLayoutView];
        _imageVLocation.image = [UIImage imageNamed:@"Task/icon_location_task.png"];
    }
    return _imageVLocation;
}

- (UIImageView *)imageVFlag
{
    if (!_imageVFlag) {
        self.imageVFlag = [UIImageView newAutoLayoutView];
        _imageVFlag.image = [UIImage imageNamed:@"Task/icon_task_flag.png"];
    }
    return _imageVFlag;
}

- (UILabel *)lblLocation
{
    if (!_lblLocation) {
        self.lblLocation = [UILabel newAutoLayoutView];
        _lblLocation.text = @"地点：";
        _lblLocation.textColor = [UIColor blackColor];
        _lblLocation.backgroundColor = [UIColor clearColor];
        _lblLocation.textAlignment = NSTextAlignmentLeft;
        _lblLocation.font = [UIFont systemFontOfSize:16];
    }
    return _lblLocation;
}

- (UILabel *)lblTaskTitle
{
    if (!_lblTaskTitle) {
        self.lblTaskTitle = [UILabel newAutoLayoutView];
        _lblTaskTitle.text = @"任务及要求：";
        _lblTaskTitle.textColor = [UIColor blackColor];
        _lblTaskTitle.backgroundColor = [UIColor clearColor];
        _lblTaskTitle.textAlignment = NSTextAlignmentLeft;
        _lblTaskTitle.font = [UIFont boldSystemFontOfSize:16];
    }
    return _lblTaskTitle;
}

- (UITextView *)textVTaskContent
{
    if (!_textVTaskContent) {
        self.textVTaskContent = [UITextView newAutoLayoutView];
        _textVTaskContent.textColor = [UIColor blackColor];
        _textVTaskContent.backgroundColor = [UIColor clearColor];
        _textVTaskContent.textAlignment = NSTextAlignmentLeft;
        _textVTaskContent.editable = NO;
#ifdef  kIOSVersion7LaterHead
        _textVTaskContent.selectable = NO;
        _textVTaskContent.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
#endif
        _textVTaskContent.font = [UIFont systemFontOfSize:15];
        _textVTaskContent.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _textVTaskContent;
}

- (UIButton *)btnStartTask
{
    if (!_btnStartTask) {
        self.btnStartTask = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnStartTask.translatesAutoresizingMaskIntoConstraints = NO;
//        _btnStartTask.backgroundColor = RGBColor(0, 120, 193, 1);
        [_btnStartTask setBackgroundImage:[UIImage imageWithColor:RGBColor(0, 120, 193, 1)] forState:UIControlStateNormal];
        [_btnStartTask setTitle:@"进入任务" forState:UIControlStateNormal];
        [_btnStartTask setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnStartTask addTarget:self action:@selector(btnStartTaskClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btnStartTask];
    }
    return _btnStartTask;
}

@end
