import type { FormPropsSchema, FormSchema } from "@/components/Form/src/types";
import { reactive, computed } from "vue";
import { BaseTableProps } from "@/components/Table/src/types";
import { BaseZoningAction } from "@/type/BaseZoningAction";
import { FormRules } from "element-plus";
import type { BasicTableProps, VxeTablePropTypes } from "@/components/VxeTable";
import { validateCountType, validateFieldType } from "@/utils/feeSeting/validate";

/**
 * 费用设置配置数据
 */
export default function useBusinessFeesetModel() {
    // 币种信息
    // const storeCurrency = useCurrencyStore()
    // const { weightArithmeticList, unitTypeIntegerList, pickValueTypeList, feeItemLists } = useDropDownData()
    // 币种信息列表
    // const currencyList = computed((): ListIitem[] => storeCurrency.getCurrencyList)
    // 处理排队单位类型
    // const feeItemLists_ = computed(() => {
    //     return feeItemLists.value.map(item => ({ label: item.itemName, value: item.id }))
    // })

    // 费用设置-基础表单
    const schemasFeeset: FormSchema[] = [
        {
            field: "mpsWeightCountType",
            component: "Select",
            label: "重量计算方式：",
            defaultValue: "",
            width: 220,
            labelWidth: '110px',
            options: []//weightArithmeticList,
        },
        {
            field: "mpsWeightRound",
            component: "Input",
            label: "单件重量(Kg)：",
            width: 155,
            type: "number",
            defaultValue: "",
        },
        {
            field: "mpsTotalWeightRound",
            component: "Input",
            label: "总重(Kg)：",
            type: "number",
            width: 125,
            defaultValue: "",
        },
        {
            field: "guaranteeWeight",
            component: "Input",
            label: "保底重(Kg)：",
            type: "number",
            width: 143,
            defaultValue: "",
        },
        {
            field: "pickValueType",
            component: "Select",
            label: "取整类型：",
            defaultValue: 0,
            width: 220,
            labelWidth: '100px',
            options: []//pickValueTypeList,
        },
        {
            field: "isCountVolume",
            component: "Checkbox",
            label: "计抛",
            islabel: true,
            defaultValue: false,
        },
        {
            field: "unitType",
            component: "Select",
            label: "单位类型：",
            defaultValue: "",
            labelWidth: '110px',
            required: true,
            width: 220,
            options: []//unitTypeIntegerList,
        },
        {
            field: "orderNumber",
            component: "Input",
            label: "序号：",
            type: "number",
            labelWidth: '98px',
            width: 155,
            defaultValue: "",
        },
        {
            field: "unitTypeList",
            component: "Select",
            label: "排它单位类型：",
            defaultValue: "",
            labelWidth: '110px',
            width: 220,
            filterable: true,
            multiple: true,
            options: []//feeItemLists_,
        },
    ];

    // 表格设置
    const tableDataFeeset: BaseTableProps = reactive({
        border: true,
        maxHeight: 210,
        columns: [
            {
                prop: "orderNumber",
                label: "序号",
                width: 60,
                showOverflowTooltip: true,
            },
            {
                prop: "unitType",
                label: "单位类型",
                slot: true,
                slotName: "unitType",
                width: 130,
            },
            {
                prop: "formulaExplain",
                showOverflowTooltip: true,
                label: "公式",
            },
            // {
            //     prop: "unitTypeList",
            //     label: "排它类型",
            //     showOverflowTooltip: true,
            //     width: 150,
            //     slot: true,
            //     slotName: 'unitTypeList',
            // },
            {
                prop: "action",
                label: "操作",
                width: 150,
                slot: true,
                slotName: 'action'
            },
        ],
        dataSource: [],
    });

    // 费用设置-表格设置
    const feesetSetingTable: BaseTableProps = reactive({
        border: true,
        height: 300,
        maxHeight: 300,
        columns: [
            {
                prop: "feeItemId",
                label: "费用类型",
                slot: true,
                slotName: "feeItemId",
                width: 160,
            },
            {
                prop: "originalCalculateUnit",
                label: "计量单位",
                slot: true,
                slotName: "originalCalculateUnit",
                width: 160,
            },
            {
                prop: "countModel",
                label: "算法",
                slot: true,
                slotName: "countModel",
                width: 160,
            },
            {
                prop: "countOrderNumber",
                label: "计算优先级",
                slot: true,
                slotName: "countOrderNumber",
                width: 160,
            },
            {
                prop: "offerPrice",
                label: "费用",
                slot: true,
                slotName: "offerPrice",
                width: 160,
            },
            {
                prop: "discount",
                label: "最低折扣%",
                slot: true,
                slotName: "discount",
                width: 160,
            },
            {
                prop: "currencyCode",
                label: "币种",
                slot: true,
                slotName: "currencyCode",
                width: 160,
            },
            {
                prop: "feeItemGetType",
                label: "取值类型",
                slot: true,
                slotName: "feeItemGetType",
                width: 160,
            },
            {
                prop: "remark",
                label: "备注",
                slot: true,
                slotName: "remark",
            },
            {
                prop: "action",
                label: "+",
                width: 60,
                slot: true,
                slotHeader: true,
                slotName: 'action'
            },
        ],
        dataSource: [
            // {
            //     id: "123",
            //     feeItemId: 2,
            //     offerPrice: 100,
            //     currencyCode: "USD",
            //     countModel: {
            //         countType: 1
            //     }
            // }
        ],
    });
    // vxe-gird-费用设置校验
    const feesetSetingGridRules = reactive<VxeTablePropTypes.EditRules>({
        feeItemId: [{ message: '请选择费用类型', validator: validateFieldType }],
        // originalCalculateUnit: [{ message: '请选择计量单位', validator: validateFieldType }],
        countOrderNumber: [{ required: true, message: '请输入计算优先级' }],
        offerPrice: [{ required: true, message: '请输入费用' }],
        countModel: [{ message: '请选择算法', validator: validateCountType }],
        currencyCode: [{ message: '请选择币种', validator: validateFieldType }],
        feeItemGetType: [{ message: '请选择取值类型', validator: validateFieldType }],
    })
    // vxe-gird-费用设置表格配置
    const feesetSetingGridOptions = reactive<BasicTableProps>({
        id: 'feesetSeting_table',
        border: true,
        keepSource: true,
        showOverflow: true,
        height: 300,
        loading: false,
        headerRowStyle: { background: '#fff', color: '#909399', borderRight: '1px solid #ccc' },
        editConfig: { trigger: 'click', mode: 'row', autoClear: false, showIcon: false, showAsterisk: false },
        editRules: feesetSetingGridRules,
        rowConfig: { isHover: true, keyField: 'id' },
        cellStyle: { height: '38px', padding: 0 },
        columnConfig: { resizable: true },
        columns: [
            // { type: "seq", width: 45 },
            {
                field: 'feeItemId',
                title: '费用类型',
                slots: { default: "default_feeItemId", edit: "edit_feeItemId" },
                editRender: {},
                width: 160,
            },
            // {
            //     field: 'originalCalculateUnit',
            //     title: '计量单位',
            //     slots: { default: "default_originalCalculateUnit", edit: "edit_originalCalculateUnit" },
            //     editRender: {},
            //     width: 160
            // },
            {
                field: 'countModel',
                title: '算法',
                slots: { default: "default_countModel", edit: "edit_countModel" },
                editRender: {},
                width: 160
            },
            {
                field: 'countOrderNumber',
                title: '计算优先级',
                slots: { edit: "edit_countOrderNumber" },
                editRender: {},
                width: 160
            },
            {
                field: 'offerPrice',
                title: '费用',
                slots: { edit: "edit_offerPrice" },
                editRender: {},
                width: 160
            },
            {
                field: 'discount',
                title: '最低折扣%',
                slots: { edit: "edit_discount" },
                editRender: {},
                width: 160
            },
            {
                field: 'currencyCode',
                title: '币种',
                slots: { default: 'default_currencyCode', edit: "edit_currencyCode" },
                editRender: {},
                width: 160
            },
            {
                field: 'feeItemGetType',
                title: '取值类型',
                slots: { default: "default_feeItemGetType", edit: 'edit_feeItemGetType' },
                editRender: {},
                width: 160
            },
            {
                field: 'remark',
                title: '备注',
                minWidth: 120,
                editRender: {},
                slots: { edit: 'edit_remark' },
            },
            {
                title: '+',
                slots: { default: 'default_actions', header: "header_actions" },
                fixed: 'right',
                width: 60
            },
        ],
        data: []
    })
    // 费用明细设置-表格设置
    const feesetSetingDetailTable: BaseTableProps = reactive({
        border: true,
        height: 380,
        maxHeight: 380,
        columns: [
            {
                prop: "originalBeginWeight",
                label: "起始重量（>）",
                width: 120,
                minWidth: 120,
                fixed: true,
                slot: true,
                slotName: "originalBeginWeight"
            },
            {
                prop: "originalEndWeight",
                label: "结束重量(<=)",
                width: 120,
                minWidth: 120,
                fixed: true,
                slot: true,
                slotName: "originalEndWeight"
            },
            {
                prop: "originalCalculateUnit",
                label: "计量单位",
                width: 120,
                minWidth: 120,
                fixed: true,
                slotHeader: true,
                slot: true,
                slotName: "originalCalculateUnit"
            },
            {
                prop: "countModel",
                label: "算法",
                width: 120,
                minWidth: 120,
                fixed: true,
                slotHeader: true,
                slot: true,
                slotName: "countModel"
            },
            {
                prop: "",
                label: "分区内容",
            },
            {
                prop: "action",
                label: "+",
                width: 60,
                slot: true,
                fixed: "right",
                slotHeader: true,
                slotName: 'action'
            },
        ],
        dataSource: []
    });

    // 费用表单设置
    const schemasFormFeeset: FormSchema[] = [
        // {
        //     field: "productName",
        //     component: "Text",
        //     label: "产品名称：",
        //     defaultValue: "",
        //     width: 140,
        //     // rules: [],
        // },
        // {
        //     field: "currencyCode",
        //     component: "Select",
        //     label: "计费货币：",
        //     defaultValue: "USD",
        //     required: true,
        //     propsCol: {
        //         md: 4,
        //         llg: 6,
        //         lg: 3,
        //         xl: 3
        //     }
        //     // options: currencyList,
        // },
        // {
        //     field: "compareCustomsCurrencyCode",
        //     component: "Select",
        //     label: "报关货币：",
        //     defaultValue: "",
        //     minWidth: 100,
        //     width: 120,
        //     propsCol: {
        //         md: 4,
        //         llg: 6,
        //         lg: 3,
        //         xl: 3
        //     },
        //     required: true,
        //     // options: currencyList,
        // },
        {
            field: "minWeight",
            component: "Input",
            label: "最小重量(Kg)：",
            minWidth: 60,
            width: 80,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
            required: true,
            defaultValue: "",
        },
        {
            field: "maxWeight",
            component: "Input",
            label: "最大重量(Kg)：",
            defaultValue: "",
            minWidth: 60,
            width: 80,
            required: true,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
        },
        {
            field: "weightRound",
            component: "Input",
            label: "重量取整：",
            minWidth: 60,
            width: 80,
            required: true,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
            defaultValue: "",
        },
        {
            field: "bulkProductionMinWeight",
            component: "Input",
            label: "大货起始重量(Kg)：",
            minWidth: 60,
            width: 80,
            required: true,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
            defaultValue: "",
        },
        {
            field: "bulkProductionWeightRound",
            component: "Input",
            label: "大货取整重量(Kg)：",
            minWidth: 60,
            width: 80,
            required: true,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
            defaultValue: "",
        },
        {
            field: "firstWeight",
            component: "Input",
            label: "首重重量(Kg)：",
            required: true,
            minWidth: 60,
            width: 80,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
            defaultValue: "",
        },
        {
            field: "continuousWeight",
            component: "Input",
            label: "续重重量(Kg)：",
            minWidth: 60,
            width: 80,
            propsCol: {
                md: 4,
                llg: 4,
                lg: 3,
                xl: 3
            },
            defaultValue: "",
            required: true,
        },
    ];

    // select-分区管理
    const zoningActionOptions = [
        {
            value: BaseZoningAction.ZONING_SETING,
            label: '分区设置',
        },
        {
            value: BaseZoningAction.ZONING_VIEW,
            label: '分区查看',
        },
        {
            value: BaseZoningAction.ZONING_TEMPLATE,
            label: '导出模板',
            permissions: ['sys:customerfeeset:export', 'sys:customerfeeset:export']
            // TODO 权限按钮显示
        },
        {
            value: BaseZoningAction.ZONING_TEMPLATE_LOAD,
            label: '模板加载',
        },
        {
            value: BaseZoningAction.ZONING_SPECIAL_PARAMETER,
            label: '特殊参数',
        }
    ]

    // 口岸分区-表单验证
    const ruleForm: FormRules = reactive({
        portCode: [
            {
                required: true,
                message: "请选择内容",
                trigger: "change"
            }
        ],
        startZipCode: [{ required: true, message: "请输入内容", trigger: "blur" }],
        endZipCode: [{ required: true, message: "请输入内容", trigger: "blur" }],
        zipCodeRegex: [{ required: true, message: "请输入内容", trigger: "blur" }],
        address: [{ required: true, message: "请输入内容", trigger: "blur" }],
        unitType: [{ required: true, message: '请选择单位类型', trigger: "change" }],
        orderNumber: [{ required: true, message: '请输入序号', trigger: "blur" }],
    })

    // 费用基础信息-表单验证
    const ruleFormFeeBasicData = reactive({
        currencyCode: [{ required: true, message: '请选择计费货币', trigger: ['change', 'blur'] }],
        // compareCustomsCurrencyCode: [{ required: true, message: '请选择报关货币', trigger: ['change', 'blur'] }],
        // minWeight: [{ required: true, message: '请输入最小重量', trigger: ['change', 'blur'] }],
        // maxWeight: [{ required: true, message: '请输入最大重量', trigger: ['change', 'blur'] }],
        // bulkProductionMinWeight: [{ required: true, message: '请输入大货起始重量', trigger: ['change', 'blur'] }],
        // bulkProductionWeightRound: [{ required: true, message: '请输入大货取整重量', trigger: ['change', 'blur'] }],
        // firstWeight: [{ required: true, message: '请输入首重重量', trigger: ['change', 'blur'] }],
        // continuousWeight: [{ required: true, message: '请输入续重重量', trigger: ['change', 'blur'] }],
    })

    // 费用设置-表单验证
    const validateFeeItemId = (rule: Recordable, value: any, callback: any) => {
        if (!value) {
            return callback(new Error('请选择内容'))
        }
        if (feesetSetingTable.dataSource.length > 1) {
            let index = 0
            for (const item of feesetSetingTable.dataSource) {
                if (item.feeItemId === value) {
                    index++
                }
            }
            if (index > 1) {
                callback(new Error('选择的费用类型不能重复！'))
            } else {
                callback()
            }
        } else {
            callback()
        }
    }

    const feesetSetingDetailTableRule = computed(() => {
        const oneRules = [{ required: true, message: "请输入或选择内容！" }];
        const result = feesetSetingDetailTable.columns.reduce((acc, curr) => {
            acc[curr.prop] = oneRules;
            if (curr.prop === 'countModel') acc[curr.prop].countType = oneRules
            return acc;
        }, {});
        return result
    })

    return {
        schemasFeeset,
        tableDataFeeset,
        feesetSetingGridOptions,
        feesetSetingGridRules,
        feesetSetingDetailTable,
        schemasFormFeeset,
        zoningActionOptions,
        ruleForm,
        ruleFormFeeBasicData,
        feesetSetingDetailTableRule,
    }
}