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

export default {
  name: "DeleteDialog",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
    isCancal:{
      type:Boolean,
      require:true,
    }
  },
  data() {
    let title = this.isCancal ? '取消归档' : '归 档'
    return {
      title: title,
      visible: false,
      loading: false,
      params: { id: "", pigeonholeFile: "" },
    };
  },
  computed: {
    disabled: {
      get() {
        const {
          selectData,
        } = this;
        if(!selectData.length){
          return true
        }
        return false
        // if (selectData.length !== 1) {
        //   return true;
        // }
        // if (selectData.length === 1 && pigeonhole === "Y") {
        //   return true;
        // }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    async open(prop) {
      // this.visible = true;
      // this.params.id = prop;

      let eligibility = this.selectData.filter(item => {
        if(!this.isCancal){
          // 归档
          return (item.pigeonhole === 'Y' || item.status !== '2')

        }else{
          // 取消归档
          return (item.pigeonhole === 'N' || item.status !== '2')
        }
      });

      console.log(eligibility,'eligibility');

      if(!eligibility.length){

        try {
          this.loading = true;

          const { msg, code } = await PIGEONHOLE(
            [...this.selectData],
            { type: this.isCancal ? 'N':'Y'}
            );

          if (code === 200) {
          this.$emit("success");
          this.$notify.success({ title: msg });
        }

        } catch (error) {
          
        }finally{
          this.loading = false;
        }
      }else{
        this.$notify.warning({ 
          title: '警告',
          message: `请选择已审核并${this.isCancal ? '已' :'未'}归档数据！`,
        });
      }
      

    },
    //
    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"
  >
    {{ 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>
