NewCostViewController.m 11.1 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 26 27 28 29 30 31 32 33 34 35
//
//  NewCostViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/9/7.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "NewCostViewController.h"
#import "SurveyCell.h"
#import "HPGrowingTextView.h"
#import "ChooseCostViewController.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;

@interface NewCostViewController ()<UITableViewDataSource,UITableViewDelegate,HPGrowingTextViewDelegate,UITextFieldDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    
    UILabel *_chooseCostLabel;
    UITextField *_actualmoneyField;
    UITextField *_paidmoneyField;
    UITextField *_leftmoneyField;
    HPGrowingTextView *_noteTextView;
}
陈俊俊's avatar
陈俊俊 committed
36
@property (nonatomic,strong)NSString *selectStr;
陈俊俊's avatar
陈俊俊 committed
37 38 39 40 41 42 43 44 45 46 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103
@end

@implementation NewCostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.navTitle;
    [self initData];
    [self createTableView];
    
}
- (void)initData{
    
    _dataArr = [NSMutableArray array];
    [_dataArr addObject:@"费用"];
    [_dataArr addObject:@"应付金额"];
    [_dataArr addObject:@"已付金额"];
    [_dataArr addObject:@"尾款"];
    [_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://删除
        {
                if (self.indexPath) {
                  self.deleteTransportCost(self.accountDetail,self.indexPath);
                 [self PopViewControllerAnimated:YES];
                }else{
                    [self clearInfomation];
                }
        }
            break;
        case SaveTag:
        {
            if ([self checkCost]) {
                self.getTransportCost(self.accountDetail,self.indexPath);
                [self PopViewControllerAnimated:YES];
            }
        }
            break;
        default:
            break;
    }
104 105
    
    [[NSNotificationCenter defaultCenter] postNotificationName:SetProductTotalPrice object:nil];
陈俊俊's avatar
陈俊俊 committed
106 107 108 109 110 111
}
- (void)clearInfomation{
    _chooseCostLabel.text = @"选择费用类型";
    _chooseCostLabel.textColor = GXF_PLACEHOLDER_COLOR;
    _actualmoneyField.text = @"";
    _paidmoneyField.text = @"";
陈俊俊's avatar
陈俊俊 committed
112 113
    _leftmoneyField.text = @"0";
    _noteTextView.text = @"";
陈俊俊's avatar
陈俊俊 committed
114 115 116
}
- (BOOL)checkCost{
    
陈俊俊's avatar
陈俊俊 committed
117
    if (_chooseCostLabel.text.length == 0 || [_chooseCostLabel.text isEqualToString:@"选择费用类型"] ) {
陈俊俊's avatar
陈俊俊 committed
118 119 120 121 122 123 124 125 126 127 128
        ShowMessage(@"费用类型不能为空");
        return NO;
    }
    if (_actualmoneyField.text.length == 0 ) {
        ShowMessage(@"应付金额不能为空");
        return NO;
    }
    if (_paidmoneyField.text.length == 0) {
        ShowMessage(@"已付金额不能为空");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
129
    if (_leftmoneyField.text.length == 0 ) {
陈俊俊's avatar
陈俊俊 committed
130 131 132
        ShowMessage(@"尾款不能为空");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
133
    if ([_leftmoneyField.text floatValue] < 0) {
陈俊俊's avatar
陈俊俊 committed
134 135 136
        ShowMessage(@"检查应付金额和已付金额是否正确");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
137 138 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    if (!self.accountDetail) {
        self.accountDetail = [[FeeAcountDetail alloc]init];
    }
    self.accountDetail.accounttitle = _chooseCostLabel.text;
    self.accountDetail.accounttitlename = _chooseCostLabel.text;
    self.accountDetail.actualmoney = [NSNumber numberWithFloat:[_actualmoneyField.text floatValue]];
    self.accountDetail.paidmoney = [NSNumber numberWithFloat:[_paidmoneyField.text floatValue]];
    self.accountDetail.leftmoney = [NSNumber numberWithFloat:[_leftmoneyField.text floatValue]];
    self.accountDetail.note = _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;
        if (indexPath.row == 0) {
            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) {
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:(CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-15, TableRowHeight))];
        contentLabel.textAlignment= NSTextAlignmentRight;
        contentLabel.textColor = GXF_PLACEHOLDER_COLOR;
        contentLabel.font = GXF_FIFTEENTEN_SIZE;
        [cell.contentView addSubview:contentLabel];
        contentLabel.text = @"选择费用类型";
        _chooseCostLabel = contentLabel;
        
    }else if (indexPath.row == _dataArr.count -1){
        _noteTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-15, TableRowHeight)];
        _noteTextView.contentInset = UIEdgeInsetsMake(5, 5, 5, 0);
        _noteTextView.minNumberOfLines = 1;
        _noteTextView.maxNumberOfLines = 1;
        _noteTextView.isScrollable = YES;
        _noteTextView.font = GXF_FIFTEENTEN_SIZE;
        _noteTextView.textAlignment = NSTextAlignmentRight;
        _noteTextView.delegate = self;
        _noteTextView.returnKeyType = UIReturnKeyDone;
        _noteTextView.placeholder = @"输入备注内容";
        [cell.contentView addSubview:_noteTextView];
    }else{
陈俊俊's avatar
陈俊俊 committed
195
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-30, TableRowHeight)];
陈俊俊's avatar
陈俊俊 committed
196 197 198 199
        textField.textAlignment = NSTextAlignmentRight;
        textField.textColor = GXF_CONTENT_COLOR;
        textField.font = GXF_FIFTEENTEN_SIZE;
        textField.returnKeyType = UIReturnKeyDone;
