import { FormSchema } from '@/components/Form/src/types';
import { BaseTableProps } from '@/components/Table/src/types';
import type { BasicTableProps, VxeTablePropTypes } from '@/components/VxeTable';
import { validateCountType, validateFieldType } from '@/utils/feeSeting/validate';
import { reactive, computed } from 'vue';
/**
 * 产品费用设置-表单表格数据
 */
export function useAddEditProviderFeeSetModel() {
    // 表单校验
    const ruleFormBaseicData = reactive({
        feeSetName: [
            {
                required: true,
                message: "请输入费用名称",
                trigger: "blur"
            }
        ],
        providerId: [
            {
                required: true,
                message: "请选择服务商",
                trigger: "change"
            }
        ],
        // beginDate: [
        //     {
        //         required: true,
        //         message: "请选择时间",
        //         trigger: "change"
        //     }
        // ],
        feeItemCategory: [
            {
                required: true,
                message: "请选择费项",
                trigger: "change"
            }
        ],
        // productId: [
        //     {
        //         required: true,
        //         message: "请选择产品",
        //         trigger: "change"
        //     }
        // ],
    })
    const propsCol = reactive({
        col: {
            md: 5,
            lg: 3,
            llg: 3,
            xl: 3
        },
        time: {
            md: 5,
            lg: 3,
            xl: 3
        }
    })
    // 成本费用-表单设置
    const schemasForm: FormSchema[] = [
        // {
        //     field: "providerId",
        //     component: "Select",
        //     label: "服务商：",
        //     defaultValue: "",
        //     propsCol: {
        //         md: 5,
        //         lg: 4,
        //         xl: 4
        //     },
        //     required: true,
        //     // options: currencyList,
        // },
        {
            field: "minWeight",
            component: "Input",
            label: "最小重量：",
            minWidth: 60,
            propsCol: {
                md: 5,
                lg: 4,
                xl: 4
            },
            required: true,
            defaultValue: "",
        },
        {
            field: "maxWeight",
            component: "Input",
            label: "最大重量：",
            defaultValue: "",
            minWidth: 60,
            required: true,
            propsCol: {
                md: 5,
                lg: 4,
                xl: 4
            },
        },
        {
            field: "weightRound",
            component: "Input",
            label: "重量取整:",
            required: true,
            minWidth: 60,
            propsCol: {
                md: 5,
                lg: 4,
                xl: 4
            },
            defaultValue: "",
        },
    ];
    // 产品-费用设置表单校验
    const providerFeesetSetingRule = reactive({
        feeItemId: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        countModel: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        countOrderNumber: [
            {
                required: true,
                message: "请输入内容",
                trigger: ["change", "blur"]
            }
        ],
        offerPrice: [
            {
                required: true,
                message: "请输入内容",
                trigger: ["change", "blur"]
            }
        ],
        discount: [
            {
                required: true,
                message: "请输入内容",
                trigger: ["change", "blur"]
            }
        ],
        currencyCode: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        feeItemGetType: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        shareType: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        isValidate: [{ required: false }]
    })
    // 产品-费用明细设置表单校验
    const providerFeesetSetingDetailRule = reactive({
        originalBeginWeight: [
            {
                required: true,
                message: "请输入内容",
                trigger: ["change", "blur"]
            }
        ],
        originalEndWeight: [
            {
                required: true,
                message: "请输入内容",
                trigger: ["change", "blur"]
            }
        ],
        originalCalculateUnit: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        countModel: [
            {
                required: true,
                message: "请选择内容",
                trigger: ["change", "blur"]
            }
        ],
        partitionId: [{ required: true, message: '必填', trigger: ['blur', 'change'] }]
    })

    // 产品费用设置-表格设置
    const providerFeesetSetingTable: BaseTableProps = reactive({
        border: true,
        height: 300,
        maxHeight: 300,
        columns: [
            {
                prop: "feeItemId",
                label: "费用类型",
                slot: true,
                slotHeader: true,
                slotName: "feeItemId",
                width: 160,
            },
            {
                prop: "countModel",
                label: "算法",
                slot: true,
                width: 160,
                slotName: "countModel",
            },
            {
                prop: "shareType",
                label: "分摊方式",
                slot: true,
                width: 160,
                slotName: "shareType",
            },
            {
                prop: "countOrderNumber",
                label: "计算优先级",
                width: 160,
                slot: true,
                slotName: "countOrderNumber",
            },
            {
                prop: "offerPrice",
                label: "费用",
                slot: true,
                width: 160,
                slotName: "offerPrice",
            },
            {
                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
            //     }
            // }
        ],
    });
    // 
    const providerFeesetSetingDetailTable: BaseTableProps = reactive({
        border: true,
        height: 300,
        maxHeight: 300,
        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: 112,
                minWidth: 112,
                fixed: true,
                slot: true,
                slotHeader: true,
                slotName: "originalCalculateUnit"
            },
            {
                prop: "countModel",
                label: "算法",
                width: 112,
                minWidth: 112,
                fixed: true,
                slot: true,
                slotHeader: true,
                slotName: "countModel"
            },
            {
                prop: "",
                label: "分区内容",
            },
            {
                prop: "action",
                label: "+",
                width: 60,
                slot: true,
                fixed: "right",
                slotHeader: true,
                slotName: 'action'
            },
        ],
        dataSource: []
    });
    // 费用明细
    const providerFeesetSetingDetailTableRule = computed(() => {
        const oneRules = [{ required: true, message: "请输入或选择内容！" }];
        const result = providerFeesetSetingDetailTable.columns.reduce((acc, curr) => {
            acc[curr.prop] = oneRules;
            if (curr.prop === 'countModel') acc[curr.prop].countType = oneRules
            return acc;
        }, {});
        return result
    })
    // 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 providerSetingGridOptions = reactive<BasicTableProps>({
        id: 'providerSeting_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: { header: "header_feeItemId", default: "default_feeItemId", edit: "edit_feeItemId" },
                editRender: {},
                width: 160,
            },
            {
                field: 'countModel',
                title: '算法',
                slots: { default: "default_countModel", edit: "edit_countModel" },
                editRender: {},
                width: 160
            },
            {
                field: 'countOrderNumber',
                title: '计算优先级',
                slots: { edit: "edit_countOrderNumber" },
                editRender: {
                    autoselect: true
                },
                width: 160
            },
            {
                field: 'offerPrice',
                title: '费用',
                slots: { edit: "edit_offerPrice" },
                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,
                slots: { edit: 'edit_remark' },
                editRender: {}
            },
            {
                title: '+',
                slots: { default: 'default_actions', header: "header_actions" },
                width: 60,
                fixed: 'right'
            },
        ],
        data: []
    })

    return {
        propsCol,
        schemasForm,
        ruleFormBaseicData,
        providerFeesetSetingTable,
        providerFeesetSetingDetailTable,
        providerFeesetSetingDetailTableRule,
        providerSetingGridOptions
    }
}