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

#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 NewHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView* prcessImage;
    UIView* prcessBgView;
    UIButton* btnUnRead;
    UILabel* lblNum;
    
    UIView* cxView;
    UIView* dayView;
    UILabel *strnum;
    
    UITableView *checkList;
    UIView  *leftView;
    UIImageView *imageViewHeaderBG;
   
}
@end

@implementation NewHomeViewController
- (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.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/top_bg_tab_left.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];
    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(NewHomeViewController) *weakSelf = self;
    typePickerView.typePickerClick = ^(int index) {
        [weakSelf typePickerViewClickAtIndex:index];
    };
    
    [self initLeftView];
    [self  initRightView];
    

}
- (void)typePickerViewClickAtIndex:(int)index
{
    if(index ==0){
        [checkList setHidden:true];
        [leftView setHidden:false];
        imageViewHeaderBG.image = [UIImage imageNamed:@"NavBar/top_bg_tab_left.png"];
    } else{
        [checkList setHidden:false];
        [leftView setHidden:true];
        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 queryInitCount];
    [self requestDatas];
    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
        //[self.dictTableDatas removeAllObjects];
        //[self reRequestTableViewDatas];
        
        [UIApplication saveObject:@NO type:Number key:kShouldRequestDataWhenBack];
    }
    
}
-(void)initLeftView{
    
    leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 120, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [leftView setUserInteractionEnabled:true];
//    [leftView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:leftView];
    
    
    UIView* descView = [[UIView alloc] init];
    [leftView addSubview:descView];
    [descView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];

    
    lblNum = [[UILabel alloc] init];
    lblNum.text = @"33";
    lblNum.textColor = [UIColor whiteColor];
    lblNum.font = [UIFont systemFontOfSize:27];
    [descView addSubview:lblNum];
    [lblNum autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [lblNum autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:20];
    
    UILabel* lbldone = [[UILabel alloc] init];
    lbldone.text = @"% 已完成";
    lbldone.textColor = [UIColor whiteColor];
    lbldone.font = [UIFont systemFontOfSize:13];
    [descView addSubview:lbldone];
    [lbldone autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblNum];
    [lbldone autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblNum withOffset:-4];
    
    btnUnRead = [[UIButton alloc] init];
    [btnUnRead setBackgroundImage:[UIImage imageNamed:@"TabBar/bg_notes_home.png"] forState:UIControlStateNormal];
    [btnUnRead setTitle:@"" forState:UIControlStateNormal];
    btnUnRead.titleLabel.font = [UIFont systemFontOfSize:10];
    btnUnRead.titleLabel.textAlignment=NSTextAlignmentRight;
    
    [descView addSubview:btnUnRead];
    [btnUnRead autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [btnUnRead autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblNum];
    [btnUnRead autoSetDimensionsToSize:CGSizeMake(107, 25)];
    
    
    
    NSString* str = [NSString stringWithFormat:@"个任务待上报"];
    //[btnUnRead setTitle:str forState:UIControlStateNormal];
    
    
    
    CGFloat width1 = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,23)
                                       options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}
                                       context:nil].size.width;
    UILabel *strright=[[UILabel alloc] init];
    strright.text=str;
    strright.textAlignment=NSTextAlignmentRight;
    strright.font=[UIFont systemFontOfSize:10];
    [btnUnRead addSubview:strright];
    [strright autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:btnUnRead withOffset:-4];
    [strright autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:btnUnRead withOffset:0];
    [strright autoSetDimensionsToSize:CGSizeMake(width1, 23)];
    strright.textColor=[UIColor whiteColor];
    
    
    UILabel *strleft=[[UILabel alloc] init];
    strleft.text=@"您有";
    [btnUnRead addSubview:strleft];
    CGFloat width2 = [strleft.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,23)
                                                options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}
                                                context:nil].size.width;
    [strleft autoSetDimension:ALDimensionWidth toSize:width2];
    [strleft autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:btnUnRead];
    [strleft autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:btnUnRead withOffset:4];
    [strleft autoSetDimension:ALDimensionHeight toSize:23];
    strleft.font=[UIFont systemFontOfSize:10];
    strleft.textColor=[UIColor whiteColor];
    
    
    strnum=[[UILabel alloc] init];
    strnum.text=[NSString stringWithFormat:@"%@",_unFinishedTaskCount];
    strnum.font=[UIFont boldSystemFontOfSize:11];
    
    strnum.textAlignment=NSTextAlignmentCenter;
    
    strnum.textColor=[UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
    
    [btnUnRead addSubview:strnum];
    [strnum autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:btnUnRead];
    [strnum autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:btnUnRead];
    [strnum autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:strright];
    
    [strnum autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:strleft];
    
    
    
    
    
    
    
    UILabel* lbllocation = [[UILabel alloc] init];
    lbllocation.textColor = [UIColor whiteColor];
    lbllocation.text = [DataMgr getInstance].shopName;
    lbllocation.font = [UIFont systemFontOfSize:12];
    [descView addSubview:lbllocation];
    
    
    
    [lbllocation autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:btnUnRead withOffset:-4];
    [lbllocation autoAlignAxis:ALEdgeRight toSameAxisOfView:btnUnRead];
    
    
    
    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)];
    
    UIView* prcessView = [[UIView alloc] init];
    prcessView.layer.cornerRadius = 5;
    [descView addSubview:prcessView];
    [prcessView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblNum withOffset:10];
    [prcessView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:12];
    [prcessView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [prcessView autoSetDimension:ALDimensionHeight toSize:10.5];
    
    prcessImage = [[UIImageView alloc] init];
    prcessImage.image = [UIImage imageNamed:@"NavBar/progress_bar_gary.png"];
    [prcessView addSubview:prcessImage];
    [prcessImage autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    prcessBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 10)];
    prcessBgView.clipsToBounds = YES;
    prcessBgView.layer.cornerRadius = 5;
    prcessBgView.backgroundColor = [UIColor whiteColor];
    [prcessView addSubview:prcessBgView];
    
    UIImageView* bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    bgImage.image = [UIImage imageNamed:@"NavBar/progress_bar.png"];
    [prcessBgView addSubview:bgImage];
    
