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: '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: '地块名称或地块编码'
        }
    }
]
