<script>
import { ENABLE } from "@/api/business/purchase/catalogue";

export default {
  name: "EnableDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
    status: {
      type: String,
    },
  },
  data() {
    return {};
  },
  computed: {
    title: {
      get() {
        const { status } = this.$props;
        if (status === "Y") {
          return "启 用";
        }
        if (status === "N") {
          return "停 用";
        }
      },
      set() {},
    },
    disabled: {
      get() {
        const {
          selectData,
          status: enableStatus,
          selectData: [{ status } = {}],
        } = this.$props;
        if (selectData.length < 1) {
          return true;
        }
        if (selectData.length >= 1 && status === "N") {
          return true;
        }
        if (
          selectData.length >= 1 &&
          selectData.findIndex((item) => item.enableStatus === enableStatus) >
            -1
        ) {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    open() {
      this.$confirm(`是否${this.title.replace(/\s*/g,"")}数据项?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = "执行中...";
            try {
              // try
              const { status, selectData } = this.$props;
              const ids = selectData.map((item) => item.id);
              const { msg, code } = await ENABLE({
                ids,
                enableStatus: status,
              });
              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>
