SurveyDetailViewController.m 15.3 KB
Newer Older
n22's avatar
n22 committed
1 2 3 4 5 6 7 8 9
//
//  SurveyDetailViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/8/7.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ResultDetailViewController.h"
10
#import "SurveyDetailViewController.h"
n22's avatar
n22 committed
11
#import "SurveyUser.h"
陈俊俊's avatar
陈俊俊 committed
12
#import "UIImageView+WebCache.h"
13
#import <WebKit/WebKit.h>
n22's avatar
n22 committed
14 15 16
#define BottomHeight 50
#define LeftMargin 20
#define LeftWidth 100
陈俊俊's avatar
陈俊俊 committed
17
#define LeftHeight 20
陈俊俊's avatar
陈俊俊 committed
18
#define TopMargin 15
陈俊俊's avatar
陈俊俊 committed
19
#define ContentMargin 10
n22's avatar
n22 committed
20 21 22 23 24 25

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

26
@interface SurveyDetailViewController () <UIAlertViewDelegate, WKNavigationDelegate> {
n22's avatar
n22 committed
27 28 29
    UIScrollView *_scrollView;
    UIView *_surveyView;
    UIView *_bottomView;
陈俊俊's avatar
陈俊俊 committed
30
    UIImageView *_chartImageView;
31
    WKWebView *_webViewChart;
n22's avatar
n22 committed
32
}
33 34 35 36 37 38 39 40 41
@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;
@property (nonatomic, strong) UILabel *lefeNoteLabel;
陈俊俊's avatar
陈俊俊 committed
42

n22's avatar
n22 committed
43 44 45 46
@end

@implementation SurveyDetailViewController

47 48
- (void)viewDidLoad
{
n22's avatar
n22 committed
49
    [super viewDidLoad];
50
    self.title = @"行情调研详情";
n22's avatar
n22 committed
51
    [self bulifLayout];
陈俊俊's avatar
陈俊俊 committed
52 53 54
    [self getDataFromServer];
}

55 56 57 58
- (void)getDataFromServer
{
    __weak typeof(self) weakSelf = self;
    void (^succ)(id) = ^(id data) {
n22's avatar
n22 committed
59
        [IBTLoadingView hideHUDWithText:nil];
60
        __strong __typeof(weakSelf) strongSelf = weakSelf;
n22's avatar
n22 committed
61
        if (data) {
陈俊俊's avatar
陈俊俊 committed
62
            NSInteger success = [data[@"success"] integerValue];
63
            NSString *message = data[@"message"];
陈俊俊's avatar
陈俊俊 committed
64 65
            if (success == 1) {
                NSDictionary *dictData = data[@"data"];
66
                Survey *survey = [[Survey alloc] init];
陈俊俊's avatar
陈俊俊 committed
67 68 69
                [survey setValuesForKeysWithDictionary:dictData];
                self.survey = survey;
                [strongSelf fetchtSurveyDetail];
70
            } else {
陈俊俊's avatar
陈俊俊 committed
71 72
                [IBTLoadingView showTips:message];
            }
73
        } else {
陈俊俊's avatar
陈俊俊 committed
74
            [strongSelf fetchtSurveyDetail];
n22's avatar
n22 committed
75
        }
陈俊俊's avatar
陈俊俊 committed
76
        [self createResultChart];
n22's avatar
n22 committed
77
    };
78
    void (^fail)(id) = ^(id data) {
n22's avatar
n22 committed
79 80 81 82 83 84 85
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] getResultWithSurveyUuid:self.surveyUuid success:succ failure:fail];
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
- (void)createResultChart
{
    
    NSString *url = [NSString stringWithFormat:@"%@/cruiser-web/chart/surveychart.thor?enterprise=%@&surveyUuid=%@",HTTP_REST_REPORT_BASE_URL, [ICRUserUtil sharedInstance].orgId, self.surveyUuid];
    [_webViewChart loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    
//    void (^succ)(id) = ^(id data) {
//        if (data) {
//            NSInteger success = [data[@"success"] integerValue];
//            NSString *message = data[@"message"];
//            if (success == 1) {
//                NSString *dataStr = data[@"data"];
//                NSRange range = [HTTP_REST_API_BASE_URL rangeOfString:@"rest"];
//                NSString *serverURl = [HTTP_REST_API_BASE_URL substringToIndex:range.location];
//                NSString *urlStr = [NSString stringWithFormat:@"%@DisplayChart?filename=%@", serverURl, 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];
陈俊俊's avatar
陈俊俊 committed
116 117
}

118 119
- (void)fetchtSurveyDetail
{
n22's avatar
n22 committed
120
    self.billNumberLabel.text = [IBTCommon checkString:self.survey.billNumber];
n22's avatar
n22 committed
121
    if ([self.survey.state isEqualToString:SURVEY_STATE_INITIAL]) {
n22's avatar
n22 committed
122 123
        self.stateLabel.textColor = [UIColor redColor];
        self.stateLabel.text = @"未提交";
124
    } else if ([self.survey.state isEqualToString:SURVEY_STATE_SUBMITTED]) {
n22's avatar
n22 committed
125 126
        self.stateLabel.textColor = [UIColor greenColor];
        self.stateLabel.text = @"已提交";
127
    } else if ([self.survey.state isEqualToString:SURVEY_STATE_INSURVEY]) {
n22's avatar
n22 committed
128 129
        self.stateLabel.textColor = [UIColor grayColor];
        self.stateLabel.text = @"调研中";
130
    } else if ([self.survey.state isEqualToString:SURVEY_STATE_FINISHED]) {
n22's avatar
n22 committed
131 132 133 134
        self.stateLabel.textColor = [UIColor blackColor];
        self.stateLabel.text = @"已完成";
    }
    NSString *user = @"";
135 136
    for (NSDictionary *userDict in self.survey.users) {
        SurveyUser *surveyUser = [[SurveyUser alloc] init];
n22's avatar
n22 committed
137
        [surveyUser setValuesForKeysWithDictionary:userDict];
陈俊俊's avatar
陈俊俊 committed
138
        if (user.length == 0) {
139 140 141
            user = [user stringByAppendingFormat:@"%@", surveyUser.userName];
        } else {
            user = [user stringByAppendingFormat:@"、%@", surveyUser.userName];
陈俊俊's avatar
陈俊俊 committed
142
        }
n22's avatar
n22 committed
143 144 145 146 147 148
    }
    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];
149
    self.noteLabel.text = [NSString stringWithFormat:@"%@", self.survey.remark ? self.survey.remark : @"无"];
n22's avatar
n22 committed
150
    [self setNoteHeight];
n22's avatar
n22 committed
151 152
}

