//
//  NewHomeViewController.m
//  Macalline
//
//  Created by qianjun_work on 15-6-22.
//  Copyright (c) 2015年 JianjiYuan. All rights reserved.
//

#import "BANewHomeViewController.h"
#import "BRNetworkMgr.h"
#import "RDVTabBarController.h"
#import <UIImageView+WebCache.h>
#import <SDImageCache.h>
#import "TypePickerView.h"
#import "BATaskListViewController.h"
#import "BackGroundMusicViewController.h"
#import "NewHomeViewController.h"
#import "BRNetworkMgr.h"
#import "RDVTabBarController.h"
#import <UIImageView+WebCache.h>
#import <SDImageCache.h>
#import "TypePickerView.h"
#import "TaskListViewController.h"
#import "BackGroundMusicViewController.h"
#import "DBDaoMgr.h"
#import "NoteDetailViewController.h"
@interface BANewHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIButton* btnUnRead;
    UIButton* btnAll;
    UIButton *btnCheck;
    UIView* cxView;
    UIView* dayView;
    UILabel *lblTaskNum;
    UILabel *lblTimeCheak;
    UILabel *lblNums;
    UILabel *str2;
    UILabel *stLeft;
    UILabel *strRight;
    UILabel *str1;
    UILabel *str3;
    
    UIImageView* prcessImage;
    UIView* prcessBgView;
    UILabel* lblNum;
    
    UILabel *strnum;
    UIImageView* typePickerViewbg;
    UITableView *checkList;
    UIView  *leftView;
    UIImageView *imageViewHeaderBG;
}
@end

