<template>
  <div class="report-detail__container">
    <el-tabs v-model="activeTabs" v-loading="loading">
      <el-tab-pane name="1" :label="title">
        <el-collapse v-model="collapses">
          <el-collapse-item v-if="type === 'add' || type === 'edit'" name="1" :title="title">
            <base-form :id="id" :type="type" />
          </el-collapse-item>

          <template v-else>
            <base-detail
              ref="baseForm"
              :sitem="sitem"
              @change-status="handleChangeStatus"
              @change-num="handleChangeNum"
              @change-order-code="handleChangeOrderCode"
              @change-detail="(detail) => (sitem = detail)"
            />
          </template>
          <tempalte
            v-if="(status == '2' || status == '3') && !isSupertube && (
            _process.includes('2') || _process.includes('3') && Number(sitem.wsm_has_account) !== 0
          )"
          >
            <el-collapse-item name="3" title="待转单">
              <wait-transferred :id="id" :num="num" @refresh="handleRefresh" />
            </el-collapse-item>
          </tempalte>
        </el-collapse>
      </el-tab-pane>

      <el-tab-pane label="审批记录" name="2" v-if="type === 'view'">
        <process-time-line
          v-if="newTime !== '' && id"
          :newTime="newTime"
          :type="'BBD'"
          :orderCode="id"
        />
      </el-tab-pane>

      <el-tab-pane label="流程图" name="3" v-if="type === 'view'">
        <flow-chart process_id="22" type="BBD" :orderCode="id" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import WaitTransferred from "./cpns/wait-transferred.vue";
import ShipmentRequest from "./cpns/shipment-request.vue";
import baseDetail from "./cpns/baseDetail.vue";
import BaseForm from "./cpns/baseForm.vue";
import asyncRequest from "@/apis/service/sellOut/filing";
import { mapGetters } from "vuex";

export default {
  components: {
    BaseForm,
    // ExamForms,
    baseDetail,
    WaitTransferred,
    ShipmentRequest
  },
  data() {
    return {
      loading: false,
      activeTabs: "1",
      collapses: ["1", "2", "3", "4"],
      status: "",
      sitem: {},
      orderCode: "",
      num: "0"
    };
  },
  computed: {
    ...mapGetters(["isSupertube"]),
    title() {
      const mapTitle = {
        add: "新建报备单",
        edit: "编辑报备单",
        view: "报备单详情"
      };

      return mapTitle[this.$route.query.type];
    },
    type() {
      return this.$route.query.type || "view";
    },
    id() {
      return this.$route.query.id;
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    _process() {
      const { roleProcess } = this.$store.getters;
      const tran = roleProcess.find(i => i.process_type === "BBD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    }
  },
  methods: {
    async examFormSubmit({ companyCode, ...rest } = {}) {
      const { id } = this.$route.query;
      this.loading = true;
      await asyncRequest.status({
        ...rest,
        id,
        companyCode: Array.isArray(companyCode) ? companyCode[0] : companyCode
      });

      await this.$refs.baseForm.initData();
      this.loading = false;
    },
    handleChangeStatus(status) {
      this.status = status;
    },
    handleChangeOrderCode(orderCode) {
      this.orderCode = orderCode;
    },
    async handleRefresh() {
      this.loading = true;
      await this.$refs.baseForm.initData();
      this.loading = false;
    },
    handleChangeNum(num) {
      this.num = num;
    }
  }
};
</script>

<style lang="scss" scoped>
.report-detail__container {
  padding: 15px;
}
</style>
