<template>
  <div class="stockInDetail pagePadding">
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <p>{{ status }}---{{ powers }}</p>

      <div class="detail-page-title">
        <span class="title">备货入库单编号：{{ queryId }}</span>
      </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="0">
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="ShowDataTableColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="'mini'"
                    :type="sitem.status == '0' ? 'warning' : ''"
                    v-text="
                      (
                        statusOptions.find(
                          (item) => item.value == sitem.status
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
              </show-data-table>
              <!-- <wsm-in-order-addEdit-form
            v-if="newTime !== ''"
            :id="queryId"
            :newTime="newTime"
            :modelSitem="{}"
            :sitem="sitem"
            @refresh="initData()"
          /> -->
            </el-collapse-item>
            <el-collapse-item title="入库验收" name="2">
              <wsm-in-order-record-form
                v-if="newTime !== ''"
                :id="queryId"
                :newTime="newTime"
                :sitem="sitem"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item title="入库验收审核" name="3">
              <wsm-in-order-check-form
                v-if="newTime !== ''"
                :id="queryId"
                :newTime="newTime"
                :modelSitem="{}"
                :sitem="sitem"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item
              title="入库审核"
              v-if="status == '3' && powers.some((item) => item == '031')"
            >
              <exam-form
                :statusList="statusList"
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm"
              />
            </el-collapse-item>
            <el-collapse-item
              title="入库退货"
              v-if="status == '5' && powers.some((item) => item == '032')"
              name="5"
            >
              <wsm-in-order-return-form
                v-if="newTime !== ''"
                :id="queryId"
                :newTime="newTime"
                :modelSitem="{}"
                :sitem="sitem"
                @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="'RKD'"
            :orderCode="orderCode"
          />
        </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
   <script>
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/purchaseIn/purchaseOrder";
import wsmInOrderAddEditForm from "./components/wsm-in-order-addEdit-form";
import wsmInOrderRecordForm from "./components/wsm-in-order-record-form";
import wsmInOrderCheckForm from "./components/wsm-in-order-check-form";
import wsmInOrderReturnForm from "./components/wsm-in-order-return-form";
import ShowDataTableColumns from "./components/ShowDataTableColumns";
export default {
  name: "wsmInOrderDetail",
  mixins: [resToken],
  components: {
    wsmInOrderAddEditForm,
    wsmInOrderRecordForm,
    wsmInOrderCheckForm,
    wsmInOrderReturnForm,
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "wsmInOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      activeTabs: "1",
      orderCode: "",
      ShowDataTableColumns: ShowDataTableColumns,
      activeNames: ["0", "1", "2", "3", "4", "5"],
      sitem: null,
      status: "",
      statusOptions: [
        // { value: "0", label: "取消" },
        { value: "1", label: "入库验收" },
        { value: "2", label: "入库验收审核" },
        { value: "3", label: "同意下单" },
        { value: "4", label: "发货中" },
        { value: "5", label: "已发货" },
      ],
      statusList: [
        {
          code: "0",
          label: "待发货",
        },
        {
          code: "1",
          label: "待验货",
        },
        {
          code: "2",
          label: "待审核",
        },
        {
          code: "3",
          label: "待入库",
        },
        {
          code: "4",
          label: "无退货入库成功",
        },
        {
          code: "5",
          label: "退货成功",
        },
      ],
      newTime: "",
      loading: false,
      queryId: "",
    };
  },
  mounted() {
    this.status = "";
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async setStatus(status, message, remark) {
      let model = {
        wsm_in_code: this.queryId,
        remark: remark || "",
        status: status,
      };
      const res = await asyncRequest.orderinstatus(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" ? "4" : "2";
        await this.setStatus(type, "提交入库方审核", e.remark);
      }
    },
    async initData() {
      const res = await asyncRequest.orderininfo({ wsm_in_code: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, wsm_in_code } = this.sitem;
        this.status = status;
        this.orderCode = wsm_in_code;
        if (can && can.length > 0) {
          this.sitem.class_cat = "";
          can.forEach((x, i) => {
            this.sitem.class_cat += i === 0 ? x.name : "/" + x.name;
          });
        }
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>
   