<template>
  <div class="app">
    <Sidebar />
    
    <div class="main-wrapper">
      <Header title="Команда" subtitle="Наша команда специалистов" />

      <main class="main">
        <div v-if="loading" class="loading">
          <div class="spinner"></div>
          <span>Загрузка...</span>
        </div>

        <div v-else class="team-grid">
          <div 
            v-for="user in users" 
            :key="user.id" 
            class="team-card"
          >
            <div class="card-avatar">
              <img :src="getFullUrl(user.avatar_url)" :alt="user.name">
            </div>
            <div class="card-info">
              <h3 class="card-name">{{ user.name }}</h3>
              <div class="card-meta">
                <span v-if="user.department" class="card-department">{{ user.department }}</span>
                <span class="card-username">@{{ user.username }}</span>
              </div>
              <a 
                :href="'https://t.me/' + user.telegram.replace('@', '')" 
                target="_blank" 
                class="card-telegram"
              >
                <i data-lucide="send"></i>
                {{ user.telegram }}
              </a>
            </div>
          </div>
        </div>
      </main>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUpdated } from 'vue'
import Sidebar from '../components/Sidebar.vue'
import Header from '../components/Header.vue'
import { usersApi, getFullUrl } from '../api'

const users = ref([])
const loading = ref(true)

const fetchUsers = async () => {
  try {
    const data = await usersApi.getAll()
    if (data.success) {
      users.value = data.data
    }
  } catch (error) {
    console.error('Ошибка загрузки команды:', error)
  } finally {
    loading.value = false
  }
}

const refreshIcons = () => {
  if (window.lucide) {
    window.lucide.createIcons()
  }
}

onMounted(() => {
  fetchUsers()
  refreshIcons()
})

onUpdated(refreshIcons)
</script>

<style scoped>
.app {
  display: flex;
  min-height: 100vh;
  background: var(--bg);
}

.main-wrapper {
  flex: 1;
  margin-left: 64px;
  display: flex;
  flex-direction: column;
}

.main {
  flex: 1;
  padding: 0 20px 20px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 80px;
  color: var(--text-secondary);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.team-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
}

.team-card {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 24px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  transition: all 0.2s ease;
}

.team-card:hover {
  border-color: var(--accent);
}

.card-avatar {
  flex-shrink: 0;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.1);
}

.card-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.card-name {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.card-username {
  font-size: 14px;
  color: var(--text-muted);
}

.card-department {
  padding: 2px 8px;
  font-size: 10px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
}

.card-telegram {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 6px 12px;
  background: rgba(0, 136, 204, 0.15);
  color: #0088cc;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.15s ease;
  width: fit-content;
}

.card-telegram:hover {
  background: rgba(0, 136, 204, 0.25);
}

.card-telegram i {
  width: 14px;
  height: 14px;
}
</style>
