//
//  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:@"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)httpSubmit:(blockCallBack)callBack {
    self.request.oper = AppGlobal.user.code;
    self.request.store = [HMSaleInputDetail_store modelWithDic:AppGlobal.user.store.toDictionary];
    self.request.receiver = @"contract";
    self.request.balance = false;
    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] stringByAppendingString:@".jpg"];
        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:@"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 = [@"salesinput/" stringByAppendingString:uuid];
    
    [ZJHttpManager GET:url parameters:nil complete:^(id responseObject, NSError *error) {
        if (kIsResponse) {
            self.detail = [HMSaleInputDetail modelWithDic:responseObject[@"data"]];
            //支付方式列表
            self.arrPayments = self.detail.payments;
            
            //附件列表
            [self.picVM.arrPics removeAllObjects];
            for (NSString *name in self.detail.mediaFiles) {
                NSString *url = [NSString stringWithFormat:@"%@/media/download?moduleName=sale&num=%@&fileName=%@",REST_BASE_URL, self.detail.billNumber, name];
                ZJBaseFileModel *file = [ZJBaseFileModel new];
                file.fileUrl = url;
                [self.picVM.arrPics addObject:file];
            }
            
            callBack(true);
        }else{
            callBack(false);
            kFalseHttpTips;
        }
    }];
}

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

@end
