<template>
<div class="me">
<div class='head'>
  <div class='img'>
    <img @tap='tapAvatar' class='photo' :src='userInfo.avatarUrl' mode='aspectFill'>
    <img class='vip' v-if='vertified' src='/static/imgs/V.png'>
  </div>
  <div class='name'>{{userInfo.nickName}}</div>
  <div class='info'>{{'' || userInfo.firm_name}}</div>
  <div class='info' style='padding:0 30rpx;text-align:center;'>{{'' || userInfo.cate_name}}</div>
</div>
<div>
  <navigator class='nav-item' v-for="(item, i) in link_list" :key="i" :url="'../'+item.url+'/main'" hover-class='hover'>
    <img :src="'/static/imgs/'+ item.url + '.png'" :class='item.url'>
    {{item.title}}
    <div class='arrow'/>
    <span class="tips" v-if="item.tips==1">{{number}}</span>
  </navigator>
</div>
<!-- <div class='verify'><navigator url='../verify/main'>{{vertified?'重置认证信息':'认证为律师'}}</navigator></div> -->
<div class='reset verify' v-if='vertified'><div @tap='unVerify'>重置认证信息</div></div>
<div class='verify' v-else><navigator url='../verify/main'>认证为律师</navigator></div>

<AvatarUpload :show='AvatarChanging' ref='AvatarUpload' @exit='exit' @confirm='uploadImg'/>
</div>
</template>

<script>
import AvatarUpload from '@/components/AvatarUpload'
export default {
  components: {
    AvatarUpload
  },
  data() {
    return {
      userInfo: {},
      link_list: [
        { title: '我的消息', url: 'mynews', tips: '1' },
        { title: '我的回复', url: 'myreply', tips: '0' },
        { title: '我的评价', url: 'myassess', tips: '0' },
        { title: '我的讲座', url: 'mylecture', tips: '0' },
        { title: '考勤记录', url: 'attence', tips: '0' },
        { title: '值班证明', url: 'prove', tips: '0' },
        { title: '值班调班', url: 'schedule', tips: '0' },
        { title: '意见反馈', url: 'feedback', tips: '0' }
        // { title: '社区定位', url: 'test' },
        // { title: '获赞数', url: 'like' }
      ],
      vertified: false,
      AvatarChanging: false,
      uploading: false,
      hasMessage:false,
      number: ''
    }
  },
  methods: {
    tapAvatar(){
      if(this.uploading) return
      wx.showActionSheet({
        itemList: ['查看头像','更换头像'],
        success: res => {
          if(res.tapIndex === 0)
            wx.previewImage({urls:[this.userInfo.avatarUrl]})
          else this.changeAvatar()
        }
      })
      wx.setStorageSync('UploadAvatar_Tip_Showed', 3)
    },
    unVerify() {
      wx.showModal({
        title: '重置确认',
        content: '确定要重置吗?',
        success: res => {
          if (res.confirm) {
            wx.request({
              url: this.rootUrl + '/law/wxbinding',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              method: 'POST',
              data: { sessionID: wx.getStorageSync('sessionID') },
              success: res => {
                if(res.statusCode=='500'){
                  this.service.getUnionId(this.rootAvatar,this.rootUrl).then(res=>{
                    this.unVerify()
                  })
                }else{
                  if (res.data.status === '200') {
                    wx.setStorageSync('isVerify', false)
                    wx.removeStorageSync('userInfo')
                    this.refreshInfo()
                    wx.showModal({
                      title: '',
                      content: '当前账号没有绑定律师，请认证为律师',
                      showCancel: false,
                      success: () => wx.redirectTo({ url: '../verify/main' })
                    })
                  } else {
                    wx.showToast({ title: '解绑失败', icon: 'none' })
                  }
                }
              },
              fail: res => wx.showToast({ title: '网络错误', icon: 'none' })
            })
          }
        }
      })
    },
    refreshInfo() {
      this.vertified = wx.getStorageSync('isVerify')
      this.userInfo = wx.getStorageSync(this.vertified ? 'userInfo' : 'wxInfo')
    },
    canIGetPos(){
      wx.request({
        url: this.rootUrl + '/law/position',
        success: res => {
          console.log(res)
          console.log(this.link_list)
          const link_list = this.link_list
          if( res.data === 0 ){
            if(link_list.length > 8)
              this.link_list = this.link_list.slice(0,5)
          } else {
            if(link_list.length < 9)
              this.link_list.push({ title: '社区定位', url: 'test' })
          }
        }
      })
    },
    changeAvatar(){
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: res => {
          this.AvatarChanging = true
          wx.hideTabBar()
          this.$refs.AvatarUpload.setInitialImg(res.tempFilePaths[0])
        }
      })
    },
    exit(){
      this.AvatarChanging = false
      wx.showTabBar()
    },
    uploadImg(src){
      wx.showModal({
        title:'',
        content: '确认上传新的头像吗?',
        success: res => {
          if(res.confirm){
            this.$refs.AvatarUpload.startWaiting()
            this.uploadImg_exec(src)
          }
        }
      })
    },
    uploadImg_exec(src){
      this.uploading = true
      console.log(src)
      wx.showLoading({ title: '正在上传' })
      wx.uploadFile({
        url: this.rootUrl + 'law/lawyerpic',
        filePath: src,
        formData: {sessionID: wx.getStorageSync('sessionID')},
        name: 'file',
        header: { 'content-type': 'multipart/form-data' },
        success: res => {
          if(new RegExp(/^Maximum upload size exceeded.*?/).test(JSON.parse(res.data).message)){
            console.log('图片文件过大')
            wx.hideLoading()
            wx.showModal({title:'上传失败',content:'图片文件过大,请选择较小的图片上传'})
            this.$refs.AvatarUpload.stopWaiting()
            this.$refs.AvatarUpload.back()
            this.uploading = false
          } else if (res.statusCode == '500')
            this.service.getUnionId(this.rootAvatar, this.rootUrl).then(() => this.uploadImg_exec(src))
          else if(res.data == 1){
            console.log('图片上传成功')
            wx.hideLoading()
            wx.showToast({ title: '上传成功' })
            this.service.getUnionId(this.rootAvatar, this.rootUrl).then(() => {
              this.refreshInfo()
              this.uploading = false
              this.exit()
              this.$refs.AvatarUpload.clear()
            })
          } else {
            console.log('图片上传失败')
            wx.hideLoading()
            wx.showToast({ title: '上传失败' })
            this.$refs.AvatarUpload.stopWaiting()
            this.$refs.AvatarUpload.back()
            this.uploading = false
          }
        }
      })
    },
    checkTip(){
      const num = wx.getStorageSync('UploadAvatar_Tip_Showed') || 1
      if( num < 3 )
        wx.showModal({
          title:'提示',
          content:'点击头像可进行修改或查看',
          showCancel: false,
          confirmText:'知道了',
          success: res => {
            if(res.confirm)
              wx.setStorageSync('UploadAvatar_Tip_Showed', num + 1)
          }
        })
    },
    getLawMessage(){
      wx.request({
        url: this.rootUrl + 'message/getUnReadMessage',
        method: 'get',
        data: { sessionID: wx.getStorageSync('sessionID') },
        success: res => {
          if(res.data>0){
            this.number = res.data
            console.log(this.number)
          }else{
            this.number = ''
          }
        }
      })
    }
  },
  onLoad() {
    this.getLawMessage()
  },
  onShow() {
    this.refreshInfo()
    this.canIGetPos()
    this.checkTip()
    this.getLawMessage()
  },
  onUnload(){
    this.number = ''
  }
}
</script>
<style lang="stylus" scoped>
.me
  width 100%
  min-height 100%
  Background()
  position absolute
