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

#import "SATaskNewDetailViewController.h"
#import "RDVTabBarController.h"
#import "TaskPointViewController.h"
#import "SATaskPointDetailViewController.h"
#import "QuickLook/QuickLook.h"
#import "DBDaoMgr.h"

@interface SATaskNewDetailViewController ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>
{
    UILabel* lblTaskName;
    UIButton* btnBXNum;
    UIButton* btnKXNum;
    UILabel* lblPresent;
    UILabel* lblYear;
    UILabel* lblDay;
    
    UILabel* lblEndDay;
    UILabel* lblEndYear;
    
    
    UILabel* lblStand;
    
    UILabel* lblSYDay;
    UILabel* lblSY;
    UILabel* lblSY1;
    UILabel* lblDesc;
    UILabel* lblGQ;
    UILabel* lblUnStart;
    
    UILabel* lblActityEndDay;
    UILabel* lblActityEndYear;
    UILabel* lblActityDay;
    UILabel* lblActityYear;
    
    UIScrollView* sv;
    
    UILabel* lblInstructionBookTitle;
    UILabel* lblInstructionBookSize;
    UILabel* lblInstructionBookStatus;
    UIButton* btnEnter;
    
    NSTimer* reloadTimer;
}
@property(nonatomic,strong)NSDictionary* activityDetailsArray;
@property(nonatomic,strong)NSString* processStr;

@property(nonatomic,strong)NSArray* fileInfoArr;

@property(nonatomic,strong)NSString* downloadType;
@property(nonatomic,strong)NSString* fileNameStr;
@end

