<script>
import { TERMINATION } from "@/api/business/purchase/contract";

export default {
  name: "TerminationDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  data() {
    return {
      title: "终 止",
      visible: false,
      loading: false,
    };
  },
  computed: {
    disabled: {
      get() {
        if (this.selectData.length === 1) {
          const [{ status }] = this.selectData;
          if (status !== "2") {
            return true;
          } else {
            return false;
          }
        } else {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    open() {
      this.visible = true;
    },
    //
    hide() {
      this.visible = false;
    },
    //
    async submit(prop) {
      try {
        // try
        this.loading = true;
        const { msg, code } = await TERMINATION({ id: prop });
        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[0].id)"
          >确 认</el-button
        >
      </div>
      <el-alert title="是否终止数据项?" type="info" show-icon :closable="false">
      </el-alert>
    </el-dialog>
  </el-button>
</template>
