<script>
import { REMOVE } from "@/api/business/purchase/contract";

export default {
  name: "DeleteDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  data() {
    return {
      title: "删 除",
    };
  },
  computed: {
    disabled: {
      get() {
        const {
          selectData,
          selectData: [{ status } = {}],
        } = this;
        if (selectData.length !== 1) {
          return true;
        }
        if (selectData.length === 1 && status === "1") {
          return true;
        }
        if (selectData.length === 1 && status === "2") {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    open() {
      this.$confirm("是否删除数据项?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = "执行中...";
            try {
              // try
              const {
                selectData: [{ id }],
              } = this.$props;
              const { msg, code } = await REMOVE(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"
    :disabled="disabled"
    @click="open"
  >
    {{ title }}
  </el-button>
</template>
