<template>
  <div class="sellOutOrderDetail pagePadding">
    <div style="width: 100%; padding: 0 0 200px 0" v-if="powers.some((i) => i == '007')">
      <div class="detail-page-title">
        <span>发货申请单编号：</span><span v-if="sitem">{{ sitem.outCode }}</span>
        <!-- <el-button
          class="fr"
          v-if="
            (status + '' === '2' || status + '' === '3') &&
            powers.some((i) => i == '049') &&
            sitem &&
            sitem.order_source !== '6'
          "
          type="primary"
          plain
          :size="'mini'"
          style="margin: 0 0 0 10px"
          @click="wellReturnCode"
          >新建售后申请单
        </el-button> -->
        <!-- <el-button
          class="fr"
          v-if="status + '' === '2' && ppowers.some((i) => i == '2')"
          type="primary"
          plain
          :size="'mini'"
          style="margin: 0 0 0 10px"
          @click="wentReceive"
          >确认收货
        </el-button> -->
        <!-- <el-button
          class="fr"
          v-if="Number(status) >= 2"
          type="primary"
          plain
          :size="'mini'"
          style="margin: 0 0 0 10px"
          @click="handleShare"
          >分享
        </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="showColumns"
              >
                <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="
                      (
                        xs_order_type_options.find(
                          (item) => item.id == sitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="sale_price">
                  <!-- <span>{{
                    private_field &&
                    private_field.length > 0 &&
                    private_field.some((item) => item == "2")
                      ? sitem.sale_price
                      : "***"
                  }}</span> -->
                  {{displayPrivateField(isDisplayPrivateField('2'), sitem.sale_price)}}
                </template>
                <template slot="total_price">
                  <!-- <span>{{
                    private_field &&
                    private_field.length > 0 &&
                    private_field.some((item) => item == "2")
                      ? sitem.total_price
                      : "***"
                  }}</span> -->
                  {{displayPrivateField(isDisplayPrivateField('2'), sitem.total_price)}}
                </template>

                <!-- <template slot="saleinfo">
                  <el-table :data="sitem.saleinfo" size="mini" border>
                    <el-table-column prop="bnCode" label="批次号" width="160" show-overflow-tooltip/>
                    <el-table-column prop="wsm_code" label="仓库编号" width="170" show-overflow-tooltip/>
                    <el-table-column prop="wsm_name" label="仓库名称" width="260" show-overflow-tooltip/>
                    <el-table-column prop="num" label="批次数量" show-overflow-tooltip/>
                    <el-table-column prop="send_num" label="可发货数量" width="100px" show-overflow-tooltip/>
                    <el-table-column prop="th_num" label="退货数量" show-overflow-tooltip/>
                    <el-table-column prop="origin_price" label="成本" show-overflow-tooltip/>
                  </el-table>
                </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"
              v-if="
                status + '' === '1' &&
                ppowers.some((i) => i == '1') &&
                sitem &&
                (sitem.order_type + '' === '1' ||
                  (currentLevel + '' === '3' &&
                    (sitem.order_type + '' === '2' ||
                      sitem.order_type + '' === '3' ||
                      sitem.order_type + '' === '4')))
              "
            >
              <want-deliver
                :newTime="newTime"
                v-if="newTime !== ''"
                :sitem="sitem"
                @refresh="initData"
              />
            </el-collapse-item> -->

            <el-collapse-item
              title="售后申请单结果"
              name="4"
              v-if="
                returnCode && returnCode_type && sitem && sitem.order_return && resitem
              "
            >
              <show-data-table
                :newTime="newTime"
                v-if="newTime !== '' && resitem"
                :sitem="resitem"
                :border="
                  !(
                    (resitem.is_receive + '' === '1' && returnCode_type + '' === '4') ||
                    (resitem.is_receive + '' === '1' && returnCode_type + '' === '5')
                  )
                "
                :columns="rshowColumns"
              >
                <template slot="status">
                  <el-tag
                    :size="'mini'"
                    :type="resitem.status == '0' ? 'warning' : ''"
                    v-text="
                      (
                        (resitem.is_receive === '0'
                          ? status2Options
                          : status1Options
                        ).find((item) => item.id + '' === resitem.status + '') || {}
                      ).label || '--'
                    "
                  ></el-tag>
                  <!-- <el-tooltip
                    v-if="
                      resitem.status + '' === '3' &&
                      resitem.is_receive + '' === '0' &&
                      powers.some((i) => i == '026')
                    "
                    class="fr"
                    effect="dark"
                    content="同意退货"
                    style="padding: 3px 0 0 0"
                    placement="top"
                  >
                    <i class="el-icon-video-play tb-icon" @click="agree_return"></i>
                  </el-tooltip> -->
<!-- 
                  <el-tooltip
                    class="fr"
                    effect="dark"
                    content="取消申请"
                    style="padding: 3px 0 0 0"
                    v-if="
                      (resitem.status + '' === '1' ||
                        resitem.status + '' === '2' ||
                        resitem.status + '' === '3' ||
                        resitem.status + '' === '4') &&
                      powers.some((i) => i == '027')
                    "
                    placement="top"
                  >
                    <i class="el-icon-circle-close tb-icon" @click="close_return"></i>
                  </el-tooltip> -->
                  <el-tooltip
                    class="fr"
                    effect="dark"
                    :content="
                      resitem.is_receive + '' === '1'
                        ? '换货'
                        : resitem.is_receive + '' === '0'
                        ? '补发'
                        : '--'
                    "
                    style="padding: 3px 0 0 0"
                    v-if="
                      resitem.status + '' === '5' &&
                      resitem.is_reissue + '' === '0' &&
                      resitem.except_code + '' === '2' &&
                      powers.some((i) => i == '091')
                    "
                    placement="top"
                  >
                    <i class="el-icon-shopping-cart-2 tb-icon" @click="reissue"></i>
                  </el-tooltip>
                </template>
                <template slot="order_type">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        xs_order_type_options.find(
                          (item) => item.id == resitem.order_type
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="is_receive">
                  <el-tag :size="'mini'" :type="''">{{
                    resitem.is_receive + '' === "1"
                      ? "销售订单退货，采购订单也退货"
                      : resitem.is_receive + '' === "0"
                      ? "销售订单退货，采购订单不退货(货物已丢失，且无法找回，由业务公司承担)"
                      : "--"
                  }}</el-tag>
                </template>
                <template slot="except_code">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (
                        expect_options.find(
                          (item) => item.value == resitem.except_code
                        ) || {}
                      ).label || '--'
                    "
                  ></el-tag>
                </template>

                <template slot="error_img">
                  <img
                    class="fl hover"
                    style="width: 23px; height: 23px; margin: 0 5px 0 0"
                    v-viewer
                    v-for="(si, i) in resitem.error_img"
                    :src="si"
                    :key="si + i"
                  />
                </template>
              </show-data-table>
              <!-- <show-data-table
                :newTime="newTime"
                v-if="
                  resitem &&
                  resitem.is_receive + '' === '1' &&
                  (returnCode_type + '' === '4' || returnCode_type + '' === '5')
                "
                :sitem="resitem"
                border
                :columns="returnShowColumns"
              >
                <template slot="is_th">
                  <el-tag
                    :size="'mini'"
                    v-text="
                      (is_th_options.find((item) => item.value + '' == resitem.is_th + '') || {})
                        .label || '--'
                    "
                  ></el-tag>
                </template>
                <template slot="addr">
                  <span
                    >{{ resitem.contactor }}---{{ resitem.mobile }}---{{ resitem.addr_cn
                    }}{{ resitem.addr }}</span
                  >
                </template>
              </show-data-table>
              <show-data-table
                :newTime="newTime"
                v-if="
                  newTime !== '' &&
                  resitem &&
                  resitem.is_receive + '' === '1' &&
                  returnCode_type + '' === '5'
                "
                :sitem="resitem"
                border
                :columns="returnShowResColumns"
              >
              </show-data-table> -->
              <return-express
                :sitem="resitem"
                :newTime="newTime"
                v-if="newTime !== '' && returnCode_type + '' === '4'"
                @refresh="initData"
              />
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="审批记录" name="2">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'CKD'"
            :orderCode="queryId"
          />
        </el-tab-pane>
        <el-tab-pane
          label="物流进度"
          name="3"
          v-if="newTime !== '' && (status + '' === '2' || status + '' === '3')"
        >
          <logis-time-line v-if="newTime !== ''" :orderCode="sitem.post_code" />
        </el-tab-pane>

        <el-tab-pane label="流程图" name="4" v-if="queryType !== 'add'">
          <flow-chart process_id="5" type="CKD" :orderCode="queryId" />
        </el-tab-pane>
      </el-tabs>
      <div>
        <add-Edit-A
          :id="'add'"
          :sitem="sitem"
          :show-model="showModel"
          :is-detail="false"
          @refresh="initData"
          @cancel="showModel = false"
        />
      </div>
    </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/supplierSellOut/supplierSellOutOrder";
import { showColumns } from "./columns";
import { sellshowColumns } from "@/views/sellOut/sellAfterApply/columns";
import { xs_order_type_options } from "@/assets/js/statusList";
import { mapGetters } from "vuex";
import { shareWebUrl } from "@/config";
import returnExpress from "./components/returnExpress";
import wantDeliver from "./components/want-deliver";
import addEditForm from "./components/addEditForm"; //发货申请单详情
import addEditA from "./components/addEditA";
import privateField from "@/mixins/privateField";

export default {
  name: "sellOutOrderDetail",
  mixins: [mixinPage, resToken, privateField],
  components: {
    addEditForm,
    addEditA,
    wantDeliver,
    returnExpress,
  },
  computed: {
    ...mapGetters(["private_field", "currentLevel"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "supplierSellOutOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "CKD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      xs_order_type_options,
      returnCode: "",
      returnCode_type: "",
      activeTabs: "1",
      activeNames: ["0", "1", "2", "3", "4"],
      status: "", //存储详情接口状态
      statusList: [],
      showColumns: showColumns,
      rshowColumns: sellshowColumns,
      returnShowColumns: [
        {
          prop: "is_th",
          label: "物流承担方",
          _slot_: "is_th",
          span: 6,
        },
        {
          prop: "addr",
          label: "收货地址",
          _slot_: "addr",
          span: 18,
        },
      ],
      returnShowResColumns: [
        {
          prop: "post_fee",
          label: "退货物流费用",
          span: 6,
        },
        {
          prop: "post_company",
          label: "退货物流公司",
          span: 6,
        },
        {
          prop: "post_code",
          label: "退货物流单号",
          span: 12,
        },
      ],
      sitem: null,
      resitem: null,
      newTime: "",
      showModel: false,
      loading: false,
      orderOptions: [
        { id: "1", label: "线上商品销售" },
        { id: "2", label: "项目线上商品销售" },
        { id: "3", label: "竞价单销售" },
        { id: "4", label: "项目竞价单销售" },
        { id: "5", label: "平台部订单销售" },
      ],
      statusOptions: [
        { value: "0", label: "待采购备货" },
        { value: "1", label: "待库管发货" },
        { value: "2", label: "已发货待收货" },
        { value: "3", label: "已收货" },
        { value: "4", label: "已全部退货" },
      ],
      // 收到货状态
      status1Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "待采购审核" },
        { id: "3", label: "待设置仓库" },
        { id: "4", label: "待客户退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        { id: "7", label: "采购已驳回" },
        { id: "8", label: "申请已取消" },
      ],
      // 未收到货状态
      status2Options: [
        { id: "1", label: "待业务审核" },
        { id: "2", label: "待采购审核" },
        { id: "3", label: "待同意退货" },
        { id: "5", label: "售后已完成" },
        { id: "6", label: "业务已驳回" },
        { id: "7", label: "采购已驳回" },
        { id: "8", label: "申请已取消" },
      ],
      expect_options: [],
      expect_options1: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "换货",
        },
      ],
      expect_options0: [
        {
          value: "1",
          label: "退货",
        },
        {
          value: "2",
          label: "补发",
        },
      ],
      is_th_options: [
        {
          value: "1",
          label: "收货人承担",
        },
        {
          value: "2",
          label: "业务公司承担",
        },
        {
          value: "3",
          label: "供应商承担",
        },
      ],
      queryId: "",
      did: "", //详情页上的id
    };
  },
  mounted() {
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    handleShare() {
      const url = shareWebUrl + this.sitem.outCode;
      const input = document.createElement("input");
      document.body.appendChild(input);
      input.value = url;
      input.select();
      document.execCommand("Copy");
      this.$message.success("复制成功");
      document.body.removeChild(input);
    },
    async updateStatus(e) {
      await this.setstatus(e.status);
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async setstatus(status) {
      const model = {
        id: this.did,
        status: status,
      };
      let res = await asyncRequest.status(model);
      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);
      }
    },
    async reissue() {
      const { is_receive } = this.resitem;
      await this.$confirm(`确定要${is_receive + '' === "1" ? "换货" : "补发"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            returnCode: this.returnCode,
          };
          const res = await asyncRequest.createSaleAndCgdByAfter(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: `${is_receive + '' === "1" ? "换货" : "补发"}成功！`,
              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("取消");
        });
    },
    async wentReceive() {
      await this.$confirm("确定要确认收货？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            outCode: this.sitem.outCode,
          };
          const res = await asyncRequest.saleoutreceipt(model);
          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("取消");
        });
    },
    async initData(e) {
      this.returnCode = "";
      this.returnCode_type = "";
      let model = {
        outCode: this.queryId,
      };
      const res = await asyncRequest.detail(model);
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { status, can, id, order_return } = this.sitem;
        this.status = status;
        this.did = id;
        if (order_return) {
          this.returnCode = order_return.returnCode;
          this.returnCode_type = order_return.status;
          await this.receive_initData(this.returnCode);
        }
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    wellReturnCode() {
      console.log(this.returnCode_type);
      if (
        this.returnCode_type + '' === "1" ||
        this.returnCode_type + '' === "2" ||
        this.returnCode_type + '' === "3"
      ) {
        this.$message.warning("当前售后申请单未完成审批！");
        return;
      }
      this.showModel = true;
    },
    async receive_initData(code) {
      let model = {
        returnCode: code,
      };
      const res = await asyncRequest.afterinfo(model);
      if (res && res.code === 0 && res.data) {
        this.resitem = res.data;
        this.resitem.error_img = this.resitem.error_img.split(",");
        this.expect_options = JSON.parse(
          JSON.stringify(
            this.resitem.is_receive + '' === "1" ? this.expect_options1 : this.expect_options0
          )
        );
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async agree_return() {
      await this.$confirm("确定要同意退货？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            returnCode: this.returnCode,
            status: "5",
            remark: "",
            is_post: "",
          };
          const res = await asyncRequest.afterstatus(model);
          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("取消");
        });
    },
    async close_return() {
      await this.$confirm("确定要取消申请？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            returnCode: this.returnCode,
          };
          const res = await asyncRequest.aftercancel(model);
          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>
