//
//  SaleInputAddViewController.m
//  HDMall
//
//  Created by Javen on 2017/7/31.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "SaleInputAddViewController.h"

@interface SaleInputAddViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation SaleInputAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WS(weakSelf);
    [self.viewModel httpAllPayments:^(BOOL isSuccess) {
        [weakSelf.tableView reloadData];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.viewModel.arrPayments.count;
    }else{
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else{
        return 50;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"header"];
    return cell.contentView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }else{
        return self.viewModel.picVM.cellHeight;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        //输入各种支付方式的金额
        SaleInputAddAmountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SaleInputAddAmountCell" forIndexPath:indexPath];
        cell.isEdit = self.viewModel.type != SaleInputTypeReadOnly;
        [cell configCellWithArray:self.viewModel.arrPayments indexPath:indexPath];
        WS(weakSelf);
        cell.blockNumberChanged = ^{
            [weakSelf allInputMoney];
        };
        return cell;
    }else{
        InspectRepairAddPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InspectRepairAddPicTableViewCell" forIndexPath:indexPath];
        
        [cell configCellWithViewModel:self.viewModel.picVM
                            indexPath:indexPath
                               target:self];
        WS(weakSelf);
        cell.blockReloadData = ^{
            [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
        };
        return cell;
    }
}

- (void)allInputMoney {
    NSDecimalNumber *allInputMoney = nil;
    for (HMSaleInputDetail_payments *cellModel in self.viewModel.arrPayments) {
        allInputMoney = [CalculateHelper calculateNum1:allInputMoney num2:cellModel.total type:CalculateTypeAdd];
    }
    //本单合计
    self.totalView.labelTotal.text = [CalculateHelper getMoneyStringFrom:allInputMoney];
}

- (void)httpPayments {
    
}

#pragma mark - actions

- (IBAction)actionSubmit:(id)sender {
    self.viewModel.request.saleCount = @(self.totalView.textFieldBillCount.text.integerValue);
    self.viewModel.request.contract = self.topView.contract;
    WS(weakSelf);
    [self.viewModel httpSubmit:^(BOOL isSuccess) {
        if (isSuccess) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}

ZJLazy(SaleInputViewModel, viewModel);
@end