<template>
  <el-dialog v-model="visible" :title="title" :before-close="fnClose">
    <layout-risk-add v-model:form="form" :info="info" ref="riskAddRef" />
    <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 {
  setIdentifyingPartsRiskAdd,
  setIdentifyingPartsRiskEdit,
} from "@/request/risk_control.js";
import LayoutRiskAdd from "@/components/risk_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: Number,
    required: true,
    default: 0,
  },
  title: {
    type: String,
    required: true,
    default: "",
  },
  id: {
    type: String,
    required: true,
    default: "",
  },
  time: {
    type: Number,
    required: true,
    default: 0,
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const riskAddRef = ref(null);
const fnClose = () => {
  riskAddRef.value.reset();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await riskAddRef.value.formValidate();
    const params = {
      ...form.value,
      RISK_UNIT_ID: props.info.RISK_UNIT_ID,
      DEPARTMENT_ID: props.info.DEPARTMENT_ID,
      IDENTIFICATION_ID: props.id,
      CHECK_CONTENT: form.value.MEASURES,
      ACCIDENTS: form.value.ACCIDENTS.join(","),
      ACCIDENTS_NAME: riskAddRef.value.accidentsName(),
      SETUPTIME: props.time,
      TYPE: props.type,
    };
    if (!props.type) await setIdentifyingPartsRiskAdd(params);
    else await setIdentifyingPartsRiskEdit(params);
    ElMessage.success("操作成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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