<template>
  <a-spin :spinning="confirmLoading">
    <div v-show="modalVisible">
      <div class="table-operator">
        <w-button
          @click="submitForm"
          :disabled="head && head.id && head.status !== '000' && head.status !== '090'"
          v-has="'MergerReqs:save'"
          type="primary"
        >
          保存
        </w-button>
        <w-button
          @click="handleTransfer"
          v-has="'MergerReqs:transfer'"
          :disabled="!(head.status === '000' || head.status === '090')"
          type="primary"
        >
          转派
        </w-button>
        <w-button
          @click="handleGrouping"
          v-has="'MergerReqs:group'"
          :disabled="!(head.status === '000' || head.status === '090')"
          type="primary"
        >
          提交归类
        </w-button>
        <w-button @click="close()" type="primary"> 关闭 </w-button>
      </div>

      <a-collapse v-model="activeKey">
        <a-collapse-panel key="1" header="表头信息">
          <MergerReqsModalForm ref="realForm" :disabled="disableSubmit" @close="close" @getHeadId="getHeadId" />
        </a-collapse-panel>
        <a-collapse-panel key="2" header="表体信息">
          <MergerReqsModalBody
            ref="MergerReqsModalBody"
            :head="head"
            @openBodyForm="openBodyForm"
            @openBodyData="openBodyData"
            @modalFormClose="modalFormClose"
          />
        </a-collapse-panel>
      </a-collapse>
    </div>
    <MergerReqsModalBodyForm ref="MergerReqsModalBodyForm" :head="head" @close="closeBodyForm" />
    <MergerReqsModalBodyData ref="MergerReqsModalBodyData" :head="head" @close="closeBodyForm" />
    <MergerReqsModalTransfer ref="MergerReqsModalTransfer" @close="modalFormClose" />
  </a-spin>
</template>

<script>
import MergerReqsModalBody from './MergerReqsModalBody.vue';
import MergerReqsModalBodyData from './MergerReqsModalBodyData.vue';
import MergerReqsModalBodyForm from './MergerReqsModalBodyForm.vue';
import MergerReqsModalForm from './MergerReqsModalForm.vue';
import MergerReqsModalTransfer from './MergerReqsModalTransfer.vue';
import { getAction } from '@/api/manage';
export default {
  name: 'MergerReqsModal',
  components: {
    MergerReqsModalForm,
    MergerReqsModalBody,
    MergerReqsModalBodyForm,
    MergerReqsModalBodyData,
    MergerReqsModalTransfer,
  },
  data() {
    return {
      title: '',
      width: 800,
      modalVisible: false,
      activeKey: ['1', '2'],
      disableSubmit: false,
      confirmLoading: false,
      head: {},
    };
  },
  methods: {
    add() {
      this.modalVisible = true;
      this.head = {};
      this.$nextTick(() => {
        this.$refs.realForm.add();
        this.$refs.MergerReqsModalBody.add();
      });
    },
    edit(record) {
      this.modalVisible = true;
      this.head = record;
      this.disableSubmit = record.id && record.status !== '000' && record.status !== '090';
      this.$nextTick(() => {
        this.$refs.realForm.edit(record);
        this.$refs.MergerReqsModalBody.edit(record);
      });
    },
    submitForm() {
      this.$refs.realForm.submitForm();
    },
    close() {
      this.modalVisible = false;
      this.$refs.MergerReqsModalBody.dataSource = [];
      this.$emit('close');
    },
    /* 打开表体的表单 */
    openBodyForm(mode, record) {
      this.modalVisible = false;
      if (mode === 'add') {
        this.$refs.MergerReqsModalBodyForm.add();
      } else {
        this.$refs.MergerReqsModalBodyForm.edit(record);
      }
    },
    /* 关闭表体的表单、及关闭数据拆分合并的页面 */
    closeBodyForm() {
      this.modalVisible = true;
      this.$refs.MergerReqsModalBody.searchQuery();
    },
    /* 打开数据拆分合并的页面 */
    openBodyData(mode, record) {
      this.modalVisible = false;
      if (mode === 'split') {
        this.$refs.MergerReqsModalBodyData.split(record);
      } else {
        this.$refs.MergerReqsModalBodyData.merge(record);
      }
    },
    /* 表头新增时 表体会收不到head参数 */
    getHeadId() {
      this.head = this.$refs.realForm.model;
    },
    /* 转派 */
    handleTransfer() {
      this.$nextTick(() => {
        this.$refs.MergerReqsModalTransfer.start(this.head.id);
      });
    },
    modalFormClose() {
      getAction('/merger/mergerReqsHead/queryById', { id: this.head.id })
        .then((res) => {
          if (res.success) {
            this.$set(this, 'head', res.result);
            this.$refs.realForm.edit(res.result);
          } else {
            this.$message.warning(res.message);
          }
        })
        .finally(() => {
          this.confirmLoading = false;
        });
    },
    /* 提交归类 */
    handleGrouping() {
      const that = this;
      that.$confirm({
        content: '确认是否提交归类？',
        onOk: function () {
          that.confirmLoading = true;
          getAction('/merger/mergerReqsHead/submitMerge', { ids: that.head.id }).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.modalFormClose();
            } else {
              that.$message.warning(res.message);
              that.confirmLoading = false;
            }
          });
        },
      });
    },
  },
};
</script>
