<!-- 反审 -->
<script>
import { merge } from "@/api/business/purchase/purchase-order";
export default {
  name: "JoiningOrders",
  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) {
          // 0=自由态
          // 1=自动协议直采，2=协议直采，3=手工
          let condition = innerValue.findIndex((item) => {
            let cTime = new Date(item.createTime).getTime();
            let time = new Date();
            let cap = 7 * 24 * 60 * 60 * 1000;

            return (
              item.status !== "0" || time - cTime > cap || item.source === "3"
            );
          });

          if (condition === -1) return false;
        }
        return true;
      },
      set() {},
    },
  },
  methods: {
    async open() {
      this.$confirm("是否继续执行合单操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            this.$modal.loading("提交中...");
            let puOrderIds = this.innerValue.map((item) => Number(item.id));
            let { code, msg } = await merge({ puOrderIds });
            if (code == 200) {
              this.$notify.success({
                message: msg,
              });
              this.$emit("success");
            }
          } catch (error) {
          } finally {
            this.$modal.closeLoading();
          }
        })
        .catch(() => {});
    },
  },
  created() {},
  mounted() {},
};
</script>

<template>
  <el-button
    @click="open"
    v-bind="$attrs"
    :disabled="disabled"
    v-hasPermi="['material:order:merge']"
    >{{ title }}</el-button
  >
</template>