//    UIScrollView* scrollerView = [[UIScrollView alloc] init];
    UIScrollView* scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame)-200)];
    scrollerView.backgroundColor = [UIColor clearColor];
    [leftView addSubview:scrollerView];

//    [scrollerView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
//    [scrollerView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:descView];
  
//    [scrollerView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:leftView];
    
    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);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadUI) name:@"reloadNumber" object:nil];
}

-(void)reloadUI
{
    //NSString* str = [NSString stringWithFormat:@"个任务待上报",_unFinishedTaskCount];
    
    strnum.text=[NSString stringWithFormat:@"%@",_unFinishedTaskCount];
    
    lblNum.text = self.progress;
    float W = prcessImage.frame.size.width;
    CGFloat f = [self.progress floatValue]/100;
    prcessBgView.frame = CGRectMake(0, 0, (W*f)-1.5, 10);
    
    NSString* num = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToDacuxiao];
    if (![num isEqualToString:@""]&&![num isEqualToString:@"0"]) {
        for (UIButton* view in cxView.subviews) {
            if (view.tag == 200) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 250) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in cxView.subviews) {
            if (view.tag == 200) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 250) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    
    NSString* num1 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToXiaocuxiao];
    if (![num1 isEqualToString:@""]&&![num1 isEqualToString:@"0"]) {
        for (UIButton* view in cxView.subviews) {
            if (view.tag == 201) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 251) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in cxView.subviews) {
            if (view.tag == 201) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 251) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    
    NSString* num2 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToDaoshi];
    if (![num2 isEqualToString:@""]&&![num2 isEqualToString:@"0"]) {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 100) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 150) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 100) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 150) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    
    NSString* num3 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToMeicheng];
    if (![num3 isEqualToString:@""]&&![num3 isEqualToString:@"0"]) {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 101) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 151) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 101) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 151) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    
    NSString* num4 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToXiuxiqu];
    if (![num4 isEqualToString:@""]&&![num4 isEqualToString:@"0"]) {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 102) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 152) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 102) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 152) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    NSString* num5 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToGuanggaowei ];
    if (![num5 isEqualToString:@""]&&![num5 isEqualToString:@"0"]) {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 103) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 153) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 103) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 153) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    NSString* num6 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToBeijingyinyue];
    if (![num6 isEqualToString:@""]&&![num6 isEqualToString:@"0"]) {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 104) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 154) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 104) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 154) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    NSString* num7 = [NSString stringWithFormat:@"%@",[DataMgr getInstance].unfinishedTaskCountToYingyunwuliao];
    if (![num7 isEqualToString:@""]&&![num7 isEqualToString:@"0"]) {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 105) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 155) {
                        view1.hidden = NO;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for (UIButton* view in dayView.subviews) {
            if (view.tag == 105) {
                
                for (UIButton* view1 in view.subviews)
                {
                    if (view1.tag == 155) {
                        view1.hidden = YES;
                        break;
                    }
                }
            }
        }
    }
    [self.view addSubview:leftView];
}
-(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:true];
    [self.view addSubview:checkList];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(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) {
        if ([type isEqualToString:@"rc1"]) {
            btn.frame = CGRectMake(num*w, h, w, h);
        }
        else
        {
            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];
    
    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:@"rc"]) {
        btn.tag = 100+num;
        lblUndoneNum.tag = 150+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;
        lblUndoneNum.tag = 250+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;
        }
    }
    
    lblUndoneNum.hidden = YES;
    
    [btn addTarget:self action:@selector(btnSelcted:) forControlEvents:UIControlEventTouchDown];
    return btn;
}

