ShopDetaileViewController.m 18.3 KB
Newer Older
zhu's avatar
zhu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
//  ShopDetaileViewController.m
//  XFFruit
//
//  Created by mac on 15/8/19.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ShopDetaileViewController.h"
#import "SurveyCell.h"
#import "HPGrowingTextView.h"
#import "StartTimeView.h"
#import "ChosePersonViewController.h"
#import "ChooseProductViewController.h"
zhu's avatar
zhu committed
15
#import "SelectCompanyViewController.h"
zhu's avatar
zhu committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#import "SurveyUser.h"
#import "Survey.h"
#import "Product.h"
#define LeftMargin 15
#define BtnHeight 44
#define TableHeight 46
#define CornerRadius 5
#define TitleSize 15
#define BtnSize 19
#define TotalHeight 432
#define KeyboardHeight 258
#define DateViewHeight 300
typedef enum : NSUInteger {
    SaveTag = 2000,
    CommitTag,
} BtnTag;
@interface ShopDetaileViewController ()<UITableViewDataSource,UITableViewDelegate,HPGrowingTextViewDelegate,UITextFieldDelegate,StartTimeViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    UITextField *_titleField;
    UILabel *_productLabel;
zhu's avatar
zhu committed
38
    UILabel *_PackagingLable;
zhu's avatar
zhu committed
39 40 41 42 43 44 45
    UILabel *_startDateLabel;
    UILabel *_endDateLabel;
    UILabel *_personLabel;
    HPGrowingTextView *_noteTextView;
    UIDatePicker *_timePicker;
    BOOL _isClickStart;
    StartTimeView *_startTimeView;
陈俊俊's avatar
陈俊俊 committed
46 47 48 49 50 51 52 53 54 55
    
    UITextField *merchandise;         //商品
    UITextField *packageSpecification;//包装规格
    UITextField *packageQuantity;     //包装数量
    UITextField *packageUnit;         //包装单位
    UITextField *foundationQuantity;  //基础数量
    UITextField *foundationUnitPrice; //基础单价
    UITextField *packageUnitPrice;    //包装单价
    UITextField *totalMoney;          //总金额
    UITextField *remark;              //备注
zhu's avatar
zhu committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}
@property (nonatomic,strong)NSString *titleStr;
@property (nonatomic,strong)NSString *productCodeStr;
@property (nonatomic,strong)NSString *productNameStr;
@property (nonatomic,strong)NSString *productUuidStr;
@property (nonatomic,strong)NSString *beginDateStr;
@property (nonatomic,strong)NSString *endDateStr;
@property (nonatomic,strong)NSString *remarkStr;
@property (nonatomic,strong)NSMutableArray *users;
@end

@implementation ShopDetaileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.navTitle;
    [self initData];
    [self setupSubviews];
    // Do any additional setup after loading the view.
}
- (void)initData{
陈俊俊's avatar
陈俊俊 committed
77 78 79
    merchandise=[[UITextField alloc]init];
    packageUnit=[[UITextField alloc]init];
    merchandise.text=packageUnit.text=NULL;
zhu's avatar
zhu committed
80 81 82 83 84 85 86 87
    _dataArr = [NSMutableArray array];
    self.users = [NSMutableArray array];
    [_dataArr addObject:@"商品"];
    [_dataArr addObject:@"包装规格"];
    [_dataArr addObject:@"包装数量"];
    [_dataArr addObject:@"包装单位"];
    [_dataArr addObject:@"基础数量"];
    [_dataArr addObject:@"基础单价"];
陈俊俊's avatar
陈俊俊 committed
88
    [_dataArr addObject:@"包装单价"];
zhu's avatar
zhu committed
89 90 91 92 93 94
    [_dataArr addObject:@"总金额"];
    [_dataArr addObject:@"备注"];
}

-(void)setupSubviews
{
陈俊俊's avatar
陈俊俊 committed
95
    //    self.view.backgroundColor  = XXFBgColor;
zhu's avatar
zhu committed
96 97 98 99 100 101 102 103 104
    
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, 0,ScreenSize.width, ScreenSize.height - 64 - LeftMargin)) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, 50)];
    
