<template>
  <view v-if="!isFirstload" class="container">
    <!-- 页面头部 -->
    <view class="main-header" :style="{ height: platform == 'H5' ? '260rpx' : '320rpx', paddingTop: platform == 'H5' ? '0' : '80rpx' }">
      <image class="bg-image" src="/static/background/user-header2.png" mode="scaleToFill"></image>
      <!-- 用户信息 -->
      <view v-if="isLogin" class="user-info">
       <view class="user-avatar" @click="handlePersonal()">
          <avatar-image :url="userInfo.avatar" :width="100" />
        </view>
        <view class="user-content">
          <!-- 会员昵称 -->
          <view class="nick-name oneline-hide" @click="handlePersonal()">{{ userInfo.username }}</view>

          <!-- 会员无等级时显示手机号 -->
          <view class="mobile">{{ userInfo.cellPhone }} ({{ userInfo.username }}) </view>
		  <!-- 会员类型 -->
		  <view class="utype">{{userInfo.userType}}</view>
        </view>

      </view>
      <!-- 未登录 -->
      <view v-else class="user-info" @click="handleLogin">
        <view class="user-avatar">
          <avatar-image :width="100" />
        </view>
        <view class="user-content">
          <view class="nick-name">未登录</view>
          <view class="login-tips">点击登录账号</view>
        </view>
      </view>
    </view>
	

    <!-- 操作区域 -->
    <view class="order-navbar">
<!--      <view class="order-navbar-item" v-for="(item, index) in orderNavbar" :key="index" @click="onTargetOrder(item)">
        <view class="item-icon">
          <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
        </view>
        <view class="item-name">{{ item.name }}</view>
        <view class="item-badge" v-if="item.count && item.count > 0">
          <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
          <text v-else class="text">99+</text>
        </view>
      </view> -->
	  <!-- 我的认证 -->
	  <view class="order-navbar-item" @click="mycertinfo()">
		  <view class="item-icon">
		    <text class="iconfont icon-qpdingdan"></text>
		  </view>
		  <view class="item-name"> 我的认证</view>
	  </view>
	  <!-- 买入订单 -->
	  <view class="order-navbar-item">
		  <view class="item-icon">
			<text class="iconfont icon-daifukuan"></text>
		  </view>
		  <view class="item-name"> 买入订单</view>
	  </view>
	  <!-- 卖出订单 -->
	  <view class="order-navbar-item">
		  <view class="item-icon">
			<text class="iconfont icon-qpdingdan"></text>
		  </view>
		  <view class="item-name"> 卖出订单</view>
	  </view>
	  <!-- 发票管理 -->
	  <view class="order-navbar-item">
		  <view class="item-icon">
			<text class="iconfont icon-qpdingdan"></text>
		  </view>
		  <view class="item-name"> 发票管理</view>
	  </view>
    </view>
	
	<!-- 操作日志 -->
	<view class="oplog">
		<view class="log-list">
		  <view v-for="(item, index) in oplist" :key="index" class="log-item">
		    <view class="item-left flex-box">
		      <view class="rec-status">
		        <text>{{ item.operation }}</text>
		      </view>
		      <view class="rec-time">
		        <text>{{ item.createTime }}</text>
		      </view>
		    </view>
		    <view class="item-right col-6">
		      <text>{{ item.content }}</text>
		    </view>
		  </view>
		</view>
	</view>
	
    <!-- 退出登录 -->
    <view v-if="isLogin" class="my-logout">
      <view class="logout-btn" @click="handleLogout()">
        <text>退出登录</text>
      </view>
    </view>

  </view>
</template>

