NoticeProductViewController.m 15.4 KB
Newer Older
陈俊俊's avatar
陈俊俊 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
//
//  NoticeProductViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/9/15.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "NoticeProductViewController.h"
#import "SurveyCell.h"
#import "HPGrowingTextView.h"
#import "ChooseProductViewController.h"
#import "ChooseProductUnitViewController.h"
#define LeftMargin 15
#define BtnHeight 44
#define TableRowHeight 46
#define CornerRadius 5
#define BtnSize 19
#define TotalHeight 230
#define KeyboardHeight 258
typedef enum : NSUInteger {
    deleteTag = 10000,
    SaveTag,
} BtnTag;

陈俊俊's avatar
陈俊俊 committed
26
@interface NoticeProductViewController ()<UITableViewDataSource,UITableViewDelegate,HPGrowingTextViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>
陈俊俊's avatar
陈俊俊 committed
27 28 29 30 31 32 33 34 35
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    
    UILabel *_chooseCostLabel;
    UILabel *_choosePackUnitLabel;
    UITextField *_qpcField;
    UITextField *_qpcQuantityField;
    UITextField *_quantityField;
陈俊俊's avatar
陈俊俊 committed
36 37 38 39
    UILabel *_packCountLabel;
    UILabel *_qpcLabel;
    UILabel *_baseCountLabel;
    
陈俊俊's avatar
陈俊俊 committed
40 41 42 43 44
    HPGrowingTextView *_noteTextView;
}
@property (nonatomic,strong)NSString *productNameStr;
@property (nonatomic,strong)NSString *productCodeStr;
@property (nonatomic,strong)NSString *productUuidStr;
陈俊俊's avatar
陈俊俊 committed
45
@property (nonatomic,strong)NSString *productQpcStr;//商品规格描述
陈俊俊's avatar
陈俊俊 committed
46 47 48
@property (nonatomic,strong)NSMutableArray *selectProducts;
@property (nonatomic,strong)NSString *selectUnit;

陈俊俊's avatar
陈俊俊 committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62
@end

@implementation NoticeProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.navTitle;
    [self initData];
    [self createTableView];
    
}
- (void)initData{
    
    _dataArr = [NSMutableArray array];
陈俊俊's avatar
陈俊俊 committed
63
    self.selectProducts = [NSMutableArray array];
陈俊俊's avatar
陈俊俊 committed
64 65 66
    [_dataArr addObject:@"商品"];
    [_dataArr addObject:@"包装单位"];
    [_dataArr addObject:@"包装规格"];
陈俊俊's avatar
陈俊俊 committed
67 68
    [_dataArr addObject:@"包装数量[计划]"];
    [_dataArr addObject:@"基础数量[计划]"];
陈俊俊's avatar
陈俊俊 committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    [_dataArr addObject:@"备注"];
}

- (void)createTableView
{
    self.view.backgroundColor  = XXFBgColor;
    
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, LeftMargin,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)];
    
    UIButton *saveBtn =  [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, 10, (ScreenSize.width - LeftMargin*3)/2, BtnHeight) target:self sel:@selector(btnClick:) tag:deleteTag image:nil title:@"删除" titleColor: [UIColor whiteColor] isCorner:YES corner:CornerRadius bgColor:GXF_SAVE_COLOR];
    [footView addSubview:saveBtn];
    
    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:SaveTag image:nil title:@"保存" titleColor: [UIColor whiteColor] isCorner:YES corner:CornerRadius bgColor:GXF_COMMIT_COLOR];
    [footView addSubview:commitBtn];
    
    _tableView.tableFooterView = footView;
    [self.view addSubview:_tableView];
}