陈俊俊's avatar
陈俊俊 committed
105
    UIButton *saveBtn =  [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, 10, (ScreenSize.width - LeftMargin*3)/2, BtnHeight) target:self sel:@selector(btnClick:) tag:SaveTag image:nil title:@"删除" titleColor: [UIColor whiteColor] isCorner:YES corner:CornerRadius bgColor:GXF_SAVE_COLOR];
zhu's avatar
zhu committed
106 107
    [footView addSubview:saveBtn];
    
陈俊俊's avatar
陈俊俊 committed
108
    UIButton *commitBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(saveBtn.frame.origin.x + saveBtn.frame.size.width + LeftMargin, 10, (ScreenSize.width - LeftMargin*3)/2, BtnHeight) target:self sel:@selector(btnClick:) tag:CommitTag image:nil title:@"保存" titleColor: [UIColor whiteColor] isCorner:YES corner:CornerRadius bgColor:GXF_COMMIT_COLOR];
zhu's avatar
zhu committed
109 110
    [footView addSubview:commitBtn];
    
陈俊俊's avatar
陈俊俊 committed
111
    //    [self.view addSubview:footView];
zhu's avatar
zhu committed
112 113
    _tableView.tableFooterView = footView;
    [self.view addSubview:_tableView];
陈俊俊's avatar
陈俊俊 committed
114
    
zhu's avatar
zhu committed
115 116 117 118 119 120 121 122 123 124
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 按钮点击事件
- (void)btnClick:(UIButton *)btn{
    switch (btn.tag) {
        case SaveTag:
        {
陈俊俊's avatar
陈俊俊 committed
125 126 127
            //            if ([self checkSurvey]) {
            //                [self saveSurvey:@"initial" message:@"正在保存..."];
            //            }
zhu's avatar
zhu committed
128 129 130 131
        }
            break;
        case CommitTag:
        {
陈俊俊's avatar
陈俊俊 committed
132 133 134 135 136 137
            if ([self informationComplete]) {
                ShopDetail *shopDetail=self.shopDetail;
                self.choseShopDetail(shopDetail);//回调
                [self PopViewControllerAnimated:YES];
                //                [self saveSurvey:@"submitted" message:@"正在提交..."];
            }
zhu's avatar
zhu committed
138 139 140 141 142 143
        }
            break;
        default:
            break;
    }
}
陈俊俊's avatar
陈俊俊 committed
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 175 176 177 178 179 180 181 182 183 184
- (BOOL)informationComplete{
    self.shopDetail=[[ShopDetail alloc]init];
    self.shopDetail.merchandise=merchandise.text;
    self.shopDetail.packageSpecification=packageSpecification.text;
    self.shopDetail.packageQuantity=packageQuantity.text;
    self.shopDetail.packageUnit=packageUnit.text;
    self.shopDetail.foundationQuantity=foundationQuantity.text;
    self.shopDetail.foundationUnitPrice=foundationUnitPrice.text;
    self.shopDetail.packageUnitPrice=packageUnitPrice.text;
    self.shopDetail.totalMoney=totalMoney.text;
    self.shopDetail.remark=remark.text;
    
    if (merchandise.text.length == 0 ) {
        ShowMessage(@"商品不能为空");
        return NO;
    }
    if (packageSpecification.text.length == 0 ) {
        ShowMessage(@"包装规格不能为空");
        return NO;
    }
    if (packageQuantity.text.length == 0 ) {
        ShowMessage(@"包装数量不能为空");
        return NO;
    }
    if (packageUnit.text.length == 0) {
        ShowMessage(@"包装单位不能为空");
        return NO;
    }
    if (foundationQuantity.text.length == 0) {
        ShowMessage(@"基础数量不能为空");
        return NO;
    }
    if (foundationUnitPrice.text.length == 0) {
        ShowMessage(@"基础单价不能为空");
        return NO;
    }if (packageUnitPrice.text.length == 0) {
        ShowMessage(@"总金额不能为空");
        return NO;
    }
    if (remark.text.length == 0) {
        ShowMessage(@"备注不能为空");
zhu's avatar
zhu committed
185 186 187
        return NO;
    }
    return YES;
陈俊俊's avatar
陈俊俊 committed
188
    
zhu's avatar
zhu committed
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}


