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

#import "SaleInputAddViewController.h"
#import "SaleInputHistoryViewController.h"
@interface SaleInputAddViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation SaleInputAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建销售录入";
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 84, 0);
    [self httpPayments];
}

- (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 {
    WS(weakSelf);
    [self.viewModel httpAllPayments:^(BOOL isSuccess) {
        [weakSelf.tableView reloadData];
        [weakSelf.topView httpToDayData];
    }];
}

#pragma mark - actions

- (IBAction)actionSubmit:(id)sender {
    self.viewModel.request.saleCount = @(self.totalView.textFieldBillCount.text.integerValue);
    self.viewModel.request.contract = self.topView.contract;
    self.viewModel.request.saleDate = self.topView.textFieldDate.text;
    WS(weakSelf);
    //赋值
    kCanNotBeNil(self.viewModel.request.contract, @"请选择合同!");
    
    //负值冲账校验
//    NSDecimalNumber *limit = [CalculateHelper sub:@0 num2:self.topView.labelTodayTotal.text];
//    NSDecimalNumber *billTotal = [CalculateHelper decimalNumber:self.totalView.labelTotal.text];
//    if ([billTotal compare:limit] == NSOrderedAscending) {
//        [MBProgressHUD j_warningMessage:[NSString stringWithFormat:@"本单合计必须大于%@元", limit.stringValue]];
//        return;
//    }
    
    NSString *msg = [NSString stringWithFormat:@"销售日期：%@\n总金额： %@\n\n确认提交？",self.topView.textFieldDate.text, self.totalView.labelTotal.text];
    [self alertTitle:@"温馨提示" msg:msg okAction:^(UIAlertAction * _Nullable action) {
        [weakSelf.viewModel httpSubmit:^(BOOL isSuccess) {
            if (isSuccess) {
                weakSelf.commplete();
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        }];
        
    } cancelAction:nil];
}

ZJLazy(SaleInputViewModel, viewModel);
@end
