import { getGoodsUnitEnumItemName } from "@/common/constants/enumsName";
import { validateCommon } from "@/common/setup/validate";
import APP from '@/services';
import { Ermcp3Wrstandard, ErmcpDeliveryGoodsDetailEx } from "@/services/go/ermcp/goodsInfo/interface";
import { RuleObject } from "ant-design-vue/lib/form/interface";
import { reactive, ref, UnwrapRef } from "vue";
import { FormState } from "./interface";


/**
 * 表单
 */
export function handleForm() {
    const formRef = ref();
    const formState: UnwrapRef<FormState> = reactive({
        HedgePlanNo: '',// string 套保计划名称
        ContractType: undefined,// int32 计划类型-1:采购-1:销售
        DeliveryGoodsID: undefined,// uint64 现货品种ID
        PlanQty: null, // double 计划数量
        ConvertFactor: undefined, // double 标仓系数
        Remark: '', // string 计划备注
        OperateType: 2, // int32 操作类型-1:保存草稿2:提交申请
        WRStandardID: undefined, // uint64 现货品类ID
        Tradeuserid: undefined, // uint64 交易用户ID
        Currencyid: undefined, // uint64 结算币种ID
    })

    const v_ContractType = async (rule: RuleObject, value: number) => {
        return validateCommon(value, '请选择计划类型');
    };
    const v_DeliveryGoodsID = async (rule: RuleObject, value: number) => {
        return validateCommon(value, '请选择现货品种');
    };
    const v_Currencyid = async (rule: RuleObject, value: number) => {
        return validateCommon(value, '请选择结算币种');
    };
    const rules = {
        PlanQty: [{ required: true, message: '请输入计划数量', trigger: 'blur', type: 'number', min: 1 }],
        ContractType: [{ required: true, validator: v_ContractType, trigger: 'change' }],
        DeliveryGoodsID: [{ required: true, validator: v_DeliveryGoodsID, trigger: 'change' }],
        Currencyid: [{ required: true, validator: v_Currencyid, trigger: 'change' }],
    }
    return { formRef, formState, rules }
}

/**
 * 获取现货品种和品类数据
 */
export function getMiddleGoodsD(formState: UnwrapRef<FormState>) {
    // 现货品种列表
    const DGList = ref<ErmcpDeliveryGoodsDetailEx[]>([])
    // 品类
    const gmlist = ref<Ermcp3Wrstandard[]>([])
    // 品类更变后的单位
    const numberUnit = ref<string>('')

    // 获取品种数据
    function getDG() {
        DGList.value = APP.get('DeliveryGoodsList').filter((e: ErmcpDeliveryGoodsDetailEx) => e.data.isvalid === 1)
    }
    // 选中品种数据
    function chooseMG(id: number) {
        const temp = DGList.value.find(e => e.data.deliverygoodsid === id)
        gmlist.value = temp ? temp.gmlist : []
        formState.ConvertFactor = undefined
        formState.WRStandardID = undefined
    }
    // 选中品类
    function chooseWR(id: number | undefined) {
        if (id) {
            const temp = gmlist.value.find(e => e.wrstandardid === id) as Ermcp3Wrstandard
            if (temp.enumdicname !== null && temp.enumdicname !== '') {  // 如果有单位
                numberUnit.value = temp.enumdicname
            } else {
                numberUnit.value = getGoodsUnitEnumItemName(temp.unitid)  // 没有则去枚举找
            }
            if (temp) {
                formState.ConvertFactor = temp.convertfactor
            } else {
                formState.ConvertFactor = undefined
            }
        } else {
            formState.ConvertFactor = undefined
        }
    }


    return { DGList, getDG, gmlist, chooseMG, chooseWR, numberUnit }
}