@implementation SATaskNewDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton* btnBack = [[UIButton alloc] init];
//    [btnBack setBackgroundImage:[UIImage imageNamed:@"Task/btn_nav_back.png"] forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBack];
    [btnBack autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [btnBack autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:27];
    [btnBack autoSetDimensionsToSize:CGSizeMake(30, 27)];
    
    UIImageView* img = [[UIImageView alloc] init];
    img.image = [UIImage imageNamed:@"Task/btn_nav_back.png"];
    [btnBack addSubview:img];
    [img autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    [img autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [img autoSetDimensionsToSize:CGSizeMake(13, 27)];
    
    _downloadType = @"";
    
    UILabel* lblTitle = [[UILabel alloc] init];
    if ([self.btnType isEqualToString:kDacuxiaocode]||[self.btnType isEqualToString:kXiaocuxiaocode]) {
        lblTitle.text = @"企划部-促销";
    }
    if ([self.btnType isEqualToString:kDaoshicode]) {
        lblTitle.text = @"企划部-导视";
    }
    if ([self.btnType isEqualToString:kMeichengcode]) {
        lblTitle.text = @"企划部-美陈";
    }
    if ([self.btnType isEqualToString:kXiuxiqucode]) {
        lblTitle.text = @"企划部-公共区域";
    }
    if ([self.btnType isEqualToString:kGuanggaoweicode]) {
        lblTitle.text = @"企划部-广告位";
    }
    if ([self.btnType isEqualToString:kBeijingyinyuecode]) {
        lblTitle.text = @"企划部-背景音乐";
    }
    if ([self.btnType isEqualToString:kYingyunwuliaocode]) {
        lblTitle.text = @"企划部-综合物料";
    }
    lblTitle.textColor = RGBColor(40, 171, 198, 1);
    lblTitle.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:lblTitle];
    [lblTitle autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [lblTitle autoAlignAxis:ALAxisHorizontal toSameAxisOfView:btnBack];
    
    
    lblTaskName = [[UILabel alloc] init];
    lblTaskName.text = @"2015年6月第二季会员日";
    lblTaskName.textColor = [UIColor darkGrayColor];
    lblTaskName.font = [UIFont boldSystemFontOfSize:16];
    
    [self.view addSubview:lblTaskName];
    [lblTaskName autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [lblTaskName autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTitle withOffset:5];
    [lblTaskName setTextAlignment:NSTextAlignmentCenter];
    
    
    UIImageView* presentImage = [[UIImageView alloc] init];
    presentImage.image = [UIImage imageNamed:@"Task/ball.png"];
    [self.view addSubview:presentImage];
    [presentImage autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [presentImage autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTaskName withOffset:10];
    [presentImage autoSetDimensionsToSize:CGSizeMake(145, 145)];
    
    lblPresent = [[UILabel alloc] init];
    lblPresent.text = @"85";
    lblPresent.textColor = RGBColor(40, 171, 198, 1);
    lblPresent.font = [UIFont systemFontOfSize:40];
    [self.view addSubview:lblPresent];
    [lblPresent autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view withOffset:-5];
    [lblPresent autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:presentImage withOffset:18];
    
    UILabel* lblSign = [[UILabel alloc] init];
    lblSign.text = @"%";
    lblSign.textColor = RGBColor(40, 171, 198, 1);
    lblSign.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:lblSign];
    [lblSign autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblPresent withOffset:4];
    [lblSign autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblPresent withOffset:-5];
    
    btnBXNum = [[UIButton alloc] init];
    btnBXNum.titleLabel.font = [UIFont systemFontOfSize:14];
    [btnBXNum setTitle:@"16" forState:UIControlStateNormal];
    [btnBXNum setBackgroundImage:[UIImage imageNamed:@"Task/icon_bg_red.png"] forState:UIControlStateNormal];
    [self.view addSubview:btnBXNum];
    [btnBXNum autoAlignAxis:ALAxisHorizontal toSameAxisOfView:presentImage];
    [btnBXNum autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:presentImage withOffset:-20];
    [btnBXNum autoSetDimensionsToSize:CGSizeMake(20, 20)];
    
    UILabel* lblBX = [[UILabel alloc] init];
    lblBX.text = @"必选";
    lblBX.textColor = RGBColor(153, 153, 153, 1);
    lblBX.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:lblBX];
    [lblBX autoAlignAxis:ALAxisHorizontal toSameAxisOfView:btnBXNum];
    [lblBX autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:btnBXNum withOffset:-5];
    
    btnKXNum = [[UIButton alloc] init];
    btnKXNum.titleLabel.font = [UIFont systemFontOfSize:14];
    [btnKXNum setTitle:@"2" forState:UIControlStateNormal];
    [btnKXNum setBackgroundImage:[UIImage imageNamed:@"Task/icon_bg_green.png"] forState:UIControlStateNormal];
    [self.view addSubview:btnKXNum];
    [btnKXNum autoAlignAxis:ALAxisHorizontal toSameAxisOfView:presentImage];
    [btnKXNum autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:presentImage withOffset:20];
    [btnKXNum autoSetDimensionsToSize:CGSizeMake(20, 20)];
    
    UILabel* lblKX = [[UILabel alloc] init];
    lblKX.text = @"可选";
    lblKX.textColor = RGBColor(153, 153, 153, 1);
    lblKX.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:lblKX];
    [lblKX autoAlignAxis:ALAxisHorizontal toSameAxisOfView:btnKXNum];
    [lblKX autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:btnKXNum withOffset:5];
    
    UILabel* lblStart = [[UILabel alloc] init];
    lblStart.text = @"活动起止时间";
    lblStart.textColor = RGBColor(153, 153, 153, 1);
    lblStart.font = [UIFont boldSystemFontOfSize:13];
    [self.view addSubview:lblStart];
    [lblStart autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblBX];
    [lblStart autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:presentImage];
    
    lblYear = [[UILabel alloc] init];
    lblYear.text = @"2015";
    lblYear.textColor = RGBColor(153, 153, 153, 1);
    lblYear.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblYear];
    [lblYear autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblStart];
    [lblYear autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblStart withOffset:3];
    
    lblDay = [[UILabel alloc] init];
    lblDay.text = @"06.28";
    lblDay.textColor = RGBColor(40, 171, 198, 1);
    lblDay.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblDay];
    [lblDay autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblYear withOffset:2];
    [lblDay autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblYear];
    
    UILabel* lblGe = [[UILabel alloc] init];
    lblGe.text = @" - ";
    lblGe.textColor = RGBColor(153, 153, 153, 1);
    lblGe.font = [UIFont boldSystemFontOfSize:12];
    [self.view addSubview:lblGe];
    [lblGe autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblDay];
    [lblGe autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblDay];
    
    lblEndYear = [[UILabel alloc] init];
    lblEndYear.text = @"2015";
    lblEndYear.textColor = RGBColor(153, 153, 153, 1);
    lblEndYear.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblEndYear];
    [lblEndYear autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblGe];
    [lblEndYear autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblYear];
    
    lblEndDay = [[UILabel alloc] init];
    lblEndDay.text = @"07.28";
    lblEndDay.textColor = RGBColor(40, 171, 198, 1);
    lblEndDay.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblEndDay];
    [lblEndDay autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblEndYear withOffset:2];
    [lblEndDay autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblDay];
    
    
    lblActityEndDay = [[UILabel alloc] init];
    lblActityEndDay.text = @"07.28";
    lblActityEndDay.textColor = RGBColor(40, 171, 198, 1);
    lblActityEndDay.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblActityEndDay];
    [lblActityEndDay autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view withOffset:-10];
    [lblActityEndDay autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblDay];
    
    lblActityEndYear = [[UILabel alloc] init];
    lblActityEndYear.text = @"2015";
    lblActityEndYear.textColor = RGBColor(153, 153, 153, 1);
    lblActityEndYear.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblActityEndYear];
    [lblActityEndYear autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:lblActityEndDay withOffset:-2];
    [lblActityEndYear autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblActityEndDay];
    
    UILabel* lblGe1 = [[UILabel alloc] init];
    lblGe1.text = @" - ";
    lblGe1.textColor = RGBColor(153, 153, 153, 1);
    lblGe1.font = [UIFont boldSystemFontOfSize:12];
    [self.view addSubview:lblGe1];
    [lblGe1 autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:lblActityEndYear];
    [lblGe1 autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblActityEndYear];
    
    lblActityDay = [[UILabel alloc] init];
    lblActityDay.text = @"06.28";
    lblActityDay.textColor = RGBColor(40, 171, 198, 1);
    lblActityDay.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblActityDay];
    [lblActityDay autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:lblGe1];
    [lblActityDay autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblActityEndDay];
    
    lblActityYear = [[UILabel alloc] init];
    lblActityYear.text = @"2015";
    lblActityYear.textColor = RGBColor(153, 153, 153, 1);
    lblActityYear.font = [UIFont boldSystemFontOfSize:11];
    [self.view addSubview:lblActityYear];
    [lblActityYear autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:lblActityDay withOffset:-2];
    [lblActityYear autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:lblActityEndDay];
    
    
    
    UILabel* lblEnd = [[UILabel alloc] init];
    lblEnd.text = @"上报起止时间";
    lblEnd.textColor = RGBColor(153, 153, 153, 1);
    lblEnd.font = [UIFont boldSystemFontOfSize:12];
    [self.view addSubview:lblEnd];
    [lblEnd autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view withOffset:-10];
    [lblEnd autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:lblEndYear withOffset:-3];
    
    UIView* topLineView = [[UIView alloc] init];
    topLineView.backgroundColor = RGBColor(216, 216, 216, 1);
    [self.view addSubview:topLineView];
    [topLineView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblBX];
    [topLineView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:lblKX];
    [topLineView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblEndYear withOffset:5];
    [topLineView autoSetDimension:ALDimensionHeight toSize:1];
    
    lblGQ = [[UILabel alloc] init];
    lblGQ.textColor = [UIColor lightGrayColor];
    lblGQ.text = @"任务已过期";
    lblGQ.font = [UIFont boldSystemFontOfSize:16];
    lblGQ.hidden = YES;
    [self.view addSubview:lblGQ];
    [lblGQ autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [lblGQ autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topLineView withOffset:32];
    
    lblUnStart = [[UILabel alloc] init];
    lblUnStart.textColor = [UIColor lightGrayColor];
    lblUnStart.text = @"任务尚未开始";
    lblUnStart.font = [UIFont boldSystemFontOfSize:16];
    lblUnStart.hidden = YES;
    [self.view addSubview:lblUnStart];
    [lblUnStart autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [lblUnStart autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topLineView withOffset:32];
    
    lblSY = [[UILabel alloc] init];
    lblSY.text = @"剩余";
    lblSY.font = [UIFont boldSystemFontOfSize:16];
    lblSY.textColor =RGBColor(153, 153, 153, 1);
    [self.view addSubview:lblSY];
    [lblSY autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view withOffset:-15];
    [lblSY autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topLineView withOffset:8];
    
    lblSYDay = [[UILabel alloc] init];
    lblSYDay.text = @"8";
    lblSYDay.font = [UIFont boldSystemFontOfSize:16];
    lblSYDay.textColor =RGBColor(244, 160, 0, 1);
    [self.view addSubview:lblSYDay];
    [lblSYDay autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblSY];
    [lblSYDay autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblSY];
    
    lblSY1 = [[UILabel alloc] init];
    lblSY1.text = @"天";
    lblSY1.font = [UIFont boldSystemFontOfSize:16];
    lblSY1.textColor = RGBColor(153, 153, 153, 1);
    [self.view addSubview:lblSY1];
    [lblSY1 autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblSY];
    [lblSY1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblSYDay];
    
    lblDesc = [[UILabel alloc] init];
    lblDesc.text = @"商场在上报时间内不能完成上报或整改上报\n评分将为0";
    lblDesc.textAlignment = NSTextAlignmentCenter;
    lblDesc.numberOfLines = 2;
    lblDesc.font = [UIFont boldSystemFontOfSize:12];
    lblDesc.textColor = RGBColor(244, 160, 0, 1);
    [self.view addSubview:lblDesc];
    [lblDesc autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [lblDesc autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblSY withOffset:8];
    [lblDesc autoSetDimension:ALDimensionHeight toSize:40];
    
    UIView* footLineView = [[UIView alloc] init];
    footLineView.backgroundColor = RGBColor(216, 216, 216, 1);
    [self.view addSubview:footLineView];
    [footLineView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblBX];
    [footLineView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:lblKX];
    [footLineView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topLineView withOffset:80];
    [footLineView autoSetDimension:ALDimensionHeight toSize:1];
    
    btnEnter = [[UIButton alloc] init];
    btnEnter.titleLabel.font = [UIFont systemFontOfSize:18];
    [btnEnter setTitle:@"查看商场执行情况" forState:UIControlStateNormal];
    [btnEnter setBackgroundColor:RGBColor(40, 171, 198, 1)];
    btnEnter.layer.cornerRadius = 5;
    [btnEnter addTarget:self action:@selector(enterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnEnter];
    [btnEnter autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblBX];
    [btnEnter autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:lblKX];
    [btnEnter autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    [btnEnter autoSetDimension:ALDimensionHeight toSize:35];
    
    
    
    sv = [[UIScrollView alloc] init];
    [self.view addSubview:sv];
    [sv autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    [sv autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    [sv autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:footLineView withOffset:5];
//    [sv autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:btnEnter withOffset:-70];
//    [sv autoSetDimension:ALDimensionHeight toSize:90];
    
    lblStand = [[UILabel alloc] init];
    lblStand.text = @"物料指导书中所规划的必选物料，各区域商场保质保量完成，除了指导书中明确可以改动的地方，其它不得擅自修改。\n此外，可选物料商场根据自身实际情况布置，一下链接是会员日终端终端延展物料的完稿，指导书和渠道二维码。";
    lblStand.textAlignment = NSTextAlignmentLeft;
    lblStand.numberOfLines = 0;
    lblStand.font = [UIFont systemFontOfSize:12];
    lblStand.textColor = [UIColor lightGrayColor];
    [sv addSubview:lblStand];
    
    UIView* finstructionLineView = [[UIView alloc] init];
    finstructionLineView.backgroundColor = RGBColor(216, 216, 216, 1);
    [self.view addSubview:finstructionLineView];
    [finstructionLineView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblBX];
    [finstructionLineView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:lblKX];
    [finstructionLineView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:sv withOffset:5];
    [finstructionLineView autoSetDimension:ALDimensionHeight toSize:1];
    
    UIView* instructionBookView = [[UIView alloc] init];
    [self.view addSubview:instructionBookView];
    [instructionBookView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lblBX];
    [instructionBookView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:lblKX];
    [instructionBookView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:finstructionLineView];
    [instructionBookView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:btnEnter];
    
    UIButton* btnInstruction = [[UIButton alloc] init];
    [btnInstruction setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [btnInstruction setBackgroundImage:[UIImage imageWithColor:RGBColor(216, 216, 216, 1)] forState:UIControlStateHighlighted];
    [btnInstruction addTarget:self action:@selector(InstructionClick) forControlEvents:UIControlEventTouchUpInside];
    [instructionBookView addSubview:btnInstruction];
    [btnInstruction autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    UIImageView* pdfImage = [[UIImageView alloc] init];
    pdfImage.image = [UIImage imageNamed:@"Task/icon_pdf.png"];
    [btnInstruction addSubview:pdfImage];
    [pdfImage autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(7, -3, 12, 0) excludingEdge:ALEdgeRight];
    [pdfImage autoSetDimension:ALDimensionWidth toSize:50];
    
    lblInstructionBookTitle = [[UILabel alloc] init];
    lblInstructionBookTitle.text = @"活动指导书";
    lblInstructionBookTitle.textColor = [UIColor darkGrayColor];
    lblInstructionBookTitle.font = [UIFont boldSystemFontOfSize:13];
    [btnInstruction addSubview:lblInstructionBookTitle];
    [lblInstructionBookTitle autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:pdfImage withOffset:5];
    [lblInstructionBookTitle autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:pdfImage withOffset:-1];
    [lblInstructionBookTitle autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:btnInstruction withOffset:5];
    [lblInstructionBookTitle autoSetDimension:ALDimensionHeight toSize:22];
    
    lblInstructionBookSize = [[UILabel alloc] init];
    lblInstructionBookSize.text = @"大小:15.5Mb";
    lblInstructionBookSize.textColor = [UIColor darkGrayColor];
    lblInstructionBookSize.font = [UIFont boldSystemFontOfSize:13];
    [btnInstruction addSubview:lblInstructionBookSize];
    [lblInstructionBookSize autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:pdfImage withOffset:5];
    [lblInstructionBookSize autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:pdfImage withOffset:1];
    [lblInstructionBookSize autoSetDimension:ALDimensionHeight toSize:22];
    
    lblInstructionBookStatus = [[UILabel alloc] init];
    lblInstructionBookStatus.text = @"(点击下载)";
    lblInstructionBookStatus.textColor = [UIColor darkGrayColor];
    lblInstructionBookStatus.font = [UIFont boldSystemFontOfSize:13];
    [btnInstruction addSubview:lblInstructionBookStatus];
    [lblInstructionBookStatus autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblInstructionBookSize withOffset:8];
    [lblInstructionBookStatus autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblInstructionBookSize];
    [lblInstructionBookStatus autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:btnInstruction withOffset:5];
    [lblInstructionBookStatus autoSetDimension:ALDimensionHeight toSize:22];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addTimer) name:@"startDown" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadProcessSuccess) name:@"downSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadProcessFail) name:@"downFail" object:nil];
    
    [self loadDatas];
}
#pragma ---------------------   请求活动数据   -----------------------
-(void)loadDatas
{
    NSDictionary *dicParam = @{@"activityId":self.activityId};
    
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryActivityDetailsEX";
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}


#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"queryActivityDetailsEX"])
    {
        _activityDetailsArray = [result[@"activityDetails"] validDataOfClass:[NSDictionary class]];
        _processStr = [NSString stringWithFormat:@"%@",result[@"progress"]];
        [self reloadUI];
    }
}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    
}
#pragma ---------------------   刷新页面UI   -----------------------
-(void)reloadUI
{
    lblTaskName.text = [_activityDetailsArray objectForKey:@"activityName"];
    
    [btnBXNum setTitle:[NSString stringWithFormat:@"%@",[_activityDetailsArray objectForKey:@"requiredCheckPointNum"]] forState:UIControlStateNormal];
    [btnKXNum setTitle:[NSString stringWithFormat:@"%@",[_activityDetailsArray objectForKey:@"optionalCheckPointNum"]] forState:UIControlStateNormal];
    
    lblPresent.text = _processStr;
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDateFormatter *inputFormatter1 = [[NSDateFormatter alloc] init];
    [inputFormatter1 setDateFormat:@"yyyy.MM.dd"];
//
    NSDate* endDate1 = [inputFormatter dateFromString:[_activityDetailsArray objectForKey:@"endDate"]];
    NSDate* startDate1 = [inputFormatter dateFromString:[_activityDetailsArray objectForKey:@"startDate"]];
    NSString* endDateStr1 = [inputFormatter1 stringFromDate:endDate1];
    //NSTimeInterval timeInterval = 24*60*60;
    //_startDate = [NSDate dateWithTimeInterval:timeInterval*-num sinceDate:[NSDate date]];
    NSString* startDateStr1 = [inputFormatter1 stringFromDate:startDate1];
    
    
    NSArray *startArray1 = [startDateStr1 componentsSeparatedByString:@"."];
    NSArray *endArray1 = [endDateStr1 componentsSeparatedByString:@"."];
    
    lblEndYear.text = [endArray1 objectAtIndex:0];
    lblEndDay.text = [NSString stringWithFormat:@"%@.%@",[endArray1 objectAtIndex:1],[endArray1 objectAtIndex:2]];
    
    lblYear.text = [startArray1 objectAtIndex:0];
    lblDay.text = [NSString stringWithFormat:@"%@.%@",[startArray1 objectAtIndex:1],[startArray1 objectAtIndex:2]];
//
    NSDate* endDate = [inputFormatter dateFromString:[_activityDetailsArray objectForKey:@"reportEndDate"]];
    NSDate* startDate = [inputFormatter dateFromString:[_activityDetailsArray objectForKey:@"reportStartDate"]];
    NSString* endDateStr = [inputFormatter1 stringFromDate:endDate];
    NSString* startDateStr = [inputFormatter1 stringFromDate:startDate];
    
    
    NSArray *startArray = [startDateStr componentsSeparatedByString:@"."];
    NSArray *endArray = [endDateStr componentsSeparatedByString:@"."];
    
    lblActityEndYear.text = [endArray objectAtIndex:0];
    lblActityEndDay.text = [NSString stringWithFormat:@"%@.%@",[endArray objectAtIndex:1],[endArray objectAtIndex:2]];
    
    lblActityYear.text = [startArray objectAtIndex:0];
    lblActityDay.text = [NSString stringWithFormat:@"%@.%@",[startArray objectAtIndex:1],[startArray objectAtIndex:2]];

    NSString* sTimt =[DataMgr getInstance].serverTime;
    NSDate* serverTime = [inputFormatter dateFromString:[sTimt substringToIndex:10]];
    
    NSTimeInterval cha=[endDate timeIntervalSinceDate:serverTime];
    
    if ([[startDate laterDate:[NSDate date]] isEqualToDate:startDate]) {
        lblUnStart.hidden = NO;
        lblSYDay.hidden = YES;
        lblSY.hidden = YES;
        lblSY1.hidden = YES;
        lblDesc.hidden = YES;
    }
    else
    {
        NSString* todayDateStr = [inputFormatter stringFromDate:[NSDate date]];
        NSDate*todayDate = [inputFormatter dateFromString:todayDateStr];
        
        if([[endDate laterDate:[NSDate date]] isEqualToDate:endDate]|| [endDate isEqualToDate:todayDate])
        {
            [DataMgr getInstance].taskIsEnd = @"NO";
            if (cha/86400>=1)
            {
                lblSYDay.text = [NSString stringWithFormat:@"%.0f", cha/86400+1];
            }
            else
            {
                lblSYDay.text = @"1";
            }
        }
        else
        {
            [DataMgr getInstance].taskIsEnd = @"YES";
            lblGQ.hidden = NO;
            lblSYDay.hidden = YES;
            lblSY.hidden = YES;
            lblSY1.hidden = YES;
            lblDesc.hidden = YES;
        }
    }
    
    
    if ([[_activityDetailsArray objectForKey:@"instructorUrl"] isEqualToString:@""]) {
        
        [sv autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:btnEnter withOffset:-15];
        lblInstructionBookTitle.hidden = YES;
        lblInstructionBookSize.hidden = YES;
        lblInstructionBookStatus.hidden = YES;
    }
    else
    {
        [sv autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:btnEnter withOffset:-70];
        lblInstructionBookTitle.hidden = NO;
        lblInstructionBookSize.hidden = NO;
        lblInstructionBookStatus.hidden = NO;
    }
    
    NSString* str = [_activityDetailsArray objectForKey:@"details"];
    
    CGFloat h = [str boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.view.frame)-30, CGFLOAT_MAX)
                                  options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}
                                  context:nil].size.height+15;
    
    lblStand.text = str;
    
    [lblStand setFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame)-30, h)];
    
    sv.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)-30, h);
    
    lblInstructionBookTitle.text = [_activityDetailsArray objectForKey:@"instructorName"];
    NSString* bookSize = [_activityDetailsArray objectForKey:@"instructorSize"];
    CGFloat kbNum = [bookSize floatValue]/1024;
    if (kbNum < 1024) {
        lblInstructionBookSize.text = [NSString stringWithFormat:@"%.2fKb",kbNum];
    }
    else
    {
        lblInstructionBookSize.text = [NSString stringWithFormat:@"%.2fMb",kbNum/1024];
    }
    
    NSString* url = [_activityDetailsArray objectForKey:@"instructorUrl"];
    NSArray *arr = [url componentsSeparatedByString:@"/"];
    _fileNameStr = [arr lastObject];
    
    NSArray* array = [DBDaoMgr fetchInstructionBookInfoByName:_fileNameStr];
    if (array.count == 0 || [_fileNameStr isEqualToString:@""]) {
        _downloadType = @"";
    }
    else
    {
        NSString* str = array[0][@"isDownLoadSuccess"];
        if ([str isEqualToString:@"YES"]) {
            _downloadType = @"success";
            lblInstructionBookStatus.text = [NSString stringWithFormat:@"(点击打开)"];
        }
        else
        {
            _downloadType = @"making";
            [self addTimer];
        }
        
    }
}
-(void)enterClick
{
    //
    SATaskPointDetailViewController* vc = [[SATaskPointDetailViewController alloc] init];
    vc.activityId = self.activityId;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [super viewWillAppear:animated];
    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
        [self loadDatas];
    }
    
    self.navigationController.navigationBarHidden = YES;
    //self.rdv_tabBarController.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)InstructionClick
{
    //    [self downloadFileURL:[_activityDetailsArray objectForKey:@"instructorUrl"] fileName:@"baidu.pdf" tag:1];
    //    [self downloadFileURL:@"http://down.mumayi.com/292416/mbaidu" fileName:@"baidu.apk" tag:1];
    if ([_downloadType isEqualToString:@""]||[_downloadType isEqualToString:@"failed"]) {
        //        [UIApplication downloadFileURL:@"http://down.mumayi.com/292416/mbaidu" fileName:_fileNameStr];
        [UIApplication downloadFileURL:[_activityDetailsArray objectForKey:@"instructorUrl"] fileName:_fileNameStr];
        [DBDaoMgr insertInstructionBookInfo:@"0" andTotalBytesExpectedToRead:[_activityDetailsArray objectForKey:@"instructorSize"] andName:_fileNameStr andTaskStartTime:_activityDetailsArray[@"startDate"] andTaskEndTime:_activityDetailsArray[@"endDate"]];
    }
    
    if ([_downloadType isEqualToString:@"making"]) {
        [UIApplication showToastViewMessage:@"正在下载,请稍后"];
    }
    
    if ([_downloadType isEqualToString:@"success"]) {
        
        QLPreviewController * qlPreview = [[QLPreviewController alloc]init];
        qlPreview.title = _fileNameStr;
        qlPreview.dataSource = self; //需要打开的文件的信息要实现dataSource中的方法
        qlPreview.delegate = self;  //视图显示的控制
        [self presentViewController:qlPreview animated:YES completion:^{
            //需要用模态化的方式进行展示
        }];
    }
}

