<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, getRemark } from '@/utils/index'
import { paysidtype } from '@/utils/status'
import { getCurrentInstance } from 'vue'

//得到i18n的locale token, info,
const { locale: language, t } = useI18n()
let t1 = null
const route = useRoute()
const { isEur, info } = useUserStore()
const orderSn = ref(route.query.id || GetRequest('orderSn'))
let { $cdn } = getCurrentInstance()?.proxy

const selectedPayType = ref(isEur ? 'paypal' : 'alipay')
watch(selectedPayType, () => {
  getCode()
})
const qrCodeUrl = ref('')
const orderDetal = ref({})
const response = ref({
  price: 0.01,
  src: ''
})
onMounted(() => {
  // getDetial()
})

function inverRequest() {
  clearInterval(t1)
  t1 = null
  t1 = setInterval(() => {
    handleQueryOrderStatus()
  }, 5000)
}
async function handleQueryOrderStatus() {
  if (t1) {
    let response = await getOrderInfo(orderSn.value)
    orderDetal.value = response
    if (response.payStatus === 0) {
      //未支付
    } else {
      //支付失败
      t1 = null
      t1 && clearInterval(t1)
      showConfirm({
        text: response.autoPay
          ? t('payInfo.autoPayTips', {paytype: response.payType == 5 ? 'PayPal' : 'Stripe'})
          : 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
    if (isEur) {
      selectedPayType.value = orderDetal.value.payType == 6 ? 'stripe' : 'paypal'
    }
    getCode()
  })
}
getDetial()
function handlePay() {
  if (isEur) {
    //打开一个不被拦截的新窗口
    // var win = window.open();
    // win.document.body.innerHTML="loading......";
    openPay({
      orderSn: orderSn.value,
      payType: paysidtype[selectedPayType.value],
      productName: getRemark(orderDetal.value.goodsInfo, orderDetal.value.orderType, t),
      openId: ''
    }).then((res) => {
      qrCodeUrl.value = res.qrCodeUrl || res.redirect
      window.location.href = qrCodeUrl.value
    })
  } else {
    window.open(qrCodeUrl.value)
  }
}
async function getCode() {
  inverRequest()
  if (orderDetal.value.payType == 5 || orderDetal.value.payType == 6) return
  qrCodeUrl.value = ''
  openPay({
    orderSn: orderSn.value,
    payType: paysidtype[selectedPayType.value],
    productName: getRemark(orderDetal.value.goodsInfo, orderDetal.value.orderType, t),
    openId: ''
  }).then((res) => {
    qrCodeUrl.value = res.qrCodeUrl || res.redirect
  })
}
</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 == 'cooperationOrder'
                      ? 'cooperationOrder'
                      : orderDetal.orderType == 'downOrder'
                      ? 'downdesc'
                      : 'goqy'
                  }`
                )
              }}：
              <span
                v-for="(item, index) in orderDetal.goodsInfo"
                :key="index"
                style="margin-right: 10px"
              >
                <span v-if="item">
                  <span v-if="index != 0">、</span>

                  <span style="margin-right: 5px" v-if="orderDetal.orderType == 'cooperationOrder'">
                    {{ $t('mall.cooperationOrderName', { count: item.count }) }}
                  </span>
                  <span
                    style="margin-right: 5px"
                    v-else-if="orderDetal.orderType != 'incrementOrder'"
                  >
                    {{
                      orderDetal.orderType == 'downOrder'
                        ? `${item.name} (${item.sceneNum})`
                        : language === 'zh'
                        ? item.name
                        : item.nameEn
                    }}
                  </span>
                  <span style="margin-right: 5px" v-else>
                    {{
                      item.type == 1
                        ? $t('payInfo.gjhy')
                        : isEur
                        ? $t('payInfo.zyhys')
                        : $t('payInfo.zyhy')
                    }}
                  </span>
                  <span
                    v-if="
                      orderDetal.orderType != 'downOrder' &&
                      orderDetal.orderType != 'cooperationOrder'
                    "
                    >× {{ item.count }}</span
                  >
                </span>
                <!-- <span v-else>1</span> -->
                <span v-if="orderDetal.orderType == 'incrementOrder'"
                  >，{{
                    item.type == 1
                      ? $t(`payInfo.expirationmon`, { num: item.monthQy || 1 })
                      : $t(`payInfo.expiration`, { num: 1 })
                  }}</span
                >
              </span></span
            >
          </div>
          <div class="pay-types">
            <p class="label">{{ $t('mall.payType') }}：</p>
            <div
              v-if="!isEur"
              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="@/assets/images/vip_true.svg" class="t-click" alt />
              {{ $t('mall.aliPay') }}
            </div>
            <div
              v-if="!isEur"
              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="@/assets/images/vip_true.svg" 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
                style="height: 26px; width: 100px"
                src="@/assets/images/paypal.png"
                class="t-icon"
                alt
              />
              <h-icon type="vip_true" class="select-icon"></h-icon>
              <img src="@/assets/images/vip_true.svg" class="t-click" alt />
            </div>

            <div
              v-if="isEur"
              class="pay-type paypal-pay"
              @click="selectedPayType = 'stripe'"
              :class="{ 'is-active': selectedPayType === 'stripe' }"
            >
              <img
                style="height: 26px; width: 62px"
                src="@/assets/images/stripe.png"
                class="t-icon"
                alt
              />
              <h-icon type="vip_true" class="select-icon"></h-icon>
              <img src="@/assets/images/vip_true.svg" class="t-click" alt />
            </div>
          </div>
          <div class="pay-info">
            <p class="label">{{ $t('mall.payNum') }}：</p>
            <p class="price">{{ isEur ? '$' : '￥' }}{{ orderDetal.orderMoney }}</p>
            <div class="pay-qrcode" style="min-height: 220px" v-if="!isEur">
              <QrcodeVue
                v-if="qrCodeUrl"
                :value="qrCodeUrl"
                class="enter-x flex justify-center xl:justify-start"
                :size="185"
              />
              <p>
                {{
                  $t('mall.payTip', {
                    type: selectedPayType === 'alipay' ? $t('mall.aliPay') : $t('mall.wechat')
                  })
                }}
              </p>
            </div>
            <div v-else class="payment" @click="handlePay">
              {{ orderDetal.autoPay == 1 ? $t('payInfo.autoRenew') : $t('payInfo.payOrder') }}
            </div>
            <div v-show="!isEur" :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;
          z-index: 1;
        }
      }
      .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: #ff0000;
      font-size: 40px;
      line-height: 50px;
    }
  }
  .pay-qrcode {
    padding: 60px 0 0 0px;
    text-align: center;
    img {
      width: 190px;
      height: 190px;
      margin-bottom: 7px;
      display: block;
    }
  }
  .payment {
    width: 216px;
    height: 55px;
    line-height: 55px;
    background: #29b2ff;
    margin: 60px auto 0 auto;
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    text-align: center;
    font-size: 16px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #ffffff;
    cursor: pointer;
  }
  .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: calc(50% + 40px + 92px);
    &.isEn {
      background-image: url(@/assets/images/scan-tip-en.png);
    }
  }
}
</style>
