WriteFeedbackViewController.m 18.8 KB
Newer Older
freecui's avatar
freecui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//
//  WriteFeedbackViewController.m
//  XFFruit
//  填写行情反馈
//  Created by freecui on 15/8/20.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//



#import "WriteFeedbackViewController.h"

#import "HGPhWViewController.h"
#import "ZYQAssetPickerController.h"
#import "IBTImagePicker.h"
#import "SurveyResult.h"
#import "XFDB.h"
#import "XFPhoto.h"
@interface WriteFeedbackViewController ()<ZYQAssetPickerControllerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate>



#define IBT_DEFAULT_CELL_HEIGHT          (44.0f)
#define BTN_PADDING  20
#define BOTTOM_BTN_WIDTH self.view.width - BTN_PADDING*2 //只有一个按钮时的长度
#define BTN_HEIGHT 40


@property (strong, nonatomic) IBTTableViewInfo *tableViewInfo;
@property (strong, nonatomic) NSString *imgName;
@property (strong, nonatomic) HGPhWViewController *hgPhVC;
@property (strong, nonatomic) IBTImagePicker *m_imagePicker;
@property (strong, nonatomic) UIActionSheet *m_actionSheet;
@property (strong, nonatomic) NSMutableArray *imgesMuArr;
@property (strong, nonatomic) SurveyResult *surveyResult;
36 37
@property (strong, nonatomic) UIImageView *headImageView;
@property (nonatomic, strong) UIImageView *noImageHeadView;
freecui's avatar
freecui committed
38 39 40 41 42 43 44
//@property (strong, nonatomic) NSDictionary *surveyResultDict;
//@property (strong, nonatomic) HGPhWViewController *hgphVC;

typedef NS_ENUM(NSUInteger, feedbackCellInfo){
    kfeedbackCellPlace = 0,
    kfeedbackCellPrice,
    kfeedbackCellCapacity,
45 46
    kfeedbackCellQulity,
    kfeedbackCellRemark
freecui's avatar
freecui committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
};

@end

@implementation WriteFeedbackViewController


- (instancetype)initWithImgName:(NSString *)imgName {
    self = [super init];
    if (!self) {
        return nil;
    }
    self.imgName = imgName;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"填写行情反馈";
    
    [self initData];
    [self initSubviews];
   
}
- (void)viewDidAppear:(BOOL)animated {
    
    
    if (_hgPhVC) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imgFiles:) name:kimgsFiles object:_hgPhVC];

    } else {
        return;
    }
    
}
- (void)imgFiles:(NSNotification *)notification {
    //HGPhWViewController *h
}
- (void)initData {
    
    self.imgesMuArr = [NSMutableArray array];
90

freecui's avatar
freecui committed
91 92 93 94 95 96 97 98 99 100
    void (^succ)(id) = ^(id data){
    
    };
    void (^fail)(id) = ^(id data){
    
    };
    [[ICRHTTPController sharedController] getResultsWithUuid:_surveyUuid
                                                     success:succ
                                                     failure:fail];
    
101
#ifdef DEBUG
102 103 104 105 106 107 108 109 110
//    self.surveyResult = [[SurveyResult alloc]init];
//    self.surveyResult.unit = @"南汇8424西瓜调研情况";
//    self.surveyResult.place = @"上海徐汇iOS";
//    self.surveyResult.price = @"26";
//    self.surveyResult.capacity = @"250";
//    self.surveyResult.quality = @"优质";
//    self.surveyResult.userName = @"张三";
//    self.surveyResult.feedbackTime = @"2015-04-05 12:04:06";
//    self.surveyResult.reMark = @"今天工作人员效率很高";
111
#endif
freecui's avatar
freecui committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
 

}

- (void)initSubviews {
    [self buildPhoto];
    [self buildContent];
    [self buildBottomButton];
    
}

- (void)buildPhoto {
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height * 0.4)];
    backView.backgroundColor =  HexColor(@"7ebf74");//ICR_TABBAR_TINT_COLOR;
    [self.view addSubview:backView];
127

freecui's avatar
freecui committed
128 129
    UIImageView *imgV = [[UIImageView alloc]init];
    imgV.userInteractionEnabled = YES;
