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

const userStore: any = useUserStore();
// 提前获取政策依据二级
const zc = ref<any>([])
async function getZCer() {
  zc.value = await dict({ key: 'dk_zcyjej' })
}

getZCer()
export const columns: BasicColumn[] = [
  {
    title: '市',
    dataIndex: 'cityName',
    resizable: true,
    key: 'cityName'
  },
  {
    title: '县',
    dataIndex: 'countryName',
    resizable: true,
    key: 'countryName'
  },
  {
    title: '地块名称',
    dataIndex: 'wrdkmc',
    resizable: true,
  },
  {
    title: '地块编码',
    dataIndex: 'wrdkbm',
    resizable: true,
  },
  {
    title: '行业类别',
    dataIndex: 'hylb',
    resizable: true,
    key: 'hylb'
  },
  {
    title: '政策依据',
    dataIndex: 'faliYiju',
    resizable: true,
    key: 'faliYiju'
  },
  {
    title: '政策依据条款',
    dataIndex: 'faliErji',
    resizable: true,
    key: 'faliErji'
  },

  {
    title: '地块类型',
    dataIndex: 'dklx',
    resizable: true,
    key: 'dklx',
  },

  {
    title: '所处阶段',
    dataIndex: 'mlbs',
    resizable: true,
    customRender: ({ record }: any) => {
      let name = ''
      for (let i = 0; i < jd.value.length; i++) {
        if (jd.value[i].value == record.mlbs) {
          name = jd.value[i].text
          break
        }
      }
      return name
    },
  },
  // {
  //   title: '阶段内状态',
  //   dataIndex: 'jdnzt',

  // },
  // {
  //   title: '是否依据土壤法纳入建设用地土壤污染风险管控与修复名录',
  //   dataIndex: 'sfyjtrfnrjsydtrwrfxgkyxfml',
  //   key: 'sfyjtrfnrjsydtrwrfxgkyxfml'
  // },

  {
    title: '登录账号',
    dataIndex: 'userId',
    resizable: true,
    key: 'userId'
  },
  {
    title: '创建地块时间',
    dataIndex: 'createTsamp',
    resizable: true,
    key: 'createTsamp'
  },
];

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: 'faliYiju',
    component: 'ApiSelect',
    componentProps: {
      api: dict,
      params: { key: 'dk_zcyj' },
      labelField: 'text',
      valueField: 'value',
    },
  },
  {
    label: '政策依据二级',
    field: 'faliYijuerji',
    component: 'ApiSelect',
    show: ({ values }) => {
      return values?.faliYiju == 1 ? true : false
    },
    componentProps: {
      api: dict,
      params: { key: 'dk_zcyjej' },
      labelField: 'text',
      valueField: 'value'
    },
  },
  {
    label: '地块类型',
    field: 'dklx',
    component: 'ApiSelect',
    componentProps: {
      api: dict,
      params: { key: 'dk_dklx' },
      labelField: 'text',
      valueField: 'value'
    },
  },
  {
    label: '行业分类',
    field: 'isSuspect',
    component: 'ApiSelect',
    slot: 'suspect',
  },
  {
    label: '行业类别',
    field: 'hylb',
    component: 'TreeSelect',
    slot: 'category',
  },
  {
    label: '所处阶段',
    field: 'mlbs',
    component: 'ApiSelect',
    componentProps: {
      api: dict,
      params: { key: 'dk_scqd' },
      labelField: 'text',
      valueField: 'value'
    },
  },
  {
    label: '地块名称或编码',
    field: 'wrdkbm',
    component: 'Input',
  },
];

