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

#import "SaleInputAddViewController.h"
#import "SaleInputViewModel.h"
#import "SaleInputAmountCollectionViewCell.h"
#import "SaleInputAddAmountCell.h"
#import "InspectRepairAddPicTableViewCell.h"
#import "CalculateHelper.h"
#import "SalesInputTotalVIew.h"
@interface SaleInputAddViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 计算本单合计、输入总笔数 */
@property (nonatomic, weak) IBOutlet SalesInputTotalVIew *totalView;
@property (weak, nonatomic) IBOutlet UIButton *btnBottom;
@property (strong, nonatomic) SaleInputViewModel *viewModel;
@property (strong, nonatomic) NSMutableArray *arrData;
@end

@implementation SaleInputAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WS(weakSelf);
    [self.viewModel httpAllPayments:^(NSMutableArray<HMSaleInputDetail_payments_payment *> *arrPayments) {
        weakSelf.arrData = arrPayments;
        [weakSelf.tableView reloadData];
        CLog(@"%@", arrPayments);
    }];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.arrData.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.arrData 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.arrData) {
        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 httpUpload];
}


ZJLazy(SaleInputViewModel, viewModel);
ZJLazy(NSMutableArray, arrData);
@end