130
    //[img originImage:img scaleToSize:CGSizeMake(50,50)];//(imgV.width / 2.0, imgV.height / 2.0)];
freecui's avatar
freecui committed
131 132 133 134 135
    
    UITapGestureRecognizer *tpGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectPhotos:)];
    tpGes.numberOfTapsRequired = 1;
    [imgV addGestureRecognizer:tpGes];
    [backView addSubview:imgV];
136
    _headImageView = imgV;
管鹏飞's avatar
管鹏飞 committed
137

138
    imgV.frame = backView.frame;
freecui's avatar
freecui committed
139
    
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = (CGRect){
        .origin.x = imgV.width * 0.2,
        .origin.y = imgV.height * 0.8,
        .size.width = 24,
        .size.height = 24
    };
    [btn setBackgroundImage:[UIImage imageNamed:@"xf_camera_add_pic"] forState:UIControlStateNormal];
    [imgV addSubview:btn];
    
    UIButton *delbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    delbtn.frame = (CGRect){
        .origin.x = imgV.width * 0.8,
        .origin.y = imgV.height * 0.8,
        .size.width = 24,
        .size.height = 24
    };
    [delbtn addTarget:self action:@selector(photosDisplay) forControlEvents:UIControlEventTouchUpInside];
    [delbtn setBackgroundImage:[UIImage imageNamed:@"xf_delete_pic"] forState:UIControlStateNormal];
    [imgV addSubview:delbtn];
    _headImageView.hidden = YES;
    
    _noImageHeadView = [[UIImageView alloc] init];
    _noImageHeadView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tpGes2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectPhotos:)];
    tpGes2.numberOfTapsRequired = 1;
    [_noImageHeadView addGestureRecognizer:tpGes2];
    [backView addSubview:_noImageHeadView];
    _noImageHeadView.frame = (CGRect){
        .origin.x = backView.width / 4.0,
        .origin.y = backView.height / 4.0,
        .size.width = backView.width / 2.0,
        .size.height = backView.height / 2.0
    };
    _noImageHeadView.image = [UIImage imageNamed:@"xf_camera_no_pic"];
freecui's avatar
freecui committed
175 176 177 178 179 180 181


}

- (void)buildContent {
    self.tableViewInfo = [[IBTTableViewInfo alloc]

182
                          initWithFrame:CGRectMake(10, self.view.height * 0.4, self.view.width - 20, IBT_DEFAULT_CELL_HEIGHT * 7)
freecui's avatar
freecui committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

                          style:UITableViewStylePlain];

    

    
    IBTTableViewSectionInfo *secInfo;
    IBTTableViewCellInfo *cellInfo;
     UIColor *titleColor = [UIColor colorWithW:145 a:1];
    CGFloat fCellHeight = IBT_DEFAULT_CELL_HEIGHT;
    secInfo = [IBTTableViewSectionInfo sectionInfoDefaut];
    
    //产地
    cellInfo =  [IBTTableViewCellInfo editorCellForSel:nil
                                                   target:nil
                                                    title:@"产地:"
                                                   margin:0
                                                      tip:nil
                                              autoCorrect:NO
                                                    focus:NO

                                                     text:_surveyResult.place];

    [cellInfo addUserInfoValue:titleColor forKey:CInfoTitleColorKey];
    cellInfo.fCellHeight = fCellHeight;
    [secInfo addCell:cellInfo];
    
    
    //价格
    cellInfo =  [IBTTableViewCellInfo editorCellForSel:nil
                                                target:nil
                                                 title:@"价格:"
                                                margin:0
                                                   tip:nil
                                           autoCorrect:NO
                                                 focus:NO

                                                  text:_surveyResult.price];

    [cellInfo addUserInfoValue:titleColor forKey:CInfoTitleColorKey];
    cellInfo.fCellHeight = fCellHeight;
    [secInfo addCell:cellInfo];
    
    //市场容量
    cellInfo =  [IBTTableViewCellInfo editorCellForSel:nil
                                                target:nil
                                                 title:@"市场容量:"
                                                margin:0
                                                   tip:nil
                                           autoCorrect:NO
                                                 focus:NO
                                                  text:_surveyResult.capacity];

    [cellInfo addUserInfoValue:titleColor forKey:CInfoTitleColorKey];
    cellInfo.fCellHeight = fCellHeight;
    [secInfo addCell:cellInfo];
    //质量情况
    cellInfo =  [IBTTableViewCellInfo editorCellForSel:nil
                                                target:nil
                                                 title:@"质量情况:"
                                                margin:0
                                                   tip:nil
                                           autoCorrect:NO
                                                 focus:NO

                                                  text:_surveyResult.quality];

250

freecui's avatar
freecui committed
251 252 253
    [cellInfo addUserInfoValue:titleColor forKey:CInfoTitleColorKey];
    cellInfo.fCellHeight = fCellHeight;
    [secInfo addCell:cellInfo];
254 255 256 257 258 259 260 261 262 263
    
    
    //备注
    cellInfo = [IBTTableViewCellInfo editorCellForSel:nil
                                               target:nil
                                                title:@"备注:"
                                               margin:0
                                                  tip:nil
                                          autoCorrect:NO
                                                focus:NO
264
                                                 text:_surveyResult.reMark];
265 266 267 268
    [cellInfo addUserInfoValue:titleColor forKey:CInfoTitleColorKey];
    cellInfo.fCellHeight = fCellHeight;
    [secInfo addCell:cellInfo];
    
freecui's avatar
freecui committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    [self.tableViewInfo addSection:secInfo];
    
    IBTTableView *tableView = [_tableViewInfo getTableView];
    [self.view addSubview:tableView];
    
}

