<template>
  <div class="conatainer">
    <div class="header">
      <div class="avatar">
        <img :src="userInfo.userImgUrl" alt="">
      </div>
      <p class="name">{{ userInfo.userName }}</p>
      <div class="card">
        <p class="level">1</p>
        <p class="expire">{{ userInfo.expireTime }} 到期</p>
        <p class="right" @click="funCheckMyBenefits">我的会员权益></p>
      </div>
    </div>
    <div class="menu-wrap">
      <router-link class="link-item" :to="{path: '/loan/coupon'}">
        <span class="label-wrap">
          <img class="icon-left" src="./image/icon_wd_quan@2x.png" alt="">
          <i class="label">我的券</i>
        </span>
        <span class="value-wrap">
          <span class="box">
            <i class="text">有</i>
            <i class="value">{{ userInfo.couponNum }}</i>
            <i class="text">张</i>
          </span>
          <img class="icon-right" src="./image/btn_next@2x.png" alt="">
        </span>
      </router-link>
      <a class="link-item" href="javascript:;">
        <span class="label-wrap">
          <img class="icon-left" src="./image/kefu@2x.png" alt="">
          <i class="label">我的专属客服</i>
        </span>
        <span class="value-wrap">
          <span class="box">
            <a class="value" href="javascript:;">{{ userInfo.privateName }}</a>
            <a :href="'tel:' + userInfo.privatePhone">
              <img class="icon-phone" src="./image/btn_phone@2x.png" alt="">
            </a>
          </span>
        </span>
      </a>
    </div>
    <!--驼驼来送礼，邀请好友-->
    <router-link class="btn-invite"
                 :to="{name: 'loanInvite'}">
      <img src="./image/xiaoshou_bg_tuiguang@2x.png"
           alt="">
    </router-link>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { getUserInfo } from './api'

export default {
  name: 'mine',
  data () {
    return {
      userInfo: {}
    }
  },
  async activated () {
    this.funInit()
    await this.$nextTick()
    this.$refreshTitle('我的')
  },
  methods: {
    funInit () {
      getUserInfo().then(res => {
        const { code, data, msg, status } = res
        if (!status) {
          Toast({
            message: msg,
            onClose: () => {
              if (code === 204) {
                this.$router.push({ path: '/loan/register' })
              }
            }
          })
          return
        }
        this.userInfo = data
        this.$store.commit('loan/UPDATE_USERINFO', data)
      }).catch((err) => {
        Toast(err)
      })
    },
    async funCheckMyBenefits () {
      this.$store.commit('loan/UPDATE_CHECKMYBENFITS_VALUE', 1)
      await this.$nextTick()
      this.$router.push({ path: '/loan/register' })
    }
  }
}
</script>

<style lang="scss" scoped>
.conatainer {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  min-height: 100vh;
  background: #fff;
}

.header {
  position: relative;
  left: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 220px;
  padding-top: 25px;
  background: url("./image/header_bg@2x.png") center top/100% 100% no-repeat;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  overflow: hidden;

  img {
    display: block;
    width: 100%;
  }
}

.name {
  margin-top: 5px;
  line-height: 25px;
  font-size: 18px;
  font-weight: 500;
  color: #fff;
}

.card {
  position: absolute;
  left: 50%;
  bottom: 0;
  z-index: 1;
  width: 344px;
  height: 92px;
  margin-left: -172px;
  background: url("./image/min_card@2x.png") center top/100% 100% no-repeat;

  .level {
    position: absolute;
    left: 158px;
    top: 20px;
    z-index: 1;
    font-size: 20px;
    font-weight: bolder;
    color: #C9AD8D;
    line-height: 27px;
    text-shadow: 0 14px 9px #DBD0C2;
    background: linear-gradient(90deg, #E5C7A5 0%, #CFAA7F 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .expire {
    position: absolute;
    left: 14px;
    bottom: 16px;
    z-index: 1;
    line-height: 22px;
    font-size: 16px;
    color: #fff;
  }

  .right {
    position: absolute;
    right: 28px;
    top: 33px;
    z-index: 1;
    width: 103px;
    height: 32px;
    border-radius: 4px;
    border: 1px solid #C9AD8D;
    line-height: 32px;
    font-size: 14px;
    font-weight: 500;
    text-align: center;
    white-space: nowrap;
    color: #C9AD8D;
  }
}

.menu-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  margin-top: 11px;

  a.link-item {
    display: flex;
    justify-content: space-between;
    width: 355px;
    padding: 23px 18px 23px 16px;
    border-radius: 4px;
    box-shadow: 0 2px 0px -1px rgba(224, 224, 224, 0.5);
  }

  .label-wrap,
  .value-wrap,
  .box {
    display: flex;
    align-items: center;
  }

  .label {
    margin-left: 12px;
    line-height: 22px;
    font-size: 16px;
    font-weight: 500;
    color: #333;
  }

  .value {
    padding-top: 1px;
    margin: 0 2px;
    line-height: 22px;
    font-size: 16px;
    font-weight: 500;
    color: #333;
  }

  .text {
    line-height: 20px;
    font-size: 14px;
    color: #999;
  }

  .icon-left {
    width: 20px;
    height: 20px;
  }

  .icon-right {
    width: 22px;
    height: 22px;
    margin-left: 24px;
  }

  .icon-phone {
    display: block;
    width: 40px;
    height: 40px;
    margin-left: 5px;
  }
}

.btn-invite {
  margin-top: 16px;

  img {
    display: block;
    width: 342px;
    height: 117px;
  }
}
</style>
