<template>
  <div class="app-container" style="height: 100%;overflow:auto;">
    <el-row v-if="user" style="height: 100%;">
      <el-col :span="6" :xs="24" style="padding: 10px;height: 100%;box-sizing: border-box">
        <user-card :user="userCardInfo" :avatar="avatar" ref="userCard" style="height: 100%;min-height: 772px;"/>
      </el-col>

      <el-col :span="18" :xs="24" style="padding: 10px;height: 100%;box-sizing: border-box">
        <el-card class="r-card" style="height: 100%;min-height: 772px;" :body-style="{padding:'0 20px',height: '100%'}">
          <el-tabs v-model="activeTab">
            <el-tab-pane :label="userLabel" :name="userLabel">
              <user-update ref="userUpdate" />
            </el-tab-pane>
            <el-tab-pane :label="bzLabel" :name="bzLabel">
              <bz-update ref="bzUpdate" />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>

    </el-row>
  </div>
</template>

<script>
import UserCard from './components/UserCard'
import UserUpdate from './components/UserUpdate'
import BzUpdate from './components/BzUpdate'
import { getToken } from '@main/utils/auth'
import { query,getBzList } from '@main/api/user.js'

export default {
  name: 'Profile',
  components: { UserCard,UserUpdate, BzUpdate },
  data() {
    return {
      user: {},
      activeTab: this.$t("hivuiMain_app_user", { default: '用户' }),
      userInfo:{},
      bzList:[],
      userLabel: this.$t("hivuiMain_app_user", { default: '用户' }),
      bzLabel: this.$t("hivuiMain_app_orgbz", { default: '岗编' })
    }
  },
  computed: {
    avatar(){
      let me=this;
      if(me.userInfo && me.userInfo.FUSERPHOTO){
        return me._getImageUrl(me.userInfo.FUSERPHOTO)
      }else{
        return false
      }
    },
    userCardInfo(){
      let me=this;
      console.log('userInfo', me.userInfo)
      return Object.assign(me.user, me.userInfo || {})
    },
    reviewUrl(){
      let me=this;
      return me.$HI.review.includes(window.HIVUI_SETTING.url) ? me.$HI.review : window.HIVUI_SETTING.url + me.$HI.review
    }
  },
  mounted() {
    let me=this;
    me.getUser();
    me.getUserInfo();
    me.getBzList();
  },
  methods: {
    getBzList(){
      let me=this;
      getBzList().then(res =>{
        me.bzList = res.dataPack;
        let curtBz = me.bzList.filter(res => res.fdefaultbz)[0]
        me.$refs.userCard.bzList = me.bzList
        me.$refs.bzUpdate.bzList = me.bzList
        if(curtBz && curtBz.fbzid){
          me.$refs.bzUpdate.curtBzId = curtBz.fbzid
        }
      })
    },
    getUser() {
      let me=this;
      me.user = top.Scp.User
    },
    getUserInfo(){
      let me=this;
      var param = {
        modelFilePath: "/platf/user-info/entity/SysUserInfoBizEntity.xml",
        pageSize: 10,
        pageNum: 1,
        __zcQuery: false,
        __returnCount: true,
        __bottomSummary: false,
      }
      let userId = top.Scp.User && top.Scp.User.fuserid
      if(userId){
        let queryCdions = {
          join: 'and',
          items:[
            {
              sign: 'eq',
              name: 'FUSERID',
              dataType: 'dbString',
              value: userId,
              enable: true
            }
          ]
        }
        let __body = JSON.stringify({queryCdions: queryCdions})
        param.__body = __body
        query(param).then((res)=>{
          let data = res.dataPack && res.dataPack.rows
          if(data && data.length){
            me.userInfo = data[0]
            let form = {
              FSEX: "",
              FBIRTHDAY: "",
              FMBPHONE: "",
              FEMAIL: "",
              FQQ: "",
              FWEIXIN: "",
              FUSERSFZ: "",
              FADDRESS: [],
              FUSERPHOTO: '',
              FUSERSFZPIC1: '',
              FUSERSFZPIC2: '',
            }
            form = Object.assign(form, data[0])
            me.$refs.userUpdate.form = form
            me.$refs.userUpdate.copyForm = {...form}
          }
        })
      }
    },
    _getImageUrl(path) {
      let me=this;
      let token = getToken();
      if (typeof path == "string") {
        path = path.replace(/\\/g, "/");
        if(path.includes(";") && path.split(";").length >= 2){
          path = path.split(";")[2];
        }
      }
      return `${me.reviewUrl}?relativePath=${path}&access_token=${token}`;
    },
  }
}
</script>
