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); // 只显示前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: 'wrdkbm',
    resizable: true,
    width: '150px',
    slots: { customRender: 'wrdkbm' },
  },
  {
    title: '地块名称',
    dataIndex: 'wrdkmc',
    resizable: true,
  },
  {
    title: '开发利用现状',
    dataIndex: 'kflyxzmc',
    resizable: true,
    customRender: ({ text, record, index, column }) => {
      console.log(text, record, index, column);
      const res: any = record
      if (!res?.kfxzmsmc) {
        return res?.kflyxzmc
      } else {
        return res?.kflyxzmc + '-' + res?.kfxzmsmc
      }
    },
  },
  {
    title: '风险管控与修复阶段',
    resizable: true,
    children: [
      {
        title: '污染状况调查',


        children: [
          {
            title: '工作',
            resizable: true,
            dataIndex: 'dkdcgz',

          },
          {
            title: '结论',
            resizable: true,
            dataIndex: 'dkdcjl',


          },
          {
            title: '报告',
            resizable: true,
            dataIndex: 'dkdcbg',


          },
        ]
      },
      {
        title: '风险评估',
        resizable: true,

        children: [
          {
            title: '工作',
            resizable: true,
            dataIndex: 'fxpggz',

          },
          {
            title: '结论',
            resizable: true,
            dataIndex: 'fxpgjl',


          },
          {
            title: '报告',
            resizable: true,
            dataIndex: 'fxpgbg',

          },
        ]
      },
      {
        title: '风险管控与修复',
        resizable: true,

        children: [
          {
            title: '工作',
            resizable: true,
            dataIndex: 'gkxfgz',

          },
          {
            title: '类型',
            resizable: true,
            dataIndex: 'gkxflx',

          },
          {
            title: '报告',
            resizable: true,
            dataIndex: 'gkxfbg',

          },
        ]
      },
      {
        title: '效果评估',
        resizable: true,
        children: [
          {
            title: '工作',
            resizable: true,
            dataIndex: 'xgpggz',

          },
          {
            title: '报告',
            resizable: true,
            dataIndex: 'xgpgbg',

          },
        ]
      },
    ],
  },
  {
    title: '状态',
    dataIndex: 'hszt',
    resizable: true,
  },
  {
    title: '核查时间',
    dataIndex: 'hssj',
    resizable: true,
  },
];

// T1：填报中 DSL：待受理 T2：待评审 SH：评审通过 SHBTG：评审不通过 SHHDWS：评审后待完善 BPS：不评审 DKDTC：地块待退出 DKYTC：地块已退出
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: 'dklx',
    component: 'JSearchSelect',
    componentProps: {
      dict: 'dk_dklx',
    },
  },
  {
    label: '地块名称或者编码',
    field: 'wrdkbm',
    component: 'Input',
  },
  {
    label: '核实情况',
    field: 'hsstatus',
    component: 'Select',
    componentProps: {
      options: [
        { value: '1', label: '已核实' },
        { value: '0', label: '未核实' },
      ],
    },
  },
];

// 方案列表
export const Fxcolumns: BasicColumn[] = [
  {
    title: '风险管控方案标题',
    dataIndex: 'bt',
    resizable: true,
  },
  {
    title: '编制单位',
    dataIndex: 'bzdw',
    resizable: true,
  },
  {
    title: '填报状态',
    dataIndex: 'tbzt',
    resizable: true,
  },
  {
    title: '更新日期',
    dataIndex: 'tsamp',
    resizable: true,
  },

];
