<template>
  <div class="user-card">
    <!-- 标题栏 -->
    <div class="card-header">
      <div class="title-wrapper">
        <div class="title-bar"></div>
        <h3 class="title">个人信息</h3>
      </div>
    </div>

    <!-- 用户信息区域 -->
    <div class="user-info-section">
      <!-- 头像 -->
      <div class="avatar-wrapper">
        <img 
          :src="userInfo?.avatar || defaultAvatar" 
          :alt="userInfo?.nickname || userInfo?.username"
          class="avatar"
        />
      </div>

      <!-- 用户详细信息 -->
      <div class="user-details">
        <!-- 用户名和性别 -->
        <div class="user-name-row">
          <span class="username">{{ userInfo?.nickname || userInfo?.username || '未设置昵称' }}</span>
          <div class="gender-tag" v-if="userInfo?.gender && genderIcon">
            <img :src="genderIcon" :alt="genderText" class="gender-icon" />
            <span class="gender-text">{{ genderText }}</span>
          </div>
        </div>

        <!-- 详细信息 -->
        <div class="info-row">
          <span class="info-item">所属部门：{{ departmentName || '未分配部门' }}</span>
          <span class="info-item">联系方式：{{ userInfo?.phone || '未设置' }}</span>
          <div class="level-item">
            <span class="info-label">学习等级：</span>
            <img :src="arrowDownIcon" alt="等级" class="level-icon" />
            <span class="level-text">等级</span>
          </div>
        </div>
      </div>
      <button class="edit-btn" @click="handleEdit">编辑资料</button>

    </div>
  </div>
</template>

<script setup lang="ts" name="UserCard">
import { computed, ref, onMounted } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { UserVO } from '@/types';
import {userApi} from '@/apis/system/user'
import defaultAvatarImg from '@/assets/imgs/default-avatar.png';
import maleIcon from '@/assets/imgs/male.svg';
import femaleIcon from '@/assets/imgs/female.svg';
import arrowDownIcon from '@/assets/imgs/arrow-down.svg';

const store = useStore();
const router = useRouter();

// 默认头像
const defaultAvatar = defaultAvatarImg;

// 从 store 获取用户信息
const loginDomain = computed(() => store.state.auth.loginDomain);
const userInfo = ref<UserVO>();

// 获取部门名称
const departmentName = computed(() => {
  const roles = loginDomain.value?.roles || [];
  if (roles.length > 0 && roles[0].dept) {
    return roles[0].dept.name;
  }
  return '';
});

// 性别文本
const genderText = computed(() => {
  const gender = userInfo.value?.gender;
  if (gender === 1) return '男';
  if (gender === 2) return '女';
  return '';
});

// 性别图标
const genderIcon = computed(() => {
  const gender = userInfo.value?.gender;
  if (gender === 1) return maleIcon;
  if (gender === 2) return femaleIcon;
  return '';
});

// 编辑资料
const handleEdit = () => {
  router.push('/profile/personal-info');
};

onMounted(async () => {
  const res = await userApi.getCurrentUser();
  userInfo.value = res.data;
});
</script>

<style lang="scss" scoped>
.user-card {
  background: #FFFFFF;
  border-radius: 10px;
  max-width: 1200px;
  width: 100%;
  min-height: 190px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.title-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
}

.title-bar {
  width: 4px;
  height: 16px;
  background: #C62828;
  border-radius: 2px;
}

.title {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 600;
  font-size: 20px;
  line-height: 28px;
  color: #141F38;
  margin: 0;
}

.edit-btn {
  padding: 4px 16px;
  background: rgba(198, 40, 40, 0.05);
  border: 1px solid #C62828;
  border-radius: 4px;
  font-family: 'PingFang SC', sans-serif;
  font-weight: 400;
  font-size: 16px;
  line-height: 24px;
  color: #C62828;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    background: rgba(198, 40, 40, 0.1);
  }

  &:active {
    background: rgba(198, 40, 40, 0.15);
  }
}

.user-info-section {
  display: flex;
  align-items: center;
  gap: 20px;
}

.avatar-wrapper {
  flex-shrink: 0;
}

.avatar {
  width: 82px;
  height: 82px;
  border-radius: 50%;
  object-fit: cover;
}

.user-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.user-name-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.username {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 600;
  font-size: 20px;
  line-height: 24px;
  color: #141F38;
}

.gender-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px;
}

.gender-icon {
  width: 16px;
  height: 16px;
}

.gender-text {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 400;
  font-size: 14px;
  line-height: 24px;
  color: #141F38;
}

.info-row {
  display: flex;
  align-items: center;
  gap: 40px;
}

.info-item {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 400;
  font-size: 14px;
  line-height: 22px;
  color: #334155;
}

.level-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.info-label {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 400;
  font-size: 14px;
  line-height: 22px;
  color: #334155;
}

.level-icon {
  width: 18px;
  height: 12px;
}

.level-text {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 500;
  font-size: 12px;
  line-height: 24px;
  color: #334155;
}
</style>