<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <view class="Mall4j page-scan-pick-box">
    <view class="prod">
      <view class="prod-box">
        <view
          v-for="(prodItem, index) in orderInfo.orderItems"
          :key="index"
          class="prod-item"
        >
          <view class="prod-common">
            <view class="prod-img-image">
              <img-show :src="prodItem.pic" />
            </view>
            <view class="prod-info">
              <view class="prod-name-price">
                <view class="name">
                  {{ prodItem.prodName }}
                </view>
                <view class="price">
                  ￥<text class="price-big">
                    {{
                      parsePrice(prodItem.price)[0]
                    }}
                  </text>{{ '.' + (parsePrice(prodItem.price)[1] || '00') }}
                </view>
              </view>
              <view class="prod-sku">
                <view
                  v-if="prodItem.skuName"
                  class="sku-name"
                >
                  {{
                    prodItem.skuName
                  }}
                </view>
                <view class="total">
                  {{ '×' + prodItem.prodCount }}
                </view>
              </view>
              <view class="prod-stutus">
                <view
                  v-if="orderInfo.orderType !== 0"
                  class="prod-type-item"
                >
                  {{
                    ['', $t('tuanGou'), $t('miaoSha')][orderInfo.orderType]
                  }}
                </view>
                <view
                  v-if="prodItem.returnMoneySts && prodItem.returnMoneySts > 0"
                  class="prod-stutus-item"
                >
                  {{ orderStatus[prodItem.returnMoneySts + ''] }}
                </view>
                <view
                  v-if="orderInfo.orderMold === 1"
                  class="prod-type-item"
                >
                  {{ $t('xuNi') }}
                </view>

                <view
                  v-if="
                    util.isAuthorities('admin:orderRefund:update') &&
                      orderInfo.refundType === 2 &&
                      prodItem.refundId
                  "
                  class="refund-btn"
                  @tap="toRefund(prodItem.refundId)"
                >
                  {{ $t('tuiKuanXinXi') }}
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="order-info">
      <view class="order-info-item">
        <text class="title">
          {{ $t('dingDanBianHao-0') }}
        </text>
        <text class="text">
          {{ orderInfo.orderNumber }}
        </text>
        <view
          class="copy"
          @tap="copy"
        >
          {{ $t('fuZhi') }}
        </view>
      </view>
      <view class="order-info-item">
        <text class="title">
          {{ $t('xiaDanShiJian') }}
        </text>
        <text class="text">
          {{ orderInfo.createTime }}
        </text>
      </view>
      <view class="order-info-item">
        <text class="title">
          {{ $t('dingDanLeiXing') }}
        </text>
        <text class="text">
          {{
            ['普通订单', '团购订单', '秒杀订单'][orderInfo.orderType]
          }}
        </text>
      </view>
      <view class="order-info-item">
        <text class="title">
          {{ $t('fuKuanFangShi') }}
        </text>
        <text
          v-if="orderInfo.payType === null || orderInfo.status === 1"
          class="text"
        >
          {{ $t('daiZhiFu') }}
        </text>
        <text
          v-else
          class="text"
        >
          {{
            [
              $t('jiFenZhiFu') ,
              $t('weiXinXiaoChengXuZhiFu') ,
              $t('zhiFuBaoPcDuanZhiFu') ,
              $t('weiXinSaoMaZhiFu') ,
              $t('weiXinH5ZhiFu') ,
              $t('weiXinGongZhongHaoZhiFu') ,
              $t('zhiFuBaoH5ZhiFu') ,
              $t('zhiFuBaoAppZhiFu') ,
              $t('weiXinAppZhiFu') ,
              $t('yongHuYuezhiFu') ,
              $t('paypalZhiFu')
            ][orderInfo.payType]
          }}
        </text>
      </view>
      <view
        v-if="orderInfo.payTime"
        class="order-info-item"
      >
        <text class="title">
          {{ $t('zhiFuShiJian') }}
        </text>
        <text class="text">
          {{ orderInfo.payTime }}
        </text>
      </view>
      <view
        v-if="orderInfo.nickName"
        class="order-info-item"
      >
        <text class="title">
          {{ $t('maiJiaNiCheng') }}
        </text>
        <text class="text">
          {{ orderInfo.nickName }}
        </text>
      </view>
      <view
        v-if="orderInfo.remarks"
        class="order-info-item"
      >
        <text class="title">
          {{ $t('maiJiaBeiZhu-0') }}
        </text>
        <text class="text">
          {{ orderInfo.remarks }}
        </text>
      </view>
      <view
        v-if="orderInfo.orderMold === 1"
        class="order-info-item"
      >
        <text class="title">
          {{ $t('heXiaoCiShu') }}
        </text>
        <text
          v-if="orderInfo.writeOffNum === -1"
          class="text"
        >
          {{ $t('duoCiHeXiao') }}
        </text>
        <text
          v-if="orderInfo.writeOffNum === 1"
          class="text"
        >
          {{ $t('danCiHeXiao') }}
        </text>
      </view>
      <view
        v-if="orderInfo.orderMold === 1"
        class="order-info-item"
      >
        <text class="title">
          {{ $t('heXiaoKaiShiShiJian') }}
        </text>
        <text class="text">
          {{ orderInfo.writeOffStart }}
        </text>
      </view>
      <view
        v-if="orderInfo.orderMold === 1"
        class="order-info-item"
      >
        <text class="title">
          {{ $t('heXiaoJieShuShiJian') }}
        </text>
        <text class="text">
          {{ orderInfo.writeOffEnd || $t('wu') }}
        </text>
      </view>
    </view>
    <view
      v-if="virtualRemarkList.length > 0"
      class="detail"
    >
      <view class="module-title">
        {{ $t('quanBuLiuYan') }}
      </view>
      <view
        v-for="(item, index) in virtualRemarkList"
        :key="index"
        class="detail-item"
      >
        <view class="detail-item-title">
          {{ item.name + ':' }}
        </view>
        <view class="detail-item-content">
          {{ item.value }}
        </view>
      </view>
    </view>
    <view
      v-if="
        util.isAuthorities('order:virtual:update') &&
          orderInfo.orderMold == 1 &&
          (orderInfo.status === 3 || orderInfo.status === 5) &&
          (!orderInfo.refundStatus || orderInfo.refundStatus > 2) &&
          orderInfo.writeOffCodes &&
          orderInfo.writeOffCodes.length > 0
      "
      class="pick-btn"
      @tap="pickUpGoods"
    >
      {{ $t('queDingHeXiao') }}
    </view>
    <view
      v-if="
        util.isAuthorities('admin:station:orderItemsDelivery') &&
          orderInfo.orderMold == 0 && orderInfo.status == 2
      "
      class="pick-btn"
      @tap="pickUpGoods"
    >
      {{ $t('tiHuo') }}
    </view>
  </view>