- (IBTImagePicker *)imagePicker {
    if (!_m_imagePicker) {
        self.m_imagePicker = [[IBTImagePicker alloc] init];
    }
    
    return _m_imagePicker;
}
#pragma  mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            [self openCamera];
289
            break;  
freecui's avatar
freecui committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        case 1:
            [self libraryPhoto];
            break;
        default:
            break;
    }
}
- (void)openCamera {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = sourceType;
    [self PresentModalViewController:picker animated:YES];
    
    
}
- (void)libraryPhoto {
//    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
//    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary
//    ;
//    picker.delegate = self;
//    picker.allowsEditing = NO;
//    [self PresentModalViewController:picker animated:YES];
    ZYQAssetPickerController *assetPicker = [[ZYQAssetPickerController alloc]init];
    assetPicker.maximumNumberOfSelection = 10;
    assetPicker.assetsFilter = [ALAssetsFilter allPhotos];
    assetPicker.showEmptyGroups = NO;
    assetPicker.delegate = self;
    assetPicker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration >= 5;
        } else {
            return YES;
        }
    }];
    
    [self PresentModalViewController:assetPicker animated:YES];
    
}
#pragma mark - UIImagePickerControllerDelegate


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//    NSURL *imgUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    
339 340
//    NSData *imgData = UIImagePNGRepresentation(img);
//    [self savePhotoToDB:imgData];
341
    [[JavenImages shareInstance] insertAImage:[img scaledToWidth:200]];
342
    [self pushToHGphVC];
freecui's avatar
freecui committed
343 344 345 346
}

//在图片上加图片
- (void)selectPhotos: (UIGestureRecognizer *)tpGe {
347 348
    //NSArray *photosArr = [[XFDB sharedInstance] fetchAllPhotosFromLocal];
    if ([JavenImages shareInstance].imageArr.count > 0) {
freecui's avatar
freecui committed
349 350 351 352 353 354 355 356
        [self photosDisplay];
    } else {
        self.m_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机", @"从手机相册获取", nil];
        [_m_actionSheet showInView:self.view];

    }

}
管鹏飞's avatar
管鹏飞 committed
357 358


freecui's avatar
freecui committed
359 360
#pragma  mark btn action
- (void)photosDisplay {
361 362
    HGPhWViewController *ghphVC = [[HGPhWViewController alloc]init];
    [self PushViewController:ghphVC animated:YES];
freecui's avatar
freecui committed
363
}
管鹏飞's avatar
管鹏飞 committed
364 365


