<template>
  <div class="app-aside">
    <!-- Logo -->
    <div class="logo">
      <div class="logo-icon">PK</div>
    </div>

    <!-- 导航菜单 -->
    <div class="navigation">
      <div
        v-for="item in navigationModule"
        :key="item.id"
        class="nav-card"
        :class="{ active: item.id === activeId }"
        @click="handleClick(item.id)"
      >
        <span class="material-icons-round nav-icon">{{ item.icon }}</span>
        <div class="nav-name">{{ item.name }}</div>
        <div v-if="item.id === 'message' && unreadCount > 0" class="red-dot">
          {{ unreadCount > 99 ? '99+' : unreadCount }}
        </div>
      </div>
    </div>

    <!-- 签到按钮 -->
    <div class="sign-in-section">
      <div class="sign-in-btn" @click="handleSignIn">
        <span class="material-icons-round sign-icon">event_available</span>
        <div class="sign-text">签到</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { getMainUserData, setStorage, getStorage } from '@/utils/pk-mini/storage'
import { goEasyGetConversations, getPkGoEasy, GoEasy } from '@/utils/pk-mini/goeasy'
import { pkUnreadStore } from '@/stores/pk-mini/notice.js'
import { signIn } from '@/api/pk-mini'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['navigate'])

const props = defineProps({
  active: {
    type: String,
    default: 'pk'
  }
})

const defaultAvatar = 'https://vv-1317974657.cos.ap-shanghai.myqcloud.com/util/default-avatar.png'
const userInfo = ref({})
const unreadStore = pkUnreadStore()
const unreadCount = computed(() => unreadStore.count)
const activeId = ref('pk')

const navigationModule = [
  { id: 'pk', name: 'PK', icon: 'sports_esports' },
  // { id: 'forum', name: '站内信', icon: 'mail' },
  { id: 'message', name: '消息', icon: 'chat' },
  { id: 'mine', name: '我的', icon: 'person' }
]

function handleClick(id) {
  activeId.value = id
  setStorage('activeId', id)
  emit('navigate', id)
}

function handleSignIn() {
  if (!userInfo.value.id) return
  signIn({ userId: userInfo.value.id }).then(() => {
    ElMessage.success('签到成功')
  }).catch(() => {})
}

function handleSettings() {
  ElMessage.info('设置功能开发中')
}

function getChatList() {
  goEasyGetConversations().then((res) => {
    unreadStore.setCount(res?.content?.unreadTotal || 0)
  }).catch(() => {})
}

function onMessageReceived() {
  unreadStore.setCount(unreadStore.count + 1)
}

onMounted(() => {
  const userData = getMainUserData()
  if (userData) {
    userInfo.value = userData
  }

  const savedId = getStorage('activeId')
  if (savedId) {
    activeId.value = savedId
  }

  setTimeout(() => {
    getChatList()
    const goeasy = getPkGoEasy()
    if (goeasy) {
      goeasy.im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, onMessageReceived)
    }
  }, 2000)
})

onUnmounted(() => {
  const goeasy = getPkGoEasy()
  if (goeasy) {
    try {
      goeasy.im.off(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, onMessageReceived)
    } catch (e) {}
  }
})
</script>

<style scoped lang="less">
.app-aside {
  width: 100%;
  height: 95%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0;
  user-select: none;
}

.logo {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: linear-gradient(135deg, #3b82f6, #2563eb); // from-blue-500 to-blue-600
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4); // shadow-blue-500/40
}

.logo-icon {
  color: white;
  font-size: 18px;
  font-weight: bold;
}

.navigation {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  margin-top: 30px;
}

.nav-card {
  position: relative;
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: #f8fafc; // slate-50
}

.nav-card:hover {
  background-color: #ffffff;
  transform: scale(1.05);
}

.nav-card.active {
  background-color: #ffffff;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); // shadow-md
}

.nav-icon {
  font-size: 24px;
  color: #64748b; // slate-500
}

.nav-card.active .nav-icon {
  color: #2563eb; // blue-600
}

.nav-name {
  font-size: 10px;
  color: #64748b; // slate-500
  margin-top: 4px;
}

.nav-card.active .nav-name {
  color: #2563eb; // blue-600
}

.red-dot {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background-color: #ef4444; // red-500
  color: white;
  font-size: 10px;
  text-align: center;
  line-height: 18px;
}

.sign-in-section {
  margin-top: auto;
}

.sign-in-btn {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: #f8fafc; // slate-50
}

.sign-in-btn:hover {
  background-color: #ffffff;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); // shadow-sm
  transform: scale(1.05);
}

.sign-icon {
  font-size: 24px;
  color: #2563eb; // blue-600
}

.sign-text {
  font-size: 10px;
  color: #2563eb; // blue-600
  margin-top: 4px;
  font-weight: bold;
}
</style>