export const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'wrdkid', component: 'Input', show: false },
  { label: '', field: 'scjdbm', component: 'Input', show: false },
  {
    label: '行政区(省)',
    field: 'provinceCode',
    component: 'Select',
    defaultValue: '440000',
    show: false
  },
  {
    label: '行政区',
    field: 'cityCode',
    component: 'ApiSelect',
    required: true,
    ifShow: ({ }) => {
      // console.log('行政区show', userStore?.userInfo?.userIdentity)
      return userStore?.userInfo?.userIdentity == '1' ? true : false
    },
    colProps: { span: 12 },
    // slot: 'form-city',
    componentProps: ({ formModel, formActionType }) => {
      // handeCount(formActionType, formModel)
      // console.log('行政区componentProps')
      return {
        api: getTown,
        params: 440000, // 行政区划编码
        labelField: 'name',
        valueField: 'id',
        onChange: (val: any) => {
          const { updateSchema } = formActionType
          console.log('行政区onChange')
          if (val) {
            formModel.countryCode = undefined;
            updateSchema({
              field: 'countryCode',
              show: true,
              componentProps: {
                api: getTown,
                params: val,
                labelField: 'name',
                valueField: 'id',
              },
            });
          } else {
            updateSchema({
              show: true,
              field: 'countryCode',
              componentProps: {
                api: undefined,
              },
            });
          }
        },
      };
    },

  },
  {
    label: '区县',
    field: 'countryCode',
    colProps: { span: 12 },
    component: 'ApiSelect',

    show: () => {
      // console.log('区县show')
      return (userStore?.userInfo?.userIdentity == '2' || userStore?.userInfo?.userIdentity == '1') ? true : false
    },
    componentProps: ({ formModel, formActionType }) => {
      console.log('区县componentProps', formModel.cityCode)
      handeCount(formActionType, formModel)
      return {
        api: getTown,
        params: formModel.cityCode, // 行政区划编码
        labelField: 'name',
        valueField: 'id',
      }
    },

  },
  {
    label: '地块名称',
    field: 'wrdkmc',
    component: 'Input',
    colProps: { span: 24 },
    required: true,
    componentProps: {
      placeholder: '建议填写规划的地块名称。没有规划的，建议填写原企业名称',
    }

  },
  {
    label: '地块类型',
    field: 'dklx',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: dict,
      params: { key: 'dk_dklx' },
      labelField: 'text',
      valueField: 'value'
    },
  },
  {
    label: '政策依据',
    field: 'faliYiju',
    component: 'ApiSelect',

    colProps: { span: 24 },
    required: true,
    componentProps: ({ formModel, formActionType }) => {
      // if (formModel.faliYiju) {
      handeFlyj(formModel.faliYiju, formActionType)
      // }

      return {
        api: dict,
        params: { key: 'dk_zcyj' },
        labelField: 'text',
        valueField: 'value',
        onChange: (val: any) => {
          if (val) {
            // 这里不能给值 如果给值任何时候都会执行影响编辑的回显
            // formModel.faliErji = null;
            // formModel.faliErji1 = null;
            handeFlyj(val, formActionType)
          }

        },
      };
    },
  },
  {
    label: '政策依据二级',
    labelWidth: 220,
    field: 'faliErji',
    component: 'CheckboxGroup',
    colProps: { span: 24 },
    show: ({ values }) => {

      return values.faliYiju == 1 ? true : false
    },
    componentProps: {
      options: zc.value,
      labelField: 'text',
      valueField: 'value',
    },

  },
  {
    label: '政策依据二级',
    field: 'faliErji1',
    component: 'Input',
    labelWidth: 220,
    colProps: { span: 24 },
    // show: ({ values }) => { return values.faliErji == 3 ? true : false }
  },
  {
    label: '行业分类',
    field: 'isSuspect',
    required: true,
    component: 'ApiSelect',
    colProps: { span: 7 },
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: dict,
        params: { key: 'dk_hyfl' },
        labelField: 'text',
        valueField: 'value',
        onChange: (val: any) => {
          formModel.hyml = null;
          formModel.hydl = null;
          formModel.hyzl = null;
          formModel.hyxl = null;
        },
      };
    },
  },
  {
    label: '',
    field: 'hyml',
    labelWidth: 5,
    colProps: { span: 4 },
    required: true,
    component: 'Select',
    ifShow: true,
    // slot: 'form-hyfl'
    componentProps: ({ formModel, formActionType }) => {

      if (formModel.isSuspect == 1) {
        treeM.value = allCategory.value.filter(item => item['is42'])
      } else if (formModel.isSuspect == 2) {
        treeM.value = allCategory.value
      }
      else
        treeM.value = []
      return {
        options: treeM?.value,
        onChange: () => {
          formModel.hydl = null;
          formModel.hyzl = null;
          formModel.hyxl = null;
        }
      }
    }
  },
  {
    label: '',
    field: 'hydl',
    labelWidth: 5,
    colProps: { span: 4 },
    required: true,
    component: 'Select',
    ifShow: true,
    componentProps: ({ formModel, formActionType }) => {
      console.log(formModel.hyml, 'formModel.hyml')
      treeD.value = treeM.value.filter((item: any) => item.value === formModel.hyml)
      return {
        options: treeD?.value[0]?.children || [],
        onChange: (valD: any) => {
          formModel.hyzl = null;
          formModel.hyxl = null;
          if (valD != 99) {
            formModel.hydlQt = null;
          }
        }
      }

    }
    // slot: 'form-hydl'
  },
  {
    label: '',
    field: 'hyzl',
    labelWidth: 5,
    colProps: { span: 5 },
    component: 'Select',
    ifShow: ({ values }) => values.hydl != 99,
    componentProps: ({ formModel, formActionType }) => {
      if (treeD?.value.length > 0) {
        treeZ.value = treeD?.value[0].children.filter((item: any) => item.value === formModel.hydl)
        return {
          options: treeZ?.value[0]?.children || [],
          onChange: () => {
            formModel.hyxl = null;
          }
        }
      }


    }
    // slot: 'form-hyzl'
  },
  {
    label: '',
    field: 'hyxl',
    labelWidth: 5,
    colProps: { span: 4 },
    component: 'Select',
    ifShow: ({ values }) => values.hydl != 99,
    componentProps: ({ formModel, formActionType }) => {
      if (treeZ?.value.length > 0) {
        treeX.value = treeZ?.value[0]?.children.filter((item: any) => item.value === formModel.hyzl)
        return {
          options: treeX?.value[0]?.children || []
        }
      }
    }
    // slot: 'form-hyxl'
  },
  {
    label: '',
    field: 'hydlQt',
    ifShow: ({ values }) => values.hydl == 99,
    labelWidth: 5,
    colProps: { span: 4 },
    component: 'Input',
  },

  {
    label: '涉及的专项工作',
    field: 'gzlx',
    component: 'Select',
    required: true,
    colProps: { span: 12 },
    componentProps: ({ formModel, formActionType }) => {
      return {
        options: [
          {
            label: '危险化学品搬迁',
            value: '1'
          },
          {
            label: '长江经济带整治',
            value: '2'
          },
          {
            label: '黄河流域整治',
            value: '3'
          },
          {
            label: '其他',
            value: '4'
          },
        ],
        onChange: (val: any) => {
          formModel.gzlxQt = ''
        }
      }

    },
  },
  {
    label: '',
    field: 'gzlxQt',
    colProps: { span: 6 },
    component: 'Input',
    componentProps: ({ formModel }) => {
      return {
        placeholder: '请输入其它内容',
        disabled: formModel.gzlx != 4,
      };
    },
  },
  {
    label: '涉及的重点区域',
    field: 'gzfw',
    component: 'Select',
    required: true,
    colProps: { span: 12 },
    componentProps: ({ formModel, formActionType }) => {
      return {
        options: [
          {
            label: '京津冀',
            value: '5'
          },
          {
            label: '长江经济带',
            value: '1'
          },
          {
            label: '黄河流域',
            value: '2'
          },
          {
            label: '粤港澳大湾区',
            value: '3'
          },
          {
            label: '其他',
            value: '4'
          },
        ],
        onChange: (val: any) => {
          formModel.gzfwQt = ''
        }
      }

    },
  },
  {
    label: '',
    field: 'gzfwQt',
    colProps: { span: 6 },
    component: 'Input',
    componentProps: ({ formModel }) => {
      return {
        placeholder: '请输入其它内容',
        disabled: formModel.gzfw != 4,
      };
    },
  },
  {
    label: '坐标系',
    field: 'zbsm',
    defaultValue: 'CGCS2000',
    component: 'CheckboxGroup',
    show: false,
  },
  {
    label: '中心经度',
    field: 'wrdkzxjd',
    component: 'Input',
    componentProps: {
      placeholder: '十进制，保留六位小数',
      tipArr: ['(注: 请填写CGCS2000坐标系下的经纬度坐标值 (形如: 120.12345，38.12345)）'],
    },
    required: true,
    dynamicRules: () => {
      return [
        { pattern: /^-?(180(\.\d{1,6})|((1[0-7]|[1-9])?\d(\.\d{1,6})))$/, message: '中心经度格式有误', required: true, },
      ];
    },
  },
  {
    label: '中心纬度',
    field: 'wrdkzxwd',
    component: 'Input',
    componentProps: {
      placeholder: '十进制，保留六位小数',
    },
    required: true,
    dynamicRules: () => {
      return [
        { pattern: /^-?(180(\.\d{1,6})|((1[0-7]|[1-9])?\d(\.\d{1,6})))$/, message: '中心纬度格式有误', required: true, },
      ];
    },
  },
  {
    label: 'isnydStatus',
    field: 'isnydStatus',
    component: 'Select',
    show: false
  },
  {
    label: '是否为农用地转”一住两公”地块',
    field: 'isnyd2yzlg',
    component: 'RadioGroup',
    defaultValue: '0',
    show: ({ values }) => {
      let flg = false
      if (values?.isnydStatus) {
        flg = true
      } else if (values?.isnydStatus === false) {
        flg = false
      } else if (values?.isnydStatus == null || values?.isnydStatus == '' || values?.isnydStatus == undefined) {
        flg = true
      }
      // return flg
      return false
    },
    componentProps: ({ formModel, formActionType }) => {
      return {
        options: [
          { label: '是', value: "1" },
          { label: '否', value: "0" },
        ],
        onChange: (val: any) => {
          console.log(val.target.value, 'zoulm')
          const { updateSchema } = formActionType;
          if (val.target.value == 1) {
            formModel.iswlyd2yzlg = '0'
            updateSchema({
              field: 'iswlyd2yzlg',
              componentProps: {
                disabled: true
              }
            })
          } else {
            updateSchema({
              field: 'iswlyd2yzlg',
              componentProps: {
                disabled: false
              }
            })
          }
        }
      }

    },
  },
  {
    label: '是否为未利用地转“一住两公”地块',
    field: 'iswlyd2yzlg',
    component: 'RadioGroup',
    defaultValue: '0',
    show: ({ values }) => {
      let flg = false
      if (values?.isnydStatus === true) {
        flg = true
      } else if (values?.isnydStatus === false) {

        flg = false
      } else if (values?.isnydStatus === null || values?.isnydStatus === '' || values?.isnydStatus === undefined) {
        flg = true
      }

      // return flg
      return false
    },
    componentProps: {
      options: [
        { label: '是', value: "1" },
        { label: '否', value: "0" },
      ],
    },
  },
  {
    label: '备注',
    field: 'bz',
    component: 'InputTextArea',
    colProps: { span: 24 },
  },
];