freecui's avatar
freecui committed
366 367 368 369 370 371 372 373 374 375 376
#pragma mark - ZYQAssetPickerController Delegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
    
    if (_imgesMuArr.count > 0) {
        [_imgesMuArr removeAllObjects];
    }
    for (int i=0; i<assets.count; i++) {
        ALAsset *asset = assets[i];
        
        CGImageRef ref = [[asset  defaultRepresentation]fullResolutionImage];
        UIImage *img = [[UIImage alloc]initWithCGImage:ref];
377
        
378
        [self.imgesMuArr addObject: [img scaledToWidth:200]];//[UIImage imageNamed:@"AttachCamera"]
freecui's avatar
freecui committed
379
    }
380 381 382 383
    
    [[JavenImages shareInstance] insertImages:self.imgesMuArr];
    
    [self pushToHGphVC];
freecui's avatar
freecui committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    
   
}

- (void)buildBottomButton {
    // 提交反馈
    UIButton *btnBack =
    [IBTUIButton RoundCornerBtnWithTitle:@"提交反馈"
                                 bgColor:nil];
    btnBack.frame = (CGRect) {
        .origin.x = BTN_PADDING,
        .origin.y = self.view.height - 64 - BTN_HEIGHT - 10,
        .size.width = BOTTOM_BTN_WIDTH,
        .size.height = BTN_HEIGHT
    };
管鹏飞's avatar
管鹏飞 committed
399
    
freecui's avatar
freecui committed
400 401 402 403 404 405 406 407
    [btnBack addTarget:self
                action:@selector(commitFeedback)
      forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnBack];
}

