<template>
  <el-dialog
    v-model="visible"
    :title="type === 'add' ? '添加隐患' : '修改隐患'"
    :on-close="fnClose"
    width="1200"
  >
    <layout-hidden-add
      ref="hiddenAddRef"
      v-model:form="form"
      :hidden-type="hiddenType"
      :info="info"
    />
    <template #footer>
      <el-button @click="fnClose">取消</el-button>
      <el-button type="primary" @click="fnSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModels } from "@vueuse/core";
import { debounce } from "throttle-debounce";
import { ElMessage } from "element-plus";
import {
  setHiddenDangerAdd,
  setCustomHiddenDangerAdd,
  setHiddenDangerEdit,
  setCustomHiddenDangerEdit,
  setHiddenDangerListAdd,
  setCustomHiddenDangerListAdd,
} from "@/request/hazard_investigation.js";
import { setUploadImg } from "@/request/api.js";
import LayoutHiddenAdd from "@/components/hidden_add/index.vue";
import { ref } from "vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  info: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  // 新增或者修改 title展示
  type: {
    type: String,
    required: true,
    default: "",
  },
  // 清单类型 1风险清单 2隐患清单
  listType: {
    type: Number,
    required: false,
    default: 1,
  },
  hiddenType: {
    type: String,
    required: true,
    default: "",
  },
  listManagerId: {
    type: String,
    required: true,
    default: "",
  },
  longitude: {
    type: Number,
    required: true,
    default: 0,
  },
  latitude: {
    type: Number,
    required: true,
    default: 0,
  },
});
const emits = defineEmits(["update:visible", "update:form", "submit"]);
const { visible, form } = useVModels(props, emits);
const hiddenAddRef = ref(null);
const fnClose = () => {
  hiddenAddRef.value.reset();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await hiddenAddRef.value.formValidate();
    let currentHiddenId = "";
    let params = {
      ...form.value,
      STATE: "0",
      HIDDENTYPE1: form.value.HIDDENTYPE[0],
      HIDDENTYPE2: form.value.HIDDENTYPE[1],
      HIDDENTYPE3: form.value.HIDDENTYPE[2],
      HIDDENTYPE_NAME: hiddenAddRef.value.hiddenDangerTypeLabel(),
      // 根据不同id 存到不同的表里
      LISTMANAGER_ID: props.listManagerId,
      CUSTOM_ID: props.listManagerId,
    };
    if (props.hiddenType === "unqualified") {
      if (!props.longitude && !props.latitude) {
        ElMessage.error("正在获取当前位置中，请等待");
        // return;
      }
      params = {
        ...params,
        // 来源 2风险清单 隐患清单
        SOURCE: "2",
        RISK_UNIT: props.info.RISKUNITNAME,
        IDENTIFICATION: props.info.PARTSNAME,
        RISK_DESCR: props.info.RISK_DESCR,
        RISK_POSITION: props.info.POSITIONNAME,
        LEVEL: props.info.DNAME5,
        CHECK_CONTENT: props.info.CHECK_CONTENT,
        RECORDITEM_ID: props.info.RECORDITEM_ID,
        RISKITEM_ID: props.info.LISTCHECKITEM_ID,
        LONGITUDE: props.longitude,
        CHECK_CATEGORY: props.info.CHECK_CATEGORY,
        CHECK_CATEGORY_NAME: props.info.CHECK_CATEGORY_NAME,
        CHECK_ITEM: props.info.CHECK_ITEM,
        CHECK_ITEM_NAME: props.info.CHECK_ITEM_NAME,
        CHECK_STANDARD: props.info.CHECK_STANDARD,
        REFERENCE_BASIS: props.info.REFERENCE_BASIS,
      };
      const resData =
        props.listType === 1
          ? await setHiddenDangerAdd(params)
          : await setCustomHiddenDangerAdd(params);
      currentHiddenId = resData.pd.HIDDEN_ID;
    }
    if (props.hiddenType === "otherHidden") {
      params = {
        ...params,
        HIDDEN_ID: props.info.HIDDEN_ID,
        SOURCE: "3",
        HAVESCHEME: 0,
      };
      if (props.type === "add") {
        const resData =
          props.listType === 1
            ? await setHiddenDangerListAdd(params)
            : await setCustomHiddenDangerListAdd(params);
        currentHiddenId = resData.pd.HIDDEN_ID;
      }
      if (props.type === "edit") {
        const resData =
          props.listType === 1
            ? await setHiddenDangerEdit(params)
            : await setCustomHiddenDangerEdit(params);
        currentHiddenId = resData.pd.HIDDEN_ID;
      }
    }
    for (let i = 0; i < form.value.hiddenImgs.length; i++) {
      if (form.value.hiddenImgs[i].raw)
        await fnUploadImage(currentHiddenId, form.value.hiddenImgs[i].raw, 3);
    }
    for (let i = 0; i < form.value.videoFiles.length; i++) {
      if (form.value.videoFiles[i].raw)
        await fnUploadImage(currentHiddenId, form.value.videoFiles[i].raw, 3);
    }
    for (let i = 0; i < form.value.rectifyImgs.length; i++) {
      if (form.value.rectifyImgs[i].raw)
        await fnUploadImage(currentHiddenId, form.value.rectifyImgs[i].raw, 4);
    }
    emits("submit", currentHiddenId, form.value.HIDDENDESCR);
    ElMessage.success("保存成功");
    fnClose();
  },
  { atBegin: true }
);
const fnUploadImage = async (HIDDEN_ID, FFILE, TYPE) => {
  const formData = new FormData();
  formData.append("FOREIGN_KEY", HIDDEN_ID);
  formData.append("TYPE", TYPE);
  formData.append("FFILE", FFILE);
  await setUploadImg(formData);
};
</script>

<style scoped lang="scss"></style>
