import { getAreaUserId } from "@/services/bus/user";
import {
    GldErmcpSpotContractOperateReq,
    GldSpotContractInfo
} from "@/services/proto/spotcontract/interface";
import { orderContractControl } from "@/views/information/spot-contract/components/setup";
import moment from 'moment';
import { Ref } from 'vue';
import { FormState } from "../interface";


/**
 * 新增现货合同 表单提交
 */
export function addContractReq() {
    /**
     * @param form 表单信息
     *  @Param type  1: 保存草稿  2: 提交申请
     */
    function sendReq(form: FormState, loading: Ref<boolean>, OperateType: 1 | 2): Promise<string> {
        loading.value = true
        const info: GldSpotContractInfo = {
            UserID: getAreaUserId(),//  机构ID
            ProductType: 1, //                              产品类型 产品类型-1:标准仓单2:等标3:非标
            ContractNo: form.ContractNo,      //            现货合同编号
            ContractType: form.ContractType, //             现货合同类型-1:采购-1:销售
            BuyUserID: form.BuyUserID, //                   采购方ID
            SellUserID: form.SellUserID,//                  客户ID
            DeliveryGoodsID: form.DeliveryGoodsID as number,//   现货品种ID
            WrStandardID: form.WrStandardID as number,//         品类ID
            SpotGoodsBrandID: form.SpotGoodsBrandID as number,// 品牌ID
            ConvertFactor: form.ConvertFactor as number, //      标仓系数
            SpotGoodsDesc: form.SpotGoodsDesc, //                商品规格
            PriceType: form.PriceType,//                         定价类型
            CurrencyID: form.CurrencyID as number,//             结算币种
            Qty: Number(form.Qty), //                     数量
            Price: Number(form.Price), //                 价格
            TradeDate: moment().format("YYYYMMDD"),//             交易日
            SignDate: moment().format("YYYY-MM-DD HH:mm:ss"), //  签订日期
            // 以上必填
            // 以下选填
            BizType: form.BizType, //                       业务类型 - 1:套保 2:套利
            Remark: form.Remark,  //       合同备注
            // ContractAttachment: ,  // 合同附件
            ContractMargin: form.ContractMargin ? Number(form.ContractMargin) : 0, //         合同保证金

            Amount: [1, 3].includes(form.PriceType) ? Number(form.Price) * Number(form.Qty) : 0, //    金额
            PriceMove: Number(form.PriceMove as number), //  升贴水

            StartDate: form.StartDate, //                       点价开始时间
            EndDate: form.EndDate, //                           点价结束时间
            DeliveryStartDate: form.DeliveryStartDate,//        交收期开始
            DeliveryEndDate: form.DeliveryEndDate, //           交收期结束

            GoodsID: form.GoodsID as number, //               点价合约ID-0:为现货，其它为期货商品合约ID[2:点价3:暂定价]

            MerUserID: form.MerUserID || 0, //             跟单员ID
            TradeUserID: form.TradeUserID || 0, //           交易员ID
            SaleUserID: form.SaleUserID || 0,//             业务员id
        }
        const params: GldErmcpSpotContractOperateReq = {
            SpotContractID: '0',
            OperateType,
            Remark: '',
            Info: info,
        }
        return orderContractControl(params, loading)
            .then(res => {
                return Promise.resolve(res);
            })
            .catch(err => {
                return Promise.reject(err);
            })
    }

    return { sendReq }
}
