<template>
  <el-dialog v-model="visible" title="分析" :before-close="fnClose">
    <layout-risk-add v-model:form="form" :rules="rules" ref="riskAddRef">
      <el-col :span="24">
        <el-form-item label="是否生成新风险因素" prop="IS_NEW">
          <el-radio-group v-model="form.IS_NEW" disabled>
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </layout-risk-add>
    <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 LayoutRiskAdd from "@/components/risk_add/index.vue";
import { ref, watch } from "vue";
import { setHazardAnalysisSubmit } from "@/request/continuous_improvement.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  IS_NEW: [{ required: true, message: "请选择", trigger: "change" }],
};
const riskAddRef = ref(null);
const fnClose = () => {
  riskAddRef.value.reset();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await riskAddRef.value.formValidate();
    await setHazardAnalysisSubmit({
      STATUS: 0,
      ACCIDENTS: form.value.ACCIDENTS.join(","),
      ACCIDENTS_NAME: riskAddRef.value.accidentsName(),
      ...form.value,
    });
    ElMessage.success("操作成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
watch(
  () => props.form.IDENTIFICATION_ID,
  (newValue, oldValue) => {
    if (oldValue) {
      form.value.IS_NEW = 1;
    }
  }
);
</script>

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