<template>
  <el-dialog
    v-model="visible"
    :title="title ? title : type === 'add' ? '新增' : '修改'"
    :before-close="fnClose"
  >
    <layout-risk-add v-model:form="form" :control="control" 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 { ref } from "vue";
import { useVModels } from "@vueuse/core";
import { debounce } from "throttle-debounce";
import { ElMessage } from "element-plus";
import {
  setRiskControlLedgerAdd,
  setRiskControlLedgerEdit,
} from "@/request/risk_control.js";
import LayoutRiskAdd from "@/components/risk_add/index.vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  control: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: "",
  },
});
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,
      CHECK_CONTENT: form.value.MEASURES,
      ACCIDENTS: form.value.ACCIDENTS.join(","),
      ACCIDENTS_NAME: riskAddRef.value.accidentsName(),
    };
    let resData = {};
    if (props.type === "add") resData = await setRiskControlLedgerAdd(params);
    else resData = await setRiskControlLedgerEdit(params);
    emits("get-data", resData.pd);
    ElMessage.success("操作成功");
    fnClose();
  },
  { atBegin: true }
);
</script>

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