import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { ref } from 'vue';
import { categoryList, dict } from '/@/api/common/api'

enum Api {
  list = '/qdml/qbdk?mlbs=1',
  save = '/qdml/addJsyddk',
  edit = '/qdml/queryByWrdkId',
  saveEdit = '/qdml/wsdk',
  editDK = '/qdml/editdkmc',
  deleteOne = '/qdml/deldk',
  allocation = '/qdml/fpzh',
  modify = '/qdml/xgzb',
  movie = '/qdml/yrygz',
  deleteBatch = '/dkgl/gWrdkjbxxb/deleteBatch',
  importExcel = '/dkgl/gWrdkjbxxb/importExcel',
  exportXls = '/dkgl/gWrdkjbxxb/exportXls',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params) => {
  return defHttp.post({ url: Api.deleteOne, params })
}
// 修改地块名称
export const editDk = (params) => {
  return defHttp.post({ url: Api.editDK, params })
}
// 分配账号
export const allocation = (params) => {
  return defHttp.post({ url: Api.allocation, params })
}
// 修改坐标
export const modify = (params) => {
  return defHttp.post({ url: Api.modify, params })
}
// 移入关注
export const movie = (params) => {
  return defHttp.post({ url: Api.movie, params })
}
// 修改接口
export const edit = (params) => {
  return defHttp.get({ url: `${Api.edit}?wrdkid=${params}` })
}
/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 保存或者更新
 * @param params
 * @param isUpdate 是否是更新数据
 */
export const saveOrUpdate = async (params, isUpdate) => {
  // 添加hllb=行业大类名称
  await getName(params.hydl)
  params.hylb = name.value
  // 提交的时候政策依据需要加给判断，因为样式问题的调整
  if (params.faliYiju == 3) {
    params.faliErji = params.faliErji1
  }
  delete params.faliErji1
  let url = isUpdate ? Api.saveEdit : Api.save;
  return defHttp.post({ url: url, params });
}
// 获取行业类别
const allCategory = ref([])
const name = ref('')
async function getcategory() {
  allCategory.value = await categoryList()
}
getcategory()
//获取大类的名称
function getName(key) {
  allCategory.value.forEach((v: any) => {
    v.children.forEach(item => {
      if (item.id == key) {
        name.value = item.name
      }

    })
  })
}