<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>-->
      <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_name }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <p>仓库名称编号 : {{ sitem.wsm_code }}</p>
                        <p>仓库负责人 : {{ sitem.contactor_name }}</p>
                      </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" v-if="sitem">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="sitem.order_type"
                :skucode="sitem.skuCode"
                :spucode="sitem.spuCode"
                :iscgd="isCgd"
              />
            </el-collapse-item>

            <el-collapse-item
              title="入库验收"
              name="3"
              v-if="status == '1' && ppowers.some((i) => i == '1') && !isSupertube"
            >
              <wsm-in
                v-if="newTime !== '' && canInStock"
                :id="queryId"
                :newTime="newTime"
                :modelSitem="{}"
                :sitem="sitem"
                @refresh="initData()"
              />

              <el-alert v-else type="warning" :title="'当前节点只能由仓库管理员 ' + sitem.contactor_name + ' 操作'" :closable="false" show-icon />
            </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="4">
          <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 asyncRequest from "@/apis/service/purchaseIn/purchaseOrder";
import ShowDataTableColumns from "./components/ShowDataTableColumns";
import { cg_order_type_options } from "@/assets/js/statusList";
import { statusOptions } from "./columns"; //表格列参数
import resToken from "@/mixins/resToken";
import wsmIn from "./components/wsm-in";
import { mapGetters } from "vuex";

export default {
  name: "wsmInOrderDetail",
  mixins: [resToken],
  components: { wsmIn },
  computed: {
    ...mapGetters(["isSupertube", "userInfo"]),
    isCgd(){
      const { order_type, order_source } = this.sitem || {};
      if(order_source === "7"  && order_type === "4"){
        return false
      }

      if(order_source === "9"  && order_type === "3"){
        return  false
      }

      return true
    },
    canInStock() {
      const { contactor } = this.sitem || {};
      const { id } = this.userInfo || {};
      console.log(contactor, id);
      if (!contactor || !id) return false;
      return String(contactor) === String(id);
    },
    powers() {
      const { btnList } = this.$store.getters;
      const tran =
        btnList.find(item => item.menu_route == "wsmInOrderDetail") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const { roleProcess } = this.$store.getters;
      const tran = 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,
      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>
