<script setup lang="ts">
import { mapState } from 'pinia'
import { useI18n } from 'vue-i18n'
import { ref, watch, onMounted, nextTick } from 'vue'
import logoCn from '@/assets/images/logoCn.png'
import logoEn from '@/assets/images/logoEn.png'
import { useUserStore } from '@/stores/user'
import QrcodeVue from 'qrcode.vue'
import { show, showConfirm } from '@/components/Toast'
import { openPay, queryOrderStatus, getOrderInfo } from '@/api/api'
import { useRouter, useRoute } from 'vue-router'
import { GetRequest } from '@/utils/index'
import { getCurrentInstance } from 'vue'

//得到i18n的locale token, info,
const { locale: language, t  } = useI18n()
let t1 = null
const route = useRoute()
const { isEur, info } = useUserStore()
console.log('route', route)
const orderSn = ref(route.query.id || GetRequest('orderSn'))
let { $cdn } = getCurrentInstance()?.proxy
let PAYSID = {
  wechatPay: 0,
  alipay: 1,
  paypal: 2
}
const selectedPayType = ref('alipay')
watch(selectedPayType, () => {
  getCode()
})
const qrCodeUrl = ref('')
const orderDetal = ref({})
const response = ref({
  price: 0.01,
  src: ''
})
console.log('orderSn', orderSn, route)
onMounted(() => {
  getDetial()
  getCode()
})

function inverRequest() {
  clearInterval(t1)
  t1 = null
  t1 = setInterval(() => {
    handleQueryOrderStatus()
  }, 5000)
}
async function handleQueryOrderStatus() {
  if (t1) {
    let response = await getOrderInfo(orderSn.value)
    console.log('response', response)
    orderDetal.value = response
    if (response.payStatus === 0) {
      //未支付
      console.log('未支付')
    } else {
      //支付失败
      t1 = null
      t1 && clearInterval(t1)
      showConfirm({
        text: response.payStatus === 2 ? t('payInfo.payfail') : t('mall.paySuccessTip'),
        type: response.payStatus === 2 ? 'err' : 'success',
        callback: () => {
          if (response.payStatus === 2) {
            getCode()
          } else {
            window.close()
          }
        }
      })
    }
  }
}
function getDetial() {
  getOrderInfo(orderSn.value).then((res) => {
    orderDetal.value = res
  })
}
async function getCode() {
  qrCodeUrl.value = ''
  let paysidtype = {
    wechatPay: 2,
    alipay: 3,
    paypal: 0
  }
  openPay({
    orderSn: orderSn.value,
    payType: paysidtype[selectedPayType.value],
    openId: ''
  }).then((res) => {
    qrCodeUrl.value = res.qrCodeUrl
    inverRequest()
  })
}
</script>
<template>
  <div class="pcPage">
    <div class="mall-pay">
      <div class="container">
        <div class="pay-header">
          <div class="logo">
            <img :src="language === 'zh' ? logoCn : logoEn" alt="" />
          </div>
          <p>{{ $t('mall.payCenter') }}</p>
        </div>
        <div class="pay-content">
          <div class="account">
            <span class="label">{{ $t('mall.nowAccount') }}：</span
            ><span>{{ orderDetal.userName }}</span>
          </div>
          <div class="account">
            <span class="label"
              >{{ $t(`mall.${orderDetal.orderType == 'incrementOrder'?'vip':orderDetal.orderType == 'downOrder'?'downdesc':'goqy'}`) }}：
              <span>
                <span v-if="orderDetal.goodsInfo && orderDetal.goodsInfo[0]">{{orderDetal.goodsInfo[0].name}}{{
                  orderDetal.goodsInfo[0].count
                }}</span>
                <span v-else>1</span>
                <span v-if="orderDetal.orderType == 'incrementOrder'">，{{ $t(`payInfo.expiration`,{num:1}) }}</span>
                </span
              ></span
            >
          </div>
          <div class="pay-types">
            <p class="label">{{ $t('mall.payType') }}：</p>
            <div
              class="pay-type ali-pay"
              @click="selectedPayType = 'alipay'"
              :class="{ 'is-active': selectedPayType === 'alipay' }"
            >
              <img :src="`${$cdn}images/ali-pay.png`" class="t-icon" alt />
              <h-icon type="vip_true" class="select-icon"></h-icon>
              <img :src="`${$cdn}images/tag-icon.png`" class="t-click" alt />
              {{ $t('mall.aliPay') }}
            </div>
            <div
              class="pay-type wechat-pay"
              @click="selectedPayType = 'wechatPay'"
              :class="{ 'is-active': selectedPayType === 'wechatPay' }"
            >
              <img :src="`${$cdn}images/wechat-pay.png`" class="t-icon" alt />
              <h-icon type="vip_true" class="select-icon"></h-icon>
              <img :src="`${$cdn}images/tag-icon.png`" class="t-click" alt />
              {{ $t('mall.wechatPay') }}
            </div>
            <div
              v-if="isEur"
              class="pay-type paypal-pay"
              @click="selectedPayType = 'paypal'"
              :class="{ 'is-active': selectedPayType === 'paypal' }"
            >
              <img :src="`${$cdn}images/paypal.png`" class="t-icon" alt />
              <h-icon type="vip_true" class="select-icon"></h-icon>
              <img :src="`${$cdn}images/tag-icon.png`" class="t-click" alt />
              paypal
            </div>
          </div>
          <div class="pay-info">
            <p class="label">{{ $t('mall.payNum') }}：</p>
            <p class="price">￥{{ response.price }}</p>
            <div class="pay-qrcode" style="min-height: 220px">
              <QrcodeVue
                v-if="qrCodeUrl"
                :value="qrCodeUrl"
                class="enter-x flex justify-center xl:justify-start"
                :size="190"
              />
              <p>
                {{
                  $t('mall.payTip', {
                    type: selectedPayType === 'alipay' ? $t('mall.aliPay') : $t('mall.wechat')
                  })
                }}
              </p>
            </div>
            <div :class="`pay-scan-tip ${language === 'zh' ? '' : 'isEn'}`"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<style lang="less" scoped>
