{% extends 'base.html' %}
{% load static %}

{% block title %}Staff Members - Rapha-Bethel BNPS{% endblock %}

{% block extra_css %}
<style>
    .staff-card {
        transition: transform 0.3s, box-shadow 0.3s;
        border-left: 4px solid transparent;
    }
    .staff-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.1);
    }
    .staff-card.admin { border-left-color: #dc3545; }
    .staff-card.teacher { border-left-color: #28a745; }
    .staff-card.support { border-left-color: #ffc107; }
    
    .staff-avatar {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        background: linear-gradient(135deg, #4CAF50, #2196F3);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.5rem;
        font-weight: bold;
    }
    
    .role-badge {
        position: absolute;
        top: 10px;
        right: 10px;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0 text-gray-800">Staff Members</h1>
            <p class="text-muted">Total: {{ total_staff }} | Active: {{ active_staff }}</p>
        </div>
        <div class="btn-group">
            <a href="{% url 'staff:staff_add' %}" class="btn btn-primary">
                <i class="fas fa-user-plus"></i> Add Staff
            </a>
            <a href="{% url 'staff:staff_import' %}" class="btn btn-success">
                <i class="fas fa-upload"></i> Import
            </a>
            <a href="{% url 'staff:staff_export' %}" class="btn btn-outline-secondary">
                <i class="fas fa-download"></i> Export
            </a>
        </div>
    </div>

    <!-- Search and Filter -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Search & Filter</h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <div class="input-group">
                        <input type="text" class="form-control" name="q" 
                               placeholder="Search by name, ID, or phone..." 
                               value="{{ request.GET.q }}">
                        <button class="btn btn-primary" type="submit">
                            <i class="fas fa-search"></i>
                        </button>
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-control" name="role">
                        <option value="">All Roles</option>
                        {% for role_value, role_name in role_choices %}
                        <option value="{{ role_value }}" 
                                {% if request.GET.role == role_value %}selected{% endif %}>
                            {{ role_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <select class="form-control" name="department">
                        <option value="">All Departments</option>
                        {% for dept_value, dept_name in department_choices %}
                        <option value="{{ dept_value }}" 
                                {% if request.GET.department == dept_value %}selected{% endif %}>
                            {{ dept_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-filter"></i> Filter
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Staff Grid -->
    <div class="row">
        {% for staff in staff_members %}
        <div class="col-xl-3 col-md-4 col-sm-6 mb-4">
            <div class="card staff-card {{ staff.department }} h-100 position-relative">
                <div class="card-body">
                    <div class="role-badge">
                        <span class="badge bg-{% if staff.role == 'admin' %}danger{% elif staff.role == 'teacher' %}success{% else %}warning{% endif %}">
                            {{ staff.get_role_display }}
                        </span>
                    </div>
                    
                    <div class="d-flex align-items-center mb-3">
                        <div class="staff-avatar me-3">
                            {% if staff.photo %}
                            <img src="{{ staff.photo.url }}" class="w-100 h-100 rounded-circle" alt="{{ staff.get_display_name }}">
                            {% else %}
                            {{ staff.user.first_name|first|upper }}{{ staff.user.last_name|first|upper }}
                            {% endif %}
                        </div>
                        <div>
                            <h5 class="card-title mb-1">{{ staff.get_display_name }}</h5>
                            <p class="text-muted small mb-0">ID: {{ staff.employee_id }}</p>
                        </div>
                    </div>
                    
                    <div class="staff-info mb-3">
                        <p class="mb-1">
                            <i class="fas fa-phone text-primary me-2"></i>
                            {% if staff.phone_number %}{{ staff.phone_country_code }} {{ staff.phone_number }}{% else %}No phone{% endif %}
                        </p>
                        <p class="mb-1">
                            <i class="fas fa-building text-success me-2"></i>
                            {{ staff.get_department_display }}
                        </p>
                        <p class="mb-1">
                            <i class="fas fa-calendar-alt text-info me-2"></i>
                            Hired: {{ staff.hire_date|date:"M Y"|default:"Not set" }}
                        </p>
                    </div>
                    
                    <div class="d-flex justify-content-between align-items-center">
                        <span class="badge bg-{% if staff.is_active %}success{% else %}secondary{% endif %}">
                            {{ staff.is_active|yesno:"Active,Inactive" }}
                        </span>
                        <div>
                            <a href="{% url 'staff:staff_detail' staff.pk %}" class="btn btn-sm btn-outline-primary">
                                <i class="fas fa-eye"></i>
                            </a>
                            <a href="{% url 'staff:staff_edit' staff.pk %}" class="btn btn-sm btn-outline-warning">
                                <i class="fas fa-edit"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% empty %}
        <div class="col-12">
            <div class="text-center py-5">
                <i class="fas fa-users fa-3x text-muted mb-3"></i>
                <h4 class="text-muted">No staff members found</h4>
                <p class="text-muted">Add your first staff member to get started</p>
                <a href="{% url 'staff:staff_add' %}" class="btn btn-primary">
                    <i class="fas fa-user-plus"></i> Add Staff
                </a>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- Pagination -->
    {% if is_paginated %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">
                    Previous
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
            {% if page_obj.number == num %}
            <li class="page-item active">
                <a class="page-link" href="#">{{ num }}</a>
            </li>
            {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
            <li class="page-item">
                <a class="page-link" href="?page={{ num }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">
                    {{ num }}
                </a>
            </li>
            {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">
                    Next
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}