<template>
   <div class="app-container">
      <el-row :gutter="20">
         <el-col :span="6" :xs="24">
            <el-card class="box-card" shadow="never">
               <template v-slot:header>
                 <div class="card-header">
                   <span>个人信息</span>
                 </div>
               </template>
               <div class="profile-box">
                  <div class="text-center">
                     <userAvatar />
                     <div class="user-name">{{ state.user.nickName || state.user.userName }}</div>
                     <div class="user-role">{{ state.roleGroup }}</div>
                  </div>
                  <ul class="list-group list-group-striped">
                     <li class="list-group-item">
                        <svg-icon icon-class="user" />
                        <span class="list-label">用户名称</span>
                        <div class="pull-right">{{ state.user.userName }}</div>
                     </li>
                     <li class="list-group-item">
                        <svg-icon icon-class="phone" />
                        <span class="list-label">手机号码</span>
                        <div class="pull-right">{{ state.user.phonenumber }}</div>
                     </li>
                     <li class="list-group-item">
                        <svg-icon icon-class="email" />
                        <span class="list-label">用户邮箱</span>
                        <div class="pull-right">{{ state.user.email }}</div>
                     </li>
                     <li class="list-group-item">
                        <svg-icon icon-class="tree" />
                        <span class="list-label">所属部门</span>
                        <div class="pull-right" v-if="state.user.dept">{{ state.user.dept.deptName }} / {{ state.postGroup }}</div>
                     </li>
                     <li class="list-group-item">
                        <svg-icon icon-class="date" />
                        <span class="list-label">创建日期</span>
                        <div class="pull-right">{{ state.user.createTime }}</div>
                     </li>
                  </ul>
               </div>
            </el-card>
         </el-col>
         <el-col :span="18" :xs="24">
            <el-card class="box-card" shadow="never">
               <template v-slot:header>
                 <div class="card-header">
                   <span>基本资料</span>
                 </div>
               </template>
               <el-tabs v-model="selectedTab" class="custom-tabs">
                  <el-tab-pane label="基本资料" name="userinfo">
                     <userInfo :user="state.user" />
                  </el-tab-pane>
                  <el-tab-pane label="修改密码" name="resetPwd">
                     <resetPwd />
                  </el-tab-pane>
               </el-tabs>
            </el-card>
         </el-col>
      </el-row>
   </div>
</template>

<script setup name="Profile">
import userAvatar from "./userAvatar"
import userInfo from "./userInfo"
import resetPwd from "./resetPwd"
import { getUserProfile } from "@/api/system/user"

const route = useRoute()
const selectedTab = ref("userinfo")
const state = reactive({
  user: {},
  roleGroup: {},
  postGroup: {}
})

function getUser() {
  getUserProfile().then(response => {
    state.user = response.data
    state.roleGroup = response.roleGroup
    state.postGroup = response.postGroup
  })
}

onMounted(() => {
  const activeTab = route.params && route.params.activeTab
  if (activeTab) {
    selectedTab.value = activeTab
  }
  getUser()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);
}

.box-card {
  border: none;
  border-radius: 8px;
  background: #fff;
  margin-bottom: 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.card-header {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  line-height: 1.5;
}

.profile-box {
  padding: 10px 0;
  
  .user-name {
    font-size: 20px;
    font-weight: 600;
    margin-top: 15px;
    color: #303133;
  }
  
  .user-role {
    font-size: 13px;
    color: #909399;
    margin-top: 5px;
    margin-bottom: 20px;
  }
}

.list-group {
  padding-left: 0;
  list-style: none;
  border-top: 1px solid #ebeef5;
}

.list-group-item {
  border-bottom: 1px solid #ebeef5;
  padding: 14px 0;
  font-size: 14px;
  color: #606266;
  display: flex;
  align-items: center;
  
  .svg-icon {
    margin-right: 10px;
    font-size: 16px;
    color: #909399;
  }
  
  .list-label {
    flex: 1;
  }
  
  .pull-right {
    color: #303133;
    font-weight: 500;
  }
}

.custom-tabs {
  :deep(.el-tabs__nav-wrap::after) {
    background-color: transparent;
  }
  
  :deep(.el-tabs__item) {
    font-size: 15px;
    color: #606266;
    
    &.is-active {
      color: var(--el-color-primary);
      font-weight: 600;
    }
  }
}
</style>
