//
//  SurveyDetailViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/8/7.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "SurveyDetailViewController.h"
#import "ResultDetailViewController.h"
#import "SurveyUser.h"
#import "UIImageView+WebCache.h"
#define BottomHeight 50
#define LeftMargin 20
#define LeftWidth 100
#define LeftHeight 30
#define TopMargin 15


typedef enum : NSUInteger {
    DetailTag = 6000,
    EndTag,
} BtnTag;

@interface SurveyDetailViewController ()
{
    UIScrollView *_scrollView;
    UIView *_surveyView;
    UIView *_bottomView;
    UIImageView *_chartImageView;
}
@property (nonatomic,strong)UILabel *billNumberLabel;
@property (nonatomic,strong)UILabel *stateLabel;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *productLabel;
@property (nonatomic,strong)UILabel *beginDateLabel;
@property (nonatomic,strong)UILabel *endDateLabel;
@property (nonatomic,strong)UILabel *userLabel;
@property (nonatomic,strong)UILabel *noteLabel;
@end

@implementation SurveyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"行情调研详情";
    [self bulifLayout];
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message  = data[@"message"] ;
            if (success == 1) {
                NSDictionary *dictData = data[@"data"];
                Survey *survey = [[Survey alloc]init];
                [survey setValuesForKeysWithDictionary:dictData];
                self.survey = survey;
                [strongSelf fetchtSurveyDetail];
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
          [strongSelf fetchtSurveyDetail];
        }
        [self createResultChart];
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] getResultWithSurveyUuid:self.surveyUuid success:succ failure:fail];
}