</template>

<script setup>
import util from '@/utils/util'
// 订单编号
let pickCode = ''
// 订单信息
const orderInfo = ref({})
// 用户留言列表
const virtualRemarkList = ref([])

onLoad((options) => {
  const _pickCode = options.pickCode
  if (_pickCode) {
    pickCode = _pickCode
  }
  getPickGoodsInfo()
})
onShow(() => {
  uni.setNavigationBarTitle({
    title: $t('heXiaoDingDan')
  })
})

// 获取商品信息
const getPickGoodsInfo = () => {
  uni.showLoading()
  const param = {
    url: '/order/order/getOrderByCode',
    method: 'GET',
    data: {
      code: pickCode
    },
    hasCatch: true
  }
  http.request(param).then(({ data: res }) => {
    orderInfo.value = res
    const _virtualRemarkList = JSON.parse(res.virtualRemark) || []
    virtualRemarkList.value = _virtualRemarkList.filter(item => {
      return item.value
    })
    uni.hideLoading()
  }).catch((data) => {
    uni.hideLoading()
    uni.showModal({
      title: $t('tiShi'),
      content: data.msg,
      showCancel: false,
      confirmText: $t('queDing'),
      success: (res) => {
        if (res.confirm) {
          uni.switchTab({
            url: '/pages/index/index'
          })
        }
      }
    })
  })
}

// 确定核销 或 提货
const pickUpGoods = () => {
  uni.showLoading()
  // orderMold 1虚拟订单(核销)   0普通订单(提货)
  const orderMold = orderInfo.value.orderMold
  const param = {
    url: orderMold === 1 ? '/admin/station/orderWriteOffByOrderNumber' : '/admin/station/changeStatusAndOrderStation',
    method: 'PUT',
    data: {
      orderNumber: orderInfo.value.orderNumber
    },
    hasCatch: true
  }
  http.request(param).then(() => {
    uni.hideLoading()
    uni.showToast({
      title: $t('caoZuoChengGong-0'),
      icon: 'none'
    })
    // 提示信息结束后，更新订单信息
    setTimeout(() => {
      uni.switchTab({
        url: '/pages/index/index'
      })
    }, 1500)
  }).catch((res) => {
    uni.hideLoading()
    uni.showToast({
      title: res.data.msg,
      icon: 'none'
    })
  })
}

const parsePrice = (price) => {
  if (price.toString().indexOf('.') > -1) {
    return price.toString().split('.')
  }
  return [price]
}
// 复制文本
const copy = () => {
  util.uniCopy({
    content: orderInfo.value.orderNumber,
    success: (res) => {
      uni.showToast({
        title: res,
        icon: 'none'
      })
    },
    error: (e) => {
      uni.showToast({
        title: e,
        icon: 'none',
        duration: 3000
      })
    }
  })
}

</script>

<style lang="scss" scoped>
@use './scan-pick.scss';
</style>