#pragma mark - 保存
- (void)saveSurvey:(NSString *)state message:(NSString *)msg
{
    //保存
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message = data[@"message"];
            if (success == 1) {
                //成功
                [IBTLoadingView showTips:@"操作成功"];
                [self clearAllInformation];
                if (self.survey) {
                    [ICRUserUtil sharedInstance].needFresh = YES;
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }else{
                [IBTLoadingView showTips:message];
            }
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    id uuidObject = [NSNull null];
    if(self.survey.uuid.length > 0){
        uuidObject = self.survey.uuid;
    }
    id billNumberObject = [NSNull null];
    if (self.survey.billNumber > 0) {
        billNumberObject = self.survey.billNumber;
    }
    NSNumber *version = [NSNumber numberWithLong:0];
    if (self.survey) {
        version = self.survey.version;
    }
    NSDictionary *dict = @{@"uuid":uuidObject,
                           @"version":version,
                           @"billnumber":billNumberObject,
                           @"title":[IBTCommon checkString:self.titleStr],
                           @"state":state,
                           @"productUuid":[IBTCommon checkString:self.productUuidStr],
                           @"productCode":[IBTCommon checkString:self.productCodeStr],
                           @"productName":[IBTCommon checkString:self.productNameStr],
                           @"beginDate":[IBTCommon checkString:self.beginDateStr],
                           @"endDate":[IBTCommon checkString:self.endDateStr],
                           @"remark":[IBTCommon checkString:self.remarkStr],
                           @"users":self.users};
    [IBTLoadingView showProgressLabel:msg];
    [[ICRHTTPController sharedController] saveSurveyWithAuthenticode:dict success:succ failure:fail];
}

- (void)clearAllInformation{
    _titleField.text = @"";
    _productLabel.text = @"请输入商品";
    _productLabel.textColor = HexColor(@"bbbbbb");
    _startDateLabel.text = @"选择日期";
    _startDateLabel.textColor = HexColor(@"bbbbbb");
    _endDateLabel.text = @"选择日期";
    _endDateLabel.textColor = HexColor(@"bbbbbb");
    _personLabel.text = @"选择调研人员";
    _personLabel.textColor = HexColor(@"bbbbbb");
    _noteTextView.text =  @"";
    //清空数据
    self.titleStr = @"";
    self.productCodeStr = @"";
    self.productNameStr = @"";
    self.productUuidStr = @"";
    self.beginDateStr = @"";
    self.endDateStr = @"";
    [self.users removeAllObjects];
    self.remarkStr = @"";
}





#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
陈俊俊's avatar
陈俊俊 committed
280 281 282 283 284
    //    if (indexPath.row == 0){
    //        return TableHeight*2;
    //    }else{
    return TableHeight;
    //    }
zhu's avatar
zhu committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    SurveyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[SurveyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row==0||indexPath.row==3) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        [self createViewInCell:cell indexPath:indexPath];
    }
    [cell setTitleStr:_dataArr[indexPath.row]];
    return cell;
}