@implementation BANewHomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //self.title = @"首页";
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCustomNavBar];
    self.view.backgroundColor = [UIColor colorWithRed:0.9333 green:0.9333 blue:0.9333 alpha:1];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItems = nil;
    self.navigationController.navigationBarHidden = YES;
    //    UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
    //    statusView.backgroundColor = RGBColor(0, 120, 193, 1);
    //    [self.view addSubview:statusView];
    
    UIView* headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 200)];
    headView.backgroundColor = [UIColor colorWithRed:0.9608 green:0.9608 blue:0.9608 alpha:1];
    [self.view addSubview:headView];
    UIImageView* headbg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 200)];
    headbg.image = [UIImage imageNamed:@"NavBar/bigAreaNav_bg.png"];
    [headView addSubview:headbg];
    imageViewHeaderBG = headbg;
    
    UILabel* lblTitle = [[UILabel alloc] init];
    lblTitle.text = @"首页";
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.font = [UIFont boldSystemFontOfSize:19];
    [headView addSubview:lblTitle];
    
    [lblTitle autoAlignAxis:ALAxisVertical toSameAxisOfView:headView];
    [lblTitle autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:25];
    
    typePickerViewbg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 75, CGRectGetWidth(self.view.frame), 40)];
    typePickerViewbg.image = [UIImage imageNamed:@"NavBar/tab_home_left_daqu.png"];
    [headView addSubview:typePickerViewbg];
    
    NSArray *arrTitles = @[@"企划在线检查", @"查询考核"];
    TypePickerView *typePickerView = [[TypePickerView alloc] initWithFrame:
                                      CGRectMake(0, 50, CGRectGetWidth(self.view.frame), 40)];
    typePickerView.backgroundColor = [UIColor clearColor];
    typePickerView.titles = arrTitles;
    [headView addSubview:typePickerView];
    [typePickerView autoAlignAxis:ALAxisHorizontal toSameAxisOfView:headView withOffset:-3];
    [typePickerView autoAlignAxis:ALAxisVertical toSameAxisOfView:headView];
    [typePickerView autoPinEdgeToSuperviewEdge:ALEdgeLeft ];
    [typePickerView autoPinEdgeToSuperviewEdge:ALEdgeRight ];
    [typePickerView autoSetDimension:ALDimensionHeight toSize:40];
    //配置按钮
    for (int i = 0; i<arrTitles.count; i++) {
        UIButton *btnTitle = [typePickerView buttonAtIndex:i];
        //        btnTitle.layer.borderColor = [UIColor lightGrayColor].CGColor;
        //        btnTitle.layer.borderWidth = 0.5;
        [btnTitle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnTitle setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        //        [btnTitle setBackgroundImage:[UIImage imageNamed:@"tab-bg.png"] forState:UIControlStateSelected];
        btnTitle.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        UIImageView* img = [[UIImageView alloc] init];
        img.image = [UIImage imageNamed:@"arrow_fill.png"];
        [btnTitle addSubview:img];
        [img autoAlignAxis:ALAxisVertical toSameAxisOfView:btnTitle  withOffset:23];
        [img autoAlignAxis:ALAxisHorizontal toSameAxisOfView:btnTitle];
        [img autoSetDimensionsToSize:CGSizeMake(8 , 6)];
    }
    //[self reloadItemTitles];
    typeof(BANewHomeViewController) *weakSelf = self;
    typePickerView.typePickerClick = ^(int index) {
        [weakSelf typePickerViewClickAtIndex:index];
    };
    
    UIView* descView = [[UIView alloc] init];
    descView.backgroundColor = RGBColor(0, 99, 159, 1);
    [headView addSubview:descView];
    [descView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [descView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:typePickerView withOffset:-2];
    

    
    btnCheck = [[UIButton alloc] init];
    [btnCheck setBackgroundColor:[UIColor colorWithRed:0.2314 green:0.7098 blue:0.9373 alpha:1]];
    [btnCheck setTitle:@"去抽查" forState:UIControlStateNormal];
    [btnCheck setTintColor:[UIColor whiteColor]];
    btnCheck.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    btnCheck.layer.cornerRadius = 5;
    [descView addSubview:btnCheck];
    [btnCheck autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:5];
    [btnCheck autoAlignAxis:ALAxisHorizontal toSameAxisOfView:descView withOffset:10];
    [btnCheck autoSetDimensionsToSize:CGSizeMake(50, 35)];
    [btnCheck addTarget:self action:@selector(goCheak) forControlEvents:UIControlEventTouchUpInside];
    UILabel* lbllocation = [[UILabel alloc] init];
    lbllocation.textColor = [UIColor whiteColor];
    lbllocation.text = [DataMgr getInstance].bigAreaName;
    lbllocation.font = [UIFont systemFontOfSize:12];
    [descView addSubview:lbllocation];
    //[lbllocation autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:btnUnRead];
    [lbllocation autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:btnCheck withOffset:-10];
    [lbllocation autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:btnCheck withOffset:-2];
    
    UIImageView* locationImage = [[UIImageView alloc] init];
    locationImage.image = [UIImage imageNamed:@"Task/icon_site_white.png"];
    [descView addSubview:locationImage];
    [locationImage autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:lbllocation withOffset:-2];
    [locationImage autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lbllocation];
    [locationImage autoSetDimensionsToSize:CGSizeMake(12, 18)];
    
//    btnAll = [[UIButton alloc] init];
//    [btnAll setBackgroundColor:RGBColor(20, 122, 190, 1)];
//    //[btnAll setTitle:@"共有50个任务" forState:UIControlStateNormal];
//    btnAll.titleLabel.font = [UIFont systemFontOfSize:12];
//    btnAll.layer.cornerRadius = 26/2;
//    [descView addSubview:btnAll];
//    [btnAll autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:btnUnRead withOffset:-10];
//    [btnAll autoAlignAxis:ALAxisHorizontal toSameAxisOfView:descView withOffset:10];
//    [btnAll autoSetDimensionsToSize:CGSizeMake(105, 26)];
    
    
    lblTaskNum=[[UILabel alloc] init];
    [lblTaskNum setText:@""];
    [descView addSubview:lblTaskNum];
    [lblTaskNum autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:btnCheck withOffset:-10];
    [lblTaskNum autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:descView withOffset:5];
    [lblTaskNum autoSetDimension:ALDimensionHeight toSize:30];
    [lblTaskNum autoSetDimension:ALDimensionWidth toSize:200];
  
    [lblTaskNum setTextColor:[UIColor whiteColor]];
    lblTaskNum.textAlignment=NSTextAlignmentLeft;
    lblTaskNum.font=[UIFont systemFontOfSize:12];
    
    lblTimeCheak=[[UILabel alloc] init];
    [lblTimeCheak setText:@""];
    [descView addSubview:lblTimeCheak];
    [lblTimeCheak autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:btnCheck withOffset:6];
    [lblTimeCheak  autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:descView withOffset:5];
    [lblTimeCheak autoSetDimension:ALDimensionWidth toSize:320];
    [lblTimeCheak autoSetDimension:ALDimensionHeight toSize:30];
    lblTimeCheak.textColor=[UIColor whiteColor];
    
    lblTimeCheak.textAlignment=NSTextAlignmentLeft;
    lblTimeCheak.font=[UIFont systemFontOfSize:12];
  
    
    
    int finishedNum = [_unSurveyNum intValue];
    NSString *strTaskNmuLeft=@"您当前有";
    NSString *strTaskNmuRight=@"个抽查任务";
    CGFloat width1 = [strTaskNmuLeft boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
                                                  options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                                  context:nil].size.width;
    stLeft=[[UILabel alloc] init];
    stLeft.text=strTaskNmuLeft;
    stLeft.textAlignment=NSTextAlignmentLeft;
    stLeft.font=[UIFont systemFontOfSize:12];
    [lblTaskNum addSubview:stLeft];
    [stLeft autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblTaskNum withOffset:0];
    [stLeft autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblTaskNum withOffset:0];
    [stLeft autoSetDimensionsToSize:CGSizeMake(width1, 30)];
    stLeft.textColor=[UIColor whiteColor];
    
    lblNums=[[UILabel alloc] init];
    lblNums.text=[NSString stringWithFormat:@"%d",finishedNum];
    lblNums.textColor=[UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
    lblNums.font=[UIFont systemFontOfSize:12];
    [lblTaskNum addSubview:lblNums];
    [lblNums autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:stLeft withOffset:1];
    [lblNums autoSetDimension:ALDimensionHeight toSize:30];
    [lblNums autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:stLeft];
    
    CGFloat width2 = [lblNums.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
                                                options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                                context:nil].size.width;
    
    [lblNums autoSetDimension:ALDimensionWidth toSize:width2+1];
    strRight=[[UILabel alloc] init];
    strRight.text=strTaskNmuRight;
    strRight.textAlignment=NSTextAlignmentLeft;
    strRight.font=[UIFont systemFontOfSize:12];
    [lblTaskNum addSubview:strRight];
//    [strRight autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblNums withOffset:0];
//    [strRight autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:stLeft ];
    strRight.textColor=[UIColor whiteColor];
    [strRight autoSetDimension:ALDimensionHeight toSize:30];
    
    
    //您还没有进行过抽查
    //    NSString *strUnCheck=@"您还没有进行过抽查";
    //    UILabel *lblUnCheck=[[UILabel alloc] init];
    //    [lblUnCheck setTextColor:[UIColor whiteColor]];
    //    [lblTimeCheak addSubview:lblUnCheck];
    //    [lblUnCheck autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblTimeCheak];
    //    [lblUnCheck autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblTimeCheak];
    //    [lblUnCheck autoSetDimension:ALDimensionHeight toSize:30];
    //    lblUnCheck.text=strUnCheck;
    //    lblUnCheck.font=[UIFont systemFontOfSize:12];
    //
    //
    
    
    
    
    
    
    
    NSString *strLeft1;
    NSString *strMid1;
    
    if ([self.surveyTime isEqualToString:@""]) {
        strMid1=self.surveyTime;
        strLeft1=@"您还没有进行过抽查";
    }
    else
    {
        strMid1=[self.surveyTime substringToIndex:16];
        strLeft1=@"您最近一次抽查在";
    }
    str1=[[UILabel alloc] init];
    str1.text=strLeft1;
//    CGFloat width3 = [str1.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
//                                             options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
//                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
//                                             context:nil].size.width;
    [lblTimeCheak addSubview:str1];
    [str1 setTextColor:[UIColor whiteColor]];
    str1.textAlignment=NSTextAlignmentLeft;
//    [str1 autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblTimeCheak];
//    [str1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblTimeCheak];
//    [str1 autoSetDimension:ALDimensionHeight toSize:30];
//    [str1 autoSetDimension:ALDimensionWidth toSize:width3];
    str1.font=[UIFont systemFontOfSize:12];
    
    str2=[[UILabel alloc] init];
    str2.text=strMid1;
//    CGFloat width4 = [str2.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
//                                             options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
//                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
//                                             context:nil].size.width;
    [lblTimeCheak addSubview:str2];
    [str2 setTextColor:[UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1]];
    str2.textAlignment=NSTextAlignmentLeft;
//    [str2 autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblTimeCheak];
//    [str2 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:str1 withOffset:1];
//    [str2 autoSetDimension:ALDimensionHeight toSize:30];
//    [str2 autoSetDimension:ALDimensionWidth toSize:width4+1];
    str2.font=[UIFont systemFontOfSize:12];
    
    NSString *strRight1=@",尚未完成";
    str3=[[UILabel alloc] init];
    str3.text=strRight1;
    str3.textAlignment=NSTextAlignmentLeft;
    [lblTimeCheak addSubview:str3];
    [str3 autoSetDimension:ALDimensionHeight toSize:30];
    [str3 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:str2 withOffset:1];
    [str3 autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:str2];
    str3.textColor=[UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
    str3.font=[UIFont systemFontOfSize:12];
    
    
    
    if ([self.surveyTime isEqualToString:@""]) {
        str2.hidden = YES;
        str3.hidden = YES;
    }
    else
    {
        if ([self.surveyStatus isEqualToString:@"1"]) {
            str2.hidden = NO;
            str3.hidden = YES;
        }
        else
        {
            str2.hidden = NO;
            str3.hidden = YES;
        }
    }
    
    
    
    
    
    UIScrollView* scrollerView = [[UIScrollView alloc] init];
    scrollerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollerView];
    [scrollerView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [scrollerView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:headView];
    
    UILabel* lblCX = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 25)];
    lblCX.textColor = [UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
    lblCX.font = [UIFont boldSystemFontOfSize:14];
    lblCX.text = @"促销";
    [scrollerView addSubview:lblCX];
    
    CGFloat w = self.view.bounds.size.width/3;
    CGFloat h = w-20;
    cxView = [[UIView alloc] initWithFrame:CGRectMake(0, 25, w*3, h)];
    cxView.backgroundColor  = [UIColor redColor];
    [scrollerView addSubview:cxView];
    
    for (int i = 0; i < 3; i++) {
        UIButton* btn = [self creatBtnWithIndex:i withType:@"cx"];
        [cxView addSubview:btn];
    }
    
    
    
    UILabel* lblRC = [[UILabel alloc] initWithFrame:CGRectMake(10, h+25, 320, 25)];
    lblRC.textColor = [UIColor colorWithRed:0.2314 green:0.7098 blue:0.9373 alpha:1];
    lblRC.font = [UIFont boldSystemFontOfSize:14];
    lblRC.text = @"日常";
    [scrollerView addSubview:lblRC];
    dayView = [[UIView alloc] initWithFrame:CGRectMake(0, h+50, w*3, h*2)];
    [scrollerView addSubview:dayView];
    
    for (int i = 0; i < 6; i++) {
        UIButton* btn = [self creatBtnWithIndex:i withType:@"rc"];
        [dayView addSubview:btn];
    }
    
    scrollerView.contentSize = CGSizeMake(w*3, h*3+50);
    
    [UIApplication showLoadingView:@"正在加载数据"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadUI) name:@"reloadNumber" object:nil];
    [self initRightView];
    
}
#pragma ----------------去抽查-----------
-(void)goCheak
{
    spotCheckViewController *spotCheak=[[spotCheckViewController alloc] init];
    [self.rdv_tabBarController.navigationController pushViewController:spotCheak animated:YES];
    spotCheak.title=@"我的抽查";
}



#pragma ---------------------   刷新UI   -----------------------
-(void)reloadUI
{
    
//  您当前有1个抽查任务
    
    int finishedNum = [_unSurveyNum intValue];
    lblNums.text=[NSString stringWithFormat:@"%d",finishedNum];
    
    CGFloat width2 = [lblNums.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
                                                options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                                context:nil].size.width;
    
    [lblNums autoSetDimension:ALDimensionWidth toSize:width2+1];
    
    [strRight autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblNums withOffset:0];
    [strRight autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:stLeft ];
    strRight.textColor=[UIColor whiteColor];
    [strRight autoSetDimension:ALDimensionHeight toSize:30];
    
    if ([self.surveyTime isEqualToString:@""]) {
        str2.text=self.surveyTime;
        str1.text=@"您还没有进行过抽查";
    }
    else
    {
        str2.text=[self.surveyTime substringToIndex:16];
        str1.text=@"您最近一次抽查在";
    }
    
    
    CGFloat width3 = [str1.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
                                             options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                             context:nil].size.width+10;
    [str1 autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblTimeCheak];
    [str1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblTimeCheak];
    [str1 autoSetDimension:ALDimensionHeight toSize:30];
    [str1 autoSetDimension:ALDimensionWidth toSize:width3];
    
    CGFloat width4 = [str2.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,30)
                                             options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                             context:nil].size.width;
    [str2 autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:lblTimeCheak];
    [str2 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:str1 withOffset:1];
    [str2 autoSetDimension:ALDimensionHeight toSize:30];
    [str2 autoSetDimension:ALDimensionWidth toSize:width4+1];
    
    [str3 autoSetDimension:ALDimensionHeight toSize:30];
    [str3 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:str2 withOffset:1];
    [str3 autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:str2];
    
    if ([self.surveyTime isEqualToString:@""]) {
        str2.hidden = YES;
        str3.hidden = YES;
    }
    else
    {
        if ([self.surveyStatus isEqualToString:@"1"]) {
            str2.hidden = NO;
            str3.hidden = YES;
        }
        else
        {
            str2.hidden = NO;
            str3.hidden = YES;
        }
    }
    
    
    NSString* num = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToDacuxiao];
    if (![num isEqualToString:@""]&&![num isEqualToString:@"0"]) {
        for (UIView* view in cxView.subviews) {
            if (view.tag == 200) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    
    NSString* num1 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToXiaocuxiao];
    if (![num1 isEqualToString:@""]&&![num1 isEqualToString:@"0"]) {
        for (UIView* view in cxView.subviews) {
            if (view.tag == 201) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    
    NSString* num2 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToDaoshi];
    if (![num2 isEqualToString:@""]&&![num2 isEqualToString:@"0"]) {
        for (UIView* view in dayView.subviews) {
            if (view.tag == 100) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    NSString* num3 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToMeicheng];
    if (![num3 isEqualToString:@""]&&![num3 isEqualToString:@"0"]) {
        for (UIView* view in dayView.subviews) {
            if (view.tag == 101) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    NSString* num4 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToXiuxiqu];
    if (![num4 isEqualToString:@""]&&![num4 isEqualToString:@"0"]) {
        for (UIView* view in dayView.subviews) {
            if (view.tag == 102) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    NSString* num5 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToGuanggaowei ];
    if (![num5 isEqualToString:@""]&&![num5 isEqualToString:@"0"]) {
        for (UIView* view in dayView.subviews) {
            if (view.tag == 103) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    NSString* num6 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToBeijingyinyue];
    if (![num6 isEqualToString:@""]&&![num6 isEqualToString:@"0"]) {
        for (UIView* view in dayView.subviews) {
            if (view.tag == 104) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
    NSString* num7 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToYingyunwuliao];
    if (![num7 isEqualToString:@""]&&![num7 isEqualToString:@"0"]) {
        for (UIView* view in dayView.subviews) {
            if (view.tag == 105) {
                CGFloat w = self.view.bounds.size.width/3;
                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;
                [view addSubview:lblUndoneNum];
                break;
            }
        }
    }
}

- (void)typePickerViewClickAtIndex:(int)index
{
    if(index ==0){
        [checkList setHidden:true];
        [leftView setHidden:false];
        typePickerViewbg.image = [UIImage imageNamed:@"NavBar/tab_home_left_daqu.png"];
//        imageViewHeaderBG.image = [UIImage imageNamed:@"NavBar/top_bg_tab_left.png"];
    } else{
        [checkList setHidden:false];
        [leftView setHidden:true];
        typePickerViewbg.image = [UIImage imageNamed:@"NavBar/tab_home_right_daqu.png"];
//        imageViewHeaderBG.image = [UIImage imageNamed:@"NavBar/top_bg_tab_right.png"];
        
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    self.rdv_tabBarController.navigationController.navigationBarHidden = YES;
    [self requestDatas];
    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
//        [self.dictTableDatas removeAllObjects];
//        [self reRequestTableViewDatas];
        [[NSNotificationCenter defaultCenter] postNotificationName:kShouldReQueryCountInit object:nil];
        [UIApplication saveObject:@NO type:Number key:kShouldRequestDataWhenBack];
    }
    
}

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

#pragma ---------------------   创建页面图标   -----------------------
-(UIButton*)creatBtnWithIndex:(int)num withType:(NSString*)type
{
    CGFloat w = self.view.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 = @"导视";
        }
        if (num == 1) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_display.png"];
            lblName.text = @"美陈";
        }
        if (num == 2) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_relax.png"];
            lblName.text = @"公共区域";
        }
        if (num == 3) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_ad.png"];
            lblName.text = @"广告位";
        }
        if (num == 4) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_music.png"];
            lblName.text = @"背景音乐";
        }
        if (num == 5) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_supplies.png"];
            lblName.text = @"综合物料";
        }
    }
    
    if ([type isEqualToString:@"cx"]) {
        btn.tag = 200+num;
        if (num == 0) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_bigSales.png"];
            lblName.text = @"大促销";
        }
        if (num == 1) {
            images.image = [UIImage imageNamed:@"NavBar/icon_home_smallSales.png"];
            lblName.text = @"小促销";
        }
        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;
}


