<template>
  <el-dialog v-model="visible" title="分析" :on-close="fnClose">
    <layout-risk-view :info="info">
      <el-descriptions-item label="是否生成新风险因素" :span="2">
        {{ info.IS_NEW === 1 ? "是" : "否" }}
      </el-descriptions-item>
      <el-descriptions-item label="风险成因" :span="2">
        <el-select v-model="riskCauses" placeholder="请选择">
          <el-option
            v-for="item in riskCausesList"
            :key="item.DICTIONARIES_ID"
            :label="item.NAME"
            :value="item.DICTIONARIES_ID"
          />
        </el-select>
      </el-descriptions-item>
    </layout-risk-view>
    <template #footer>
      <el-button type="primary" @click="fnSubmit(1)">通过</el-button>
      <el-button type="danger" @click="fnSubmit(0)">打回</el-button>
      <el-button @click="fnClose">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModel } from "@vueuse/core";
import { ref } from "vue";
import { layoutFnGetRiskCauses } from "@/assets/js/data_dictionary.js";
import { debounce } from "throttle-debounce";
import {
  setRiskAnalysisAdopt,
  setRiskAnalysisRepulse,
} from "@/request/continuous_improvement.js";
import { ElMessage } from "element-plus";
import LayoutRiskView from "@/components/risk_view/index.vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  info: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});
const emits = defineEmits(["update:visible", "get-data"]);
const visible = useVModel(props, "visible", emits);
const riskCauses = ref("");
const riskCausesList = await layoutFnGetRiskCauses();
const fnClose = () => {
  visible.value = false;
  riskCauses.value = "";
};
const fnSubmit = debounce(
  1000,
  async (STATUS) => {
    if (STATUS === 0) {
      await setRiskAnalysisRepulse({
        RISKPOINT_ANALYSIS_ID: props.info.RISKPOINT_ANALYSIS_ID,
      });
    } else {
      if (!riskCauses.value) {
        ElMessage.warning("风险成因未选择");
        return;
      }
      await setRiskAnalysisAdopt({
        RISKPOINT_ANALYSIS_ID: props.info.RISKPOINT_ANALYSIS_ID,
        RISK_CAUSES: riskCauses.value,
      });
    }
    ElMessage.success("操作成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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