<script>
export default {
  name: "ExportDialog",
  props: {
    selectData: {
      type: Array,
      require: true,
    },
  },
  data() {
    return {};
  },
  computed: {
    disabled: {
      get() {
        const { selectData } = this.$props;
        if (selectData.length < 1) {
          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 } = this.$props;
              const ids = selectData.map((item) => item.id);
              await this.download(
                "/pu/price/catalogue/exportByIds",
                { ids: ids },
                `价格目录_${new Date().getTime()}.xlsx`
              );
              await done();
            } 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"
  >
    导 出
  </el-button>
</template>
