<template>
  <layout-card>
    <layout-hidden-add
      ref="hiddenAddRef"
      v-model:form="form"
      hidden-type="ledger"
    />
    <div class="tc mt-10">
      <el-button type="primary" @click="fnSubmit">确定</el-button>
    </div>
  </layout-card>
</template>

<script setup>
import LayoutHiddenAdd from "@/components/hidden_add/index.vue";
import { ref } from "vue";
import { debounce } from "throttle-debounce";
import { setHiddenDangerAdd } from "@/request/hazard_investigation.js";
import { setUploadImg } from "@/request/api.js";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
const hiddenAddRef = ref(null);
const form = ref({
  HIDDENPART: "",
  HIDDENDESCR: "",
  HIDDENLEVEL: "",
  HIDDENTYPE: [],
  hiddenImgs: [],
  videoFiles: [],
  RECTIFICATIONTYPE: "2",
  RECTIFYDESCR: "",
  rectifyImgs: [],
  RECTIFICATIONDEADLINE: "",
  RECTIFICATIONDEPT: "",
  RECTIFICATIONOR: "",
});
const fnSubmit = debounce(
  1000,
  async () => {
    await hiddenAddRef.value.formValidate();
    const resData = await setHiddenDangerAdd({
      ...form.value,
      SOURCE: "1",
      HAVESCHEME: 0,
      HIDDENTYPE1: form.value.HIDDENTYPE[0],
      HIDDENTYPE2: form.value.HIDDENTYPE[1],
      HIDDENTYPE3: form.value.HIDDENTYPE[2],
      HIDDENTYPE_NAME: hiddenAddRef.value.hiddenDangerTypeLabel(),
    });
    const 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);
    }
    ElMessage.success("保存成功");
    router.back();
  },
  { 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>
