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

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) {
    const dataList = JSON.parse(JSON.stringify(allCategory))
    if (v == 1) {
      // 只显示42号类
      treeData.value = dataList.filter((item) => item['is42']);
      treeData.value.forEach( ele => {
        ele.children = ele.children.filter((item) => item['is42']);
      })
    } else {
      treeData.value = dataList;
    }
  }
  return { data: treeData, changeIsSuspect };
};

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: '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',
  },
  {
    label: '是否超标',
    field: 'pollueted',
    component: 'JSearchSelect',
    componentProps: {
      dict: 'qd_sfcb',
    },
  },
];

export const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'wrdkid', component: 'Input', show: false },
  {
    label: '行政区(省)',
    field: 'provinceCode',
    component: 'Select',
    defaultValue: '440000',
    show: false
  },
  {
    label: '行政区',
    field: 'cityCode',
    component: 'ApiSelect',
    required: true,
    // slot: 'form-city',
    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,
              },
            });
          }
        },
      };
    },
  },
  {
    label: '',
    field: 'countryCode',
    labelWidth: 8,
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getTown,
        params: formModel.cityCode, // 行政区划编码
        labelField: 'name',
        valueField: 'id',
      };
    }
  },
  {
    label: '地块名称',
    field: 'wrdkmc',
    component: 'Input',
    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',

    required: true,
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: dict,
        params: { key: 'dk_zcyj' },
        labelField: 'text',
        valueField: 'value',
        onChange: (val: any) => {
          const { updateSchema } = formActionType;
          formModel.faliErji = null;
          formModel.faliErji1 = null;
          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,

            });
          }


        },
      };
    },
  },
  {
    label: '',
    field: 'faliErji',
    component: 'CheckboxGroup',
    show: ({ values }) => { return values.faliErji == 1 },
  },
  {
    label: '',
    field: 'faliErji1',
    component: 'Input',
    show: ({ values }) => { return values.faliErji == 3 }
  },
  {
    label: '行业分类',
    field: 'isSuspect',
    required: true,
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: dict,
        params: { key: 'dk_hyfl' },
        labelField: 'text',
        valueField: 'value',
        onChange: (val: any) => {
          const { updateSchema } = formActionType;
          formModel.hyml = null;
          formModel.hydl = null;
          formModel.hyzl = null;
          formModel.hyxl = null;
          if (val == 1) {
            updateSchema({
              field: 'hyml',
              componentProps: ({ formModel, formActionType }) => {
                return {
                  options: allCategory.value.filter(item => item['is42']),
                  onChange: (val: any) => {
                    const { updateSchema } = formActionType;
                    if (val) {
                      treeD.value = allCategory.value.filter((item: any) => item.value === val)
                      formModel.hydl = null;
                      formModel.hyzl = null;
                      formModel.hyxl = null;
                      updateSchema({
                        field: 'hydl',
                        componentProps: ({ formModel, formActionType }) => {
                          return {
                            options: treeD.value[0].children,
                            onChange: (valD: any) => {

                              const { updateSchema } = formActionType;
                              if (valD) {
                                formModel.hyzl = null;
                                formModel.hyxl = null;
                                treeZ.value = treeD?.value[0].children.filter((item: any) => item.value === valD)

                                updateSchema({
                                  field: 'hyzl',
                                  componentProps: ({ formModel, formActionType }) => {
                                    return {
                                      options: treeZ.value[0].children,
                                      onChange: (valZ: any) => {
                                        const { updateSchema } = formActionType;
                                        formModel.hyxl = null;
                                        if (valZ) {
                                          treeX.value = treeZ.value[0].children.filter((item: any) => item.value === valZ)
                                          updateSchema({
                                            field: 'hyxl',
                                            componentProps: {
                                              options: treeX.value[0].children
                                            }
                                          })
                                        }
                                      }
                                    }
                                  }
                                })
                              }
                            }
                          }

                        },

                      });
                    }
                  }
                }
              },
              // componentProps: {
              // },
            });
          } else {
            updateSchema({
              field: 'hyml',
              componentProps: ({ formModel, formActionType }) => {
                return {
                  options: allCategory.value,
                  onChange: (val: any) => {
                    const { updateSchema } = formActionType;
                    if (val) {
                      treeD.value = allCategory.value.filter((item: any) => item.value === val)
                      formModel.hydl = null;
                      formModel.hyzl = null;
                      formModel.hyxl = null;
                      updateSchema({
                        field: 'hydl',
                        componentProps: ({ formModel, formActionType }) => {
                          return {
                            options: treeD.value[0].children,
                            onChange: (valD: any) => {

                              const { updateSchema } = formActionType;
                              if (valD) {
                                formModel.hyzl = null;
                                formModel.hyxl = null;
                                treeZ.value = treeD?.value[0].children.filter((item: any) => item.value === valD)

                                updateSchema({
                                  field: 'hyzl',
                                  componentProps: ({ formModel, formActionType }) => {
                                    return {
                                      options: treeZ.value[0].children,
                                      onChange: (valZ: any) => {
                                        const { updateSchema } = formActionType;
                                        formModel.hyxl = null;
                                        if (valZ) {
                                          treeX.value = treeZ.value[0].children.filter((item: any) => item.value === valZ)
                                          updateSchema({
                                            field: 'hyxl',
                                            componentProps: {
                                              options: treeX.value[0].children
                                            }
                                          })
                                        }
                                      }
                                    }
                                  }
                                })
                              }
                            }
                          }

                        },

                      });
                    }
                  }
                }
              },
            });
          }
        },
      };
    },
    // slot: 'form-hylb'
  },
  {
    label: '',
    field: 'hyml',
    labelWidth: 5,
    required: true,
    component: 'Select',
    // slot: 'form-hyfl'
    componentProps: ({ formModel, formActionType }) => {

      if (formModel.isSuspect == 1) {
        treeM.value = allCategory.value.filter(item => item['is42'])
      } else
        treeM.value = allCategory.value
      return {
        options: treeM?.value
      }
      // return {
      //   onChange: (val: any) => {
      //     console.log(val, '职工i选哪个吗')
      //     const { updateSchema } = formActionType;
      //     if (val) {
      //       treeD.value = allCategory.value.filter((item: any) => item.value === val)
      //       formModel.hydl = null;
      //       formModel.hyzl = null;
      //       formModel.hyxl = null;
      //       updateSchema({
      //         field: 'hydl',
      //         componentProps: {
      //           options: treeD?.value[0]?.children || []
      //         },
      //       });
      //     }
      //   }
      // }
    }
  },
  {
    label: '',
    field: 'hydl',
    labelWidth: 5,
    required: true,
    component: 'Select',
    componentProps: ({ formModel, formActionType }) => {
      treeD.value = treeM.value.filter((item: any) => item.value === formModel.hyml)
      return {
        options: treeD?.value[0]?.children || []
      }

    }
    // slot: 'form-hydl'
  },
  {
    label: '',
    field: 'hyzl',
    labelWidth: 5,
    component: 'Select',
    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 || []
        }
      }


    }
    // slot: 'form-hyzl'
  },
  {
    label: '',
    field: 'hyxl',
    labelWidth: 5,
    component: 'Select',
    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: 'gzlx',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        {
          label: '危险化学品搬迁',
          value: '1'
        },
        {
          label: '长江经济带整治',
          value: '2'
        },
        // {
        //   label: '黄河流域整治',
        //   value: '3'
        // },
        {
          label: '其他',
          value: '4'
        },
      ]
    },
  },
  {
    label: '涉及的重点区域',
    field: 'gzfw',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        {
          label: '长江经济带',
          value: '1'
        },
        {
          label: '黄河流域',
          value: '2'
        },
        {
          label: '粤港澳大湾区',
          value: '3'
        },
        {
          label: '其他',
          value: '4'
        },
      ]
    },
  },
  {
    label: '坐标系',
    field: 'zbsm',
    defaultValue: 'CGCS2000',
    component: 'CheckboxGroup',
    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, },
      ];
    },
  },
  {
    label: '是否为农用地转”一住两公”地块',
    field: 'isnyd2yzlg',
    component: 'RadioGroup',
    required: true,
    defaultValue: '0',
    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',
    required: true,
    componentProps: {
      options: [
        { label: '是', value: "1" },
        { label: '否', value: "0" },
      ],
    },
  },
  {
    label: '备注',
    field: 'bz',
    component: 'InputTextArea',
  },
];

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 zc = ref<any>([])
async function getZCer() {
  zc.value = await dict({ key: 'dk_zcyjej' })
}

getZCer()

// 获取行业类别
const allCategory = ref([])
const treeD = ref<any>([])
const treeM = ref<any>([])
const tree2 = 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()