#pragma ---------------------   点击事件   -----------------------
-(void)btnSelcted:(UIButton*)sender
{
    BATaskListViewController* vc = [[BATaskListViewController alloc] init];
    if (sender.tag < 200) {
        int num = sender.tag-100;
        if (num == 0) {
            vc.btnType = kDaoshicode;
        }
        if (num == 1) {
            vc.btnType =  kMeichengcode;
        }
        if (num == 2) {
            vc.btnType = kXiuxiqucode;
        }
        if (num == 3) {
            vc.btnType = kGuanggaoweicode;
        }
        if (num == 4) {
//            BackGroundMusicViewController* bvc = [[BackGroundMusicViewController alloc] init];
//            [self.rdv_tabBarController.navigationController pushViewController:bvc animated:YES];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"功能未开放" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            return;
        }
        if (num == 5) {
            vc.btnType = kYingyunwuliaocode;
        }
        
    }
    else
    {
        int num = sender.tag-200;
        if (num == 0) {
            vc.btnType = kDacuxiaocode;
        }
        if (num == 1) {
            vc.btnType = kXiaocuxiaocode;
        }
        if (num == 2) {
            vc.btnType = kXiaocuxiaocode;
        }
    }
    vc.dayCount = _dayCount;
    [self.rdv_tabBarController.navigationController pushViewController:vc animated:YES];
    NSLog(@"btnTag:%d",sender.tag);
}

