//
//  SATaskPartView.m
//  Macalline
//
//  Created by qianjun_work on 15-7-1.
//  Copyright (c) 2015年 JianjiYuan. All rights reserved.
//

#import "SATaskPartView.h"

@implementation SATaskPartView
{
    UIDatePicker* startDatePicker;
    UIDatePicker* endDatePicker;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self initCommonView];
    }
    return self;
}
-(id) initWithFrame:(CGRect)frame andTitle:(NSString*)title andStartTime:(NSDate *)sDate andEndTime:(NSDate *)eDate
{
    if (self = [super initWithFrame:frame]) {
        //self.venueID = title;
        if ([title isEqualToString:@"date"]) {
            [self initDateCommonViewWithStartTime:sDate andEndTime:eDate];
            self.startDate = sDate;
            self.endDate = eDate;
        }
        else
        {
            [self initCommonView];
        }
    }
    return self;
}
#pragma ---------------------   初始化方法   -----------------------
- (void)initDateCommonViewWithStartTime:(NSDate*)sDate andEndTime:(NSDate*)eDate
{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    UIView* contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor colorWithRed:0.9333 green:0.9333 blue:0.9333 alpha:1];
    contentView.clipsToBounds = YES;
    [self addSubview:contentView];
    [contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [contentView autoSetDimension:ALDimensionHeight toSize:405];
    
    UILabel* lblTitle = [[UILabel alloc] init];
    lblTitle.backgroundColor = [UIColor whiteColor];
    lblTitle.textColor = [UIColor darkGrayColor];
    lblTitle.text = @"选择开始时间";
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.font = [UIFont boldSystemFontOfSize:15];
    [contentView addSubview:lblTitle];
    [lblTitle autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [lblTitle autoSetDimension:ALDimensionHeight toSize:35];
    
    UIView* cxView = [[UIView alloc] init];
    cxView.backgroundColor  = [UIColor whiteColor];
    [contentView addSubview:cxView];
    [cxView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [cxView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [cxView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTitle withOffset:1];
    [cxView autoSetDimension:ALDimensionHeight toSize:135];
    
    startDatePicker = [[UIDatePicker alloc] init];
    startDatePicker.datePickerMode = UIDatePickerModeDate;
    [startDatePicker setDate:sDate];
    [cxView addSubview:startDatePicker];
    [startDatePicker autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
    [startDatePicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    
    UILabel* lblTitleEnd = [[UILabel alloc] init];
    lblTitleEnd.backgroundColor = [UIColor whiteColor];
    lblTitleEnd.textColor = [UIColor darkGrayColor];
    lblTitleEnd.text = @"选择结束时间";
    lblTitleEnd.textAlignment = NSTextAlignmentCenter;
    lblTitleEnd.font = [UIFont boldSystemFontOfSize:15];
    [contentView addSubview:lblTitleEnd];
    [lblTitleEnd autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [lblTitleEnd autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [lblTitleEnd autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:cxView withOffset:1];
    [lblTitleEnd autoSetDimension:ALDimensionHeight toSize:35];
    
    UIView* endView = [[UIView alloc] init];
    endView.backgroundColor  = [UIColor whiteColor];
    [contentView addSubview:endView];
    [endView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [endView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [endView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTitleEnd withOffset:1];
    [endView autoSetDimension:ALDimensionHeight toSize:135];
    
    endDatePicker = [[UIDatePicker alloc] init];
    endDatePicker.datePickerMode = UIDatePickerModeDate;
    [endDatePicker setDate:eDate];
    [endView addSubview:endDatePicker];
    [endDatePicker autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
    [endDatePicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    
    UIButton* btnSelect = [[UIButton alloc] init];
    btnSelect.layer.cornerRadius = 3;
    [btnSelect setBackgroundColor:RGBColor(0, 171, 202, 1)];
    [btnSelect setTitle:@"确定" forState:UIControlStateNormal];
 
    

    [btnSelect addTarget:self action:@selector(btnSelectClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnSelect];
    [btnSelect autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 20, 20, 20) excludingEdge:ALEdgeTop];
    [btnSelect autoSetDimension:ALDimensionHeight toSize:35];
    
}
//-(void)btnSelectClick
//{
//    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
//    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
//    NSString* endDateStr = [inputFormatter stringFromDate:[endDatePicker date]];
//    NSString* startDateStr = [inputFormatter stringFromDate:[startDatePicker date]];
//   
//    if([[[endDatePicker date] earlierDate:[startDatePicker date]] isEqualToDate:[endDatePicker date]] )
//    {
//        
//    }
//    else
//    if (self.SelectTimeClick)
//    {//回调
//                self.SelectTimeClick(startDateStr,endDateStr);
//    }
//}
//
-(void)btnSelectClick
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString* endDateStr = [inputFormatter stringFromDate:[endDatePicker date]];
    NSString* startDateStr = [inputFormatter stringFromDate:[startDatePicker date]];
        if (self.SelectTimeClick)
        {//回调

            NSTimeInterval val =  [[endDatePicker date] timeIntervalSinceDate:[startDatePicker date]];
            
            if (val < 0 )
            {
                
                //                [UIApplication showToastViewMessage:[NSString stringWithFormat:@"你选的日期只能在\n%@\n %@\n范围内",self.startDate,self.endDate]];
                [UIApplication showToastViewMessage:@"开始时间不能小于结束时间！"];
            }
            else
            {
                    self.SelectTimeClick(startDateStr,endDateStr);
            }
        }
}

-(void)valueChange:(UIDatePicker*)sender
{
    
}
- (void)initCommonView
{
    
    CGFloat w = self.bounds.size.width/3;
    CGFloat h = w-20;
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    UIView* contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor colorWithRed:0.9333 green:0.9333 blue:0.9333 alpha:1];
    contentView.clipsToBounds = YES;
    [self addSubview:contentView];
    [contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [contentView autoSetDimension:ALDimensionHeight toSize:85+(h*3)];
    
    UILabel* lblTitle = [[UILabel alloc] init];
    lblTitle.backgroundColor = [UIColor whiteColor];
    lblTitle.textColor = [UIColor darkGrayColor];
    lblTitle.text = @"选择任务分类";
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.font = [UIFont boldSystemFontOfSize:15];
    [contentView addSubview:lblTitle];
    [lblTitle autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [lblTitle autoSetDimension:ALDimensionHeight toSize:35];
    
    UIView* viewCX = [[UIView alloc] init];
    viewCX.backgroundColor  = [UIColor clearColor];
    [contentView addSubview:viewCX];
    [viewCX autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [viewCX autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTitle withOffset:1];
    
//    for (int i = 0; i < 8; i++) {
//        UIButton* btn = [self creatBtnWithIndex:i withType:@""];
//        [cxView addSubview:btn];
//    }
    
    UILabel* lblCX = [[UILabel alloc] init];
    lblCX.textColor = [UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
    lblCX.font = [UIFont boldSystemFontOfSize:14];
    lblCX.text = @"促销";
    [contentView addSubview:lblCX];
    [lblCX autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [lblCX autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [lblCX autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTitle];
    [lblCX autoSetDimension:ALDimensionHeight toSize:25];
    
    UIView* cxView = [[UIView alloc] init];
    cxView.backgroundColor  = [UIColor redColor];
    [contentView addSubview:cxView];
    
    for (int i = 0; i < 3; i++) {
        UIButton* btn = [self creatBtnWithIndex:i withType:@"cx"];
        [cxView addSubview:btn];
    }
    [cxView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [cxView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [cxView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblCX];
    [cxView autoSetDimension:ALDimensionHeight toSize:h];
    
    
    UILabel* lblRC = [[UILabel alloc] init];
    lblRC.textColor = [UIColor colorWithRed:0.2314 green:0.7098 blue:0.9373 alpha:1];
    lblRC.font = [UIFont boldSystemFontOfSize:14];
    lblRC.text = @"日常";
    [contentView addSubview:lblRC];
    [lblRC autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [lblRC autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [lblRC autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:cxView];
    [lblRC autoSetDimension:ALDimensionHeight toSize:25];
    
    UIView* dayView = [[UIView alloc] initWithFrame:CGRectMake(0, h+50, w*3, h*2)];
    [contentView addSubview:dayView];
    
    for (int i = 0; i < 6; i++) {
        UIButton* btn = [self creatBtnWithIndex:i withType:@"rc"];
        [dayView addSubview:btn];
    }
    
    [dayView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [dayView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [dayView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblRC];
    [dayView autoSetDimension:ALDimensionHeight toSize:h*2];
}

-(UIButton*)creatBtnWithIndex:(int)num withType:(NSString*)type
{
    CGFloat w = self.bounds.size.width/3;
    CGFloat h = w-20;
    
    UIButton* btn = [[UIButton alloc] init];
    btn.layer.borderWidth = 0.5;
    btn.layer.borderColor = [UIColor colorWithRed:0.9333 green:0.9333 blue:0.9333 alpha:1].CGColor;
    [btn setBackgroundColor:[UIColor whiteColor]];
    if (num < 3) {
        btn.frame = CGRectMake(num*w, 0, w, h);
    }
    else
    {
        btn.frame = CGRectMake((num-3)*w, h, w, h);
    }
    UIImageView* images = [[UIImageView alloc] init];
    
    [btn addSubview:images];
    [images autoAlignAxis:ALAxisVertical toSameAxisOfView:btn];
    [images autoAlignAxis:ALAxisHorizontal toSameAxisOfView:btn withOffset:-15];
    
    UILabel* lblName = [[UILabel alloc] init];
    lblName.font = [UIFont systemFontOfSize:15];
    lblName.textColor = [UIColor darkGrayColor];
    [btn addSubview:lblName];
    [lblName autoAlignAxis:ALAxisVertical toSameAxisOfView:btn];
    [lblName autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    
    if ([type isEqualToString:@"rc"]) {
        btn.tag = 100+num;
        if (num == 0) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_sight.png"];
            lblName.text = @"导视";
            NSString* num2 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToDaoshi];
            if (![num2 isEqualToString:@""]&&![num2 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 1) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_display.png"];
            lblName.text = @"美陈";
            NSString* num3 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToMeicheng];
            if (![num3 isEqualToString:@""]&&![num3 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 2) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_relax.png"];
            lblName.text = @"公共区域";
            NSString* num4 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToXiuxiqu];
            if (![num4 isEqualToString:@""]&&![num4 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 3) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_ad.png"];
            lblName.text = @"广告位";
            NSString* num5 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToGuanggaowei ];
            if (![num5 isEqualToString:@""]&&![num5 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 4) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_music.png"];
            lblName.text = @"背景音乐";
            NSString* num6 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToBeijingyinyue];
            if (![num6 isEqualToString:@""]&&![num6 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 5) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_supplies.png"];
            lblName.text = @"综合物料";
            NSString* num7 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToYingyunwuliao];
            if (![num7 isEqualToString:@""]&&![num7 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
    }
    
    if ([type isEqualToString:@"cx"]) {
        btn.tag = 200+num;
        if (num == 0) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_bigSales.png"];
            lblName.text = @"大促销";
            NSString* num0 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToDacuxiao];
            if (![num0 isEqualToString:@""]&&![num0 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 1) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_smallSales.png"];
            lblName.text = @"小促销";
            NSString* num1 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToXiaocuxiao];
            if (![num1 isEqualToString:@""]&&![num1 isEqualToString:@"0"]) {
                UIButton* lblUndoneNum = [[UIButton alloc] initWithFrame:CGRectMake(w-13, 5, 8, 8)];
                [lblUndoneNum setBackgroundColor:[UIColor redColor]];
                lblUndoneNum.titleLabel.font = [UIFont systemFontOfSize:11];
                lblUndoneNum.layer.cornerRadius = 4;
                [btn addSubview:lblUndoneNum];
            }
        }
        if (num == 2) {
            [btn setBackgroundImage:[UIImage imageNamed:@"NavBar/no_icon_bg.png"] forState:UIControlStateNormal];
            btn.enabled = NO;
        }
    }
    
    [btn addTarget:self action:@selector(btnSelcted:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
-(void)btnSelcted:(UIButton*)sender
{
    if (sender.tag < 200) {
        int num = sender.tag-100;
        if (num == 0) {
            self.BtnClickBlock(@"daoshi");
        }
        if (num == 1) {
            self.BtnClickBlock(@"meichen");
        }
        if (num == 2) {
            self.BtnClickBlock(@"xiuxiqu");
        }
        if (num == 3) {
            self.BtnClickBlock(@"guanggaowei");
        }
        if (num == 4) {
            self.BtnClickBlock(@"beijingyinyue");
        }
        if (num == 5) {
            self.BtnClickBlock(@"yingyunwuliao");
        }
        
    }
    else
    {
        int num = sender.tag-200;
        if (num == 0) {
            self.BtnClickBlock(@"dacuxiao");
        }
        if (num == 1) {
            self.BtnClickBlock(@"xiaocuxiao");
        }
        if (num == 2) {
            self.BtnClickBlock(@"xiaocuxiao");
        }
    }
}

@end
