<template>
  <div class="share-pay container">
    <div class="section supplier">
      <div class="info">
        <span class="name">{{ authUser.name }}</span>
        <span class="mobile">{{ authUser.mobile }}</span>
      </div>
      <div class="current-down">
        <span class="tip">倒计时</span>
        <span class="bg">{{ countDownTime.hh * 1 + countDownTime.dd * 24 * 1 }}</span>
        <span class="exp">:</span>
        <span class="bg">{{ countDownTime.mm }}</span>
        <span class="exp">:</span>
        <span class="bg">{{ countDownTime.ss }}</span>
      </div>
    </div>
    <div class="section package">
      <div class="info">
        <span class="vip-icon" />
        <span>购买认证通会员</span>
        <span>套餐</span>
        <span>{{ formatMonth }}</span>
      </div>
      <div class="total">
        <span>支付总额：</span>
        <span class="price">￥{{ orderPayLink.unpaidAmount | formatPrice }}</span>
      </div>
    </div>
    <div class="section bank">
      <div class="tip">
        请选择以下一家银行进行付款<span>（请在倒计时时间内使用</span><i>企业网银</i><span>完成本次支付）</span>
      </div>
      <pay-bank
        ref="payBank"
        :data="orderInfo"
        :show-price="false"
        :copy-status="false"
        :pay-way="3"
        :confirm="confirmVisiable"
        :pay-submit="false"
        @change="handleBankChange"
        @pay-confirm="handleConfirmPay"
      />
    </div>
    <div class="submit">
      <div class="container">
        <el-button type="danger" @click="onSubmit">立即支付</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import PayBank from '@/views/components/payment/pay-bank.vue'
import { checkedUnionPaySuccess, fetchPayInfoWithLinkLogo } from '@/api/pay'
import { countDown } from '@/utils'
export default {
  components: {
    PayBank
  },
  data() {
    return {
      confirmVisiable: false,
      linkLogo: '',
      orderPayLink: {},
      authUser: {},
      submitInfo: {},
      timer: null,
      countDownTime: {}
    }
  },
  computed: {
    orderInfo() {
      return {
        pageType: 4,
        payAmount: this.orderPayLink.unpaidAmount,
        vipId: '',
        vipRecordId: this.orderPayLink.authVipRecordId
      }
    },
    formatMonth() {
      const vipMonth = this.orderPayLink.vipMonth
      return vipMonth % 12 ? `${vipMonth}月` : `${vipMonth / 12}年`
    }
  },
  created() {
    this.init()
    console.log(this.linkLogo)
  },
  methods: {
    // 初始化
    init() {
      this.linkLogo = this.$route.params.linkLogo
      this.fetchPayInfoWithLinkLogo()
    },
    // 确认支付
    handleBankChange(e) {
      this.submitInfo = e
    },
    // 获取订单信息
    fetchPayInfoWithLinkLogo() {
      fetchPayInfoWithLinkLogo({ linkLogo: this.linkLogo }).then((res) => {
        const result = JSON.parse(res.data)
        this.orderPayLink = result.orderPayLink
        this.authUser = result.authUser
        console.log(result)
        this.countDown()
      })
    },
    // 确认支付事件
    handleConfirmPay(e) {
      this.redirectUrlQuery = {
        payAmount: e.data.payAmount
      }
      checkedUnionPaySuccess({ mbOrderId: e.data.mbOrderId, orderFlag: 0 }).then((res) => {
        res = JSON.parse(res.data)
        console.log(res)
        if (res.rt10_orderStatus.toLocaleLowerCase() === 'success') {
          this.$router.push({
            path: '/share/pay-success',
            query: this.redirectUrlQuery
          })
        } else {
          this.$router.push({
            path: '/share/pay-faild',
            query: this.redirectUrlQuery
          })
        }
      })
    },
    // 跳转支付
    onSubmit() {
      this.$refs.payBank.onSubmit()
    },
    // 倒计时
    countDown() {
      const diffTime = new Date(this.orderPayLink.effectiveTime).getTime()
      const loadTime = new Date().getTime()
      this.timer && clearTimeout(this.timer)
      countDown(diffTime, loadTime, {}, (item) => {
        this.countDownTime = item.conttainer
        this.timer = item.t
      })
    }
  }
}
</script>

<style scoped lang="scss">
.share-pay {
  width: 1200px;
  margin: 0 auto;
  margin-bottom: 100px;
}

.section {
  padding: 24px;
  margin-bottom: 16px;
  background: #fff;
}

.supplier {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .info {
    font-size: 18px;

    .name {
      font-weight: bold;
      margin-right: 48px;
    }
    .mobile {
      color: #333333;
    }
  }
}

.package {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .info {
    color: #333333;
    font-size: 18px;

    span {
      &:nth-child(2) {
        margin: 0 16px 30px;
      }
    }
  }
  .total {
    font-size: 24px;

    .price {
      color: #bc1724;
      font-weight: bold;
    }
  }
}

.bank {
  .tip {
    font-size: 18px;
    margin-bottom: 24px;
    span {
      color: #bc1724;
    }
    i {
      color: #bc1724;
      font-style: normal;
      font-weight: bold;
    }
  }
}

.submit {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 24px 0;
  background: #fff;
  opacity: 1;

  .container {
    width: 1200px;
    margin: 0 auto;
    text-align: right;
  }

  .el-button {
    width: 232px;
    height: 48px;
  }
  .el-button--danger {
    color: #ffffff;
    background-color: #bc1724;
    border-color: #bc1724;
  }
}

.current-down {
  span {
    display: inline-block;
    vertical-align: middle;
    &.tip {
      font-family: 'YouSheBiaoTiHei';
      margin-right: 10px;
      font-size: 24px;
      font-weight: bold;
      font-style: italic;
    }
    &.exp {
      font-weight: bold;
      margin: 0 8px;
    }
    &.bg {
      background: #bc1724;
      color: #fff;
      padding: 4px;
      border-radius: 2px;
    }
  }
}
</style>