#pragma ---------------------   Navigation背景   -----------------------
- (void)setCustomNavBar
{
#if kIOSVersion7Later
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
#endif
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           [UIFont boldSystemFontOfSize:19], NSFontAttributeName, nil]];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >=7.0) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar/bigAreaNav_bg.png"]
                                           forBarMetrics:UIBarMetricsDefault];
    }else{
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar/bigAreaNav_bg.png"]
                                           forBarMetrics:UIBarMetricsDefault];
    }
    
}

-(void)initRightView{
    checkList = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-164)];
    //    checkList = [[UITableView alloc]init];
    checkList.delegate = self;
    checkList.dataSource = self;
    [checkList setHidden:YES];
    [self.view addSubview:checkList];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *cellIdentifier = @"NoticeListCell";
    CheckListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[CheckListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.lblTitle.text = self.arrContents[indexPath.row][@"title"];
    cell.lblcontent.text = self.arrContents[indexPath.row][@"context"];
    
    cell.lblCreateTime.text = [self.arrContents[indexPath.row][@"createTime"] substringToIndex:10];
    if ([self.arrContents[indexPath.row][@"isRead"] isEqualToString:@"01"]) {
        cell.lblTitle.textColor = [UIColor darkGrayColor];
        cell.lblCreateTime.textColor = [UIColor darkGrayColor];
        cell.lblcontent.textColor = [UIColor darkGrayColor];
        cell.view.hidden = NO;
    }else if ([self.arrContents[indexPath.row][@"isRead"] isEqualToString:@"02"]) {
        cell.lblTitle.textColor = [UIColor lightGrayColor];
        cell.lblCreateTime.textColor = [UIColor lightGrayColor];
        cell.lblcontent.textColor = [UIColor lightGrayColor];
        cell.view.hidden = YES;
    }
    cell.tag = 100+[self.arrContents[indexPath.row][@"isRead"] intValue];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *strTitle = _noticeArr[indexPath.row][@"title"];
    CGFloat hight1 = [strTitle boundingRectWithSize:CGSizeMake(215, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}
                                            context:nil].size.height;
    
    NSString *strContent = _noticeArr[indexPath.row][@"content"];
    CGFloat hight2 = [strContent boundingRectWithSize:CGSizeMake(275, CGFLOAT_MAX)
                                              options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}
                                              context:nil].size.height+10+22+10+5;
    if (hight2 > 20) {
        return hight1+15+40+10;
    }
    else
    {
        return hight1+15+20+10;
    }
}

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

