import { BasicColumn, FormSchema } from '/@/components/Table';
import { getTown } from '/@/api/common/api';
import { useUserStore } from '/@/store/modules/user';

const userStore: any = useUserStore();

export const columns: BasicColumn[] = [
  {
    title: '标识代码',
    dataIndex: 'bsm',
    resizable: true,
  },
  {
    title: '要素代码',
    dataIndex: 'ysdm',
    resizable: true,
  },
  {
    title: '行政区代码',
    dataIndex: 'xzqdm',
    resizable: true,
  },
  {
    title: '行政区名称',
    dataIndex: 'xzqmc',
    resizable: true,
  },
  {
    title: '地块名称',
    dataIndex: 'dkmc',
    resizable: true,
  },
  {
    title: '地块编码',
    dataIndex: 'dkdm',
    resizable: true,
  },
  {
    title: '地块面积',
    dataIndex: 'ydmj',
    resizable: true,
  },
  {
    title: '地块土壤污染风险管控和修复阶段',
    dataIndex: 'dktrwrfxgkhxfjd',
    resizable: true,
  },
  {
    title: '土壤污染风险管控和修复阶段完成时间',
    dataIndex: 'trwrfxgkhxfjdwcsj',
    resizable: true,
    customRender: ({ text }) => {
      if (text && text.length === 8) {
        return text.substring(0, 4) + '-' + text.substring(4, 6) + '-' + text.substring(6, 8);
      } else {
        return text;
      }
    },
  },
  {
    title: '土壤环境质量',
    dataIndex: 'trhjzl',
    resizable: true,
  },
  {
    title: '地下水质量',
    dataIndex: 'dxszl',
    resizable: true,
  },
  {
    title: '土壤污染风险评估情况',
    dataIndex: 'trwrfxpgqk',
    resizable: true,
  },
  {
    title: '土壤污染风险管控和修复名录管理情况',
    dataIndex: 'trwrfxgkhxfmlglqk',
    resizable: true,
  },
  {
    title: '后期管理要求',
    dataIndex: 'hqglyq',
    resizable: true,
  },
  {
    title: '地块数据最新更新时间',
    dataIndex: 'dksjzxgxsj',
    resizable: true,
    customRender: ({ text }) => {
      if (text) {
        return text.substring(0, 4) + '-' + text.substring(4, 6) + '-' + text.substring(6, 8);
      } else {
        return '';
      }
    },
  },
  {
    title: '备注',
    dataIndex: 'bz',
    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.xzqdm = undefined;
            if (val) {
              updateSchema({
                field: 'xzqdm',
                componentProps: {
                  api: getTown,
                  params: val,
                  labelField: 'name',
                  valueField: 'id',
                },
              });
            } else {
              updateSchema({
                field: 'xzqdm',
                componentProps: {
                  api: undefined,
                  labelField: 'name',
                  valueField: 'id',
                },
              });
            }
          },
        };
      },
    },
    {
      label: '区县',
      field: 'xzqdm',
      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: 'xzqdm',
            ifShow: true,
            componentProps: {
              api: getTown,
              params: userStore?.userInfo?.codeRegion,
              labelField: 'name',
              valueField: 'id',
            },
          });
        }
        return {
          labelField: 'name',
          valueField: 'id',
        }
      },
    },
  {
    label: '地块名称或编码',
    field: 'dkdm',
    component: 'Input',
  },
  {
    label: '地块土壤污染风险管控和修复阶段',
    field: 'dktrwrfxgkhxfjd',
    component: 'Select',
    // colProps: { span: 12 },
    labelWidth: '240px',
    componentProps: {
      options: [
        { label: '未调查', value: '01 未调查' },
        { label: '调查', value: '02 调查' },
        { label: '风险评估', value: '03 风险评估' },
        { label: '风险管控', value: '04 风险管控' },
        { label: '修复', value: '05 修复' },
        { label: '管控及修复', value: '06 管控及修复' },
        { label: '风险管控效果评估', value: '07 风险管控效果评估' },
        { label: '修复效果评估', value: '08 修复效果评估' },
        { label: '管控及修复效果评估', value: '09 管控及修复效果评估' },
      ],
    }
  },
];

export const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  {label: '', field: 'id', component: 'Input', show: false},
  {
    label: '市编码',
    field: 'code',
    component: 'Input',
  },
  {
    label: '接收人',
    field: 'jsr',
    component: 'Input',
  },
  {
    label: '文件',
    field: 'wjlj',
    component: 'Input',
  },
  {
    label: '说明',
    field: 'remark',
    component: 'Input',
  },
  {
    label: '审核',
    field: 'audits',
    component: 'Input',
  },
  {
    label: '操作人名称',
    field: 'createName',
    component: 'Input',
  },
]