.mall-pay {
  padding: 61px 0 61px;
  background: #f7f7f7;
  .container {
    background: #fff;
  }
  .pay-header {
    border-bottom: 1px solid #e4e4e4;
    padding: 26px 0 21px 92px;
    font-size: 24px;
    line-height: 32px;
    .logo,
    p {
      display: inline-block;
      vertical-align: middle;
    }
    .logo {
      margin-right: 52px;
      position: relative;
      &::after {
        content: '';
        display: block;
        width: 1px;
        height: 34px;
        background: #c8c8c8;
        position: absolute;
        right: -30px;
        top: 11px;
      }
    }
  }
  .pay-content {
    padding: 0 92px 80px;
  }
  .account {
    height: 56px;
    line-height: 56px;
    font-weight: bold;
    border-bottom: 1px solid #e4e4e4;
  }
  .label {
    font-weight: bold;
    margin-right: 13px;
  }
  .pay-types {
    padding: 50px 0;
    border-bottom: 1px solid #e4e4e4;
    & > p,
    & > div {
      display: inline-block;
    }
    .pay-type {
      width: 190px;
      height: 40px;
      line-height: 40px;
      margin-right: 30px;
      border: 1px solid #909090;
      border-radius: 2px;
      padding-left: 60px;
      cursor: pointer;
      position: relative;
      &.is-active {
        border-color: @primary-color;
        &::after {
          content: '';
          display: block;
          position: absolute;
          right: 0;
          bottom: 0;
          border-bottom: 20px solid @primary-color;
          border-left: 22px solid transparent;
        }
        .t-click {
          display: block;
        }
      }
      .select-icon {
        color: #fff;
        position: absolute;
        bottom: 0;
        right: 0;
        z-index: 1;
        line-height: 1;
        font-size: 12px;
      }
      .t-icon {
        width: 30px;
        height: 30px;
        padding: 6px;
        left: 0;
        position: absolute;
      }
      .t-click {
        position: absolute;
        right: 0;
        bottom: 0;
        display: none;
        display: none;
      }
    }
    .pay-icon {
      position: absolute;
      left: 4px;
      top: 0;
      height: 40px;
      width: 34px;
      display: inline-block;
      background: url(~@/assets/images/refactor/mall/ali-icon.png) no-repeat center center;
      background-size: 34px 34px;
      &.wechat-icon {
        background: url(~@/assets/images/refactor/mall/wechat-icon.png) no-repeat center center;
        background-size: 28px 25px;
      }
    }
  }
  .pay-info {
    padding-top: 24px;
    position: relative;
    & > p {
      display: inline-block;
      vertical-align: middle;
    }
    .price {
      color: @primary-color;
      font-size: 40px;
      line-height: 50px;
    }
  }
  .pay-qrcode {
    padding: 60px 0 0 337px;
    img {
      width: 190px;
      height: 190px;
      margin-bottom: 7px;
      display: block;
    }
  }
  .pay-scan-tip {
    width: 186px;
    height: 165.5px;
    background: url(@/assets/images/scan-tip.png) no-repeat center center;
    background-size: contain;
    position: absolute;
    top: 113px;
    left: 562px;
    &.isEn {
      background-image: url(@/assets/images/scan-tip-en.png);
    }
  }
}
</style>