- (void)createResultChart {
    void(^succ)(id) = ^(id data) {
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message  = data[@"message"] ;
            if (success == 1) {
                NSString *dataStr = data[@"data"];
               NSString *urlStr = [NSString stringWithFormat:@"%@/DisplayChart?filename=%@",Server_Url,dataStr];
                [_chartImageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
            }else{
                [IBTLoadingView showTips:message];
            }
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [[ICRHTTPController sharedController] resultChartWithResultUuid:self.surveyUuid width:[NSNumber numberWithFloat:CGRectGetWidth(_chartImageView.frame)] height:[NSNumber numberWithFloat:CGRectGetHeight(_chartImageView.frame)] success:succ failure:fail];
}



- (void)fetchtSurveyDetail{
    self.billNumberLabel.text = [IBTCommon checkString:self.survey.billNumber];
    if ([self.survey.state isEqualToString:@"initial"]) {
        self.stateLabel.textColor = [UIColor redColor];
        self.stateLabel.text = @"未提交";
    }else if ([self.survey.state isEqualToString:@"submitted"]) {
        self.stateLabel.textColor = [UIColor greenColor];
        self.stateLabel.text = @"已提交";
    }else if ([self.survey.state isEqualToString:@"insurvey"]) {
        self.stateLabel.textColor = [UIColor grayColor];
        self.stateLabel.text = @"调研中";
    }else if ([self.survey.state isEqualToString:@"finished"]) {
        self.stateLabel.textColor = [UIColor blackColor];
        self.stateLabel.text = @"已完成";
    }
    NSString *user = @"";
    for (NSDictionary *userDict  in self.survey.users) {
        SurveyUser *surveyUser = [[SurveyUser alloc]init];
        [surveyUser setValuesForKeysWithDictionary:userDict];
        if (user.length == 0) {
            user = [user stringByAppendingFormat:@"%@",surveyUser.userName];
        }else{
            user = [user stringByAppendingFormat:@"、%@",surveyUser.userName];
        }
    }
    self.titleLabel.text = [IBTCommon checkString:self.survey.title];
    self.productLabel.text = [IBTCommon checkString:self.survey.productName];
    self.beginDateLabel.text = [IBTCommon checkString:self.survey.beginDate];
    self.endDateLabel.text = [IBTCommon checkString:self.survey.endDate];
    self.userLabel.text = [IBTCommon checkString:user];
    self.noteLabel.text = [IBTCommon checkString:self.survey.remark];
    [self setNoteHeight];
}



#pragma mark - 视图初始化
- (void)bulifLayout{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64 - BottomHeight)];
    _scrollView.showsHorizontalScrollIndicator  = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = HexColor(@"f8f8f8");
    [self.view addSubview:_scrollView];
    
    
    UIButton *endBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, ScreenSize.height  - 64 - BottomHeight +5, ScreenSize.width - LeftMargin*2, 40) target:self sel:@selector(btnClick:) tag:EndTag image:nil title:@"结束" titleColor:[UIColor whiteColor] isCorner:YES corner:8 bgColor:HexColor(@"f69100")];
    [self.view addSubview:endBtn];
    
    _surveyView= [[UIView alloc]initWithFrame:CGRectMake(0, TopMargin, ScreenSize.width, LeftHeight*9+LeftMargin/2)];
    _surveyView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_surveyView];
    
    UILabel *tagLabel = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin/2, 10, LeftWidth*2, LeftHeight)];
    tagLabel.text = @"【调研内容】";
    tagLabel.font =  FontSize(17);
    tagLabel.textColor = [UIColor blueColor];
    [_surveyView addSubview:tagLabel];
    
    NSArray *leftArr = @[@"单号:",@"状态:",@"标题:",@"商品:",@"开始日期:",@"截止日期:",@"调研人员:",@"备注:"];
    for (NSInteger i = 0 ; i < leftArr.count; i++) {
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin, CGRectGetMaxY(tagLabel.frame) + LeftHeight *i, LeftWidth, LeftHeight)];
        leftLabel.font = FontSize(17);
        leftLabel.text = leftArr[i];
        [_surveyView addSubview:leftLabel];
        
        UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftLabel.frame), CGRectGetMaxY(tagLabel.frame) + LeftHeight *i, _surveyView.frame.size.width - LeftMargin - LeftWidth, LeftHeight)];
        rightLabel.font = FontSize(17);

        if (i == 0) {
            self.billNumberLabel = rightLabel;
        }else if (i == 1) {
            self.stateLabel = rightLabel;
        }else if(i == 2){
            self.titleLabel = rightLabel;
        }else if(i == 3){
            self.productLabel = rightLabel;
        }else if(i == 4){
            self.beginDateLabel = rightLabel;
        }else if(i == 5){
            self.endDateLabel = rightLabel;
        }else if(i == 6){
            self.userLabel = rightLabel;
        }else if(i == 7){
            rightLabel.numberOfLines = 0;
            self.noteLabel = rightLabel;
        }
        [_surveyView addSubview:rightLabel];
    }

    
    _bottomView= [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_surveyView.frame) + TopMargin, ScreenSize.width, 200)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_bottomView];
    
    
    UILabel *resultLabel = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin/2, 10, LeftWidth*2, LeftHeight)];
    resultLabel.text = @"【行情反馈】";
    resultLabel.font =  FontSize(17);
    resultLabel.textColor = [UIColor blueColor];
    [_bottomView addSubview:resultLabel];
    
    _chartImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(resultLabel.frame) + 10, CGRectGetWidth(_bottomView.frame) - 20, CGRectGetHeight(_bottomView.frame) - LeftHeight - 20)];
    [_bottomView addSubview:_chartImageView];
    
    UIButton *btn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(CGRectGetWidth(_bottomView.frame) - 100, 10, 100, LeftHeight) target:self sel:@selector(btnClick:) tag:DetailTag image:nil title:@">>查看详情" titleColor:[UIColor blueColor] isCorner:NO corner:0 bgColor:nil];
    [_bottomView addSubview:btn];
    
    
    if ([self.survey.state isEqualToString:@"finished"]) {
        endBtn.hidden = YES;
        CGRect scrollViewFrame = _scrollView.frame;
        scrollViewFrame.size.height = ScreenSize.height - 64;
        _scrollView.frame = scrollViewFrame;
    }

    
    
}
- (void)btnClick:(UIButton *)btn{
    if (btn.tag == DetailTag) {
        ResultDetailViewController *rvc = [ResultDetailViewController new];
        rvc.surveyUuid = self.surveyUuid;
        [self PushViewController:rvc animated:YES];
    }else if (btn.tag == EndTag){
        [self finishSurveyRequest];
    }
}

#pragma mark - 结束行情调研
- (void)finishSurveyRequest{
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message  = data[@"message"] ;
            if (success == 1) {
                
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"返回异常"];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] surveyFinishWithUuid:self.surveyUuid success:succ failure:fail];
}

- (void)setNoteHeight
{
    CGFloat height =  [self.noteLabel calculateHeight];
    
    CGRect noteFrame = self.noteLabel.frame;
    noteFrame.size.height = height;
    self.noteLabel.frame = noteFrame;
    
    CGFloat totalHeight = height + LeftHeight*9;
    CGRect surveyFrame = _surveyView.frame;
    surveyFrame.size.height = totalHeight;
    _surveyView.frame = surveyFrame;
    
    
    CGRect bottomFrame = _bottomView.frame;
    bottomFrame.origin.y = CGRectGetMaxY(_surveyView.frame) + TopMargin;
    _bottomView.frame = bottomFrame;
    
    _scrollView.contentSize = CGSizeMake(ScreenSize.width, totalHeight + CGRectGetHeight(_bottomView.frame) + TopMargin*2);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
