<template>
  <div class="purchaseOrderDetail pagePadding">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <div
        class="detail-page-title"
        v-if="status === '1' && powers.some((i) => i == '082')"
      >
        <span>采购单编号：</span><span>{{ queryId }} </span>
        <tolerance
          :newTime="newTime"
          v-if="newTime !== ''"
          :sitem="sitem"
          :showBtn="status === '1' && powers.some((i) => i == '082')"
        />
      </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="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>
                  <el-tooltip
                    class="fr"
                    v-if="
                      sitem &&
                      ppowers.some((i) => i == '0') &&
                      sitem.status === '0' &&
                      ((sitem.has_account + '' === '0' &&
                        (sitem.order_type + '' === '2' ||
                          sitem.order_type + '' === '3')) ||
                        sitem.order_type + '' === '1')
                    "
                    effect="dark"
                    content="已于供应商确认"
                    placement="top"
                  >
                    <i class="el-icon-video-play tb-icon" @click="statusConfirm()"></i>
                  </el-tooltip>
                </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>
                  <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>
                  <el-popover
                    placement="top"
                    width="300"
                    v-if="sitem.order_type !== '1'"
                    trigger="hover"
                  >
                    <ul>
                      <li>
                        <span>订单编号：</span><span>{{ sitem.orderCode }}</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="gold_price">
                  <div
                    v-if="
                      sitem.status == '0' &&
                      sitem.can[0].id == '6' &&
                      cost_detailArr.is_gold_price == '1'
                    "
                  >
                    {{ sitem.gold_price }}
                  </div>
                  <div v-else>----</div>
                </template>
                <template slot="customer">
                  <span>{{ sitem.customer.companyName }}</span>
                  <el-popover
                    placement="top"
                    width="300"
                    trigger="hover"
                    v-if="sitem.order_type != 1"
                  >
                    <ul>
                      <li>
                        <span>客户编号：</span><span>{{ sitem.customer.companyNo }}</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">
              <order-out-table :newTime="newTime" v-if="newTime !== ''" :sitem="sitem" />
            </el-collapse-item>
            <el-collapse-item title="商品信息" name="3">
              <show-goods-data-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :type="sitem.order_type"
                :skucode="sitem.skuCode"
                :spucode="sitem.spuCode"
                :iscgd="true"
              />

              <editPrice
                :newTime="newTime"
                v-if="
                  newTime !== '' &&
                  sitem.status == '0' &&
                  sitem.can[0].id == '6' &&
                  cost_detailArr.is_gold_price == '1'
                "
                :sitem="sitem"
                :cost_detailArr="cost_detailArr"
                @refresh="refresh"
              >
              </editPrice>
            </el-collapse-item>
            <el-collapse-item
              title="入库单"
              name="4"
              v-if="sitem && sitem.status !== '0'"
            >
              <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.order_type !== '1' &&
                sitem.status !== '0' &&
                sitem.purcheasediff
              "
            >
              <work-diff-table :newTime="newTime" v-if="newTime !== ''" :sitem="sitem" />
            </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="queryId"
          />
        </el-tab-pane>

        
        <el-tab-pane label="流程图" name="3">
          <flow-chart process_id="1" type="CGD" :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/purchaseIn/purchaseOrder";
import orderInTable from "./components/order-in-table";
import workDiffTable from "./components/work-diff-table";
import ShowDataTableColumns from "./components/ShowDataTableColumns";
import { statusOptions } from "./columns";
import { cg_order_type_options } from "@/assets/js/statusList";
import tolerance from "./components/tolerance";
import urlConfig from "@/apis/url-config";
import editPrice from "./components/editPrice";
import orderOutTable from "./components/order-out-table";
export default {
  name: "purchaseOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    orderInTable,
    tolerance,
    workDiffTable,
    editPrice,
    orderOutTable,
  },

  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "purchaseOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "CGD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      hasdiff: false,
      activeTabs: "1",
      ShowDataTableColumns: ShowDataTableColumns,
      isMetal: false,
      finishStatus: "finish",
      activeNames: ["0", "1", "2", "3", "4", "5", "10"],
      sitem: null,
      statusOptions: statusOptions,
      cg_order_type_options,
      status: "",
      newTime: "",
      loading: false,
      queryId: "",
      ReturnAddModel: false, //采购单退货弹窗
      cost_detailArr: [],
      fileUrl: urlConfig.baseURL,
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    // editPriceIf(sitem){
    //   console.log(sitem.status,"aaaaaaaa")
    //     if(sitem.status == "0" ){

    //       return false
    //     }else{
    //       return true
    //     }

    // },
    refresh() {
      this.initData();
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },

    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "2" : "0";
        await this.setStatus(type, "提交入库方审核", e.remark);
      }
    },
    async goldPrice() {
      let res = {};
      let { order_type } = this.sitem;
      if (order_type == "1" || order_type == "2") {
        res = await asyncRequest.cost_detail({ spuCode: this.sitem.spuCode });
      } else {
        res = await asyncRequest.cove_detail({ spuCode: this.sitem.spuCode });
      }
      if (res && res.code === 0 && res.data) {
        this.cost_detailArr = res.data;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async initData() {
      this.loading = true;
      const res = await asyncRequest.detail({ cgdNo: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        this.goldPrice();
        const { status, can, cgdNo } = this.sitem;
        this.status = status;
        console.log(this.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);
      }
      this.loading = false;
    },

    async statusConfirm() {
      await this.$confirm(`确定已与供应商确认？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let model = {
            cgdNo: [this.sitem.cgdNo],
            status: "1",
          };
          const res = await asyncRequest.status(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "提交成功！",
              message: "",
            });
            await this.initData();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.purchaseOrderDetail {
}
</style>
