<template>
  <div class="allotDetail pagePadding" v-loading="loading">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <div class="detail-page-title">
        <span>调拨编号：</span>
        <span v-if="sitem">{{ sitem.allot_code }}</span>
        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          class="fr"
          :size="'mini'"
          v-if="status !== '' && status !== '5' && powers.some((i) => i == '015')"
        >作废该条信息</el-button>
      </div>

      <!-- <status-bar
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      />-->
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="业务详情" name="1">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item title="调拨详情" name="-1">
              <add-form
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item
              title="发起审核流程"
              name="0"
              v-if="
                (status === '0' && ppowers.some((i) => i == '0')) ||
                (status === '1' && powers.some((i) => i == '014'))
              "
            >
              <div class="exam-content">
                <el-button
                  type="primary"
                  class="fr"
                  :size="'mini'"
                  @click="statusConfirm('1', '发起审核流程')"
                  v-if="status === '0' && ppowers.some((i) => i == '0')"
                >发起审核流程</el-button>

                <el-button
                  @click="statusConfirm('0', '取消审核流程')"
                  plain
                  class="fr"
                  :size="'mini'"
                  v-if="status === '1' && powers.some((i) => i == '014')"
                >取消审核流程</el-button>
              </div>
            </el-collapse-item>
            <el-collapse-item
              title="入库方审核"
              name="1"
              v-if="status == '1' && ppowers.some((i) => i == '1')"
            >
              <exam-form
                :statusList="statusList"
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm"
              />
            </el-collapse-item>
            <el-collapse-item
              title="出库方发货"
              name="2"
              v-if="status !== '' && status !== '0' && status !== '1'"
            >
              <go-stock-audit
                :newTime="newTime"
                :sitem="sitem"
                v-if="newTime !== ''"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item title="入库方验货" name="3" v-if="status == '3'">
              <inspection-table
                :newTime="newTime"
                :sitem="sitem"
                v-if="newTime !== ''"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item title="入库方验货审核" name="4" v-if="status == '4'">
              <inspection-table
                :newTime="newTime"
                :sitem="sitem"
                v-if="newTime !== ''"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'DBD'"
            :orderCode="code"
          />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="3">
          <flow-chart process_id="11" type="DBD" :orderCode="code" />
       </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/stock/allot/detail";
import addForm from "./components/addForm"; //新建申请页面
import GoStockAudit from "./components/goStock-audit"; //出库方发货
import InspectionTable from "./components/Inspection-table"; //入库验货 入库验货审核

export default {
  name: "allotDetail",
  mixins: [mixinPage, resToken],
  components: {
    addForm,
    GoStockAudit,
    InspectionTable
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          item => item.menu_route == "allotDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find(i => i.process_type === "DBD") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    }
  },
  data() {
    return {
      activeTabs: "1",
      code: "",
      activeNames: ["-1", "0", "1", "2", "3", "4", "5"],
      sitem: null,
      status: "",
      newTime: "",
      loading: false,
      queryId: "",
      statusOptions: [
        { value: "0", label: "待发起审核流程" },
        { value: "1", label: "入库方审核" },
        { value: "2", label: "出库方发货" },
        { value: "3", label: "入库方验货" },
        { value: "4", label: "验货审核" },
        { value: "5", label: "调拨完成" },
        { value: "6", label: "调拨驳回" },
      ],
    };
  },
  mounted() {
    this.status = "";
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    handClick(value) {
      console.log("执行子组件事件", value);
      this.company = value;
    },
    async statusConfirm(status, message) {
      await this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          if (status === "-1") {
            await this.deleteById(message);
          } else {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },

    async deleteById(message) {
      const res = await asyncRequest.delete({ id: this.queryId });
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: ""
        });

        this.routeGoto("check", {});
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async setStatus(status, message, remark) {
      let model = {
        id: this.queryId,
        remark: remark || "",
        status: status
      };
      const res = await asyncRequest.status(model);
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: ""
        });
        this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "2" : "6";
        await this.setStatus(type, "提交入库方审核", e.remark);
      }
    },
    async initData() {
      this.newTime = "";
      this.loading = true;
      const res = await asyncRequest.detail({ id: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        this.status = this.sitem.status;
        // console.log(res.data);
        this.code = res.data.allot_code;
        //;
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
      this.loading = false;
    }
  }
};
</script>
