<template>
    <div class="uc-header">
      <div class="uc-header-box" >
        <div class="personalData">
            <div class="uesr-avaer">
              <img :src="fileBaseUrl+userData.avatar" v-if="userData.avatar !== '' ">
              <div v-else class="uavatar">
                  <div v-if="userData.sex === 1 "><img :src="`${webBaseUrl}/images/Avatarman.png`" style="width: 100%;" alt=""></div>
                  <div v-if="userData.sex === 2 "><img :src="`${webBaseUrl}/images/Avatarwoman.png`" style="width: 100%;" alt=""></div>
              </div>
            </div>

           <div class="user-content">
              <div class="content-top">
                <h6 content-bottom><router-link to="/home/index"> {{userData.name}}</router-link>
                 </h6>
                 <div class="grade">
                     <div>{{statData.level}}</div>
                     <span style="color: #A3680A;margin-left:12px;line-height: 24px;">经验值：{{statData.evalue}}</span>
                 </div>
                 <el-button round plain class="btn-user" size="small" v-if="pageId != userInfo.aid && !isFollowHas" @click="toFollow()">关注TA</el-button>
                 <el-button round plain class="btn-user" size="small" @click="cancelFollow()" v-if="isFollowHas">已关注</el-button>
                 <el-button round plain class="btn-user" size="small"><router-link :to="'/home/'+pageId+'/leaving'">{{pageId == userInfo.aid ? '留言板':'去留言'}}</router-link></el-button>
              </div>
              <div class="content-bottom">
                <p class="portal-summary-text">{{userData.sign}}</p>
              </div>

           </div>
        </div>
        <div class="learningData">
          <div class="learning-info">
             <div class="learning-qus">当月学习时长</div>
             <div class="learning-an"><span>{{statData.monthStudyHour}}</span>h</div>
          </div>
           <div class="learning-info">
             <div class="learning-qus">累计学习时长</div>
             <div class="learning-an"><span>{{statData.totalStudyHour}}</span>h</div>
          </div>
          <div class="learning-border" ></div>
           <div class="learning-info" style="margin-left:22px">
             <div class="learning-qus">当月学习天数</div>
             <div class="learning-an"><span>{{statData.monthStudyDays}}</span>天</div>
          </div>
           <div class="learning-info">
             <div class="learning-qus">累计学习天数</div>
             <div class="learning-an"><span>{{statData.totalStudyDays}}</span>天</div>
          </div>
           <div class="learning-border" ></div>
           <div class="learning-info" style="margin-left:22px">
             <div class="learning-qus">我的U币（累计）</div>
             <div class="learning-an"><span>{{statData.uvalue}}</span></div>
          </div>
          <div class="learning-info">
              <div @click="jumrank" style="cursor: pointer;" class="ranking-link"> BOE 排行榜 >></div>
          </div>
        </div>
      </div>
    </div>
  </template>

  <script>
  import {userAvatarText,cutFullName} from "@/utils/tools.js";
    import { mapGetters } from 'vuex'
    import apiStart from '@/api/phase2/stat.js';
    import apiFollow from "@/api/phase2/userfollow.js"
    import apiUser from "@/api/system/user.js";
    export default {
      name: 'UcHeader',
      computed:{
        ...mapGetters(['curIdentity','identity','userInfo']),
              avatarText(){
                  return userAvatarText(this.userInfo.name);
              }
          },
      data(){
        return {
          fileBaseUrl:process.env.VUE_APP_FILE_BASE_URL,
          statData:{
            evalue: 0,//经验值
            level: "LV1",//级别
            monthStudyHour:0,//当月学习时长
            totalStudyHour: 0, //累计学习时长
            monthStudyDays:0, //当月学习天数
            totalStudyDays:0, //累计学习天数
            uvalue:0 //U币数量
          },
          orgInfo:'',
          sex:'',
          pageId:'',
          isFollowHas:false,
          userData:{
              aid: "",
              avatar: "",
              code: "",
              name: "",
              orgInfo: "",
              sex: '',
              sign: "",
          }
        }
      },
      mounted() {
        this.pageId = this.$xpage.getHomeId(this.$route);
        this.sex = this.userInfo.sex;
        // 判断路由是进入的学员默认页面就重置setCurIdentity
        if(this.$route.path == '/uc/study/task' || this.$route.path == '/study/index'){
          this.setCurIdentity(1);
        }
        this.orgInfo=cutFullName(this.userInfo.departFullName,1);

        this.loadUserStat();
        if(this.pageId !== this.userInfo.aid) {
          this.followHas();
        }
      },
      methods:{
        cancelFollow() {
          apiFollow.remove(this.pageId).then(res=>{
            if(res.status == 200) {
               this.isFollowHas = false;
            } else {
              this.$message.error(res.message);
            }
          })
        },
        loadUserStat(){//获取经验值和等级
            apiUser.getByIds([this.pageId]).then(res => {
                if (res.status == 200) {
                   this.userData = res.result[0];
                } else {
                   this.$message.error(res.message);
                }
            });
            apiStart.getUserStatTotalInfo(this.userInfo.aid).then(res=>{
              if(res.status == 200) {
                 this.statData.evalue = res.result.evalue;
                 this.statData.level = res.result.level;
                 this.statData.monthStudyHour = res.result.monthStudyHour;
                 this.statData.totalStudyHour = res.result.totalStudyHour;
                 this.statData.monthStudyDays = res.result.monthStudyDays;
                 this.statData.totalStudyDays = res.result.totalStudyDays;
                 this.statData.uvalue = res.result.uvalue;
                 this.medalList=res.result.medalList;
              }
            });
        },
        followHas() {//他人主页，判断关注状态
          apiFollow.has(this.pageId).then(res=>{
            if(res.status == 200) {
               this.isFollowHas = res.result;
            } else {
              this.$message.error(res.message);
            }
          })
        },
        toFollow() {
          apiFollow.save(this.pageId).then(res=>{
            if(res.status == 200) {
              this.$message.success(res.message);
              this.followHas();
            } else {
              this.$message.error(res.message);
            }
          })
        },
        setCurIdentity(iden){
          this.$store.dispatch('SetCurIdentity',iden);
        },
        jumrank(){
          this.$router.push('/user/ranking');
        },
        toPage(url){
          this.$router.push({path:url})
        },
      }
    }
  </script>