153 154 155
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [MBProgressHUD showHUDAddedTo:webView animated:YES];
}
n22's avatar
n22 committed
156

157 158 159
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [MBProgressHUD hideHUDForView:webView animated:YES];
}
n22's avatar
n22 committed
160

n22's avatar
n22 committed
161
#pragma mark - 视图初始化
162 163 164 165
- (void)bulifLayout
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64 - BottomHeight)];
    _scrollView.showsHorizontalScrollIndicator = NO;
n22's avatar
n22 committed
166
    _scrollView.showsVerticalScrollIndicator = NO;
陈俊俊's avatar
陈俊俊 committed
167
    _scrollView.backgroundColor = XXFBgColor;
n22's avatar
n22 committed
168
    [self.view addSubview:_scrollView];
169 170 171 172 173 174 175 176 177 178 179

    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:GXF_COMMIT_COLOR];
n22's avatar
n22 committed
180
    [self.view addSubview:endBtn];
181 182

    _surveyView = [[UIView alloc] initWithFrame:CGRectMake(0, TopMargin, ScreenSize.width, LeftHeight * 9 + ContentMargin * 10)];
n22's avatar
n22 committed
183 184
    _surveyView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_surveyView];
185 186

    UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(LeftMargin / 2, ContentMargin, LeftWidth * 2, LeftHeight)];
n22's avatar
n22 committed
187
    tagLabel.text = @"【调研内容】";
188
    tagLabel.font = GXF_SEVENTEENTH_SIZE;
陈俊俊's avatar
陈俊俊 committed
189
    tagLabel.textColor = HexColor(@"7ebf74");
n22's avatar
n22 committed
190
    [_surveyView addSubview:tagLabel];
191 192 193 194

    NSArray *leftArr = @[ @"单号:", @"状态:", @"标题:", @"商品:", @"开始日期:", @"截止日期:", @"调研人员:", @"备注:" ];
    for (NSInteger i = 0; i < leftArr.count; i++) {
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(LeftMargin, CGRectGetMaxY(tagLabel.frame) + ContentMargin + (LeftHeight + ContentMargin) * i, LeftWidth, LeftHeight)];
陈俊俊's avatar
陈俊俊 committed
195
        leftLabel.font = GXF_SEVENTEENTH_SIZE;