200
        textField.keyboardType = UIKeyboardTypeDecimalPad;
陈俊俊's avatar
陈俊俊 committed
201 202
        textField.delegate = self;
        [cell.contentView addSubview:textField];
陈俊俊's avatar
陈俊俊 committed
203 204 205 206 207 208 209
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(textField.frame)+ 5, 0, 20, TableRowHeight)];
        label.textColor = GXF_CONTENT_COLOR;
        label.font = GXF_FIFTEENTEN_SIZE;
        label.text = @"元";
        [cell.contentView addSubview:label];
        
陈俊俊's avatar
陈俊俊 committed
210 211
        if (indexPath.row == 1) {
            textField.placeholder = @"输入应付金额";
陈俊俊's avatar
陈俊俊 committed
212
            [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventAllEditingEvents];
陈俊俊's avatar
陈俊俊 committed
213 214
            _actualmoneyField = textField;
        }else if(indexPath.row == 2){
陈俊俊's avatar
陈俊俊 committed
215
            [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventAllEditingEvents];
陈俊俊's avatar
陈俊俊 committed
216 217 218
            textField.placeholder = @"输入已付金额";
            _paidmoneyField = textField;
        }else{
陈俊俊's avatar
陈俊俊 committed
219 220
            [textField setEnabled:NO];
            textField.text = @"0";
陈俊俊's avatar
陈俊俊 committed
221
            textField.textColor = GXF_NAVIGAYION_COLOR;
陈俊俊's avatar
陈俊俊 committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            _leftmoneyField = textField;
        }
    }
    if (self.accountDetail) {
        [self prepareDataIncell];
    }
}
- (void)prepareDataIncell{
    _chooseCostLabel.text = self.accountDetail.accounttitle;
    _chooseCostLabel.textColor = GXF_CONTENT_COLOR;

    _actualmoneyField.text = [self.accountDetail.actualmoney stringValue];
    _paidmoneyField.text = [self.accountDetail.paidmoney stringValue];
    _leftmoneyField.text = [self.accountDetail.leftmoney stringValue];
    _noteTextView.text = self.accountDetail.note;
    
陈俊俊's avatar
陈俊俊 committed
238 239
    self.selectStr = self.accountDetail.accounttitle;
    
陈俊俊's avatar
陈俊俊 committed
240
}
陈俊俊's avatar
陈俊俊 committed
241 242
- (void)textChange:(UITextField *)textField{
    if (_actualmoneyField.text.length > 0 && _paidmoneyField.text.length > 0   ) {
陈俊俊's avatar
陈俊俊 committed
243 244
        _leftmoneyField.text = [NSString stringWithFormat:@"%.2f",[_actualmoneyField.text floatValue] - [_paidmoneyField.text floatValue]];
        
陈俊俊's avatar
陈俊俊 committed
245 246 247 248
    }
}


陈俊俊's avatar
陈俊俊 committed
249 250 251 252 253 254
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TableRowHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        ChooseCostViewController *cvc = [ChooseCostViewController new];
陈俊俊's avatar
陈俊俊 committed
255 256 257
        if (self.selectStr.length > 0) {
            cvc.selectStr = self.selectStr;
        }
陈俊俊's avatar
陈俊俊 committed
258 259
        cvc.choseBaseInfo = ^(NSArray *costs){
            if (costs.count > 0) {
陈俊俊's avatar
陈俊俊 committed
260 261
                Accounttitle *type = costs[0];
                _chooseCostLabel.text = type.name;
陈俊俊's avatar
陈俊俊 committed
262
                _chooseCostLabel.textColor = GXF_CONTENT_COLOR;
陈俊俊's avatar
陈俊俊 committed
263
                self.selectStr = type.name;
陈俊俊's avatar
陈俊俊 committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
            }
        };
        [self PushViewController:cvc animated:YES];
    }
}
#pragma mark delegate
- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    [_noteTextView resignFirstResponder];
    return YES;
}

- (void)keyboardHidden{
    [_actualmoneyField resignFirstResponder];
    [_paidmoneyField resignFirstResponder];
    [_leftmoneyField resignFirstResponder];
    [_noteTextView resignFirstResponder];
}

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

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