-  (void)createViewInCell:(SurveyCell *)cell indexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0||indexPath.row==3) {
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:(CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-10, TableHeight))];
        contentLabel.textAlignment= NSTextAlignmentRight;
        contentLabel.textColor = HexColor(@"bbbbbb");
        contentLabel.font = FontSize(TitleSize);
        [cell.contentView addSubview:contentLabel];
        if (indexPath.row==0) {
            _productLabel = contentLabel;
陈俊俊's avatar
陈俊俊 committed
311
            contentLabel.text = @"选择商品";
zhu's avatar
zhu committed
312 313 314
            [self preferDataInProductLabel];
        }else if (indexPath.row==3)
        {
zhu's avatar
zhu committed
315
            _PackagingLable = contentLabel;
陈俊俊's avatar
陈俊俊 committed
316
            contentLabel.text = @"选择包装单位";
zhu's avatar
zhu committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330
            [self preferDataInProductLabel];
        }
        
    }else
    {
        _titleField = [[UITextField alloc] initWithFrame:CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-10, TableHeight)];
        _titleField.textAlignment = NSTextAlignmentRight;
        _titleField.textColor = HexColor(@"444444");
        _titleField.placeholder = @"输入调研标题";
        _titleField.font = FontSize(15);
        _titleField.returnKeyType = UIReturnKeyDone;
        _titleField.delegate = self;
        [cell.contentView addSubview:_titleField];
        if (indexPath.row==1) {
陈俊俊's avatar
陈俊俊 committed
331
            packageSpecification=_titleField;//包装规格
zhu's avatar
zhu committed
332 333 334
            _titleField.placeholder = @"输入包装规格";
        }else if (indexPath.row==2)
        {
陈俊俊's avatar
陈俊俊 committed
335
            packageQuantity=_titleField;     //包装数量
zhu's avatar
zhu committed
336 337 338
            _titleField.placeholder = @"输入包装数量";
        }else if (indexPath.row==4)
        {
陈俊俊's avatar
陈俊俊 committed
339
            foundationQuantity=_titleField;  //基础数量
zhu's avatar
zhu committed
340 341 342
            _titleField.placeholder = @"输入基础数量";
        }else if (indexPath.row==5)
        {
陈俊俊's avatar
陈俊俊 committed
343
            foundationUnitPrice=_titleField; //基础单价
zhu's avatar
zhu committed
344 345 346
            _titleField.placeholder = @"输入基础单价";
        }else if (indexPath.row==6)
        {
陈俊俊's avatar
陈俊俊 committed
347
            packageUnitPrice=_titleField;    //包装单价
zhu's avatar
zhu committed
348 349 350
            _titleField.placeholder = @"输入包装单价";
        }else if (indexPath.row==7)
        {
陈俊俊's avatar
陈俊俊 committed
351
            totalMoney=_titleField;          //总金额
zhu's avatar
zhu committed
352 353 354
            _titleField.placeholder = @"输入总金额";
        }else if (indexPath.row==8)
        {
陈俊俊's avatar
陈俊俊 committed
355
            remark=_titleField;              //备注
zhu's avatar
zhu committed
356 357
            _titleField.placeholder = @"输入备注内容";
        }
陈俊俊's avatar
陈俊俊 committed
358
        
zhu's avatar
zhu committed
359 360 361 362 363
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
陈俊俊's avatar
陈俊俊 committed
364 365
    
    if(indexPath.row == 0){
zhu's avatar
zhu committed
366
        ChooseProductViewController *cvc = [ChooseProductViewController new];
zhu's avatar
zhu committed
367 368
        cvc.choseBaseInfo = ^(NSArray  *products){
            Product *product=products [0];
zhu's avatar
zhu committed
369
            _productLabel.text = [NSString stringWithFormat:@"%@[%@]",product.name,product.code];
陈俊俊's avatar
陈俊俊 committed
370
            merchandise.text=[NSString stringWithFormat:@"%@[%@]",product.name,product.code];
zhu's avatar
zhu committed
371 372 373 374 375 376 377
            _productLabel.textColor = HexColor(@"444444");
            self.productNameStr = product.name;
            self.productUuidStr = product.uuid;
            self.productCodeStr = product.code;
        };
        cvc.isMoreChose = NO;
        [self PushViewController:cvc animated:YES];
zhu's avatar
zhu committed
378 379
    }else if (indexPath.row ==3)
    {
陈俊俊's avatar
陈俊俊 committed
380
        SelectCompanyViewController *vc=[SelectCompanyViewController new];
zhu's avatar
zhu committed
381 382 383
        vc.chosePackaging = ^(NSString *Packaging){
            
            _PackagingLable.text = [NSString stringWithFormat:@"%@",Packaging];
陈俊俊's avatar
陈俊俊 committed
384
            packageUnit.text=[NSString stringWithFormat:@"%@",Packaging];
zhu's avatar
zhu committed
385
            _PackagingLable.textColor = HexColor(@"444444");
陈俊俊's avatar
陈俊俊 committed
386
            
zhu's avatar
zhu committed
387
        };
陈俊俊's avatar
陈俊俊 committed
388 389
        
        
zhu's avatar
zhu committed
390
        [self PushViewController:vc animated:YES];
陈俊俊's avatar
陈俊俊 committed
391
        
zhu's avatar
zhu committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    }
}

