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

export default {
  name: "SubmitDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  data() {
    return {
      title: "提交OA",
    };
  },
  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() {
      let _this = this;
      this.$confirm(`是否提交数据项至OA系统?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(async () => {
          try {
            const { selectData } = this.$props;
            const ids = selectData.map((item) => item.id);
            const { msg, code } = await SUBMIT({
              submitIds: ids,
              checkAmount: true,
            });
            if (code === 200) {
              this.$emit("success");
              this.$notify.success(msg);
            }
            if (code === 10000) {
              _this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 65vh;padding: 10px 20px 0;'>" +
                  msg +
                  "</div>",
                "提示",
                {
                  showCancelButton: true,
                  dangerouslyUseHTMLString: true,
                  confirmButtonText: "确认",
                  cancelButtonText: "取消",
                  beforeClose: async (action, instance, done) => {
                    if (action === "confirm") {
                      instance.confirmButtonLoading = true;
                      instance.confirmButtonText = "执行中...";
                      try {
                        let { code, msg } = await SUBMIT({
                          submitIds: ids,
                          checkAmount: false,
                        });
                        if (code == 200) {
                          done();
                          _this.$emit("success");
                          _this.$notify.success(msg);
                        }
                      } catch (error) {
                        instance.confirmButtonText = "确认";
                      } finally {
                        instance.confirmButtonLoading = false;
                      }
                    } else {
                      done();
                    }
                  },
                }
              );
            }
          } catch (err) {
            // catch
            console.error(err);
            instance.confirmButtonText = "确认";
          } finally {
            // finally
            instance.confirmButtonLoading = false;
          }
        })
        .catch(() => {});
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open"
  >
    {{ title }}
  </el-button>
</template>