<style lang="scss" scoped>
  .ranking-link{
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    margin-top: 20px;
  }
  .uc-header{
    display: flex;
    justify-content: space-between;
    height: 210px;
    background-color: #ffffff;
    background-image: url('../../../public/images/userbgimg.png');
    background-repeat: no-repeat;
    background-size:445px 210px;
    background-position: right 0;
  }
  .uc-header-box{
    display: flex;
    justify-content: space-between;
    height: 210px;
    margin: 0 auto;
  }
  .list{
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    margin-top: 20px;
  }
  .learningData{
    padding-top: 70px;
    box-sizing: border-box;
    margin-left:auto;
    .learning-info{
      float: left;
      margin-right: 22px;
       .learning-qus{
            font-size: 14px;
            color: #666660;
            margin-bottom: 24px;
       }
       .learning-an{
            color: #999999;
            text-align: center;
            font-size: 14px;
            span{
              font-weight: 600;
              font-size: 22px;
              color: #0060FF;
              margin-right: 6px;
            }
       }
    }
    .learning-border{
      width: 1px;
      height: 49px;
      opacity: 0.3;
      border: 1px solid #AAA6A6;
      float: left;
      margin-top: 5px;
    }
  }
//   .content-bottom{
//     margin-top: 12px;
     .medal{
       float: left;
       img{
           width: 36px;
           height: 40px;
           margin-right: 10px;
           vertical-align: top;
       }
     }

     .medalbutt{
       color: #333333;
       font-size: 14px;
       float: left;
       margin-top: 10px;
     }
     .grade{
       float: left;
       margin-top: 10px;
       div{
         margin-left: 22px;
         font-weight: 600;
          float: left;
          width: 57px;
          height: 24px;
          text-align: center;
          line-height: 24px;
          color: #9D6110 ;
          background: url('../../../public/images/lvbg.png') no-repeat 100% / 100%;
       }
     }
