<script>
import { delTargetTemplate,checkQuote, } from "@/api/business/spd/starget/targetTemplate";

export default {
  name: "DeleteDialog",
  props: ['selectData'],
  data() {
    return {
      title: "删 除",
    };
  },
  watch: {},
  methods: {
    async open() {
      const { selectData } = this.$props;
      const { code, data } = await checkQuote(selectData.id);
      if(code == '200'){
        if(data){
          this.$modal.msgError("该目标模板存在引用，无法删除！");
          this.visible = false;
          return;
        }
      }else{
        this.$modal.msgError("校验该目标模板是否存在引用异常，无法删除！");
        this.visible = false;
        return;
      }
      this.$confirm(`是否删除数据项?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = "执行中...";
            try {
              // try
              const { msg, code } = await delTargetTemplate(selectData.id);
              if (code === 200) {
                done();
                this.$emit("success");
                this.$notify.success(msg);
              }
            } catch (err) {
              // catch
              console.error(err);
              instance.confirmButtonText = "确认";
            } finally {
              // finally
              instance.confirmButtonLoading = false;
            }
          } else {
            done();
          }
        },
      })
        .then(() => {})
        .catch(() => {});
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    @click="open"
  >
    {{ title }}
  </el-button>
</template>
