<template>
  <div class="friends-management" :class="{ 'dark': themeStore.isDarkMode }">
    <div class="management-content">
      <h1 class="page-title">好友管理</h1>

      <!-- 搜索和筛选 -->
      <div class="search-filter-section">
        <CustomInput
          v-model="searchQuery"
          placeholder="搜索好友..."
          prefix-icon="fas fa-search"
          clearable
          class="search-input"
        />
        <select class="filter-select">
          <option value="all">全部好友</option>
          <option value="online">在线好友</option>
          <option value="offline">离线好友</option>
        </select>
      </div>

      <!-- 好友网格 -->
      <div class="friends-grid">
        <div
          v-for="friend in filteredFriends"
          :key="friend.id"
          class="friend-card"
        >
          <div class="card-content">
            <div
              class="friend-avatar"
              :style="{ background: friend.color }"
            >
              {{ friend.nick_name.charAt(0) }}
            </div>
            <h3 class="friend-name">{{ friend.nick_name }}</h3>
            <p class="friend-id">ID: {{ friend.id }}</p>
            <div class="friend-actions">
              <button class="action-btn primary-btn" @click="startChat(friend)">
                聊天
              </button>
              <button class="action-btn secondary-btn">
                详情
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, computed} from 'vue';
import {useChatStore} from '../stores/chat.ts';
import {useUserStore} from '../stores/user.ts';
import {useThemeStore} from '../stores/theme.ts';
import CustomInput from './CustomInput.vue';

const chatStore = useChatStore();
const userStore = useUserStore();
const themeStore = useThemeStore();

const searchQuery = ref('');

const filteredFriends = computed(() => {
  if (!searchQuery.value) return chatStore.friends;
  const query = searchQuery.value.toLowerCase();
  return chatStore.friends.filter(friend =>
    friend.nick_name.toLowerCase().includes(query) ||
    friend.id.toString().includes(query)
  );
});

const startChat = (friend) => {
  chatStore.setCurrentFriend(friend);
  chatStore.switchFriend(friend, userStore.currentUser.doctor_id);
};
</script>

<style scoped>
.friends-management {
  height: 100%;
  background: #f8fafc;
  overflow-y: auto;
}

.friends-management.dark {
  background: #111827;
}

.management-content {
  padding: 24px;
}

.page-title {
  font-size: 28px;
  font-weight: 700;
  color: #1a202c;
  margin-bottom: 24px;
}

.friends-management.dark .page-title {
  color: #f7fafc;
}

.search-filter-section {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
}

.search-input {
  flex: 1;
}

.filter-select {
  padding: 12px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  background: white;
  color: #1a202c;
  font-size: 14px;
  min-width: 140px;
  cursor: pointer;
}

.friends-management.dark .filter-select {
  background: #374151;
  border-color: #4b5563;
  color: #f7fafc;
}

.friends-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.friend-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  border: 1px solid #e2e8f0;
}

.friend-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.friends-management.dark .friend-card {
  background: #374151;
  border-color: #4b5563;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.friends-management.dark .friend-card:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.card-content {
  padding: 24px;
  text-align: center;
}

.friend-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin: 0 auto 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.friend-name {
  font-size: 18px;
  font-weight: 600;
  color: #1a202c;
  margin: 0 0 8px 0;
}

.friends-management.dark .friend-name {
  color: #f7fafc;
}

.friend-id {
  font-size: 14px;
  color: #64748b;
  margin: 0 0 20px 0;
}

.friends-management.dark .friend-id {
  color: #9ca3af;
}

.friend-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.action-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.primary-btn {
  background: linear-gradient(135deg, #4361ee, #3f37c9);
  color: white;
}

.primary-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(67, 97, 238, 0.3);
}

.secondary-btn {
  background: #f1f5f9;
  color: #64748b;
  border: 1px solid #e2e8f0;
}

.secondary-btn:hover {
  background: #e2e8f0;
  color: #475569;
}

.friends-management.dark .secondary-btn {
  background: #4b5563;
  color: #d1d5db;
  border-color: #6b7280;
}

.friends-management.dark .secondary-btn:hover {
  background: #6b7280;
  color: #f3f4f6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .management-content {
    padding: 16px;
  }

  .search-filter-section {
    flex-direction: column;
  }

  .friends-grid {
    grid-template-columns: 1fr;
  }
}
</style>