-(void)addTimer
{
    _downloadType = @"making";
    reloadTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(reloadPrcess) userInfo:nil repeats:YES];
}

-(void)reloadPrcess
{
    self.fileInfoArr = [DBDaoMgr fetchInstructionBookInfoByName:_fileNameStr];
    
    NSString* bookSize = self.fileInfoArr[0][@"totalBytesRead"];
    CGFloat kbNum = [bookSize floatValue]/1024;
    
    if (kbNum < 1024) {
        lblInstructionBookStatus.text = [NSString stringWithFormat:@"(%.2fKb/%@)",kbNum,lblInstructionBookSize.text];
    }
    else
    {
        lblInstructionBookStatus.text = [NSString stringWithFormat:@"(%.2fMb/%@)",kbNum/1024,lblInstructionBookSize.text];
    }
}

-(void)reloadProcessSuccess
{
    _downloadType = @"success";
    [reloadTimer invalidate];
    lblInstructionBookStatus.text = [NSString stringWithFormat:@"(点击打开)"];
}
-(void)reloadProcessFail
{
    _downloadType = @"failed";
    [reloadTimer invalidate];
    //[DBDaoMgr deleteInstructionBookInfoByName:_fileNameStr];
    [self deleteFile];
    lblInstructionBookStatus.text = [NSString stringWithFormat:@"(下载失败,点击重新下载)"];
}

