<template>
  <div class="salesOrderDetail pagePadding">
    <div style="width: 100%" v-if="powers.some((i) => i == '007')">
      <div style="height: 30px" v-if="queryType !== 'add'">
        <span>订单编号：</span><span v-if="sitem">{{ sitem.orderCode }}</span>
        <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="
                      (
                        xs_order_type_options.find(
                          (item) => item.id == sitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="order_source">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        xs_order_source_options.find(
                          (item) => item.id == sitem.order_source
                        ) || {}
                      ).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' || sitem.order_type === '2'"
                    ><span
                      >{{ sitem.is_activity === "1" ? "参与活动" : "不参与活动" }}/{{
                        (
                          goodtype_options.find(
                            (item) => item.value == sitem.good_type
                          ) || {}
                        ).label || "--"
                      }}</span
                    ></span
                  >
                  <span v-else>--</span>
                </template>
                <template slot="proof_id">
                  <el-popover
                    placement="top"
                    width="300"
                    trigger="hover"
                    v-if="sitem.proof_type === '1'"
                  >
                    <video width="275" controls class="fl">
                      <source :src="sitem.proof_url" type="video/mp4" />
                      <source :src="sitem.proof_url" type="video/avi" />
                      您的浏览器不支持Video标签。
                    </video>
                    <i
                      class="el-icon-video-camera-solid hover"
                      style="font-size: 18px"
                      slot="reference"
                    ></i>
                  </el-popover>

                  <img
                    v-else-if="sitem.proof_type === '2'"
                    style="width: 28px; height: 28px"
                    :src="sitem.proof_url"
                    class="avatar fl hover"
                    v-viewer
                  />

                  <a
                    v-else-if="sitem.proof_type === '3'"
                    :href="sitem.proof_url"
                    download="凭证文件"
                    >点击下载</a
                  >
                  <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-tab-pane label="流程图" name="3" v-if="queryType !== 'add'">
          <flow-chart process_id="4" type="XSQRD" :orderCode="queryId" />
       </el-tab-pane>
      </el-tabs>


      <!-- <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 { showColumns } from "./components/ShowDataTableColumns";
import setPartial from "./setPartial";
import { xs_order_source_options, xs_order_type_options } from "@/assets/js/statusList";
export default {
  name: "salesOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    addForm,
    orderOutTable,
    setPartial,
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "salesOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      finishStatus: "finish",
      activeTabs: "1",
      activeNames: ["-1", "0", "1", "2", "3", "4", "5", "6"],
      showColumns: showColumns,
      setShowModel: false,
      goodtype_options: [
        {
          value: "1",
          label: "常规商品",
        },
        {
          value: "2",
          label: "赠品",
        },
        {
          value: "3",
          label: "样品",
        },
      ],
      xs_order_type_options,
      xs_order_source_options,
      statusOptions: [
        { value: "0", label: "待发货" },
        { value: "1", label: "待发货完成" },
        { value: "2", label: "发货已完成" },
        { value: "3", label: "订单已取消" },
      ],
      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;
    },
  },
};
</script>
<style lang="scss" scoped>
.salesOrderDetail {
}
</style>