export const delFormSchema: FormSchema[] = [

  { label: '处理意见', field: 'clyj', required: true, component: 'Input' },
  {
    label: '处理意见附件路径',
    field: 'clyjfj',
    required: true,
    component: 'JUpload',
    componentProps: {
      showUploadList: false,
    },
  },

  { label: '污染地块ID', field: 'wrdkid', required: true, component: 'Input', show: false },
  {
    label: '意见类型 ',
    field: 'yjlx',
    component: 'Input',
    defaultValue: '0',
    show: false
  },
]

export const DkFormSchema: FormSchema[] = [
  { label: '污染地块ID', field: 'wrdkid', required: true, component: 'Input', show: false },
  { label: '污染地块名称', field: 'wrdkmc', required: true, component: 'Input' },
]

export const ModifyFormSchema: FormSchema[] = [
  { label: '污染地块ID', field: 'wrdkid', required: true, component: 'Input', show: false },
  {
    label: '中心经度',
    field: 'wrdkzxjd',
    component: 'Input',
    componentProps: {
      placeholder: '十进制，保留六位小数',
    },
    required: true,
    dynamicRules: () => {
      return [
        { pattern: /^-?(180(\.\d{1,6})|((1[0-7]|[1-9])?\d(\.\d{1,6})))$/, message: '中心经度码格式有误', required: true, },
      ];
    },
  },
  {
    label: '中心纬度',
    field: 'wrdkzxwd',
    component: 'Input',
    componentProps: {
      placeholder: '十进制，保留六位小数',
    },
    required: true,
    dynamicRules: () => {
      return [
        { pattern: /^-?(180(\.\d{1,6})|((1[0-7]|[1-9])?\d(\.\d{1,6})))$/, message: '中心纬度格式有误', required: true, },
      ];
    },
  },
]

