<template>
  <div class="salesOrderDetail pagePadding">
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <div style="height: 30px">
        <span>订单编号：</span><span v-if="sitem">{{ sitem.orderCode }}</span>
        <!-- v-if="status === '4' && powers.some((item) => item == '051')" -->
        <el-button
          style="margin: 0 0 0 10px"
          class="fr"
          type="primary"
          :size="'mini'"
          @click="openModal()"
          >新建销售退货单
        </el-button>
        <el-button
          style="margin: 0 0 0 10px"
          class="fr"
          type="primary"
          :size="'mini'"
          @click="setShowModel=true"
          >设置部分字段
        </el-button>
      </div>
      <el-tabs v-model="activeTabs">
        <el-tab-pane label="新建销售订单" name="0" v-if="queryType === 'add'">
          <add-form
            :newTime="newTime"
            v-if="newTime !== ''"
            :type="queryType"
            :sitem="sitem"
            :id="queryId"
            @refresh="routeReGoto('salesOrder', {})"
          />
        </el-tab-pane>
        <el-tab-pane label="业务详情" name="1" v-if="queryType !== 'add'">
          <el-collapse v-model="activeNames" style="margin: -18px 0 0 0">
            <el-collapse-item title="销售订单信息" name="-1">
              <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="
                      (
                        orderOptions.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="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="is_active">
                  <span v-if="sitem.order_type === '1'"
                    ><span
                      >{{
                        sitem.is_activity === "1" ? "参与活动" : "不参与活动"
                      }}/{{ sitem.good_type }}</span
                    ></span
                  >
                  <span v-else>--</span>
                </template>
                <template slot="send_type">
                  <el-tag :size="'mini'">{{
                    sitem.send_type === "1"
                      ? "直接发货"
                      : sitem.send_type === "2"
                      ? "延迟发货"
                      : "--"
                  }}</el-tag>
                </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.good_code"
              />
            </el-collapse-item>
            <el-collapse-item title="发货申请" name="3">
              <order-out-table
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                :id="queryId"
                @refresh="initData()"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2" v-if="queryType !== 'add'">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'XSQRD'"
            :orderCode="queryId"
          />
        </el-tab-pane>
      </el-tabs>
      <add-model
        :id="modelId"
        :sitem="s_sitem"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      />
      <!-- <edit-add
        :id="modelId"
        :sitem="sitem"
        :show-model="showModell"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      /> -->
      <set-partial
        :sitem="sitem"
        :show-model="setShowModel"
        @refresh="initData"
        @cancel="setShowModel = 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/sellOut/salesOrder/detail";
import addForm from "./components/addForm";
import orderOutTable from "./components/order-out-table";
import addModel from "@/views/sellOut/sellReturn/components/addModel";
import { showColumns } from "./components/ShowDataTableColumns";
import setPartial from "./setPartial";
export default {
  name: "salesOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    addModel,
    addForm,
    orderOutTable,
    setPartial,
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "salesOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      finishStatus: "finish",
      activeTabs: "1",
      activeNames: ["-1", "0", "1", "2", "3", "4", "5", "6"],
      showColumns: showColumns,
      setShowModel:false,
      orderOptions: [
        { id: "1", label: "线上商品销售" },
        { id: "2", label: "项目线上商品销售" },
        { id: "3", label: "咨询单销售" },
        { id: "4", label: "项目咨询单销售" },
      ],
      statusOptions: [
        { value: "0", label: "待发货" },
        { value: "1", label: "待发货完成" },
        { value: "2", label: "发货已完成" },
        { value: "3", label: "订单已取消" },
      ],
      sitem: null,
      s_sitem: null,
      orderCode: "",
      status: "",
      newTime: "",
      loading: false,
      queryId: "",
      queryType: "",
    };
  },
  mounted() {
    this.status = "";
    const { id, type } = this.$route.query;
    this.queryId = id;
    this.queryType = type;
    this.activeTabs = type === "add" ? "0" : "1";
    this.loading = true;
    if (this.queryType === "add") {
      this.sitem = {};
      this.getNewTime();
    } else {
      this.initData();
    }
    this.loading = false;
  },
  methods: {
    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("取消");
        });
    },
    getNewTime() {
      this.newTime = new Date().valueOf() + "";
    },

    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 = {
        id: this.queryId,
        remark: remark || "",
        status: status,
      };
      const res = await asyncRequest.status(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" ? "2" : "0";
        await this.setStatus(type, "提交入库方审核", e.remark);
      }
    },
    async initData() {
      this.loading = true;
      const res = await asyncRequest.detail({ id: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        this.status = this.sitem.status;
        this.orderCode = this.sitem.orderCode;
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
      this.loading = false;
    },
    openModal() {
      this.modelId = "add";
      this.s_sitem = this.sitem;
      this.showModel = true;
    },
  },
};
</script>
   <style lang="scss" scoped>
.salesOrderDetail {
}
</style>
   
   