<template>
  <div class="pay-layout">
    <div class="pay-header">
      <div class="h-left">
        <img :src="language==='en'?`${$cdn}images/logo-black-en.png`:`${$cdn}images/logo-black.png`" alt>
        <span class="h-line"></span>
        <span>{{languagePay.paycenter}}</span>
      </div>
      <div class="h-right">{{languagePay.name}}{{info.userName}}</div>
    </div>
    <div class="pay-con">
      <div class="pay-left">
        <div class="mid">
          <div class="attr">{{languagePay.method}}</div>
          <div class="body">
            <div class="pay-tag" :class="{'tag-active':paytype==='alipay'}" @click="paytype='alipay'">
              <img :src="`${$cdn}images/ali-pay.png`" class="t-icon" alt>
              <span>{{languagePay.ali}}</span>
              <img :src="`${$cdn}images/tag-icon.png`" class="t-click" alt>
            </div>
            <div class="pay-tag" :class="{'tag-active':paytype==='wechatPay'}" @click="paytype='wechatPay'">
              <img :src="`${$cdn}images/wechat-pay.png`" class="t-icon" alt>
              <span>{{languagePay.wechatpay}}</span>
              <img :src="`${$cdn}images/tag-icon.png`" class="t-click" alt>
            </div>
          </div>
        </div>
        <div class="bottom">
          <div class="attr">{{languagePay.amount}}</div>
          <div class="price-detail">
            <img class="scanicon" :src="language==='en'?`${$cdn}images/scanicon-en.png`:`${$cdn}images/scanicon.png`" alt="">
            <div class="price">￥{{response.price}}</div>
            <img v-if="response.src" :src="`${$serverName}${response.src}`" alt="">
            <div class="dec">{{languagePay.use1}}<span style="color:#ff0000">{{payZH}}</span>{{languagePay.use2}}</div>
          </div>
        </div>
      </div>
      <!-- <div class="pay-right">
        <p class="p-title">功能卓越</p>
        <ul class="pay-icon">
          <li v-for="(item,i) in payicon" :key="i">
            <img :src="item.img" >
            <p>{{item.name}}</p>
          </li>
        </ul>
      </div> -->
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
let payTypes = {
  wechatPay: '微信',
  alipay: '支付宝',
  paypal: 'paypal'
}

let payTypesEn = {
  wechatPay: 'Wechat',
  alipay: 'Alipay',
  paypal: 'paypal'
}

let PAYS = {
  0: 'wechatPay',
  1: 'alipay',
  2: 'paypal'
}

let PAYSID = {
  wechatPay: 0,
  alipay: 1,
  paypal: 2
}
export default {
  data () {
    let payicon = [{
      img: this.$cdn + 'images/paylogo1.png',
      name: '轻松操作高效建模'
    }, {
      img: this.$cdn + 'images/paylogo2.png',
      name: '高精还原空间漫游'
    }, {
      img: this.$cdn + 'images/paylogo3.png',
      name: '室内外皆可拍摄'
    }, {
      img: this.$cdn + 'images/paylogo4.png',
      name: '自由编辑随时更新'
    }]
    return {
      payicon,
      privilege: 'year',
      response: '',
      t1: null,
      paytype: PAYS[this.$route.params.payType]
    }
  },
  computed: {
    ...mapState({
      language: state => state.language.current,
      languagePay: state => state.language.home.paypage,
      token: state => state.user.token,
      info: state => state.user.info,
      langToast: state => state.language.home.toast
    }),
    payZH () {
      return this.language === 'en' ? payTypesEn[this.paytype] : payTypes[this.paytype]
    }
  },
  watch: {
    paytype (newVal, oldVal) {
      this.getCode()
    }
  },
  mounted () {
    this.getCode()
  },
  methods: {
    inverRequest () {
      clearInterval(this.t1)
      this.t1 = null
      this.t1 = setInterval(() => {
        this.queryOrderStatus()
      }, 5000)
    },
    async queryOrderStatus () {
      if (this.t1) {
        let {orderSn, orderType} = this.$route.params
        if (!orderSn) {
          return this.$toast.show('error', this.langToast['1'])
        }

        let params = {
          orderSn,
          orderType: Number(orderType),
          payType: Number(PAYSID[this.paytype])
        }
        let res = await this.$http
          .post('/user/order/queryOrderStatus', params, {
            headers: {
              token: this.token
            }
          })
        let response = res.data
        if (response.code === 0 && response.data) {
          this.t1 = null
          this.t1 && clearInterval(this.t1)
          this.$router.replace({
            name: 'payresult',
            params: {
              isSuccess: 'success'
            }
          })
        }
      }
    },
    async getCode () {
      let {orderId, orderType} = this.$route.params
      if (!orderId || (!orderType && orderType !== 0)) {
        return this.$toast.show('error', this.langToast['1'])
      }
      if (this.paytype !== 'paypal') {
        let params = {
          orderId: orderId,
          orderType: Number(orderType)
        }
        let res = await this.$http
          .post(`/order/pay/${this.paytype}`, params, {
            headers: {
              token: this.token
            }
          })
        let response = res.data
        if (response.code !== 0) {
          return this.$toast.show('error', this.langToast['2'], () => {
            this.t1 = null
            this.t1 && clearInterval(this.t1)
          })
        }
        this.response = response.data
        this.inverRequest()
      } else {
        this.t1 = null
        this.t1 && clearInterval(this.t1)
        this.$toast.show('warn', this.langToast['3'])
      }
    }
  },
  beforeDestroy () {
    clearInterval(this.t1)
    this.t1 = null
  }
}
</script>
<style lang="scss" scoped>
$theme-color: #1fe4dc;