export const FllowFormSchema: FormSchema[] = [
  { label: '污染地块ID', field: 'wrdkid', required: true, component: 'Input', show: false },
  {
    label: '污染地块名称', field: 'wrdkmc', required: true, component: 'Input', componentProps: {
      disabled: true,
    }
  },
  { label: '移入原由', field: 'clyj', required: true, component: 'Input' },
  {
    label: '文件',
    field: 'clyjfj',
    required: true,
    component: 'JUpload',
    componentProps: {
      showUploadList: false,
    },
  },
]
// 获取所属接口因为写内部拿不到之 时间紧迫后期再优化这块
const jd = ref<any>([])
async function getSsjd() {
  jd.value = await dict({ key: 'dk_scqd' })
}
getSsjd()



// 获取行业类别
const allCategory = ref([])
const treeD = ref<any>([])
const treeM = ref<any>([])
const treeZ = ref<any>([])
const treeX = ref<any>([])
async function gethyfl() {
  const res = await categoryList()
  allCategory.value = transformArray(res, 'name', 'id', 'label', 'value')
}
gethyfl()

// 法理依据回显处理
function handeFlyj(val, formActionType) {
  const { updateSchema } = formActionType;
  if (val == 1) {
    updateSchema({
      field: 'faliErji',
      component: 'CheckboxGroup',
      show: true,

      componentProps: {
        options: zc.value,
        labelField: 'text',
        valueField: 'value',
      },
    },

    );
    updateSchema({
      field: 'faliErji1',
      component: 'Input',
      show: false,

    });
  }
  else if (val == 3) {
    updateSchema({
      field: 'faliErji',
      component: 'CheckboxGroup',
      show: false,
      componentProps: {
        options: zc.value,
        labelField: 'text',
        valueField: 'value',
      },
    },

    );
    updateSchema({
      field: 'faliErji1',
      component: 'Input',
      show: true,

    });
  } else {
    updateSchema({
      field: 'faliErji',
      show: false,
    },

    );
    updateSchema({
      field: 'faliErji1',
      show: false,

    });
  }
}
// 县回显处理
function handeCount(formActionType, formModel) {
  const { updateSchema } = formActionType;
  if (userStore?.userInfo?.userIdentity == '2' && !formModel.wrdkid) {

    updateSchema({
      label: '区县',
      labelWidth: 220,
      field: 'countryCode',
      show: true,
      componentProps: {
        api: getTown,
        params: userStore?.userInfo?.codeRegion,
        labelField: 'name',
        valueField: 'id',
      },
    });
  }
}
