<template>
  <div class="sellReturnDetail pagePadding">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <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
                style="margin: 0; padding: 0"
                :sitem="sitem"
                v-if="status"
                :columns="showColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (statusOptions.find((item) => item.value == sitem.status) || {})
                        .label || '--'
                    "
                  ></el-tag>
                </template>

                <template slot="order_type">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        xs_order_type_options.find(
                          (item) => item.id == sitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>

                <template slot="supplierName">
                  <span>{{ sitem.supplierName }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>供应商编号:</span>
                        <span>{{ sitem.supplierNo }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </template>

                <template slot="wsm_code">
                  <span>{{ sitem.wsm_name }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>仓库编号:</span>
                        <span>{{ sitem.wsm_code }}</span>
                      </li>
                      <li>
                        <span>仓库负责人:</span>
                        <span>{{ sitem.contactor_name }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </template>

                <template slot="companyName">
                  <span>{{ sitem.companyName }}</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="customer">
                  <span>{{ sitem.customer_name }}</span>
                  <el-popover placement="top" width="300" trigger="hover">
                    <ul>
                      <li>
                        <span>客户公司编号:</span>
                        <span>{{ sitem.customer_code }}</span>
                      </li>
                    </ul>
                    <i class="el-icon-warning-outline fr" slot="reference"></i>
                  </el-popover>
                </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"
              />
            </el-collapse-item>

            <el-collapse-item
              v-if="sitem && sitem.status === '1' && !isSupertube && ppowers.includes('1')"
              title="待库管发货"
              name="3"
            >
              <express-node v-if="canSend" @refresh="() => initData()" :sitem="sitem" />
              <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="'FHGD'"
            :orderCode="queryId"
          />
        </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/sellOut/deliveryWorkOrder";
import { showColumns } from "./components/ShowDataTableColumns";
import { xs_order_type_options } from "@/assets/js/statusList";
import ExpressNode from "./components/express.vue";
import { mapGetters } from "vuex";
export default {
  name: "deliveryWorkOrderDetail",
  mixins: [mixinPage, resToken],
  components: { ExpressNode },
  computed: {
    ...mapGetters(["isSupertube", "userInfo"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          item => item.menu_route == "supplierDeliveryWorkOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    canSend() {
      const { id: userId } = this.userInfo || {};
      const { contactor: manangerUserId, order_type, has_account } =
        this.sitem || {};
      const isStock = String(order_type) === "1";

      /* 不是库存品&&有供应商端账号 **/
      if (!isStock && String(has_account) === "1") {
        return true;
      }

      /* 库存品&&用户id和库管id一致 **/
      if (!userId || !manangerUserId) return false;
      return userId === manangerUserId;
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find(i => i.process_type === "FHGD") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    }
  },
  data() {
    return {
      activeTabs: "1",
      showColumns: showColumns,
      statusOptions: [
        { value: "1", label: "待发货" },
        { value: "2", label: "发货完成" },
        // { value: "3", label: "已收货" },
        // { value: "4", label: "已全部收货" },
        { value: "5", label: "已取消" }
      ],
      xs_order_type_options,
      sitem: null, //传给组件
      activeNames: ["0", "1", "2", "3", "4", "5", "6"],
      status: "", //存储详情接口状态
      statusList: [
        {
          value: "1",
          label: "待验货"
        },
        {
          value: "2",
          label: "待验货审核"
        },
        {
          value: "3",
          label: "待业务审核"
        }
      ],
      newTime: "",
      loading: false,
      is_stock: "",
      queryId: ""
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    async initData() {
      const { code, data, message } = await asyncRequest.detail({
        outChildCode: this.queryId
      });
      if (code === 0) {
        const { is_stock, status } = data;
        this.is_stock = is_stock;
        this.status = status;
        this.sitem = data;
        this.getNewTime();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    }
  }
};
</script>