n22's avatar
n22 committed
196
        leftLabel.text = leftArr[i];
陈俊俊's avatar
陈俊俊 committed
197
        leftLabel.textColor = GXF_DETAIL_COLOR;
n22's avatar
n22 committed
198
        [_surveyView addSubview:leftLabel];
199 200

        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftLabel.frame), CGRectGetMaxY(tagLabel.frame) + ContentMargin + (LeftHeight + ContentMargin) * i, _surveyView.frame.size.width - LeftMargin - LeftWidth, LeftHeight)];
陈俊俊's avatar
陈俊俊 committed
201 202
        rightLabel.font = GXF_SEVENTEENTH_SIZE;
        rightLabel.textColor = GXF_DETAIL_COLOR;
n22's avatar
n22 committed
203 204
        if (i == 0) {
            self.billNumberLabel = rightLabel;
205
        } else if (i == 1) {
n22's avatar
n22 committed
206
            self.stateLabel = rightLabel;
207
        } else if (i == 2) {
n22's avatar
n22 committed
208
            self.titleLabel = rightLabel;
209
        } else if (i == 3) {
n22's avatar
n22 committed
210
            self.productLabel = rightLabel;
211
        } else if (i == 4) {
n22's avatar
n22 committed
212
            self.beginDateLabel = rightLabel;
213
        } else if (i == 5) {
n22's avatar
n22 committed
214
            self.endDateLabel = rightLabel;
215
        } else if (i == 6) {
陈俊俊's avatar
陈俊俊 committed
216
            rightLabel.numberOfLines = 0;
n22's avatar
n22 committed
217
            self.userLabel = rightLabel;
218
        } else if (i == 7) {
n22's avatar
n22 committed
219 220
            rightLabel.numberOfLines = 0;
            self.noteLabel = rightLabel;
陈俊俊's avatar
陈俊俊 committed
221
            self.lefeNoteLabel = leftLabel;
n22's avatar
n22 committed
222 223 224
        }
        [_surveyView addSubview:rightLabel];
    }
n22's avatar
n22 committed
225

226
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_surveyView.frame) + TopMargin, ScreenSize.width, 500)];
n22's avatar
n22 committed
227 228
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_bottomView];
229 230

    UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(LeftMargin / 2, 10, LeftWidth * 2, LeftHeight)];
n22's avatar
n22 committed
231
    resultLabel.text = @"【行情反馈】";
232
    resultLabel.font = GXF_SEVENTEENTH_SIZE;
陈俊俊's avatar
陈俊俊 committed
233
    resultLabel.textColor = HexColor(@"7ebf74");
n22's avatar
n22 committed
234
    [_bottomView addSubview:resultLabel];
235

n22's avatar
n22 committed
236
    
237 238 239 240 241 242 243 244 245
    NSString *jScript;
    if (ScreenSize.width != 320) {
        jScript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, maximum-scale=2.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
    }else{
        jScript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=410, maximum-scale=2.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
    }
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
陈俊俊's avatar
陈俊俊 committed
246
    
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
    _webViewChart = [[WKWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(resultLabel.frame), CGRectGetWidth(_bottomView.frame), CGRectGetHeight(_bottomView.frame) - LeftHeight ) configuration:wkWebConfig];
    [_bottomView addSubview:_webViewChart];

    UIButton *btn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(CGRectGetWidth(_bottomView.frame) - 100, 10, 100, LeftHeight)
                                                   target:self
                                                      sel:@selector(btnClick:)
                                                      tag:DetailTag
                                                    image:nil
                                                    title:@">>查看详情"
                                               titleColor:HexColor(@"7ebf74")
                                                 isCorner:NO
                                                   corner:0
                                                  bgColor:nil];
n22's avatar
n22 committed
262
    [_bottomView addSubview:btn];
263

陈俊俊's avatar
陈俊俊 committed
264 265 266 267 268 269
    if ([self.survey.state isEqualToString:@"finished"]) {
        endBtn.hidden = YES;
        CGRect scrollViewFrame = _scrollView.frame;
        scrollViewFrame.size.height = ScreenSize.height - 64;
        _scrollView.frame = scrollViewFrame;
    }
