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

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

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">Staff Attendance</h1>
            <p class="text-muted">Track and manage staff attendance records</p>
        </div>
        <a href="{% url 'staff:mark_attendance' %}" class="btn btn-primary">
            <i class="fas fa-clipboard-check me-2"></i>Mark Today's Attendance
        </a>
    </div>

    <!-- Filter Card -->
    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Staff Member</label>
                    <select name="staff" class="form-control">
                        <option value="">All Staff</option>
                        {% for s in all_staff %}
                        <option value="{{ s.pk }}" {% if request.GET.staff == s.pk|stringformat:"s" %}selected{% endif %}>
                            {{ s.user.get_full_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Date From</label>
                    <input type="date" name="date_from" class="form-control" value="{{ request.GET.date_from }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Date To</label>
                    <input type="date" name="date_to" class="form-control" value="{{ request.GET.date_to }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-control">
                        <option value="">All Statuses</option>
                        <option value="present" {% if request.GET.status == "present" %}selected{% endif %}>Present</option>
                        <option value="absent" {% if request.GET.status == "absent" %}selected{% endif %}>Absent</option>
                        <option value="late" {% if request.GET.status == "late" %}selected{% endif %}>Late</option>
                        <option value="leave" {% if request.GET.status == "leave" %}selected{% endif %}>On Leave</option>
                    </select>
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary me-2"><i class="fas fa-filter me-1"></i>Filter</button>
                    <a href="{% url 'staff:attendance_list' %}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <!-- Summary Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Present Today</div>
                    <div class="h5 mb-0 font-weight-bold">{{ today_present|default:"0" }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-danger shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-danger text-uppercase mb-1">Absent Today</div>
                    <div class="h5 mb-0 font-weight-bold">{{ today_absent|default:"0" }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Late Today</div>
                    <div class="h5 mb-0 font-weight-bold">{{ today_late|default:"0" }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-info text-uppercase mb-1">On Leave</div>
                    <div class="h5 mb-0 font-weight-bold">{{ today_leave|default:"0" }}</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Attendance Table -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Attendance Records</h6>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>Staff Member</th>
                            <th>Role</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Check In</th>
                            <th>Check Out</th>
                            <th>Remarks</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for record in attendance_records %}
                        <tr>
                            <td>
                                <div class="fw-bold">{{ record.staff.user.get_full_name }}</div>
                                <small class="text-muted">{{ record.staff.employee_id }}</small>
                            </td>
                            <td><span class="badge bg-secondary">{{ record.staff.get_role_display }}</span></td>
                            <td>{{ record.date|date:"d M Y" }}</td>
                            <td>
                                <span class="badge bg-{% if record.status == 'present' %}success{% elif record.status == 'absent' %}danger{% elif record.status == 'late' %}warning{% elif record.status == 'leave' or record.status == 'sick' %}info{% else %}secondary{% endif %}">
                                    {{ record.get_status_display }}
                                </span>
                            </td>
                            <td>{{ record.check_in_time|default:"—" }}</td>
                            <td>{{ record.check_out_time|default:"—" }}</td>
                            <td class="text-muted small">{{ record.remarks|default:"—" }}</td>
                            <td>
                                <a href="{% url 'staff:attendance_detail' record.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-4 text-muted">
                                <i class="fas fa-clipboard fa-2x mb-2 d-block"></i>
                                No attendance records found.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {% if is_paginated %}
    <nav 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 }}">Previous</a></li>
            {% endif %}
            <li class="page-item active"><a class="page-link" href="#">{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</a></li>
            {% if page_obj.has_next %}
            <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