//   }
  .personalData{
    flex: 1;
    padding-top: 40px;
    box-sizing: border-box;
    display: flex;
    .uesr-avaer{
      width: 120px;
      height: 120px;
      margin-right: 30px;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        vertical-align: top;
      }
    }
    .user-content{
      flex: 1;
      .content-top{
        margin-top: 15px;
        height: 56px;
        .btn-user{
            margin-top: 6px;
            width: 80px;
            height: 30px;
            border-radius: 20px;
            border: 1px solid #0060FF;
            color: #0060FF;
        }
        h6{
          color: #333333;
          font-size: 26px;
          margin:0;
          float: left;
        }
        span{
          color: #999999;
          font-size: 14px;
          margin-left: 5px;
          margin-right: 24px;
        }
        .editbutt{
          font-size: 14px;
          color: #333333;


        }
      }
    }
  }

   .upicon{
     text-align: center;
     cursor: pointer;
     margin-top: 10px;
     margin-left: 15px;
     font-size: 14px;
     i{font-size: 30px;color: #ffb30f;}
     div{color: #7b7b7b;padding: 5px 0px 10px 0px;}
   }
   .upicon:focus div{
       color: #588afc;
   }
   .identity{
     padding: 5px 10px;
     border: 1px solid #fcfcfc;
     border-radius: 8px;
     cursor: pointer;
     background-color: #ffffff;
     margin: 5px;
   }
   .active{
     background-color: #ffb30f;
     color: #FFFFFF;
      border: 1px solid #ffb30f;
   }
   .uc-alert{
    margin: 10px;
    text-align: center;
    padding: 0px 50x;
    .uc-alert-label{
       color: #3f3f3f;
    }
    .uc-alert-value{
      padding-top: 10px;
       color: #aa0000;
    }
   }


@media screen and (max-width: 1366px){
  .uc-header-box {
    width: 1160px;
  }
  .learningData{
    margin-left:auto;
    .learning-info{
      margin-left: 10px;
      margin-right: 10px;
      .learning-qus{
        font-size: 12px;
      }
      .learning-an{
        font-size: 12px;
      }
    }
  }
  .personalData{
    .uesr-avaer{
      margin-right: 10px;
    }
    .user-content{
      .content-top{
        .grade{
          div{
              width: 48px;
          }
          span{
            font-size: 14px;
          }
        }
        h6{
          font-size: 26px;
        }
        span{
          font-size: 14px;
          margin-left: 5px;
          margin-right: 10px;
        }
        .editbutt{
          width: 80px;
          height: 25px;
          line-height: 5px;
          text-align: center;
          font-size: 12px;
        }
      }
    }
  }
  .content-bottom{
     .medalbutt{
       margin-right: 20px;
     }

  }
}
@media screen and (max-width: 1680px) and (min-width:1367px){
  .uc-header-box {
    width: 1300px;
  }
  .learningData{
    margin-left:auto;
    .learning-info{
      margin-left: 10px;
      margin-right: 12px;
    }
  }

  .personalData{
    .uesr-avaer{
      margin-right: 10px;
    }
    .user-content{
      .content-top{
        .grade{
          div{
              width: 48px;
          }
          span{
            font-size: 14px;
          }
        }
        h6{
          font-size: 26px;
        }
        span{
          font-size: 14px;
          margin-left: 5px;
          margin-right: 15px;
        }
        .editbutt{
          width: 80px;
          height: 25px;
          line-height: 5px;
          text-align: center;
          font-size: 12px;
        }
      }
    }
  }
}
@media screen and (max-width: 1920px) and (min-width: 1681px){
  .uc-header-box {
    width: 1600px;
  }
  .learningData{
    margin-left:auto;
    .learning-info{
      margin-left: 20px;
      margin-right: 22px;
    }
  }
}

@media screen and (min-width: 1921px){
  .uc-header-box {
    width: 1800px;
  }
  .learningData{
    margin-left:auto;
    .learning-info{
      margin-left: 30px;
      margin-right: 30px;
    }
  }
}
  </style>
