<script>
import { SUBMIT } from "@/api/business/purchase/apply";

export default {
  name: "SubmitDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  data() {
    return {
      title: "提交OA",
      visible: false,
      loading: false,
    };
  },
  computed: {
    disabled: {
      get() {
        const { selectData } = this;
        if (selectData.length < 1) {
          return true;
        }
        if (
          selectData.length >= 1 &&
          selectData.findIndex(({ status }) => status === "1") > -1
        ) {
          return true;
        }
        if (
          selectData.length >= 1 &&
          selectData.findIndex(({ status }) => status === "2") > -1
        ) {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    open() {
      this.visible = true;
    },
    //
    hide() {
      this.visible = false;
    },
    //
    async submit(prop) {
      try {
        // try
        this.loading = true;
        const ids = prop.map((item) => item.id).join(",");
        const { msg, code } = await SUBMIT(ids);
        if (code === 200) {
          this.hide();
          this.$emit("success");
          this.$notify.success(msg);
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open"
  >
    {{ title }}
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="25%"
      append-to-body
      @close="hide"
    >
      <div slot="footer">
        <el-button
          :size="$attrs.size"
          :loading="loading"
          @click="visible = false"
          >取 消</el-button
        >
        <el-button
          type="primary"
          :size="$attrs.size"
          :loading="loading"
          @click="submit(selectData)"
          >确 认</el-button
        >
      </div>
      <el-alert
        title="是否提交数据项至OA系统"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 10px"
      >
      </el-alert>
    </el-dialog>
  </el-button>
</template>
