{% extends 'layout.html' %}

{% block content %}
<div class="teams-admin-container">
    <!-- Header Section -->
    <div class="page-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="page-title">
                    <span class="page-icon">👥</span>
                    Team Management
                </h1>
                <p class="page-subtitle">Manage teams and their members across your organization</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('teams.create_team') }}" class="btn btn-primary">
                    <span class="icon">+</span>
                    Create New Team
                </a>
            </div>
        </div>
    </div>

    <!-- Team Statistics -->
    {% if teams %}
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-value">{{ teams|length if teams else 0 }}</div>
            <div class="stat-label">Total Teams</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ teams|map(attribute='users')|map('length')|sum if teams else 0 }}</div>
            <div class="stat-label">Total Members</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ (teams|map(attribute='users')|map('length')|sum / teams|length)|round(1) if teams else 0 }}</div>
            <div class="stat-label">Avg Team Size</div>
        </div>
    </div>
    {% endif %}

    <!-- Main Content -->
    <div class="teams-content">
        {% if teams %}
            <!-- Search Bar -->
            <div class="search-section">
                <div class="search-container">
                    <span class="search-icon">🔍</span>
                    <input type="text" 
                           class="search-input" 
                           id="teamSearch" 
                           placeholder="Search teams by name or description...">
                </div>
            </div>

            <!-- Teams Grid -->
            <div class="teams-grid" id="teamsGrid">
                {% for team in teams %}
                    <div class="team-card" data-team-name="{{ team.name.lower() }}" data-team-desc="{{ team.description.lower() if team.description else '' }}">
                        <div class="team-header">
                            <div class="team-icon-wrapper">
                                <span class="team-icon">👥</span>
                            </div>
                            <div class="team-meta">
                                <span class="member-count">{{ team.users|length }} members</span>
                            </div>
                        </div>
                        
                        <div class="team-body">
                            <h3 class="team-name">{{ team.name }}</h3>
                            <p class="team-description">
                                {{ team.description if team.description else 'No description provided' }}
                            </p>
                            
                            <div class="team-info">
                                <div class="info-item">
                                    <span class="info-icon">📅</span>
                                    <span class="info-text">Created {{ team.created_at.strftime('%b %d, %Y') }}</span>
                                </div>
                                {% if team.users %}
                                <div class="info-item">
                                    <span class="info-icon">👤</span>
                                    <span class="info-text">Led by {{ team.users[0].username }}</span>
                                </div>
                                {% endif %}
                            </div>
                            
                            <!-- Member Avatars -->
                            {% if team.users %}
                            <div class="member-avatars">
                                {% for member in team.users[:5] %}
                                    <div class="member-avatar" title="{{ member.username }}">
                                        {{ member.username[:2].upper() }}
                                    </div>
                                {% endfor %}
                                {% if team.users|length > 5 %}
                                    <div class="member-avatar more" title="{{ team.users|length - 5 }} more members">
                                        +{{ team.users|length - 5 }}
                                    </div>
                                {% endif %}
                            </div>
                            {% endif %}
                        </div>
                        
                        <div class="team-actions">
                            <a href="{{ url_for('teams.manage_team', team_id=team.id) }}" class="btn btn-manage">
                                <span class="icon">⚙️</span>
                                Manage Team
                            </a>
                            <form method="POST" 
                                  action="{{ url_for('teams.delete_team', team_id=team.id) }}" 
                                  class="delete-form"
                                  onsubmit="return confirm('Are you sure you want to delete the team \"{{ team.name }}\"? This action cannot be undone.');">
                                <button type="submit" class="btn btn-delete" title="Delete Team">
                                    <span class="icon">🗑️</span>
                                </button>
                            </form>
                        </div>
                    </div>
                {% endfor %}
            </div>
            
            <!-- No Results Message -->
            <div class="no-results" id="noResults" style="display: none;">
                <div class="empty-icon">🔍</div>
                <p class="empty-message">No teams found matching your search</p>
                <p class="empty-hint">Try searching with different keywords</p>
            </div>
        {% else %}
            <!-- Empty State -->
            <div class="empty-state">
                <div class="empty-icon">👥</div>
                <h2 class="empty-title">No Teams Yet</h2>
                <p class="empty-message">Create your first team to start organizing your workforce</p>
                <a href="{{ url_for('teams.create_team') }}" class="btn btn-primary btn-lg">
                    <span class="icon">+</span>
                    Create First Team
                </a>
            </div>
        {% endif %}
    </div>
</div>

<style>
/* Container */
.teams-admin-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2rem;
}

.page-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-icon {
    font-size: 2.5rem;
    display: inline-block;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.page-subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0.5rem 0 0 0;
}

/* Stats Section */
.stats-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    color: #667eea;
}