-(void)btnSelcted:(UIButton*)sender
{
    TaskListViewController* vc = [[TaskListViewController alloc] init];
    if (sender.tag < 200) {
               int num = sender.tag-100;
        if (num == 0) {
            vc.btnType = kDaoshicode;
            vc.title=@"任务一览－导视";
        }
        if (num == 1) {
            vc.btnType =  kMeichengcode;
            vc.title=@"任务一览－美陈";
        }
        if (num == 2) {
            vc.btnType = kXiuxiqucode;
            vc.title=@"任务一览－公共区域";
        }
        if (num == 3) {
            vc.btnType = kGuanggaoweicode;
            vc.title=@"任务一览－广告位";
        }
        if (num == 4) {
            BackGroundMusicViewController* bvc = [[BackGroundMusicViewController alloc] init];
             [self.rdv_tabBarController.navigationController pushViewController:bvc animated:YES];
            return;
        }
        if (num == 5) {
            vc.btnType = kYingyunwuliaocode;
            vc.title=@"任务一览－综合物料";
        }
        
    }
    else
    {
        int num = sender.tag-200;
        if (num == 0) {
            vc.btnType = kDacuxiaocode;
            vc.title=@"任务一览－大促销";
        }
        if (num == 1) {
            vc.btnType = kXiaocuxiaocode;
            vc.title=@"任务一览－小促销";
        }
        if (num == 2) {
            vc.btnType = kXiaocuxiaocode;
            vc.title=@"任务一览－小促销";
        }
    }
    
    [self.rdv_tabBarController.navigationController pushViewController:vc animated:YES];
}
- (void)queryInitCount
{
    NSString *interface = @"queryInitCountEX";
    AFHTTPRequestOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                                interface:interface
                                                                   params:nil
                                                                 dataType:BRNetMgrDataTypeJSON
                                                                   method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}


-(void)selectNoticeDatas
{
    _arrContents = [DBDaoMgr fetchNoticeByUserId:[DataMgr getInstance].userID];
//    _arrContents = [DBDaoMgr fetchNoticeByUserId:@"277"];
    
    [checkList reloadData];
    
}
- (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)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.
}
*/





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


- (UIImageView *)imageVNoResult
{
    
    if (!_imageVNoResult) {
        self.imageVNoResult = [[UIImageView alloc ]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-244)] ;
        _imageVNoResult.image = [UIImage imageNamed:@"pic_zanwushuju.png"];
        [self.view addSubview:_imageVNoResult];
    }
    return _imageVNoResult;
}
-(void)setTableViewBackground:(NSString *) imgName{
    
    if(imgName != nil){
        UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
        checkList.backgroundView = view;
    }
 
}




@end


@implementation CheckListCell

