import { ref } from 'vue';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { dict, getTown, getHylb } from '/@/api/common/api'
import { convertTreeData } from '/@/utils/modifFiled';
import { useUserStore } from '/@/store/modules/user';

const userStore: any = useUserStore();
export const useSuspectsData = () => {
    const suspects = ref([]);
    // 获取行业分类
    const getIsSuspect = async () => {
        suspects.value = await dict({ key: 'dk_hyfl' });
    };
    getIsSuspect();
    return { data: suspects };
};

export const useCategoryData = () => {
    let allCategory = [];
    const treeData = ref([]);
    // 获取行业类别
    const getData = async () => {
        allCategory = await getHylb({});
        allCategory = convertTreeData(allCategory); // 只显示前2级
        // allCategory.forEach((item: any) => {
        //     item.disabled = true; // 第1层级不能选中
        // });
        treeData.value = allCategory;
    };
    getData();
    // 改变行业分类
    function changeIsSuspect(v) {
        if (v == 1) {
            // 只显示42号类
            treeData.value = allCategory.filter((item) => item['is42']);
        } else {
            treeData.value = allCategory;
        }
    }
    return { data: treeData, changeIsSuspect };
};
// 搜索条件
export const Columns: BasicColumn[] = [

    {
        title: '市',
        dataIndex: 'cityName',
        resizable: true,
    },
    {
        title: '县',
        dataIndex: 'countryName',
        resizable: true,
    },
    {
        title: '地块名称',
        dataIndex: 'wrdkmc',
        resizable: true,
    },
    {
        title: '地块编码',
        dataIndex: 'wrdkbm',
        resizable: true,
    },
    {
        title: '行业类别',
        dataIndex: 'hylb',
        resizable: true,
    },
    {
        title: '政策依据',
        dataIndex: 'faliyiju',
        resizable: true,
    },

    {
        title: '地块类型',
        dataIndex: 'dklx',
        resizable: true,
    },
    {
        title: '是否移出',
        dataIndex: 'sfyc',
        resizable: true,
    },
    {
        title: '创建地块时间',
        dataIndex: 'createTsamp',
        resizable: true,
    },
    {
        title: '更新时间',
        dataIndex: 'updateTsamp',
        resizable: true,
    },
    {
        title: '同步国家库',
        dataIndex: 'tbgjk',
        resizable: true,
    },
    {
        title: '同步结果',
        dataIndex: 'tbjg',
        resizable: true,
    },
    {
        title: '上报时间',
        dataIndex: 'tsrq',
        resizable: true,
    },
]
// 搜索条件
export const ColumnsNo: BasicColumn[] = [

    {
        title: '市',
        dataIndex: 'cityName',
        resizable: true,
    },
    {
        title: '县',
        dataIndex: 'countryName',
        resizable: true,
    },
    {
        title: '地块名称',
        dataIndex: 'wrdkmc',
        resizable: true,
    },
    {
        title: '地块编码',
        dataIndex: 'wrdkbm',
        resizable: true,
    },
    {
        title: '行业类别',
        dataIndex: 'hylb',
        resizable: true,
    },
    {
        title: '政策依据',
        dataIndex: 'faliyiju',
        resizable: true,
    },

    {
        title: '地块类型',
        dataIndex: 'dklx',
        resizable: true,
    },
    {
        title: '所在阶段',
        dataIndex: 'szjd',
        resizable: true,
    },

    {
        title: '创建地块时间',
        dataIndex: 'createTsamp',
        resizable: true,
    },
]
export const searchFormSchema: FormSchema[] = [
    {
        label: '市',
        field: 'cityCode',
        component: 'ApiSelect',
        ifShow: () => {
            return userStore?.userInfo?.userIdentity == '1' ? true : false
        },
        componentProps: ({ formModel, formActionType }) => {
            return {
                api: getTown,
                params: 440000, // 行政区划编码
                labelField: 'name',
                valueField: 'id',
                onChange: (val: any) => {
                    const { updateSchema } = formActionType;
                    formModel.countryCode = undefined;
                    if (val) {
                        updateSchema({
                            field: 'countryCode',
                            componentProps: {
                                api: getTown,
                                params: val,
                                labelField: 'name',
                                valueField: 'id',
                            },
                        });
                    } else {
                        updateSchema({
                            field: 'countryCode',
                            componentProps: {
                                api: undefined,
                                labelField: 'name',
                                valueField: 'id',
                            },
                        });
                    }
                },
            };
        },
    },
    {
        label: '县',
        field: 'countryCode',
        component: 'ApiSelect',
        ifShow: () => {
            return (userStore?.userInfo?.userIdentity == '2' || userStore?.userInfo?.userIdentity == '1') ? true : false
        },
        componentProps: ({ formModel, formActionType }) => {
            if (userStore?.userInfo?.userIdentity == '2') {
                const { updateSchema } = formActionType;
                updateSchema({
                    label: '区县',
                    field: 'countryCode',
                    ifShow: true,
                    componentProps: {
                        api: getTown,
                        params: userStore?.userInfo?.codeRegion,
                        labelField: 'name',
                        valueField: 'id',
                    },
                });
            }
            return {
                labelField: 'name',
                valueField: 'id',
            }
        },
    },
    {
        label: '行业类别',
        field: 'hydl',
        component: 'TreeSelect',
        slot: 'category',
    },

    {
        label: '地块类型',
        field: 'dklx',
        component: 'ApiSelect',
        componentProps: {
            api: dict,
            params: { key: 'dk_dklx' },
            labelField: 'text',
            valueField: 'value',
        }
    },
    {
        label: '是否移出',
        field: 'sfyc',
        component: 'ApiSelect',
        componentProps: {
            api: dict,
            params: { key: 'dk-sfyc' },
            labelField: 'text',
            valueField: 'value',
        }
    },
    {
        label: '同步国家库',
        field: 'tbgjk',
        component: 'Select',
        componentProps: {
            options: [
                { value: '1', label: '已同步' },
                { value: '0', label: '未同步' },
            ]
        }
    },
    {
        label: '政策依据',
        field: 'faliYiju',
        component: 'JSearchSelect',
        componentProps: {
            dict: 'dk_zcyj',
        },
    },
    {
        label: '地块名称或编码',
        field: 'wrdkbm',
        component: 'Input',
        componentProps: {
            placeholder: '地块名称或编码'
        }
    },
    {
        label: '上报日期',
        field: 'data',
        component: 'RangeDate',
        componentProps: {
            format: 'YYYY-MM-DD',
            //范围文本描述用集合
            placeholder: ['请选择开始日期', '请选择结束日期']
        },
    },
]
export const searchFormSchemaNo: FormSchema[] = [
    {
        label: '市',
        field: 'cityCode',
        component: 'ApiSelect',
        ifShow: () => {
            return userStore?.userInfo?.userIdentity == '1' ? true : false
        },
        componentProps: ({ formModel, formActionType }) => {
            return {
                api: getTown,
                params: 440000, // 行政区划编码
                labelField: 'name',
                valueField: 'id',
                onChange: (val: any) => {
                    const { updateSchema } = formActionType;
                    formModel.countryCode = undefined;
                    if (val) {
                        updateSchema({
                            field: 'countryCode',
                            componentProps: {
                                api: getTown,
                                params: val,
                                labelField: 'name',
                                valueField: 'id',
                            },
                        });
                    } else {
                        updateSchema({
                            field: 'countryCode',
                            componentProps: {
                                api: undefined,
                                labelField: 'name',
                                valueField: 'id',
                            },
                        });
                    }
                },
            };
        },
    },
    {
        label: '县',
        field: 'countryCode',
        component: 'ApiSelect',
        ifShow: () => {
            return (userStore?.userInfo?.userIdentity == '2' || userStore?.userInfo?.userIdentity == '1') ? true : false
        },
        componentProps: ({ formModel, formActionType }) => {
            if (userStore?.userInfo?.userIdentity == '2') {
                const { updateSchema } = formActionType;
                updateSchema({
                    label: '区县',
                    field: 'countryCode',
                    ifShow: true,
                    componentProps: {
                        api: getTown,
                        params: userStore?.userInfo?.codeRegion,
                        labelField: 'name',
                        valueField: 'id',
                    },
                });
            }
            return {
                labelField: 'name',
                valueField: 'id',
            }
        },
    },
    {
        label: '行业类别',
        field: 'hydl',
        component: 'TreeSelect',
        slot: 'category',
    },

    {
        label: '地块类型',
        field: 'cdlx',
        component: 'ApiSelect',
        componentProps: {
            api: dict,
            params: { key: 'dk_dklx' },
            labelField: 'text',
            valueField: 'value',
        }
    },
    {
        label: '政策依据',
        field: 'faliYiju',
        component: 'JSearchSelect',
        componentProps: {
            dict: 'dk_zcyj',
        },
    },
    {
        label: '地块名称或编码',
        field: 'wrdkbm',
        component: 'Input',
        componentProps: {
            placeholder: '地块名称或编码'
        }
    }
]