.stat-label {
    font-size: 0.9rem;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

/* Search Section */
.search-section {
    margin-bottom: 2rem;
}

.search-container {
    position: relative;
    max-width: 500px;
}

.search-icon {
    position: absolute;
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.25rem;
    opacity: 0.6;
}

.search-input {
    width: 100%;
    padding: 1rem 1rem 1rem 3rem;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    font-size: 1rem;
    transition: all 0.3s ease;
    background: white;
}

.search-input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* Teams Grid */
.teams-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

/* Team Card */
.team-card {
    background: white;
    border-radius: 12px;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

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

.team-header {
    background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
    padding: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.team-icon-wrapper {
    width: 60px;
    height: 60px;
    background: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.team-icon {
    font-size: 2rem;
}

.member-count {
    background: white;
    color: #6b7280;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.875rem;
    font-weight: 600;
}

.team-body {
    padding: 1.5rem;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.team-name {
    font-size: 1.5rem;
    font-weight: 700;
    color: #1f2937;
    margin: 0 0 0.5rem 0;
}

.team-description {
    color: #6b7280;
    line-height: 1.6;
    margin-bottom: 1.5rem;
    flex: 1;
}

.team-info {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}

.info-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: #6b7280;
    font-size: 0.875rem;
}

.info-icon {
    font-size: 1rem;
}

/* Member Avatars */
.member-avatars {
    display: flex;
    margin-bottom: 1rem;
}

.member-avatar {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    font-weight: 600;
    margin-right: -8px;
    border: 2px solid white;
    position: relative;
    z-index: 1;
    transition: all 0.2s ease;
}

.member-avatar:hover {
    transform: scale(1.1);
    z-index: 10;
}

.member-avatar.more {
    background: #6b7280;
    font-size: 0.75rem;
}

/* Team Actions */
.team-actions {
    padding: 1.5rem;
    background: #f8f9fa;
    border-top: 1px solid #e5e7eb;
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.delete-form {
    margin: 0;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.btn-manage {
    background: white;
    color: #667eea;
    border: 2px solid #e5e7eb;
    flex: 1;
    justify-content: center;
}

.btn-manage:hover {
    background: #f3f4f6;
    border-color: #667eea;
}

.btn-delete {
    background: #fee2e2;
    color: #dc2626;
    padding: 0.75rem;
    min-width: auto;
}

.btn-delete:hover {
    background: #dc2626;
    color: white;
}

.btn-lg {
    padding: 1rem 2rem;
    font-size: 1.1rem;
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    background: white;
    border-radius: 12px;
    border: 2px dashed #e5e7eb;
}

.empty-icon {
    font-size: 4rem;
    margin-bottom: 1.5rem;
    opacity: 0.3;
}

.empty-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.empty-message {
    font-size: 1.1rem;
    color: #6b7280;
    margin-bottom: 2rem;
}

.empty-hint {
    color: #9ca3af;
    font-size: 0.875rem;
}

/* No Results */
.no-results {
    text-align: center;
    padding: 3rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    .teams-admin-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .page-title {
        font-size: 2rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .header-stats {
        grid-template-columns: 1fr;
    }
    
    .teams-grid {
        grid-template-columns: 1fr;
    }
    
    .team-actions {
        flex-direction: column;
    }
    
    .btn-manage {
        width: 100%;
    }
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.team-card {
    animation: slideIn 0.4s ease-out;
    animation-fill-mode: both;
}

.team-card:nth-child(1) { animation-delay: 0.05s; }
.team-card:nth-child(2) { animation-delay: 0.1s; }
.team-card:nth-child(3) { animation-delay: 0.15s; }
.team-card:nth-child(4) { animation-delay: 0.2s; }
.team-card:nth-child(5) { animation-delay: 0.25s; }
.team-card:nth-child(6) { animation-delay: 0.3s; }
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.getElementById('teamSearch');
    const teamsGrid = document.getElementById('teamsGrid');
    const noResults = document.getElementById('noResults');
    
    if (searchInput && teamsGrid) {
        searchInput.addEventListener('input', function() {
            const searchTerm = this.value.toLowerCase().trim();
            const teamCards = teamsGrid.querySelectorAll('.team-card');
            let visibleCount = 0;
            
            teamCards.forEach(card => {
                const teamName = card.getAttribute('data-team-name');
                const teamDesc = card.getAttribute('data-team-desc');
                
                if (teamName.includes(searchTerm) || teamDesc.includes(searchTerm)) {
                    card.style.display = '';
                    visibleCount++;
                } else {
                    card.style.display = 'none';
                }
            });
            
            // Show/hide no results message
            if (noResults) {
                noResults.style.display = visibleCount === 0 ? 'block' : 'none';
            }
        });
    }
});
</script>

{% endblock %}