.pay-layout {
  width: 700px;
  min-height: 74vh;
  margin: 25px auto;
  .pay-header {
    width: 100%;
    margin-bottom: 20px;
    display: flex;
    align-items: flex-start;
    flex-direction: column;
    .h-left {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .h-line {
        width: 1px;
        height: 20px;
        background: #000;
        margin: 0 10px;
        display: inline-block;
      }
      span {
        font-size: 20px;
        color: #2d2d2d;
      }
    }
    .h-right {
      font-size: 14px;
      color: #2d2d2d;
      margin-top: 20px;
    }
  }
  .pay-con {
    background: #f4f4f4;
    padding: 40px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 80px;

    .pay-left {
      .top,
      .mid,
      .bottom {
        display: flex;
        margin-bottom: 50px;
        .attr {
          font-size: 14px;
          color: #2d2d2d;
        }

        .val {
          display: flex;
          align-items: center;
          margin-left: 20px;
          .tag {
            height: 100%;
            font-size: 0;
            margin-right: 50px;
            display: flex;
            align-items: center;
            border: 1px solid #f4f4f4;
            position: relative;
            .year {
              width: 52px;
              height: 48px;
              line-height: 48px;
              text-align: center;
              display: inline-block;
              background: #ededed;
              color: #777777;
              font-size: 14px;
              flex-shrink: 0;
            }
            .price {
              background: #fff;
              height: 48px;
              width: 160px;
              line-height: 48px;
              display: inline-block;
              font-size: 18px;
              color: #ff0000;
              text-align: center;
              .per {
                font-size: 12px;
                margin-left: 20px;
                color: #777777;
              }
            }
            img {
              position: absolute;
              bottom: 0;
              right: 0;
              width: 14px;
            }
          }
          .tag-active {
            border: 1px solid $theme-color;
            box-sizing: border-box;
          }
        }
        .body {
          margin-left: 20px;
          .pay-tag {
            cursor: pointer;
            border: 1px solid #e7e7e7;
            width: 200px;
            line-height: 36px;
            height: 36px;
            margin-bottom: 20px;
            position: relative;
            span {
              margin-left: 36px;
              display: inline-block;
              text-align: center;
              padding-left: 60px;
            }
            img {
              position: absolute;
            }
            .t-icon {
              width: 36px;
              height: 36px;
              padding: 6px;
              left: 0;
              border-right: 1px solid #e7e7e7;
            }
            .t-click {
              right: 0;
              bottom: 0;
              display: none;
            }
          }
          .paypal-con{
            position: relative;
            .form-con{
              opacity: 0;
              input[type='text']{
                width: 1px;
                height: 1px;
                position: absolute;
                top: 0;
                opacity: 0;
                left: 0;
              }
              input[type='submit']{
                width: 100%;
                position: absolute;
                top: 0;
                left: 0;
                opacity: 0;
                height: 100%;
              }
            }
          }
          .tag-active {
            border: 1px solid $theme-color;
            span {
              color: #000;
            }
            .t-icon {
              border-right: 1px solid $theme-color;
            }
            .t-click {
              display: inline-block;
            }
          }
        }
        .price-detail{
          margin-left: 6px;
          text-align: center;
          position: relative;
          width: 165px;
          top: -15px;
          .price{
            font-size: 30px;
            color: #ff0000;
            margin: 0 0 0 8px;
            text-align: left;
            padding: 0;
            margin-bottom: 24px;
          }
          .sao{
            font-size: 14px;
            color: #2d2d2d;
            margin-bottom: 5px;
          }
          .dec{
            color: #2d2d2d;
            font-size: 12px;
            margin-top: 10px;
          }
          img{
            width: 130px;
            box-shadow: 0 0 10px rgba(0,0,0,0.2);
            height: 130px;
          }
          .scanicon{
            position: absolute;
            left: 180px;
            width: 200px;
            top: 30px;
            height: auto;
             box-shadow:none;
          }
        }
      }
      .bottom {
        margin-bottom: 0;
      }
      .mid{
        margin-bottom: 40px;
      }
    }
    .pay-right {
      text-align: center;
      border-left: 1px solid #e5e5e5;
      padding-left: 28px;
      .p-title{
        font-size: 20px;
        color: #777777;
      }
      .pay-icon{
        margin-top: 20px;
        img{
          margin: 15px 0;
        }
        p{
          color: #969696;
          font-size: 14px;
        }
      }
      .pay-detail{
        height: 28px;
        margin: 90px 0 20px;
        cursor: pointer;
        line-height: 28px;
        font-size: 14px;
        color: #777777;
        width: 128px;
        border: 1px solid #e5e5e5;
      }
    }
  }
  .pay-bottom{
    color: #969696;
    font-size: 12px;
    text-align: center;
    margin-top: 20px;
  }
}
</style>
