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

#import "SaleInputViewModel.h"
#import "CalculateHelper.h"
@implementation SaleInputViewModel

/**
 获取全部支付方式
 
 @param callBack 获取到支付方式的回调（如果arrPayments = nil， 则代表请求失败）
 */
- (void)httpAllPayments:(blockCallBack)callBack {
    [ZJHttpManager GET:@"hdmall/salesinput/getAllPayments" parameters:nil complete:^(id responseObject, NSError *error) {
        if (kIsResponse) {
            NSMutableArray *arrData = [HMSaleInputDetail_payments_payment modelsFromArray:responseObject[@"data"]];
            
            //后台返回的结构是HMSaleInputDetail_payments_payment，需要处理一下，变成HMSaleInputDetail_payments才符合使用需求
            NSMutableArray *arrPayments = [NSMutableArray array];
            for (HMSaleInputDetail_payments_payment *pay in arrData) {
                HMSaleInputDetail_payments *payments = [HMSaleInputDetail_payments new];
                payments.payment = pay;
                [arrPayments addObject:payments];
            }
            self.arrPayments = arrPayments;
            callBack(true);
        }else{
            callBack(false);
            kFalseHttpTips;
        }
    }];
}

- (void)httpUpload:(void (^)(void))complete {
    
}

- (void)httpSubmit:(blockCallBack)callBack {
    //赋值
    if (self.request.contract == nil) {
        [MBProgressHUD j_error:@"请选择合同" complete:nil];
        return;
    }
    self.request.oper = AppGlobal.user.code;
    self.request.store = [HMSaleInputDetail_store modelWithDic:AppGlobal.user.store.toDictionary];
//    self.request.tenant = [HMSaleInputDetail_tenant modelWithDic:AppGlobal.user.tenant.toDictionary];
    self.request.receiver = @"contract";
    self.request.balance = false;
    self.request.saleDate = [[NSDate date] httpParameterString];
    self.request.payments = (id)[NSMutableArray array];
    for (HMSaleInputDetail_payments *payments in self.arrPayments) {
        HMSaleInputDetail_payments *paymentsCopy = [HMSaleInputDetail_payments modelWithDic:payments.toDictionary];
        if (paymentsCopy.total == nil) {
            paymentsCopy.total = @(0);
        }
        [self.request.payments addObject:paymentsCopy];
    }
    
    //上传附件
    NSMutableArray *arrImage = [NSMutableArray array];
    for (UIImage *img in self.picVM.arrPics) {
        ZJBaseFileModel *file = [ZJBaseFileModel new];
        file.data = [img imageDataForUpload];
        file.name = [file.data md5String];
        file.keyName = @"file";
        file.mimeType = @"image/jpeg";
        [arrImage addObject:file];
    }
    
    [MBProgressHUD j_loading:@"上传中…"];
    [ZJHttpManager upLoadFiles:arrImage params:nil commplete:^(NSMutableArray *attachments) {
        self.request.mediaFiles = attachments;
        [MBProgressHUD j_loading:@"提交中…"];
        [ZJHttpManager POST:@"hdmall/salesinput/save" parameters:self.request.toDictionary complete:^(id responseObject, NSError *error) {
            if (kIsResponse) {
                [MBProgressHUD j_hideLoadingView];
                callBack(true);
            }else{
                callBack(false);
                kFalseHttpTips;
            }
        }];
    }];
}

- (void)httpDetailWithUuid:(NSString *)uuid callBack:(blockCallBack)callBack {
    NSString *url = [@"hdmall/salesinput/" stringByAppendingString:uuid];
    
    [ZJHttpManager GET:url parameters:nil complete:^(id responseObject, NSError *error) {
        if (kIsResponse) {
            
        }else{
            kFalseHttpTips;
        }
    }];
}

ZJLazy(PicViewModel, picVM);
ZJLazy(NSMutableArray, arrPayments);
ZJLazy(HMSaleInputDetail, request);

@end