- (void)preferDataInPersonLabel{
    if (self.survey) {
        if (self.survey.users.count > 0) {
            NSString *personStr = @"";
            for (SurveyUser * surveyUser in self.survey.users) {
                if (personStr.length == 0) {
                    personStr = [personStr stringByAppendingFormat:@"%@",surveyUser.userName];
                }else {
                    personStr = [personStr stringByAppendingFormat:@"、%@",surveyUser.userName];
                }
            }
            _personLabel.text = personStr;
            _personLabel.textColor = HexColor(@"444444");
            
        }
        if (self.users > 0) {
            [self.users addObjectsFromArray:self.survey.users];
        }
    }
}
- (void)preferDataInProductLabel{
    if (self.survey) {
        _productLabel.text = [IBTCommon checkString:self.survey.productName];
        _productLabel.textColor = HexColor(@"444444");
        self.productCodeStr = [IBTCommon checkString:self.survey.productCode];
        self.productNameStr = [IBTCommon checkString:self.survey.productName];
        self.productUuidStr = [IBTCommon checkString:self.survey.productUuid];
    }
}


- (void)startDatePickView{
    if (!_startTimeView) {
        _startTimeView = [[StartTimeView alloc] initWithFrame:CGRectMake(0, ScreenSize.height, ScreenSize.width, ScreenSize.height - 64)];
        _startTimeView.delegate = self;
        _startTimeView.backgroundColor        = RGBA(0, 0, 0 ,0.5);
        [self.view addSubview:_startTimeView];
        
        [UIView animateWithDuration:0.15 animations:^{
            CGRect startFrame =  _startTimeView.frame;
            startFrame.origin.y =  0;
            _startTimeView.frame = startFrame;
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)cancelTimeView{
    [self clearDatePickView];
}
- (void)okTimeView:(NSDate *)time{
    if (_isClickStart) {
        _startDateLabel.textColor = HexColor(@"444444");
        _startDateLabel.text = [IBTCommon stringFromDate:time];
        self.beginDateStr = [time httpParameterString];
    }else{
        _endDateLabel.textColor = HexColor(@"444444");
        _endDateLabel.text = [IBTCommon stringFromDate:time];
        self.endDateStr = [time httpParameterString];
    }
    // 关闭选择器
    [self clearDatePickView];
}
#pragma mark - 取消
- (void)clearDatePickView{
    [UIView animateWithDuration:0.15 animations:^{
        CGRect startFrame =  _startTimeView.frame;
        startFrame.origin.y = ScreenSize.height;
        _startTimeView.frame = startFrame;
    } completion:^(BOOL finished) {
        [_startTimeView removeFromSuperview];
        _startTimeView = nil;
    }];
}






#pragma mark delegate
- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    [_noteTextView resignFirstResponder];
    [self setTableFrame:LeftMargin];
    return YES;
}
- (void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView{
    CGFloat offset = ScreenSize.height - TotalHeight - KeyboardHeight;
    if (offset < 0) {//上移
        [self setTableFrame:LeftMargin+offset];
    }
}
- (void)setTableFrame:(CGFloat)tabelH{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect tableFrame = _tableView.frame;
        tableFrame.origin.y = tabelH;
        _tableView.frame = tableFrame;
    }];
}

- (void)keyboardHidden{
    [_titleField resignFirstResponder];
    [_noteTextView resignFirstResponder];
    [self setTableFrame:LeftMargin];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

/*
陈俊俊's avatar
陈俊俊 committed
508 509 510 511 512 513 514 515
 #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.
 }
 */
zhu's avatar
zhu committed
516 517

@end