<template>
  <div class="purchaseOrderDetail 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>采购单编号：</span
        ><span
          >{{ queryId }}
          <!-- ---{{ sitem.bkcode }} -->
        </span>
        <el-button
          type="primary"
          :size="'mini'"
          class="fr"
          v-if="
            (status === '1' || status === '2') &&
            powers.some((item) => item == '007') &&
            sitem &&
            sitem.noble_name
          "
          style="margin: 0 0 0 12px"
          @click="diffAdd = true"
          >填写总工差</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">
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :columns="ShowDataTableColumns"
              >
                <template slot="supplier_name">
                  <span>{{ sitem.supplier_name }}</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="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="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="
                      (
                        order_type_options.find(
                          (item) => item.value == sitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                  <el-popover
                    placement="top"
                    width="300"
                    v-if="sitem.order_type === '1'"
                    trigger="hover"
                  >
                    <ul>
                      <li>
                        <span>备库单编号：</span><span>{{ sitem.bkcode }}</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>
              </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">
              <order-in-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
            <el-collapse-item
              title="工差单"
              name="4"
              v-if="sitem && sitem.noble_name"
            >
              <work-diff-table />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'CGD'"
            :orderCode="orderCode"
          />
        </el-tab-pane>
      </el-tabs>
      <diff-order-add
        :sitem="sitem"
        :show-model="diffAdd"
        @refresh="initData"
        @cancel="diffAdd = false"
      />

   
    </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/purchaseIn/purchaseOrder";
import orderInTable from "./components/order-in-table";
import ShowDataTableColumns from "./components/ShowDataTableColumns";
import { order_type_options, statusOptions } from "./columns";
export default {
  name: "purchaseOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    orderInTable,
  },

  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "purchaseOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      activeTabs: "1",
      ShowDataTableColumns: ShowDataTableColumns,
      isMetal: false,
      finishStatus: "finish",
      activeNames: ["0", "1", "2", "3", "4", "5", "10"],
      sitem: null,
      statusOptions: statusOptions,
      order_type_options: order_type_options,
      status: "",
      newTime: "",
      loading: false,
      queryId: "",
      diffAdd: false,
      orderCode: "",
      ReturnAddModel: false, //采购单退货弹窗
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
  
    openModal() {
      this.diffAdd = true;
      this.ReturnAddModel = true;
    },
    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "2" : "0";
        await this.setStatus(type, "提交入库方审核", e.remark);
      }
    },
    async initData() {
      this.loading = true;
      const res = await asyncRequest.detail({ cgdNo: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, cgdNo } = this.sitem;
        this.status = status;
        this.orderCode = cgdNo;
        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);
      }
      this.loading = false;
    },
    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("取消");
        });
    },

    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 = {
        cgdNo: this.sitem.cgdNo,
        remark: remark || "",
        status: status,
      };
      const res = await asyncRequest.status(model);
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        await this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>
   <style lang="scss" scoped>
.purchaseOrderDetail {
}
</style>
   
   