#pragma mark - 按钮点击事件
- (void)btnClick:(UIButton *)btn{
    switch (btn.tag) {
        case deleteTag://删除
        {
陈俊俊's avatar
陈俊俊 committed
99 100 101
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请确认删除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            alertView.delegate = self;
            [alertView show];
陈俊俊's avatar
陈俊俊 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
        }
            break;
        case SaveTag:
        {
            if ([self checkCost]) {
                self.choseNoticeProduct(self.noticeProduct,self.indexPath);
                [self PopViewControllerAnimated:YES];
            }
        }
            break;
        default:
            break;
    }
}
陈俊俊's avatar
陈俊俊 committed
116 117 118 119 120 121 122 123 124 125
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        if (self.indexPath) {
            self.deleteNoticeProduct(self.noticeProduct,self.indexPath);
            [self PopViewControllerAnimated:YES];
        }else{
            [self clearInfomation];
        }
    }
}
陈俊俊's avatar
陈俊俊 committed
126 127 128 129 130 131 132 133
- (void)clearInfomation{
    _chooseCostLabel.text = @"选择商品";
    _chooseCostLabel.textColor = GXF_PLACEHOLDER_COLOR;
    _choosePackUnitLabel.text = @"选择包装单位";
    _choosePackUnitLabel.textColor = GXF_PLACEHOLDER_COLOR;
    _qpcField.text = @"";
    _qpcQuantityField.text = @"";
    _quantityField.text = @"";
陈俊俊's avatar
陈俊俊 committed
134
    _noteTextView.text = @"";
陈俊俊's avatar
陈俊俊 committed
135 136 137
}
- (BOOL)checkCost{
    
陈俊俊's avatar
陈俊俊 committed
138
    if (_chooseCostLabel.text.length == 0 || [_chooseCostLabel.text isEqualToString:@"选择商品"] ) {
陈俊俊's avatar
陈俊俊 committed
139 140 141
        ShowMessage(@"商品不能为空");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
142 143 144 145
    if (_choosePackUnitLabel.text.length == 0 || [_choosePackUnitLabel.text isEqualToString:@"选择包装单位"] ) {
        ShowMessage(@"包装单位不能为空");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
146 147 148 149 150 151 152 153 154 155 156 157
    if (_qpcField.text.length == 0 ) {
        ShowMessage(@"包装规格不能为空");
        return NO;
    }
    if (_qpcQuantityField.text.length == 0) {
        ShowMessage(@"包装数量不能为空");
        return NO;
    }
    if (_quantityField.text.length == 0) {
        ShowMessage(@"基础数量不能为空");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
158
    
陈俊俊's avatar
陈俊俊 committed
159 160 161 162 163 164 165
    if (!self.noticeProduct) {
        self.noticeProduct = [[NoticeProduct alloc]init];
    }
    self.noticeProduct.productCode = self.productCodeStr;
    self.noticeProduct.productName = self.productNameStr;
    self.noticeProduct.productUuid = self.productUuidStr;
    
陈俊俊's avatar
陈俊俊 committed
166
    self.noticeProduct.baseUnit = _qpcLabel.text;//规格单位
陈俊俊's avatar
陈俊俊 committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    self.noticeProduct.packUnit = _choosePackUnitLabel.text;
    self.noticeProduct.qpc = [NSNumber numberWithFloat:[_qpcField.text floatValue]];
    self.noticeProduct.qpcQuantity = [NSNumber numberWithFloat:[_qpcQuantityField.text floatValue]];
    self.noticeProduct.quantity = [NSNumber numberWithFloat:[_quantityField.text floatValue]];
    self.noticeProduct.remark = _noteTextView.text;
    return YES;
}

#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (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;
陈俊俊's avatar
陈俊俊 committed
189
        cell.titleLabel.width = 120;
陈俊俊's avatar
陈俊俊 committed
190 191 192 193 194 195 196 197 198 199 200
        if (indexPath.row == 0 || indexPath.row == 1) {
            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 == 1) {
陈俊俊's avatar
陈俊俊 committed
201
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:(CGRectMake(cell.titleLabel.right, 0, ScreenSize.width - cell.titleLabel.width - LeftMargin*2-15, TableRowHeight))];
陈俊俊's avatar
陈俊俊 committed
202 203 204 205 206 207 208 209 210 211 212 213 214
        contentLabel.textAlignment= NSTextAlignmentRight;
        contentLabel.textColor = GXF_PLACEHOLDER_COLOR;
        contentLabel.font = GXF_FIFTEENTEN_SIZE;
        [cell.contentView addSubview:contentLabel];
        
        if (indexPath.row == 0) {
            contentLabel.text = @"选择商品";
            _chooseCostLabel = contentLabel;
        }else if(indexPath.row == 1) {
            contentLabel.text = @"选择包装单位";
            _choosePackUnitLabel = contentLabel;
        }
    }else if (indexPath.row == _dataArr.count -1){
陈俊俊's avatar
陈俊俊 committed
215
        _noteTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(cell.titleLabel.right, 0, ScreenSize.width - cell.titleLabel.width - LeftMargin*2-15, TableRowHeight)];
陈俊俊's avatar
陈俊俊 committed
216 217
        _noteTextView.contentInset = UIEdgeInsetsMake(5, 5, 5, 0);
        _noteTextView.minNumberOfLines = 1;
陈俊俊's avatar
陈俊俊 committed
218 219
        _noteTextView.maxNumberOfLines = 4;
//        _noteTextView.isScrollable = YES;
陈俊俊's avatar
陈俊俊 committed
220 221 222 223 224 225
        _noteTextView.font = GXF_FIFTEENTEN_SIZE;
        _noteTextView.textAlignment = NSTextAlignmentRight;
        _noteTextView.delegate = self;
        _noteTextView.returnKeyType = UIReturnKeyDone;
        _noteTextView.placeholder = @"输入备注内容";
        [cell.contentView addSubview:_noteTextView];
陈俊俊's avatar
陈俊俊 committed
226 227 228 229
        
        CGRect lineFrame =  cell.lineLabel.frame;
        lineFrame.origin.y = TableRowHeight*2-1;
        cell.lineLabel.frame = lineFrame;
陈俊俊's avatar
陈俊俊 committed
230
    }else{
陈俊俊's avatar
陈俊俊 committed
231
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake( cell.titleLabel.right, 0, ScreenSize.width -  cell.titleLabel.width - LeftMargin*2-30, TableRowHeight)];
陈俊俊's avatar
陈俊俊 committed
232 233 234 235 236 237
        textField.textAlignment = NSTextAlignmentRight;
        textField.textColor = GXF_CONTENT_COLOR;
        textField.font = GXF_FIFTEENTEN_SIZE;
        textField.returnKeyType = UIReturnKeyDone;
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.delegate = self;
陈俊俊's avatar
陈俊俊 committed
238
        [textField addTarget:self action:@selector(textEditClick:) forControlEvents:UIControlEventAllEditingEvents];
陈俊俊's avatar
陈俊俊 committed
239
        [cell.contentView addSubview:textField];
陈俊俊's avatar
陈俊俊 committed
240 241 242 243 244 245
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(textField.frame)+ 5, 0, 20, TableRowHeight)];
        label.textColor = GXF_CONTENT_COLOR;
        label.font = GXF_FIFTEENTEN_SIZE;
        [cell.contentView addSubview:label];
        
