import type { FinancialBillDetailDto } from '@/api/financial/financialBillDetailList/financialBillDetailListModle';
import { ref, reactive, computed, unref, nextTick } from 'vue';
import { useFeesetList } from '@/store/list_feeset';
import { useDropDownData } from '@/hooks/useDropDownData';
import { FormSchema } from '@/components/Form/src/types';
import { cloneDeep } from '@/utils/cloneDeep';
import { isNumber, isString } from '@/utils/is';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { deal_FindItemName } from '@/utils/dealFindData';
import { feeItemFindName } from '@/utils/feeSeting/data';
type DefineEmit = {
    (e: 'saveConfirm', params: FinancialBillDetailDto, onclose: Fn): void
}
/**
 * 编辑财务应付明细
 * @returns 
 */
function useFinancialBillDetailSet(onClose: Fn, onShow: Fn, emit: DefineEmit) {
    // 下拉列表数据
    const storeFeesetList = useFeesetList()
    const {
        companyEntityTypeList,
        allProvidersList,
        airCompanyList,
        portInfoList,
        feeItemLists,
        currencyList,
        shareTypeList,
        countryList,
        calculateUnitList
    } = useDropDownData()
    const feeItemCategoryList = computed(() => storeFeesetList.getFeeItemCategoryList)
    const newFeeItemLists = computed(() => {
        return unref(feeItemLists).map((item) => ({ label: item.itemName, value: item.id }))
    })
    // 行布局
    const propsCol = {
        md: 12,
        lg: 8,
        llg: 6,
        xl: 6
    }
    const inputRegex = /^\d*\.?\d{0,3}$/
    const financialBillDetailSetFormRef = ref()
    const basisListRef = ref<{ reset: Fn }>()
    // 基础数据
    const financialBillDetailSetState = reactive<{
        rulesFinancialBillDetailSet: Recordable,
        basisSchemas: FormSchema[]
    }>({
        rulesFinancialBillDetailSet: {
            stakeholder: [{ required: true, message: '请选择主体类型', trigger: 'change' }],
            providerId: [{ required: true, message: '请选择渠道商', trigger: 'change' }],
            waybillNumber: [{ required: true, message: '请输入提单号', trigger: 'blur' }],
            // orderId: [{ required: true, message: '请输入订单号', trigger: 'blur' }],
            // trackingId: [{ required: true, message: '请输入尾程单号', trigger: 'blur' }],
            weight: [{ required: true, message: '请输入重量', trigger: 'blur' }],
            calculateUnit: [{ required: true, message: '请选择重量单位', trigger: 'change' }],
            // groupShareType: [{ required: true, message: '请选择同组分摊', trigger: 'change' }],
        },
        basisSchemas: [
            {
                field: "feeItemCategoryId",
                component: "Select",
                label: "账单类型:",
                labelWidth: 100,
                propsCol,
                options: feeItemCategoryList,
                componentProps: {
                    clearable: true,
                },
            },
            {
                field: "stakeholder",
                component: "Select",
                label: "所属主体:",
                labelWidth: 100,
                propsCol,
                options: companyEntityTypeList,
                componentProps: {
                    clearable: true,
                },
            },
            {
                field: "providerId",
                component: "SelectV2",
                label: "渠道商:",
                labelWidth: 100,
                propsCol,
                options: allProvidersList,
                componentProps: {
                    filterable: true,
                    clearable: true,
                    popperClass: "customerId-w",
                }
            },
            {
                field: "airlinesId",
                component: "Select",
                label: "航司名称:",
                labelWidth: 100,
                propsCol,
                options: airCompanyList,
                componentProps: {
                    filterable: true,
                    clearable: true,
                }
            },
            {
                field: "waybillNumber",
                component: "Input",
                label: "提单号:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "orderId",
                component: "Input",
                label: "订单号:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "trackingId",
                component: "Input",
                label: "尾程单号:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "countryId",
                component: "SelectV2",
                label: "国家/地区:",
                labelWidth: 110,
                propsCol,
                options: countryList,
                componentProps: {
                    filterable: true,
                    clearable: true,
                    popperClass: "countryId-w"
                }
            },
            {
                field: "departurePort",
                component: "Select",
                label: "起始港:",
                labelWidth: 100,
                propsCol,
                options: portInfoList,
                componentProps: {
                    filterable: true,
                    clearable: true,
                }
            },
            {
                field: "destinationPort",
                component: "Select",
                label: "目的港:",
                labelWidth: 100,
                propsCol,
                options: portInfoList,
                componentProps: {
                    filterable: true,
                    clearable: true,
                }
            },
            {
                field: "zipCode",
                component: "Input",
                label: "邮编:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "address",
                component: "Input",
                label: "地址:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "partitionName",
                component: "Input",
                label: "区间名称:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "weight",
                component: "Input",
                label: "重量:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "countFeeWeight",
                component: "Input",
                label: "计费重量:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "calculateUnit",
                component: "Select",
                label: "重量单位:",
                labelWidth: 100,
                propsCol,
                options: calculateUnitList,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "orderPieces",
                component: "Input",
                label: "件数:",
                labelWidth: 100,
                propsCol,
                inputRegex: /^(?:\+?[1-9]\d*)?$/,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "volume",
                component: "Input",
                label: "体积:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "length",
                component: "Input",
                label: "长:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "width",
                component: "Input",
                label: "宽:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "height",
                component: "Input",
                label: "高:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "feeItemId",
                component: "Select",
                label: "费用名称:",
                labelWidth: 100,
                propsCol,
                options: newFeeItemLists,
                componentProps: {
                    clearable: true,
                    filterable: true,
                }
            },
            {
                field: "feeTypeNamePostfix",
                component: "Input",
                label: "费用后缀:",
                labelWidth: 100,
                propsCol,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "currencyCode",
                component: "Select",
                label: "币种:",
                labelWidth: 100,
                propsCol,
                options: currencyList,
                componentProps: {
                    filterable: true,
                }
            },
            {
                field: "offerPrice",
                component: "Input",
                label: "单价:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "fee",
                component: "Input",
                label: "费用:",
                labelWidth: 100,
                propsCol,
                inputRegex,
                componentProps: {
                    clearable: true,
                }
            },
            {
                field: "groupShareType",
                component: "Select",
                label: "同组分摊:",
                labelWidth: 100,
                propsCol,
                options: shareTypeList,
                componentProps: {
                    filterable: true,
                    clearable: true,
                }
            },
            // {
            //     field: "remark",
            //     component: "Input",
            //     label: "备注:",
            //     labelWidth: 100,
            //     propsCol,
            //     // propsCol: {
            //     //     md: 24,
            //     //     lg: 16,
            //     //     llg: 12,
            //     //     xl: 12
            //     // },
            // },
        ]
    })
    // 参数
    const financialBillDetailSetForm = reactive<FinancialBillDetailDto>({
        id: undefined,
        summaryId: 0,
        feeItemCategoryId: undefined,
        stakeholder: undefined,
        providerId: undefined,
        airlinesId: undefined,
        waybillNumber: undefined,
        countryId: undefined,
        orderId: undefined,
        trackingId: undefined,
        departurePort: undefined,
        destinationPort: undefined,
        zipCode: undefined,
        partitionName: undefined,
        weight: undefined,
        countFeeWeight: undefined,
        calculateUnit: undefined,
        orderPieces: undefined,
        volume: undefined,
        length: undefined,
        width: undefined,
        height: undefined,
        feeItemId: undefined,
        feeTypeNamePostfix: undefined,
        currencyCode: undefined,
        offerPrice: undefined,
        fee: undefined,
        groupShareType: undefined,
        address: undefined,
        // remark: undefined,
    })
    // 校验表单
    const { formValidate, resetForm } = useEleFormValidate();
    // 更新表单数据
    const changeFinancialBillDetailSetForm = (data: FinancialBillDetailDto) => {
        const cloneData = cloneDeep(data) as FinancialBillDetailDto
        Object.keys(financialBillDetailSetForm).forEach((key) => {
            if (isString(cloneData[key]) || isNumber(cloneData[key])) {
                financialBillDetailSetForm[key] = cloneData[key];
            }
        })
        financialBillDetailSetForm.id = data.id
    }
    // 初始化表单值
    const initFormData = <T extends FinancialBillDetailDto>(data: T) => {
        const cloneData = cloneDeep(data) as T
        cloneData.providerId = String(data.providerId)
        cloneData.feeItemCategoryId = String(data.feeItemCategoryId)
        changeFinancialBillDetailSetForm(cloneData)
    }
    // 显示
    const show = (data: FinancialBillDetailDto) => {
        nextTick(() => {
            initFormData(data)
        })
        onShow()
    }
    // 隐藏
    const onclose = () => {
        unref(basisListRef)?.reset()
        resetForm(financialBillDetailSetFormRef.value)
        onClose()
    }
    // 处理参数
    const dealParams = () => {
        const feeItemName = feeItemFindName(Number(financialBillDetailSetForm.feeItemId), unref(feeItemLists))
        financialBillDetailSetForm.feeItemName = feeItemName
        if(financialBillDetailSetForm.countryId) {
            const countryName = deal_FindItemName(financialBillDetailSetForm.countryId, unref(countryList))
            financialBillDetailSetForm.countryName = countryName
        }
        if(financialBillDetailSetForm.departurePort) {
            const departurePortName = deal_FindItemName(financialBillDetailSetForm.departurePort, unref(portInfoList))
            financialBillDetailSetForm.departurePortName = departurePortName   
        }
        if(financialBillDetailSetForm.destinationPort) {
            const destinationPortName = deal_FindItemName(financialBillDetailSetForm.destinationPort, unref(portInfoList))
            financialBillDetailSetForm.destinationPortName = destinationPortName   
        }
        const params = cloneDeep(financialBillDetailSetForm)
        return params
    }
    // 确定 
    const confirm = async () => {
        const isValidate = await formValidate(financialBillDetailSetFormRef.value)
        if (!isValidate) return
        const params = dealParams()
        emit('saveConfirm', params, onclose)
    }

    return {
        financialBillDetailSetForm,
        financialBillDetailSetState,
        financialBillDetailSetFormRef,
        basisListRef,
        show,
        onclose,
        confirm,
        changeFinancialBillDetailSetForm
    }
}

export { useFinancialBillDetailSet }