<!-- 整单关闭 -->
<script>
import orderApi from "@/api/business/purchase/purchase-order";
export default {
  name: "AllClose",
  props: {
    selectData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      title: "整单关闭",
    };
  },
  computed: {
    innerValue: {
      get() {
        return this.$props.selectData;
      },
      set() {},
    },
    disabled: {
      get() {
        let { innerValue } = this;

        if (
          innerValue.length === 1 &&
          innerValue[0].status == 2 &&
          innerValue[0].isEnd === "N"
        ) {
          return false;
        }
        return true;
      },
      set() {},
    },
  },
  methods: {
    open() {
      // 未审批状态下整单关闭
      this.$prompt("请输入关闭原因", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\s*\S+?/,
        inputErrorMessage: "关闭原因不能为空",
      })
        .then(async ({ value }) => {
          let { innerValue } = this;
          try {
            this.$modal.loading("加载中...");
            let puOrderIds = innerValue.map((order) => Number(order.id));

            let { code, msg } = await orderApi.close({
              puOrderIds,
              closeReason: value,
            });

            if (code === 200) {
              this.$notify.success({
                title: msg,
              });
              this.$emit("submit");
            }
          } catch (error) {
          } finally {
            this.$modal.closeLoading();
          }
        })
        .catch(() => {});
    },
  },
  created() {},
};
</script>
<template>
  <el-button :size="$attrs.size" @click="open" :disabled="disabled">{{
    title
  }}</el-button>
</template>
