<template>
  <div class="member-center">
    <!-- 头部 -->
    <MemberHeader />
    <!-- /头部 -->
    <div class="member-banner">
      <div class="banner-box">
        <div class="login-box">
          <div class="head-portrait">
            <img :src="userPic" v-if="userPic" alt />
            <img src="~/assets/images/buyer-img.png" v-if="!userPic" alt />
          </div>
          <!-- 登录后显示 -->
          <div class="user-name">
            <span>{{$t('memberCenter.hiDear')}}{{ memberInfo.nickName }}</span>
            <!-- <div class="rank" v-if="memberInfo.levelType != 0">
              {{ memberInfo.levelName }}
            </div> -->
          </div>

          <!-- 未登录显示 -->
          <!-- <div class="user-name red">开启会员的世界</div> -->

          <!-- 未登录 -->
          <!-- <a href="javascript:void(0)" class="btn">请登录</a> -->

          <!-- 已登录未购买 -->
          <nuxt-link to="/member-center/buy-member" class="btn">{{
            memberInfo.levelType == 0 ? $t('memberCenter.purchaseMembership') : $t('memberCenter.membershipRenewal')
          }}</nuxt-link>
          <!-- 已登录已购买 -->
          <!-- <a href="javascript:void(0)" class="btn">会员续费</a> -->
          <div class="growth-box">
            <div class="text">{{$t('memberCenter.growthValue')}}：</div>
            <div class="growth">
              <span
                v-if="memberInfo.nextGrowth"
                class="percent"
                :style="{
                  width: (memberInfo.growth / memberInfo.nextGrowth) * 100 + '%'
                }"
              ></span>
              <span
                v-if="!memberInfo.nextGrowth"
                class="percent"
                style="width:100%;"
              ></span>
              <span class="number" v-if="memberInfo.nextGrowth && memberInfo.growth < memberInfo.nextGrowth">{{ memberInfo.growth }}/{{ memberInfo.nextGrowth }}</span>
              <span class="number" v-else-if="!memberInfo.nextGrowth">{{$t('memberCenter.memberFull')}}</span>
              <span class="number" v-else>{{ memberInfo.growth }} {{$t('memberCenter.upgradeable')}}</span>
            </div>
            <div class="rank">{{ memberInfo.levelName }}</div>
          </div>
          <div class="shortcut">
            <nuxt-link to="/member-center/integral-mall" class="item">
              <span class="icon">
                <img
                  src="~/assets/images/memberPic/integral-mall-icon.png"
                  alt
                />
              </span>
              <span class="text">{{$t('memberCenter.pointsMall')}}</span>
            </nuxt-link>
            <nuxt-link to="/member-center/buy-member" class="item">
              <span class="icon">
                <img src="~/assets/images/memberPic/buy-member-icon.png" alt />
              </span>
              <span class="text">{{$t('memberCenter.purchaseMembership')}}</span>
            </nuxt-link>
          </div>
        </div>
      </div>
    </div>
    <div class="content">
      <!-- 会员权益 -->
      <div class="member-equity">
        <div class="member-tit">
          <div class="text">{{$t('memberCenter.memberBenefits')}}</div>
          <nuxt-link to="/member-center/member-equity" class="more">
            {{$t('memberCenter.moreBenefits')}}
            <span class="arr">></span>
          </nuxt-link>
        </div>
        <div class="con">
          <div class="item">
            <div class="icon">
              <img src="~/assets/images/memberPic/equity-icon01.png" alt />
            </div>
            <div class="text-box">
              <div class="text">{{$t('memberCenter.productShipping')}}</div>
              <div class="des">{{$t('memberCenter.goodsFree')}}</div>
            </div>
          </div>
          <div class="item">
            <div class="icon">
              <img src="~/assets/images/memberPic/equity-icon02.png" alt />
            </div>
            <div class="text-box">
              <div class="text">{{$t('memberCenter.exclusiveCoupons')}}</div>
              <div class="des">{{$t('memberCenter.exclusiveOffers')}}</div>
            </div>
          </div>
          <div class="item">
            <div class="icon">
              <img src="~/assets/images/memberPic/equity-icon03.png" alt />
            </div>
            <div class="text-box">
              <div class="text">{{$t('memberCenter.shoppingPoints')}}</div>
              <div class="des">{{$t('memberCenter.membershipMore')}}</div>
            </div>
          </div>
          <div class="item">
            <div class="icon">
              <img src="~/assets/images/memberPic/equity-icon04.png" alt />
            </div>
            <div class="text-box">
              <div class="text">{{$t('memberCenter.memberDiscount')}}</div>
              <div class="des">{{$t('memberCenter.membersEnjoy')}}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- /会员权益 -->
      <!-- 签到&任务 -->
      <div class="sign-mission">
        <div class="tit-box">
          <div class="member-tit">
            <div class="text">{{$t('memberCenter.dailySign')}}</div>
          </div>
          <div class="member-tit mission-tit">
            <div class="text">{{$t('memberCenter.pointsTask')}}</div>
          </div>
        </div>
        <div class="con">
          <div class="sign">
            <div
              class="item"
              @click="getPoints(index)"
              v-for="(item, index) in memberScore.scoreList"
              :key="index"
            >
              <div class="number" v-if="memberScore.signInCount < index + 1">
                +{{ item }}
              </div>
              <div
                class="number active"
                v-if="
                  memberScore.signInCount == index + 1 &&
                    memberScore.isSignIn == 0
                "
              >
                {{$t('memberCenter.signIn')}}
              </div>
              <div
                class="number active"
                v-if="
                  (memberScore.signInCount >= index + 1 &&
                    memberScore.isSignIn == 1) ||
                    (memberScore.signInCount > index + 1 &&
                      memberScore.isSignIn == 0)
                "
              >
                {{$t('memberCenter.signed')}}
              </div>
              <div class="text" v-if="$store.state.locale == 'en'">Day {{ index + 1 }}</div>
              <div class="text" v-if="$store.state.locale != 'en'">第{{ index + 1 }}天</div>
            </div>
          </div>
          <div class="mission">
            <div class="item">
              <div class="text-box">
                <div class="text">{{$t('memberCenter.shoppingMall')}}</div>
                <div class="des">
                  {{$t('memberCenter.purchase1')}}{{ memberScore.shopScore }}{{$t('memberCenter.purchase2')}}
                </div>
              </div>
              <nuxt-link to="/" class="btn">{{$t('memberCenter.goShopping')}}</nuxt-link>
            </div>
            <div class="item">
              <div class="text-box">
                <div class="text">{{$t('memberCenter.registerNewUser')}}</div>
                <div class="des">
                  {{$t('memberCenter.newPoints1')}}{{ memberScore.registerScore }}{{$t('memberCenter.newPoints2')}}
                </div>
              </div>
              <a href="javascript:void(0)" class="btn default">{{$t('memberCenter.completed')}}</a>
            </div>
          </div>
        </div>
      </div>
      <!-- /签到&任务 -->
      <!-- 兑换列表 -->
      <div class="exchange-list" v-if="scroeList.length">
        <div class="member-tit">
          <div class="text">{{$t('memberCenter.exchangeGoods')}}</div>
          <nuxt-link to="/member-center/integral-mall" class="more">
            {{$t('memberCenter.viewMore')}}
            <span class="arr">></span>
          </nuxt-link>
        </div>
        <div class="goods-list integral-list">
          <div class="list-con">
            <div
              class="item"
              v-for="item in scroeList"
              :key="item.prodId"
              @click="toScoreProdDet(item.prodId)"
            >
              <div class="goods-img">
                <img :src="item.pic" alt />
              </div>
              <div class="goods-msg">
                <div class="goods-name">{{ item.prodName }}</div>
                <div class="goods-price">
                  <div class="icon">
                    <img
                      src="~/assets/images/memberPic/integral-icon.png"
                      alt
                    />
                  </div>
                  <div class="integral-price">{{ item.scorePrice }} {{$t('memberCenter.points')}}</div>
                  <div class="add" v-if="item.price > 0">+</div>
                  <div class="price" v-if="item.price > 0">
                    ￥{{ item.price }}
                  </div>
                </div>
                <div class="old-price">{{$t('memberCenter.marketPrice')}}￥{{ item.oriPrice }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- /兑换列表 -->
    </div>
  </div>
</template>

<script src='~/assets/js/load-transition'></script>
<script>
import MemberHeader from '~/components/member-header'
export default {
  layout: 'home',
  components: {
    MemberHeader
  },
  data () {
    return {
      memberInfo: {},  //会员信息
      scroeList: [],  //积分商品列表
      userPic: "",  //会员头像
      memberScore: {},  //积分中心信息
    }
  },
  mounted () {
    // 设置网页标题
    document.title = this.$i18n.t('memberCenter.memberCenter')

    //获取会员信息
    this.$axios.get('/p/score/scoreLevel/page?levelType=' + 0).then(({ data }) => {
      this.memberInfo = data
    })

    //获取积分商品列表
    this.$axios.get('/p/score/prodScorePage').then(({ data }) => {
      this.scroeList = data.records
    })

    // 获取会员头像
    this.$axios.get('/p/user/userInfo').then(({ data }) => {
      this.userPic = data.pic
    })

    // 获取积分中心信息
    this.getMemberInfo()

  },
  methods: {

    // 获取积分中心信息
    getMemberInfo () {
      this.$axios.get('/p/score/scoreInfo').then(({ data }) => {
        this.memberScore = data
      })
    },

    // 积分签到
    getPoints (index) {
      // var index = e.currentTarget.dataset.index
      // this.$axios.get('/p/score/updateUserScore').then(({ data }) => {   
      //   this.$message({
      //     message: data,
      //     duration: 1000,
      //     type: String
      //   })
      //   this.getMemberInfo()
      // })
      if (this.memberScore.isSignIn == 1 && this.memberScore.signInCount == index + 1) {
        this.$message({
          message: this.$i18n.t('memberCenter.signedTips1'),
          duration: 1000,
          type: String
        })
      }
      // else if (this.memberScore.isSignIn == 0) {
      //   this.$message({
      //     message: '时间未到,还不能签到!',
      //     duration: 1000,
      //     type: error
      //   })
      // } 
      else if (this.memberScore.isSignIn == 0 && this.memberScore.signInCount == index + 1) {
        this.$axios.get('/p/score/updateUserScore').then(({ data }) => {
          this.$message({
            message: data,
            duration: 1000,
            type: String
          })
          this.getMemberInfo()
        })
      } else if (this.memberScore.signInCount > index + 1) {
        this.$message({
          message: this.$i18n.t('memberCenter.signedTips2'),
          duration: 1000,
          type: String
        })
      } else {
        this.$message({
          message: this.$i18n.t('memberCenter.signedTips3'),
          duration: 1000,
          type: String
        })
      }
    },

    // 跳转到积分商品详情
    toScoreProdDet (prodId) {
      this.$axios.get('/p/score/prodScorePage').then(({ data }) => {
        this.$router.push({ path: '/member-center/integral-det/' + prodId })
      })
    }

  }
}
</script>

<style scoped src='~/assets/css/member-center.css'>
</style>