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

export default {
  name: "DeleteDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  data() {
    return {
      title: "归 档",
      visible: false,
      loading: false,
      params: { id: "", pigeonholeFile: "" },
    };
  },
  computed: {
    disabled: {
      get() {
        const {
          selectData,
          selectData: [{ pigeonhole } = {}],
        } = this;
        if (selectData.length !== 1) {
          return true;
        }
        if (selectData.length === 1 && pigeonhole === "Y") {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    open(prop) {
      this.visible = true;
      this.params.id = prop;
    },
    //
    hide() {
      this.visible = false;
      this.params.id = "";
      this.params.pigeonholeFile = "";
    },
    //
    async submit(prop) {
      try {
        // try
        this.loading = true;
        const { id, pigeonholeFile } = prop;
        const { msg, code } = await PIGEONHOLE({ id, pigeonholeFile });
        if (code === 200) {
          this.hide();
          this.$emit("success");
          this.$notify.success({ title: msg });
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // loading
        this.loading = false;
      }
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open(selectData[0].id)"
  >
    {{ 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(params)"
          >确 认</el-button
        >
      </div>
      <el-form
        :size="$attrs.size"
        :model="params"
        label-width="0px"
        label-position="right"
      >
        <el-form-item prop="pigeonholeFile" label="">
          <file-upload
            v-model="params.pigeonholeFile"
            :limit="1"
            style="text-align: center"
          ></file-upload>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-button>
</template>