#pragma mark =============  UITableView Delegate  =============

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString* nid = _noticeArr[indexPath.row][@"noticeId"];
    [DBDaoMgr updateNoticeById:nid];
    [[NSNotificationCenter defaultCenter] postNotificationName:kShouldReQueryCountInit object:nil];
    
    
    
    NoteDetailViewController *detailVC = [[NoteDetailViewController alloc] initWithNibName:nil bundle:nil];
    detailVC.dictNotice = self.arrContents[indexPath.row];
    [self.rdv_tabBarController.navigationController pushViewController:detailVC animated:YES];
}
- (void)requestDatas
{
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryNoticeListEX";
    NSDictionary *params = @{@"type":@"2"};
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:params
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
    
}
#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"queryInitCountEX"]) {
        
        //        vcTask.arrItemTitles = [NSMutableArray arrayWithArray:
        //                                @[[NSString stringWithFormat:@"未完成(%@)", result[@"unFinishedTaskCount"]],
        //                                  [NSString stringWithFormat:@"已完成(%@)", result[@"finishedTaskCount"]]]];
        //        [vcTask reloadItemTitles];
        self.unFinishedTaskCount = result[@"unFinishedTaskCount"];
        self.progress = [NSString stringWithFormat:@"%@",result[@"progress"]];
        
        [DataMgr getInstance].unfinishedTaskCountToDacuxiao = result[@"dacuxiaoUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToXiaocuxiao = result[@"xiaocuxiaoUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToDaoshi = result[@"daoshiUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToMeicheng = result[@"meichenUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToXiuxiqu = result[@"xiuxiquUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToGuanggaowei = result[@"guanggaoweiUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToBeijingyinyue = result[@"beijingyinyueUndoneNum"];
        [DataMgr getInstance].unfinishedTaskCountToYingyunwuliao = result[@"yingyunwuliaoUndoneNum"];
        
        [DataMgr getInstance].serverTime = result[@"serverTime"];
        NSNotification *notification =[NSNotification notificationWithName:@"reloadNumber" object:nil userInfo:nil];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    } else {
        self.arrContents = result[@"noticeList"];
        [checkList reloadData];
        if(_arrContents!= nil &&[_arrContents count]>0){
            
            [self setTableViewBackground:nil];
            
        } else{
            [self setTableViewBackground:@"pic_zanwushuju.png"];
            
        }
        
    }
    
}
-(void)setTableViewBackground:(NSString *) imgName{
    
    if(imgName != nil){
        UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
        checkList.backgroundView = view;
    }
    
}
- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
