<template>
  <el-dialog
    v-model="visible"
    :title="type === 'add' ? '添加隐患' : '修改隐患'"
    :on-close="fnClose"
    width="1200"
  >
    <layout-hidden-add
      :hidden-type="hiddenType"
      :info="info"
      v-model:form="form"
      ref="hiddenAddRef"
    />
    <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,
  setHiddenDangerEdit,
  setHiddenDangerListAdd,
} 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: () => ({}),
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  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(),
      LISTMANAGER_ID: props.listManagerId,
    };
    if (props.hiddenType === "unqualified") {
      if (!props.longitude && !props.latitude) {
        ElMessage.error("正在获取当前位置中，请等待");
        return;
      }
      params = {
        ...params,
        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,
        LATITUDE: props.latitude,
      };
      const resData = await setHiddenDangerAdd(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 = await setHiddenDangerListAdd(params);
        currentHiddenId = resData.pd.HIDDEN_ID;
      }
      if (props.type === "edit") {
        const resData = await setHiddenDangerEdit(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);
    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>
