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

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);
    // allCategory.forEach((item: any) => {
    //   item.disabled = true; 
    // });
    // 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: 'jdnzt',
  //   resizable: true,
  // },
  {
    title: '政策依据',
    dataIndex: 'faliYiju',
    resizable: true,
  },
  {
    title: '地块类型',
    dataIndex: 'dklx',
    resizable: true,
  },
  {
    title: '更新时间',
    dataIndex: 'tsamp',
    resizable: true,
  },
  {
    title: '登录帐号',
    dataIndex: 'userId',
    resizable: true,
  },
];
// T1：填报中 DSL：待受理 T2：待评审 SH：评审通过 SHBTG：评审不通过 SHHDWS：评审后待完善 BPS：不评审 DKDTC：地块待退出 DKYTC：地块已退出
export const searchFormSchema: FormSchema[] = [
  {
    label: '行政区(市)',
    field: 'cityCode',
    ifShow: () => {
      return userStore?.userInfo?.userIdentity == '1' ? true : false
    },
    component: 'ApiSelect',
    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',
              ifShow: true,
              componentProps: {
                api: getTown,
                params: val,
                labelField: 'name',
                valueField: 'id',
              },
            });
          } else {
            updateSchema({
              field: 'countryCode',
              ifShow: true,
              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: 'isSuspect',
    component: 'ApiSelect',
    slot: 'suspect',
  },
  {
    label: '行业类别',
    field: 'hylb',
    component: 'TreeSelect',
    slot: 'category',
  },
  {
    label: '地块类型',
    field: 'dklx',
    component: 'JSearchSelect',
    componentProps: {
      dict: 'dk_dklx',
    },
  },
  // {
  //   label: '地块名称',
  //   field: 'wrdkmc',
  //   component: 'Input',
  // },
  {
    label: '地块名称或者编码',
    field: 'wrdkbm',
    component: 'Input',
  },
  {
    label: '登录帐号',
    field: 'userId',
    component: 'Input',

  },

];
// 评审搜索条件
export const searchFormPsSchema: FormSchema[] = [
  {
    label: '行政区(市)',
    field: 'cityCode',
    ifShow: () => {
      return userStore?.userInfo?.userIdentity == '1' ? true : false
    },
    component: 'ApiSelect',
    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',
              ifShow: true,
              componentProps: {
                api: getTown,
                params: val,
                labelField: 'name',
                valueField: 'id',
              },
            });
          } else {
            updateSchema({
              field: 'countryCode',
              ifShow: true,
              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: 'isSuspect',
    component: 'ApiSelect',
    slot: 'suspect',
  },
  {
    label: '行业类别',
    field: 'hylb',
    component: 'TreeSelect',
    slot: 'category',
  },
  {
    label: '地块类型',
    field: 'dklx',
    component: 'JSearchSelect',
    componentProps: {
      dict: 'dk_dklx',
    },
  },
  // {
  //   label: '地块名称',
  //   field: 'wrdkmc',
  //   component: 'Input',
  // },
  {
    label: '地块名称或者编码',
    field: 'wrdkbm',
    component: 'Input',
  },
  {
    label: '地块锁定状态',
    field: 'dksdzt',
    component: 'Select',
    componentProps: {
      options: [
        { value: '1', label: '锁定中' },
      ],
    }
  },
  {
    label: '登录帐号',
    field: 'userId',
    component: 'Input',

  },

];

// 待评审-评审材料上传-专家组信息
export const opinionColumns: BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'zjxm',
    align: 'center',
    resizable: true,
  },
  {
    title: '工作单位',
    dataIndex: 'gzdw',
    align: 'center',
    resizable: true,
  },
  {
    title: '职务',
    dataIndex: 'zczw',
    align: 'center',
    resizable: true,
  },
  {
    title: '联系电话',
    dataIndex: 'lxdh',
    align: 'center',
    resizable: true,
  },
  {
    title: '邮箱',
    dataIndex: 'mail',
    align: 'center',
    resizable: true,
  },
  {
    title: '是否组长',
    dataIndex: 'isHead',
    align: 'center',
    width: 120,
    resizable: true,
  },
  {
    title: '备注',
    dataIndex: 'bz',
    align: 'center',
    resizable: true,
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    fixed: 'right',
    width: 100,
    slots: { customRender: 'action' },
  },
];
// 待评审-评审材料上传-参会人员信息
export const participantsColumns: BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'zjxm',
    align: 'center',
    resizable: true,
  },
  {
    title: '工作单位',
    dataIndex: 'gzdw',
    align: 'center',
    resizable: true,
  },
  {
    title: '职务',
    dataIndex: 'zczw',
    align: 'center',
    resizable: true,
  },
  {
    title: '联系电话',
    dataIndex: 'lxdh',
    align: 'center',
    resizable: true,
  },
  {
    title: '邮箱',
    dataIndex: 'mail',
    align: 'center',
    resizable: true,
  },
  {
    title: '备注',
    dataIndex: 'bz',
    align: 'center',
    resizable: true,
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    fixed: 'right',
    width: 100,
    slots: { customRender: 'action' },
  },
];
// 退回
export const BackFormSchema: FormSchema[] = [
  {
    label: '污染地块id',
    field: 'wrdkid',
    component: 'ApiSelect',
    show: false,
  },
  {
    label: '阶段编码',
    field: 'scjdbm',
    component: 'ApiSelect',
    show: false,
  },
  {
    label: '填报阶段编码',
    field: 'tbjdbm',
    component: 'ApiSelect',
    show: false,
  },

  {
    label: '退回意见',
    field: 'clyj',
    component: 'Input',
    required: true,
  },
  {
    label: '文件',
    field: 'clyjfj',
    component: 'JUpload',
    required: true,
    componentProps: {
      showUploadList: false,
      fileName: '退回文件',
    },
  },
];