- (void)dealloc
{
    self.lblTitle = nil;
    self.lblCreateTime = nil;
    self.viewLine = nil;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.lblCreateTime autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:12];
        [self.lblCreateTime autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
        [self.lblCreateTime autoSetDimensionsToSize:CGSizeMake(80, 20)];
        
        [self.lblTitle autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
        [self.lblTitle autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
        [self.lblTitle autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.lblCreateTime withOffset:-5];
        //[self.lblTitle autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.lblCreateTime withOffset:-10];
        
        [self.view autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.lblTitle withOffset:4];
        [self.view autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.lblTitle withOffset:-3];
        [self.view autoSetDimensionsToSize:CGSizeMake(6, 6)];
        
        [self.lblcontent autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
        [self.lblcontent autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
        [self.lblcontent autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblTitle withOffset:10];
        [self.viewLine autoSetDimension:ALDimensionHeight toSize:0.5];
        //        [self.viewLine autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblTitle withOffset:5];
        [self.viewLine autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 10, 0, 10) excludingEdge:ALEdgeTop];
        
        [self.imageview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self];
        [self.imageview autoPinEdge: ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblcontent withOffset:4];
        [self.imageview autoSetDimensionsToSize:CGSizeMake(8, 15)];
    }
    return self;
}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated
//{
//    [super setSelected:selected animated:animated];
//    if (selected) {
//        if (self.tag == 101) {//未读
//            self.lblCreateTime.backgroundColor = RGBColor(229, 0, 129, 1);
//        }else {
//            self.lblCreateTime.backgroundColor = [UIColor grayColor];
//        }
//
//    }
//
//}
//
//- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
//{
//    [super setHighlighted:highlighted animated:animated];
//    if (highlighted) {
//        if (self.tag == 101) {//未读
//            self.lblCreateTime.backgroundColor = RGBColor(229, 0, 129, 1);
//        }else {
//            self.lblCreateTime.backgroundColor = [UIColor grayColor];
//        }
//    }
//
//}

#pragma mark =============  LazyLoad Properties  =============

- (UIView *)viewLine
{
    if (!_viewLine) {
        self.viewLine = [UIView newAutoLayoutView];
        _viewLine.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_viewLine];
    }
    return _viewLine;
}
- (UIView *)view
{
    if (!_view) {
        self.view = [UIView newAutoLayoutView];
        _view.backgroundColor = RGBColor(246, 88, 95, 1);
        _view.layer.cornerRadius = 3;
        [self.contentView addSubview:_view];
    }
    return _view;
}
- (UILabel *)lblcontent
{
    if (!_lblcontent) {
        self.lblcontent = [UILabel newAutoLayoutView];
        self.lblcontent.textAlignment = NSTextAlignmentLeft;
        self.lblcontent.backgroundColor = [UIColor clearColor];
        self.lblcontent.textColor = [UIColor blackColor];
        self.lblcontent.font = [UIFont systemFontOfSize:13];
        self.lblcontent.numberOfLines = 2;
        [self.contentView addSubview:_lblcontent];
    }
    return _lblcontent;
}

- (UILabel *)lblTitle
{
    if (!_lblTitle) {
        self.lblTitle = [UILabel newAutoLayoutView];
        self.lblTitle.textAlignment = NSTextAlignmentLeft;
        self.lblTitle.backgroundColor = [UIColor clearColor];
        self.lblTitle.textColor = [UIColor blackColor];
        self.lblTitle.font = [UIFont systemFontOfSize:15];
        self.lblTitle.numberOfLines = 0;
        [self.contentView addSubview:_lblTitle];
    }
    return _lblTitle;
}

- (UILabel *)lblCreateTime
{
    if (!_lblCreateTime) {
        self.lblCreateTime = [UILabel newAutoLayoutView];
        self.lblCreateTime.textColor = [UIColor whiteColor];
        self.lblCreateTime.textAlignment = NSTextAlignmentCenter;
        self.lblCreateTime.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_lblCreateTime];
    }
    return _lblCreateTime;
}
- (UIImageView *)imageview
{
    if (!_imageview) {
        self.imageview = [UIImageView newAutoLayoutView];
        _imageview.image = [UIImage imageNamed:@"Task_Point/image_point_arrow.png"];
        [self.contentView addSubview:_imageview];
    }
    return _imageview;
}


@end

