{% extends 'base.html' %}

{% block content %}
<div class="container mt-4">
    <h2 class="mb-4">Weekly Timetable</h2>

    {% if is_admin %}
        <a href="{% url 'create_timetable' %}" class="btn btn-primary mb-3">+ Add Timetable Entry</a>
    {% endif %}

    <!-- Tabs for switching views -->
    <ul class="nav nav-tabs mb-3" id="timetableTabs" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link active" id="table-tab" data-bs-toggle="tab" data-bs-target="#tableView" type="button" role="tab">Table View</button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="calendar-tab" data-bs-toggle="tab" data-bs-target="#calendarView" type="button" role="tab">Calendar View</button>
        </li>
    </ul>

    <div class="tab-content" id="timetableTabsContent">
        <!-- Table View -->
        <div class="tab-pane fade show active" id="tableView" role="tabpanel">
            {% for day, sessions in days.items %}
                <div class="card mb-4 shadow-sm">
                    <div class="card-header bg-light">
                        <h5 class="mb-0 text-capitalize">{{ day }}</h5>
                    </div>
                    <div class="card-body p-0">
                        {% if sessions %}
                            <table class="table table-striped table-hover mb-0">
                                <thead class="table-secondary">
                                    <tr>
                                        <th>Time</th>
                                        <th>Subject</th>
                                        <th>Trainer</th>
                                        <th>Location</th>
                                        <th>Description</th>
                                        {% if is_admin %}
                                            <th>Actions</th>
                                        {% endif %}
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for session in sessions %}
                                        <tr>
                                            <td>{{ session.start_time }} - {{ session.end_time }}</td>
                                            <td>{{ session.subject }}</td>
                                            <td>{{ session.trainer.get_full_name|default:session.trainer.username }}</td>
                                            <td>{{ session.location }}</td>
                                            <td>{{ session.description|default:"-" }}</td>
                                            {% if is_admin %}
                                                <td>
                                                    <a href="{% url 'edit_timetable' session.pk %}" class="btn btn-sm btn-warning">Edit</a>
                                                    <a href="{% url 'delete_timetable' session.pk %}" class="btn btn-sm btn-danger">Delete</a>
                                                </td>
                                            {% endif %}
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        {% else %}
                            <p class="p-3 text-muted">No sessions scheduled for this day.</p>
                        {% endif %}
                    </div>
                </div>
            {% endfor %}
        </div>

        <!-- Calendar View -->
        <div class="tab-pane fade" id="calendarView" role="tabpanel">
            <div id="calendar"></div>
        </div>
    </div>
</div>

<!-- FullCalendar JS + CSS -->
<link href="https://cdn.jsdelivr.net/npm/fullcalendar@6.1.15/index.global.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/fullcalendar@6.1.15/index.global.min.js"></script>

<script>
document.addEventListener('DOMContentLoaded', function () {
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
        initialView: 'timeGridWeek',
        slotMinTime: "07:00:00",
        slotMaxTime: "20:00:00",
        allDaySlot: false,
        events: "{% url 'timetable_json' %}",
        eventDisplay: 'block',
        height: "auto",
        headerToolbar: {
            left: 'prev,next today',
            center: 'title',
            right: 'timeGridWeek,timeGridDay,listWeek'
        }
    });

    // Render calendar only when calendar tab is clicked
    document.getElementById('calendar-tab').addEventListener('shown.bs.tab', function () {
        calendar.render();
    });
});
</script>
{% endblock %}