- (void)commitFeedback {
408 409 410 411 412 413 414
    NSString *priceStr = [self getTextFieldTextWithRow:kfeedbackCellPrice];
    if ([self isPureFloat:priceStr] || [self isPureInt:priceStr]) {
        
        [self saveSurveyResult];
    }else{
        [IBTLoadingView showTips:@"请输入正确的价格"];
    }
freecui's avatar
freecui committed
415 416 417 418 419 420 421 422 423 424

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


#pragma mark - 接口调用:3.1	新增行情反馈
- (void)saveSurveyResult{
管鹏飞's avatar
管鹏飞 committed
425 426 427 428 429

    [IBTLoadingView showProgressLabel:@"正在加载..."];
    
    NSDictionary *paramDict = @{@"result":[self getSurveyResult],@"attachments":[self getAttachmentArray]};
    
430
    [[ICRHTTPController sharedController] surveySaveResultWithDict:paramDict surveyUuid:_aSurvey.uuid success:^(id succ) {
管鹏飞's avatar
管鹏飞 committed
431
        
freecui's avatar
freecui committed
432
        [IBTLoadingView hideHUDWithText:nil];
管鹏飞's avatar
管鹏飞 committed
433 434 435 436
        
        if (succ) {
            NSInteger success = [succ[@"success"] integerValue];
            NSString *message  = succ[@"message"] ;
freecui's avatar
freecui committed
437
            if (success == 1) {
管鹏飞's avatar
管鹏飞 committed
438
                // [self backSurvey];
439
                [self.navigationController popToRootViewControllerAnimated:YES];
freecui's avatar
freecui committed
440 441 442 443 444 445
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"返回异常"];
        }
管鹏飞's avatar
管鹏飞 committed
446 447 448
        
    } failure:^(id fail) {
        
freecui's avatar
freecui committed
449
        [IBTLoadingView hideHUDWithText:nil];
管鹏飞's avatar
管鹏飞 committed
450 451 452
        [IBTLoadingView showTips:fail];
        
    }];
freecui's avatar
freecui committed
453 454 455 456 457
}
 
- (NSArray *)getAttachmentArray {
    NSMutableArray *attachArr = [NSMutableArray array];
    NSMutableArray *resultMuArr = [NSMutableArray array];
管鹏飞's avatar
管鹏飞 committed
458 459
    //NSMutableArray *arrImgsData = [[XFDB sharedInstance]fetchAllPhotosDataFromLocal];
    if ([JavenImages shareInstance].imageArr.count == 0) {
freecui's avatar
freecui committed
460 461
      //  return attachArr;
    } else {
管鹏飞's avatar
管鹏飞 committed
462 463
        for (int count  = 0; count < [JavenImages shareInstance].imageArr.count; count ++) {
           UIImage *image = [JavenImages shareInstance].imageArr[count];
464 465 466
            NSData *data = [image scaledToSpaceMaxSize:20];
            NSString *enCodeImageStr = [data base64EncodedString];
            NSMutableDictionary *phontoMuDict = [NSMutableDictionary dictionaryWithObjects:@[[NSDate randomImageName],enCodeImageStr ] forKeys:@[@"fileName",@"content"]];
freecui's avatar
freecui committed
467 468 469 470 471 472 473 474 475 476 477
            [attachArr addObject:phontoMuDict];
        }
    
    }
    
    return [NSArray arrayWithArray:attachArr];
}
#pragma mark  SurveyResult数据结构
- (NSDictionary *)getSurveyResult{
    ICRUserUtil *userU = [ICRUserUtil sharedInstance];
    
478
    NSString *priceStr = [self getTextFieldTextWithRow:kfeedbackCellPrice];
479 480
    
    
481 482 483
    NSDictionary *dict = @{@"userUuid":userU.userId,
                           @"userCode":userU.userCode,
                           @"userName":userU.userName,
484
//                           @"unit":@"",
485
                           @"price":priceStr,
freecui's avatar
freecui committed
486 487
                           @"place":[self getTextFieldTextWithRow:kfeedbackCellPlace ],
                           @"capacity":[self getTextFieldTextWithRow:kfeedbackCellCapacity],
488 489
                           @"quality":[self getTextFieldTextWithRow:kfeedbackCellQulity],
                           @"remark":[self getTextFieldTextWithRow:kfeedbackCellRemark]};
freecui's avatar
freecui committed
490 491 492

    return dict;
}
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508


//1. 整形判断
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

//2.浮点形判断:
- (BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}

freecui's avatar
freecui committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
- (NSString *)getTextFieldTextWithRow: (NSInteger)cellRow {
    NSString *textStr = [NSString string];
    IBTTableViewCellInfo *cellInfo = [_tableViewInfo getCellAtSection:0 row:cellRow];
    UITextField * titleTextF = [cellInfo getUserInfoValueForKey:CInfoEditorKey];
    if (!titleTextF.text || [titleTextF.text isEqual:[NSNull null]]) {
       textStr = @"";
    } else {
        textStr = titleTextF.text;
    }
    return textStr;
}

+ (NSString *)idIsNilOrNullToString: (id)data{
    NSString *returnStr = [NSString string];
    if ([data isEqual:[NSNull null]]|| !data) {
        data = @"";
        
    }
    
    return data;
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
#pragma mark ----跳转到图片展示界面----

- (void)pushToHGphVC{
    HGPhWViewController *hgphVC = [[HGPhWViewController alloc] init];
    [self PushViewController:hgphVC animated:NO];
}

- (void)dealloc {
    [[JavenImages shareInstance] removeAllImage];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([JavenImages shareInstance].imageArr.count > 0) {
        _headImageView.image = [JavenImages shareInstance].imageArr[0];
        _headImageView.hidden = NO;
        _noImageHeadView.hidden = YES;
    }else{
管鹏飞's avatar
管鹏飞 committed
549
        _headImageView.hidden = YES;
550 551 552 553 554
        _noImageHeadView.hidden = NO;
    }
}


freecui's avatar
freecui committed
555 556
#pragma 把选择的图片放入到本地数据库中
- (void)savePhotoToDB: (NSData *)imgData {
557 558 559 560 561 562 563
//    XFPhoto *xfphoto = [[XFPhoto alloc]initWithPhotoData:imgData];
//    BOOL isSave = [[XFDB sharedInstance]savePhoto:xfphoto];
//    if (isSave) {
//        NSLog(@"保存成功");
//    } else {
//        NSLog(@"保存失败");
//    }
freecui's avatar
freecui committed
564 565
}
@end