<template>
  <view class="container">
    <!--用户信息-->
    <view class="header main-between cross-center">
      <view class="user-box main-left cross-center">
        <view class="head-img">
          <image :src="userInfo.avatar?userInfo.avatar:'@/static/image/default-head-img.png'" />
        </view>
        <view v-if="!userInfo.nickname" class="nickname cross-center" @click="handleGetUserInfo">未授权登陆</view>
        <view v-if="userInfo.nickname" class="nickname cross-center" @click="handleGetUserInfo">{{ userInfo.nickname }}</view>
      </view>
      <button open-type="getUserInfo" class="refresh" @click="handleGetUserInfo">刷新</button>
    </view>
    <!--充值-->
    <view class="recharge main-between cross-center">
      <view class="static-box main-left cross-center">
        <view class="icon">
          <image src="/static/image/gold-bag.png" mode="aspectFill" />
        </view>
        <view class="overage">3金币</view>
      </view>
      <view class="recharge-btn">充值</view>
    </view>
    <!--历史-->
    <view class="history" @click="$u.route('/pages/my/history')">
      <view class="header main-between cross-center">
        <text>历史观看记录</text>
        <u-icon name="arrow-right" :color="$colors.infoColor" bold />
      </view>
      <view class="content main-between cross-center">
        <view
          v-for="(item,index) in history"
          :key="index"
          class="episode"
        >
          <view class="cover-image">
            <image src="@/static/image/default-movie.png" mode="aspectFill" />
          </view>
        </view>
      </view>
    </view>
    <!--菜单-->
    <view class="menu-group">
      <button
        v-for="(menu,index) in menus"
        :key="index"
        class="menu-item main-between cross-center"
        :open-type="menu.type ? menu.type : ''"
        @click="handleMenu(menu)"
      >
        <view class="left-box dir-left-nowrap cross-center">
          <view class="icon">
            <image :src="menu.icon" />
          </view>
          <text>{{ menu.name }}</text>
        </view>
        <u-icon name="arrow-right" :color="$colors.infoColor" bold />
      </button>
      <vedio se />
    </view>
    <!--tab bar-->
    <tab-bar />
  </view>
</template>

<script>
import TabBar from '../../components/TabBar/index'
import { mapState } from 'vuex'
export default {
  name: 'My',
  components: { TabBar },
  data() {
    return {
      history: [1, 2, 3, 4],
      menus: [
        { icon: '/static/image/my-page/order.png', name: '消费记录', href: '/pages/my/consume' },
        { icon: '/static/image/my-page/recharge.png', name: '充值记录', href: '/pages/my/recharge' },
        { icon: '/static/image/my-page/share.png', name: '分享好友', type: 'share' },
        { icon: '/static/image/my-page/contact.png', name: '联系客服', type: 'contact' },
        { icon: '/static/image/my-page/protocol.png', name: '用户协议', href: '/pages/my/protocol' }
      ]
    }
  },
  computed: {
    ...mapState({
      userInfo: seate => seate.user.info
    })
  },
  methods: {
    handleMenu(menu) {
      if (menu.href) {
        this.$u.route(menu.href)
      }
    },
    handleGetUserInfo() {
      this.$loading('数据刷新中...')
      uni.getUserProfile({
        success: res => {
          const params = {
            encryptedData: res.encryptedData,
            iv: res.iv,
            signature: res.signature
          }
          this.$api.user.update(params).then(res => {
            this.$hideLoading()
            this.$store.dispatch('user/info', res.data)
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .container {
    padding: 20px 20rpx 0;
    font-size: 30rpx;
    >.header{
      .user-box{
        .head-img{
          image{
            width: 100rpx;
            height: 100rpx;
            border-radius: 50%;
          }
        }
        .nickname{
          color: $primary-color;
          margin-left: 30rpx;
        }
      }
      .refresh{
        background: $primary-color;
        color: #fff;
        border-radius: 30rpx;
        padding: 10rpx 0;
        width: 140rpx;
        text-align: center;
        letter-spacing: .1rem;
        line-height: initial;
      }
    }
    .recharge{
      background: url("/static/image/my-recharge-bg.png") no-repeat center;
      background-size: 100%;
      height: 200rpx;
      margin-top: 20rpx;
      margin-bottom: 10rpx;
      .static-box{
        margin-left: 30rpx;
        .icon{
          border-radius: 20rpx;
          image{
            width: 100rpx;
            height: 100rpx;
          }
        }
        .overage{
          font-weight: 800;
          margin-left: 20rpx;
          font-size: 32rpx;
        }
      }
      .recharge-btn{
        border: 1rpx solid #fff;
        padding: 10rpx 0 ;
        text-align: center;
        width: 140rpx;
        margin-right: 20rpx;
        border-radius: 30rpx;
        color: #ffffff;
      }
    }
    .history{
      background: #1B203C;
      height: 300rpx;
      border-radius: 15rpx;
      padding: 20rpx 30rpx;
      margin-bottom: 40rpx;
      >.header{
        text{
          color: $info-color;
          font-weight: 600;
        }
      }
      .content{
        margin-top: 20rpx;
        .episode{
          flex: 1;
          margin-left: 20rpx;
          &:first-child{
            margin-left: 0;
          }
          .cover-image{
            image{
              width: 100%;
              height: 200rpx;
            }
          }
        }
      }
    }
    .menu-group{
      .menu-item{
        padding: 20rpx 0;
        background: transparent;
        border: none;
        text-align: unset;
        width: 100%;
        line-height: initial;
        font-size: initial;
        justify-content: space-between;
        &:after{
          content: unset;
        }
        .left-box{
          flex: 1;
          .icon{
            transform: translateY(4rpx);
            image{
              width: 55rpx;
              height: 55rpx;
            }
          }
          text{
            color: #fff;
            margin-left: 10rpx;
          }
        }
      }
    }
  }
</style>