#pragma mark - previewControllerDataSource
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1; //需要显示的文件的个数
}
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    //返回要打开文件的地址，包括网络或者本地的地址
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *fileName = [NSString stringWithFormat:@"%@/%@", documentsDirectory, _fileNameStr];
    NSURL* url = [NSURL URLWithString:fileName];
    return url;
}
#pragma mark - previewControllerDelegate

-(CGRect)previewController:(QLPreviewController *)controller frameForPreviewItem:(id<QLPreviewItem>)item inSourceView:(UIView *__autoreleasing *)view
{
    //提供变焦的开始rect，扩展到全屏
    return CGRectMake(110, 190, 100, 100);
}
-(UIImage *)previewController:(QLPreviewController *)controller transitionImageForPreviewItem:(id<QLPreviewItem>)item contentRect:(CGRect *)contentRect
{
    //返回控制器在出现和消失时显示的图像
    return nil;
}
-(void)previewControllerDidDismiss:(QLPreviewController *)controller
{
    //控制器消失后调用
}
-(void)previewControllerWillDismiss:(QLPreviewController *)controller
{
    //控制器在即将消失后调用
}


-(void)deleteFile {
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    //文件名
    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:_fileNameStr];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        NSLog(@"no  have");
        return ;
    }else {
        NSLog(@" have");
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }
        
    }
}
/*
 #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
