<template>
  <div class="stockInDetail pagePadding">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <!-- <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="company">
                  <span>{{ sitem.company }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>购买方公司编号：</span><span>{{ sitem.companyNo }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </template>
                <template slot="wsm">
                  <span>{{ sitem.wsm_supplier }}/{{ sitem.wsm_name }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>仓库公司编号：</span><span>{{ sitem.wsm_supplierNo }}</span>
                      </li>
                      <li>
                        <span>仓库名称编号：</span><span>{{ sitem.wsm_code }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </template>
                <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>
                <template slot="order_type">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        cg_order_type_options.find(
                          (item) => item.id == sitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="sendtype">
                  <span>{{
                    sitem.sendtype == "1"
                      ? "公司自提"
                      : sitem.sendtype === "2"
                      ? "供应商包邮"
                      : "--"
                  }}</span></template
                >
              </show-data-table>
            </el-collapse-item>
            <el-collapse-item title="商品信息" name="2">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="sitem.order_type"
                :skucode="sitem.skuCode"
                :spucode="sitem.spuCode"
                :iscgd="true"
              />
            </el-collapse-item>
            <el-collapse-item
              title="入库验收登记"
              name="3"
              v-if="sitem && sitem.sendtype === '1'"
            >
              <wsm-in-order-record-form
                v-if="newTime !== ''"
                :id="queryId"
                :newTime="newTime"
                :sitem="sitem"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item
              title="入库验收登记审核"
              name="4"
              v-if="sitem && sitem.sendtype === '1' && status !== '0' && status !== '1'"
            >
              <wsm-in-order-check-form
                v-if="newTime !== ''"
                :id="queryId"
                :newTime="newTime"
                :modelSitem="{}"
                :sitem="sitem"
                @refresh="initData()"
              />
            </el-collapse-item>

            <el-collapse-item
              title="入库审核"
              name="5"
              v-if="
                status == '3' &&
                ppowers.some((i) => i == '3') &&
                sitem &&
                sitem.sendtype === '1'
              "
            >
              <exam-form
                :statusList="statusList"
                :newTime="newTime"
                v-if="newTime !== ''"
                :disabled="false"
                :isMust="false"
                @searchChange="examForm"
              />
            </el-collapse-item>

            <el-collapse-item
              title="入库退货"
              name="6"
              v-if="
                status == '5' && ppowers.some((i) => i == '5') && sitem.sendtype === '1'
              "
            >
              <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="queryId"
          />
        </el-tab-pane>
        <el-tab-pane
          label="物流进度"
          name="3"
          v-if="newTime !== '' && sitem && sitem.sendtype == '1' && status !== '0'"
        >
          <logis-time-line v-if="newTime !== ''" :orderCode="sitem.post_code" />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="4"  v-if="sitem.sendtype !== '2'">
          <flow-chart process_id="2" type="RKD" :orderCode="queryId" />
       </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 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";
import { cg_order_type_options } from "@/assets/js/statusList";
export default {
  name: "wsmInOrderDetail",
  mixins: [resToken],
  components: {
    wsmInOrderRecordForm,
    wsmInOrderCheckForm,
    wsmInOrderReturnForm,
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "wsmInOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "RKD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      activeTabs: "1",
      ShowDataTableColumns: ShowDataTableColumns,
      activeNames: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
      cg_order_type_options,
      sitem: null,
      status: "",
      statusOptions: [
        {
          value: "0",
          label: "待发货",
        },
        {
          value: "1",
          label: "待验货", //029
        },
        {
          value: "2",
          label: "待审核",
        },
        {
          value: "3",
          label: "待入库",
        },
        {
          value: "4",
          label: "入库完成",
        },
        {
          value: "5", //30  || 32
          label: "退货待物流",
        },
        {
          value: "6",
          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" ? (this.sitem.return_num === "0" ? "4" : "5") : "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;
        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>