陈俊俊's avatar
陈俊俊 committed
246
        if (indexPath.row == 2) {
陈俊俊's avatar
陈俊俊 committed
247
            _qpcLabel = label;
陈俊俊's avatar
陈俊俊 committed
248
            UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(cell.titleLabel.right, 0, 50, TableRowHeight)];
陈俊俊's avatar
陈俊俊 committed
249 250 251 252 253 254
            labelTwo.text = @"1*";
            labelTwo.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:labelTwo];
            textField.left = labelTwo.right;
            textField.width = textField.width - 50;
            label.left = textField.right+ 5;
陈俊俊's avatar
陈俊俊 committed
255 256 257
            textField.placeholder = @"输入包装规格";
            _qpcField = textField;
        }else if(indexPath.row == 3){
陈俊俊's avatar
陈俊俊 committed
258
            _packCountLabel = label;
陈俊俊's avatar
陈俊俊 committed
259 260 261
            textField.placeholder = @"输入包装数量";
            _qpcQuantityField = textField;
        }else{
陈俊俊's avatar
陈俊俊 committed
262
            _baseCountLabel = label;
陈俊俊's avatar
陈俊俊 committed
263 264 265 266 267 268 269 270
            textField.placeholder = @"输入基础数量";
            _quantityField = textField;
        }
    }
    if (self.noticeProduct) {
        [self prepareDataIncell];
    }
}
陈俊俊's avatar
陈俊俊 committed
271 272 273 274 275 276
//【基础数量】=包装规格*包装数量
- (void)textEditClick:(UITextField *)textField{
    if (textField == _qpcField) {//包装规格
        if (_qpcQuantityField.text.length > 0) {//包装数量
            //计算基础数量
            float baseCount = [_qpcField.text floatValue] * [_qpcQuantityField.text floatValue];
陈俊俊's avatar
陈俊俊 committed
277
            _quantityField.text = [NSString stringWithFormat:@"%.2f",baseCount];
陈俊俊's avatar
陈俊俊 committed
278 279 280 281 282
        }
    }else if (textField == _qpcQuantityField) {//包装数量
        if (_qpcField.text.length > 0) {//包装规格
            //计算基础数量
            float baseCount = [_qpcField.text floatValue] * [_qpcQuantityField.text floatValue];
陈俊俊's avatar
陈俊俊 committed
283
            _quantityField.text = [NSString stringWithFormat:@"%.2f",baseCount];
陈俊俊's avatar
陈俊俊 committed
284 285 286 287 288 289
        }
    }else if (textField == _quantityField) {//基础数量
        if (_qpcField.text.length > 0) {//包装规格
            //计算包装数量
            if ([_qpcField.text integerValue] > 0) {
                float packCount = [_quantityField.text floatValue] / [_qpcField.text floatValue];
陈俊俊's avatar
陈俊俊 committed
290
                _qpcQuantityField.text = [NSString stringWithFormat:@"%.2f",packCount];
陈俊俊's avatar
陈俊俊 committed
291 292 293 294 295 296 297
            }
        }
    }
}