export const searchFormSchemagjk: FormSchema[] = [
    {
        label: '地块编码',
        field: 'wrdkbm',
        component: 'Input'
    },
]

// 搜索条件
export const columnsgjk: BasicColumn[] = [
    {
        title: '推送目录',
        dataIndex: 'title'
    },
    {
        title: '推送参数',
        dataIndex: 'requestParam'
    },


    {
        title: '结果',
        dataIndex: 'rest'
    },
    {
        title: '错误信息',
        dataIndex: 'error'
    },
    {
        title: '时间',
        dataIndex: 'tsamp'
    },

]
export const columnsgjkfj: BasicColumn[] = [
    {
        title: '附件地址',
        dataIndex: 'url'
    },


    {
        title: '结果',
        dataIndex: 'rest'
    },

    {
        title: '时间',
        dataIndex: 'updateTsamp'
    }

]


export const formSchema: FormSchema[] = [
    // TODO 主键隐藏字段，目前写死为ID
    { label: '', field: 'id', component: 'Input', show: false },
    {
        label: '推送目录',
        field: 'title',
        component: 'Input',
        componentProps: {
            disabled: false
        },
    },
    {
        label: '推送参数',
        field: 'requestParam',
        component: 'Input',
        componentProps: {
            disabled: false
        },
    },


    {
        label: '结果',
        field: 'rest',
        component: 'Input',
        componentProps: {
            disabled: false
        },
    },
    {
        label: '错误信息',
        field: 'error',
        component: 'Input',
        componentProps: {
            disabled: false
        },
    },
    {
        label: '时间',
        field: 'tsamp',
        component: 'Input',
        componentProps: {
            disabled: false
        },
    },
    // {
    //     label: '污染地块编码',
    //     field: 'wrdkbm',
    //     component: 'Input',
    //     componentProps: {
    //         disabled: true
    //     },
    // },
    // {
    //     label: '污染地块编码',
    //     field: 'wrdkbm',
    //     component: 'Input',
    //     componentProps: {
    //         disabled: true
    //     },
    // },
    // {
    //     label: '推送内容',
    //     field: 'node',
    //     component: 'Input',
    //     componentProps: {
    //         disabled: true
    //     },
    // },
    // {
    //     label: '推送地址',
    //     field: 'url',
    //     component: 'Input',
    //     componentProps: {
    //         disabled: true
    //     },
    // },
    // {
    //     label: '推送参数',
    //     field: 'param',
    //     component: 'Input',
    //     componentProps: {
    //         disabled: true
    //     },
    // },
    // {
    //     label: '推送结果',
    //     field: 'rest',
    //     component: 'Input',
    //     componentProps: {
    //         disabled: true
    //     },
    // },
    // {
    //     label: '状态',
    //     field: 'status',
    //     component: 'Select',
    //     componentProps: () => {
    //         return {
    //             options: [
    //                 {
    //                     label: '成功',
    //                     value: 1,
    //                 },
    //                 {
    //                     label: '失败',
    //                     value: 0,
    //                 },
    //             ],
    //             disabled: true
    //         };
    //     },
    // },
    // {
    //     label: '推送时间',
    //     field: 'tsamp',
    //     component: 'DatePicker',
    //     componentProps: {
    //         showTime: true,
    //         valueFormat: 'YYYY-MM-DD hh:mm:ss',
    //         disabled: true
    //     },
    // },
];