.head
  Flex(flex, center, center, column)
  Height_Width(400rpx, 100%)
  background themeColor
  color white
  .img
    position relative
    Flex(flex, center, center)
    .photo
      Height_Width(155rpx)
      border-radius 50%
      border 5rpx solid rgba(255,255,255,.8)
    .vip
      position absolute
      top 120rpx
      left 110rpx
      Height_Width(50rpx)
  .name
    Font(38rpx, 80rpx)
  .info
    Font(26rpx, 40rpx)
.nav-item
  padding 0 25rpx
  Font(30rpx, 88rpx)
  Flex(flex,,center)
  background white
  border-bottom 1rpx solid #dedede
  position relative
  &:nth-child(5)
    margin-top 20rpx
    border-top 1rpx solid #dedede
  &:nth-child(8)
    margin-top 20rpx
    border-top 1rpx solid #dedede
  img
    Height_Width(38rpx)
    margin-right 25rpx
    &.attence
      width 40rpx
      margin-right 23rpx
    &.schedule
      width 36rpx
      margin-right 27rpx
  .arrow
    Height_Width(15rpx)
    Border(0, 0, 4rpx, 4rpx, #BBB)
    transform rotate(-135deg)
    margin 0 0 0 auto
.verify
  margin 30rpx 0
  Height_Width(80rpx, 100%)
  padding 0 calc(((100% - 320rpx) / 2))
  BorderBox()
  navigator, div
    Height_Width(100%)
    background themeColor
    color white
    text-align center
    Font(36rpx, 80rpx)
.reset
  div
    border 1rpx solid #DFDEE3
    color grey
    background white
.tips
  display inline-block
  padding 0 14rpx
  background-color rgb(242,6,61)
  color #fff
  font-size 24rpx
  line-height 40rpx
  border-radius 20rpx
  position absolute
  right 55rpx
  top 24rpx
  text-align center
</style>