陈俊俊's avatar
陈俊俊 committed
298 299 300 301 302
- (void)prepareDataIncell{
    _chooseCostLabel.text = [NSString stringWithFormat:@"%@[%@]",self.noticeProduct.productName,self.noticeProduct.productCode];
    self.productUuidStr = self.noticeProduct.productUuid;
    self.productCodeStr = self.noticeProduct.productCode;
    self.productNameStr = self.noticeProduct.productName;
陈俊俊's avatar
陈俊俊 committed
303
    [self.selectProducts addObject:self.noticeProduct.productUuid];
陈俊俊's avatar
陈俊俊 committed
304 305 306 307
    
    _chooseCostLabel.textColor = GXF_CONTENT_COLOR;
    _choosePackUnitLabel.text = self.noticeProduct.packUnit;
    _choosePackUnitLabel.textColor = GXF_CONTENT_COLOR;
陈俊俊's avatar
陈俊俊 committed
308 309 310 311
    
    _packCountLabel.text = self.noticeProduct.packUnit;
    _baseCountLabel.text = self.noticeProduct.baseUnit;
    _qpcLabel.text = self.noticeProduct.baseUnit;
陈俊俊's avatar
陈俊俊 committed
312 313 314 315 316 317

    _qpcField.text = [self.noticeProduct.qpc stringValue];
    _qpcQuantityField.text = [self.noticeProduct.qpcQuantity stringValue];
    _quantityField.text = [self.noticeProduct.quantity stringValue];
    _noteTextView.text = self.noticeProduct.remark;
}
陈俊俊's avatar
陈俊俊 committed
318

陈俊俊's avatar
陈俊俊 committed
319
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
陈俊俊's avatar
陈俊俊 committed
320 321 322
    if (indexPath.row == _dataArr.count - 1) {
        return TableRowHeight *2;
    }
陈俊俊's avatar
陈俊俊 committed
323 324 325 326 327
    return TableRowHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        ChooseProductViewController *cvc = [ChooseProductViewController new];
陈俊俊's avatar
陈俊俊 committed
328 329 330
        if (self.selectProducts.count > 0) {
            cvc.selectArr = self.selectProducts;
        }
陈俊俊's avatar
陈俊俊 committed
331 332 333 334 335 336 337
        cvc.choseBaseInfo = ^(NSArray  *products){
            Product *product=products [0];
            _chooseCostLabel.text = [NSString stringWithFormat:@"%@[%@]",product.name,product.code];
            _chooseCostLabel.textColor = GXF_CONTENT_COLOR;
            self.productNameStr = product.name;
            self.productUuidStr = product.uuid;
            self.productCodeStr = product.code;
陈俊俊's avatar
陈俊俊 committed
338
            _qpcLabel.text = product.measureUnit;
陈俊俊's avatar
陈俊俊 committed
339
            _baseCountLabel.text = _qpcLabel.text;
陈俊俊's avatar
陈俊俊 committed
340 341 342 343
            if (self.selectProducts.count > 0) {
                [self.selectProducts removeAllObjects];
            }
            [self.selectProducts addObject:product.uuid];
陈俊俊's avatar
陈俊俊 committed
344 345 346 347 348
        };
        cvc.isMoreChose = NO;
        [self PushViewController:cvc animated:YES];
    }else if(indexPath.row == 1){
        ChooseProductUnitViewController *cvc = [ChooseProductUnitViewController new];
陈俊俊's avatar
陈俊俊 committed
349 350 351
        if (self.selectUnit.length > 0) {
            cvc.selectStr = self.selectUnit;
        }
陈俊俊's avatar
陈俊俊 committed
352 353 354 355 356
        cvc.choseBaseInfo = ^(NSArray  *products){
            if (products.count > 0) {
                GXFProductUnit *productUnit=products [0];
                _choosePackUnitLabel.text = [NSString stringWithFormat:@"%@",productUnit.name];
                _choosePackUnitLabel.textColor = GXF_CONTENT_COLOR;
陈俊俊's avatar
陈俊俊 committed
357
                _packCountLabel.text = productUnit.name;
陈俊俊's avatar
陈俊俊 committed
358
                self.selectUnit = productUnit.name;
陈俊俊's avatar
陈俊俊 committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
            }
        };
        cvc.isMoreChose = NO;
        [self PushViewController:cvc animated:YES];
    }
}
#pragma mark delegate
- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    [_noteTextView resignFirstResponder];
    return YES;
}

- (void)keyboardHidden{
    [_qpcField resignFirstResponder];
    [_qpcQuantityField resignFirstResponder];
    [_quantityField resignFirstResponder];
    [_noteTextView resignFirstResponder];
}

/*
#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