<script>
  import store from '@/store'
  import AvatarImage from '@/components/avatar-image'
  import * as UserApi from '@/api/user'
  import * as OrderApi from '@/api/order'
  import { checkLogin } from '@/core/app'


  export default {
    components: {
      AvatarImage
    },
    data() {
      return {
        // 正在加载
        isLoading: true,
        // 首次加载
        isFirstload: true,
        // 是否已登录
        isLogin: false,
        // 当前用户信息
        userInfo: {},
		// 操作日志
		oplist:[]
      }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {
      this.onRefreshPage()
    },

    methods: {

      // 刷新页面
      onRefreshPage() {
        // 判断是否已登录
        this.isLogin = checkLogin()
		if(!this.isLogin){
			uni.navigateTo({
				url:"/pages/login/index"
			})
		}
        // 获取页面数据
        this.getPageData()
      },

      // 获取页面数据
      getPageData(callback) {
        const app = this
        app.isLoading = true
        Promise.all([app.getUserInfo(), app.getOplogs()])
          .then(result => {
            app.isFirstload = false

            // 执行回调函数
            callback && callback()
          })
          .catch(err => console.log('catch', err))
          .finally(() => app.isLoading = false)
      },


      // 获取当前用户信息
      getUserInfo() {
        const app = this
		// 从storage 中读取
		if(app.isLogin){
			app.userInfo = uni.getStorageSync("cuser")
		}  
	
      },

      // 用户登录日志
      getOplogs () {
        const app = this
		const accid = uni.getStorageSync('accountId')
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : UserApi.oplogs({'accountId': accid}, { load: app.isFirstload })
            .then(result => {
              // app.assets = result.data.assets
			  app.oplist = result.data
			  console.log("oplogs",app.oplist)
              resolve(app.oplist)
            })
            .catch(err => {
				// console.log("err",err);
              if (err && err.statusCode == 401) {
                app.isLogin = false
				uni.clearStorageSync()
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },

      // 获取当前用户的
      getOplogsTodo() {
        const app = this
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : OrderApi.todoCounts({}, { load: app.isFirstload })
            .then(result => {
              app.todoCounts = result.data.counts
              resolve(app.todoCounts)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
				store.dispatch('Logout', {})
				  .then(result => app.onRefreshPage())
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },
	  
	  //我的认证
	  mycertinfo(){
		  this.$navTo('pages/user/mycertinfo/mycertinfo')
	  },
	  

      // 跳转到登录页
      handleLogin() {
        !this.isLogin && this.$navTo('pages/login/index')
      },

      // 跳转到修改个人信息页
      handlePersonal() {
        this.$navTo('pages/user/personal/index')
      },

      // 退出登录
      handleLogout() {
        const app = this
        uni.showModal({
          title: '友情提示',
          content: '您确定要退出登录吗?',
          success(res) {
            if (res.confirm) {
              store.dispatch('Logout', {})
                .then(result => app.onRefreshPage())
            }
          }
        })
      },

      // 跳转到钱包页面
      onTargetWallet() {
        this.$navTo('pages/wallet/index')
      },

      // 跳转到订单页
      onTargetOrder(item) {
        this.$navTo('pages/order/index', { dataType: item.id })
      },

      // 跳转到我的积分页面
      onTargetPoints() {
        this.$navTo('pages/points/log')
      },

      // 跳转到我的优惠券页
      onTargetMyCoupon() {
        this.$navTo('pages/my-coupon/index')
      },

      // 跳转到服务页面
      handleService({ url }) {
        this.$navTo(url)
      },

    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
      // 获取首页数据
      this.getPageData(() => {
        uni.stopPullDownRefresh()
      })
    },


  }
</script>

<style lang="scss" scoped>
  // 页面头部
  .main-header {
    background-color: #fff;
    background-image: url('/static/background/user-header.png');
    position: relative;
    width: 100%;
    height: 280rpx;
    background-size: 100% 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    // padding-top: 40rpx;
    padding-left: 30rpx;

    .bg-image {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 0;
    }

    .user-info {
      display: flex;
      height: 100rpx;
      z-index: 1;

      .user-content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-left: 30rpx;
        color: #c59a46;

        .nick-name {
          font-size: 34rpx;
          font-weight: bold;
          max-width: 270rpx;
        }

        .mobile {
          margin-top: 15rpx;
          font-size: 28rpx;
        }
		
        .utype {
          align-self: baseline;
          display: flex;
          align-items: center;
          background: #3c3c3c;
          margin-top: 12rpx;
          border-radius: 10rpx;
          padding: 4rpx 12rpx;
        }

        .login-tips {
          margin-top: 12rpx;
          font-size: 30rpx;
        }

      }
    }
}
  
	// 日志操作 
	.oplog{
		background-color: #fff;
	}
  .log-list {
    padding: 0 30rpx;
  }
  
  .log-item {
    font-size: 28rpx;
    padding: 20rpx 20rpx;
    line-height: 1.8;
    border-bottom: 1rpx solid rgb(238, 238, 238);
    display: flex;
    justify-content: center;
    align-items: center;
  }

  // 角标组件
  .item-badge {
    position: absolute;
    top: 0;
    right: 55rpx;
    background: #fa2209;
    color: #fff;
    border-radius: 100%;
    min-width: 38rpx;
    height: 38rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1rpx;
    font-size: 24rpx;
  }

  // 订单操作
  .order-navbar {
    display: flex;
    margin: 20rpx auto 20rpx auto;
    padding: 20rpx 0;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    font-size: 30rpx;
    border-radius: 5rpx;
    background: #fff;

    &-item {
      position: relative;
      width: 25%;

      .item-icon {
        text-align: center;
        margin: 0 auto;
        padding: 10rpx 0;
        color: #545454;
        font-size: 44rpx;
      }

      .item-name {
        font-size: 28rpx;
        color: #545454;
        text-align: center;
        margin-right: 10rpx;
      }

    }
  }

  // 退出登录
  .my-logout {
    display: flex;
    justify-content: center;
    margin-top: 50rpx;

    .logout-btn {
      width: 60%;
      margin: 0 auto;
      font-size: 28rpx;
      color: #616161;
      border-radius: 20rpx;
      border: 1px solid #dcdcdc;
      padding: 16rpx 0;
      text-align: center;
    }
  }
</style>