n22's avatar
n22 committed
270
}
271 272
- (void)btnClick:(UIButton *)btn
{
n22's avatar
n22 committed
273 274
    if (btn.tag == DetailTag) {
        ResultDetailViewController *rvc = [ResultDetailViewController new];
陈俊俊's avatar
陈俊俊 committed
275
        rvc.surveyUuid = self.surveyUuid;
n22's avatar
n22 committed
276
        [self PushViewController:rvc animated:YES];
277 278 279
    } else if (btn.tag == EndTag) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请确认结束" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确"
                                                                                                                                                                           @"认", nil];
陈俊俊's avatar
陈俊俊 committed
280 281
        alertView.delegate = self;
        [alertView show];
n22's avatar
n22 committed
282 283 284
    }
}

陈俊俊's avatar
陈俊俊 committed
285
#pragma mark - 结束行情调研
286 287 288
- (void)finishSurveyRequest
{
    void (^succ)(id) = ^(id data) {
陈俊俊's avatar
陈俊俊 committed
289 290 291
        [IBTLoadingView hideHUDWithText:nil];
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
292
            NSString *message = data[@"message"];
陈俊俊's avatar
陈俊俊 committed
293
            if (success == 1) {
n22's avatar
n22 committed
294
                [self backSurvey];
295
            } else {
陈俊俊's avatar
陈俊俊 committed
296 297
                [IBTLoadingView showTips:message];
            }
298
        } else {
陈俊俊's avatar
陈俊俊 committed
299 300 301
            [IBTLoadingView showTips:@"返回异常"];
        }
    };
302
    void (^fail)(id) = ^(id data) {
陈俊俊's avatar
陈俊俊 committed
303 304 305 306
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
n22's avatar
n22 committed
307 308 309
    [[ICRHTTPController sharedController] surveyFinishWithUuid:self.surveyUuid version:self.survey.version success:succ failure:fail];
}
//返回列表
310 311
- (void)backSurvey
{
n22's avatar
n22 committed
312 313
    [ICRUserUtil sharedInstance].needFresh = YES;
    [self PopViewControllerAnimated:YES];
陈俊俊's avatar
陈俊俊 committed
314 315
}

n22's avatar
n22 committed
316 317
- (void)setNoteHeight
{
陈俊俊's avatar
陈俊俊 committed
318
    CGFloat personHeight = [self.userLabel calculateHeight];
319

陈俊俊's avatar
陈俊俊 committed
320 321 322
    CGRect personFrame = self.userLabel.frame;
    personFrame.size.height = personHeight;
    self.userLabel.frame = personFrame;
323 324 325

    CGFloat noteHeight = [self.noteLabel calculateHeight];

n22's avatar
n22 committed
326
    CGRect noteFrame = self.noteLabel.frame;
陈俊俊's avatar
陈俊俊 committed
327
    noteFrame.size.height = noteHeight;
328
    noteFrame.origin.y = CGRectGetMaxY(self.userLabel.frame) + ContentMargin;
n22's avatar
n22 committed
329
    self.noteLabel.frame = noteFrame;
330

陈俊俊's avatar
陈俊俊 committed
331
    CGRect leftNoteFrame = self.lefeNoteLabel.frame;
332
    leftNoteFrame.origin.y = CGRectGetMaxY(self.userLabel.frame) + ContentMargin;
陈俊俊's avatar
陈俊俊 committed
333
    self.lefeNoteLabel.frame = leftNoteFrame;
334 335

    CGFloat totalHeight = noteHeight + (LeftHeight * 7 + ContentMargin * 10) + personHeight;
n22's avatar
n22 committed
336 337 338
    CGRect surveyFrame = _surveyView.frame;
    surveyFrame.size.height = totalHeight;
    _surveyView.frame = surveyFrame;
339

n22's avatar
n22 committed
340
    CGRect bottomFrame = _bottomView.frame;
陈俊俊's avatar
陈俊俊 committed
341
    bottomFrame.origin.y = CGRectGetMaxY(_surveyView.frame) + TopMargin;
n22's avatar
n22 committed
342
    _bottomView.frame = bottomFrame;
343 344

    _scrollView.contentSize = CGSizeMake(ScreenSize.width, totalHeight + CGRectGetHeight(_bottomView.frame) + TopMargin * 2);
n22's avatar
n22 committed
345 346
}

347 348
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
陈俊俊's avatar
陈俊俊 committed
349 350 351 352
    if (buttonIndex == 1) {
        [self finishSurveyRequest];//结束
    }
}
n22's avatar
n22 committed
353

354 355
- (void)didReceiveMemoryWarning
{
n22's avatar
n22 committed
356 357 358
    [super didReceiveMemoryWarning];
}
@end