import { BasicColumn, FormSchema } from '/@/components/Table';
import { getTown } from '/@/api/common/api';
import { useUserStore } from '/@/store/modules/user';
import { useMessage } from '/@/hooks/web/useMessage';
const { createMessage } = useMessage();

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',
    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 addSearchFormSchema: 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',
  }
];
export const ZgshformSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  { label: '', field: 'code', component: 'Input', show: false },
  { label: '', field: 'jd', component: 'Input', show: false },
  {
    label: '审核',
    field: 'audits',
    defaultValue: '下达意见',
    component: 'RadioGroup',
    required: true,
    componentProps: () => {
      return {
        options: [  
          { label: '下达意见', value: '下达意见' }
        ]
      }
    },
  },
  {
    label: '意见',
    field: 'remark',
    component: 'InputTextArea',
    required: ({ values }) => {
      return values.audits=='下达意见'?true:false;
    },
    componentProps: {
      //可以点击清除图标删除内容
      allowClear: true,
      //是否展示字数
      // showCount: true,
      //自适应内容高度，可设置为 true | false 或对象：{ minRows: 2, maxRows: 6 }
      autoSize: {
        //最小显示行数
        minRows: 4,
      },
    },
  },
  {
    label: '文件',
    field: 'wjlj',
    component: 'JUpload',
    componentProps: {
      showUploadList: false,
      fileName: '市级自然资源部门审核附件',
      accept: '.pdf,.zip',
      beforeUpload: (file) => {
        let isshow = false;
        const isFile = file.name.split('.')[file.name.split('.').length - 1];
        console.log(file, 'file');
        if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF') {
          isshow = true;
        } else {
          createMessage.error('只能上传.pdf/.zip格式的文件');
          isshow = false;
        }
        return isshow;
      },
    },
  },
];

export const StUpformSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  { label: '', field: 'code', component: 'Input', show: false },
  { label: '', field: 'jd', component: 'Input', show: false },
  { label: '', field: 'audits', component: 'Input', show: false, defaultValue: '提交' },
  {
    label: '说明',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      //可以点击清除图标删除内容
      allowClear: true,
      //是否展示字数
      // showCount: true,
      //自适应内容高度，可设置为 true | false 或对象：{ minRows: 2, maxRows: 6 }
      autoSize: {
        //最小显示行数
        minRows: 4,
      },
    },
  },
  {
    label: '文件',
    field: 'wjlj',
    component: 'JUpload',
    componentProps: {
      showUploadList: false,
      fileName: '市级生态部门提交附件',
      accept: '.pdf,.zip',
      beforeUpload: (file) => {
        let isshow = false;
        const isFile = file.name.split('.')[file.name.split('.').length - 1];
        console.log(file, 'file');
        if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF') {
          isshow = true;
        } else {
          createMessage.error('只能上传.pdf/.zip格式的文件');
          isshow = false;
        }
        return isshow;
      },
    },
  },
];

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',
  },
];
