<!-- templates/interviews/preview_schedule.html -->
{% extends "base.html" %}
{% load static %}

{% block content %}
<div class="container mt-4">
    <h1>Interview Schedule Preview for {{ job.title }}</h1>

    <div class="card mt-4">
        <div class="card-body">
            <h5>Schedule Details</h5>
            <div class="row">
                <div class="col-md-6">
                    <p><strong>Period:</strong> {{ start_date|date:"F j, Y" }} to {{ end_date|date:"F j, Y" }}</p>
                    <p><strong>Working Days:</strong>
                        {% for day_id in working_days %}
                            {% if day_id == 0 %}Monday{% endif %}
                            {% if day_id == 1 %}Tuesday{% endif %}
                            {% if day_id == 2 %}Wednesday{% endif %}
                            {% if day_id == 3 %}Thursday{% endif %}
                            {% if day_id == 4 %}Friday{% endif %}
                            {% if day_id == 5 %}Saturday{% endif %}
                            {% if day_id == 6 %}Sunday{% endif %}
                            {% if not forloop.last %}, {% endif %}
                        {% endfor %}
                    </p>
                    <p><strong>Working Hours:</strong> {{ start_time|time:"g:i A" }} to {{ end_time|time:"g:i A" }}</p>
                </div>
                <div class="col-md-6">
                    {% if breaks %}
                    <p><strong>Break Times:</strong></p>
                    <ul>
                        {% for break in breaks %}
                        <li>{{ break.start_time|time:"g:i A" }} to {{ break.end_time|time:"g:i A" }}</li>
                        {% endfor %}
                    </ul>
                    {% endif %}
                    <p><strong>Interview Duration:</strong> {{ interview_duration }} minutes</p>
                    <p><strong>Buffer Time:</strong> {{ buffer_time }} minutes</p>
                </div>
            </div>
        </div>
    </div>

    <div class="card mt-4">
        <div class="card-body">
            <h5>Scheduled Interviews</h5>

            <!-- Calendar View -->
            <div id="calendar-container">
                <div id="calendar"></div>
            </div>

            <!-- List View -->
            <div class="table-responsive mt-4">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Time</th>
                            <th>Candidate</th>
                            <th>Email</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in schedule %}
                        <tr>
                            <td>{{ item.date|date:"F j, Y" }}</td>
                            <td>{{ item.time|time:"g:i A" }}</td>
                            <td>{{ item.candidate.name }}</td>
                            <td>{{ item.candidate.email }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <form method="post" class="mt-4">
                {% csrf_token %}
                <button type="submit" name="confirm_schedule" class="btn btn-success">
                    <i class="fas fa-check"></i> Confirm Schedule
                </button>
                <a href="{% url 'schedule_interviews' job_id=job.id %}" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Back to Edit
                </a>
            </form>
        </div>
    </div>
</div>

<!-- Include FullCalendar CSS and JS -->
<link href="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.js"></script>

<script>
document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
        initialView: 'dayGridMonth',
        headerToolbar: {
            left: 'prev,next today',
            center: 'title',
            right: 'dayGridMonth,timeGridWeek'
        },
        events: [
            {% for item in schedule %}
            {
                title: '{{ item.candidate.name }}',
                start: '{{ item.date|date:"Y-m-d" }}T{{ item.time|time:"H:i:s" }}',
                url: '#',
                extendedProps: {
                    email: '{{ item.candidate.email }}',
                    time: '{{ item.time|time:"g:i A" }}'
                }
            },
            {% endfor %}
            {% for break in breaks %}
            {
                title: 'Break',
                start: '{{ start_date|date:"Y-m-d" }}T{{ break.start_time|time:"H:i:s" }}',
                end: '{{ start_date|date:"Y-m-d" }}T{{ break.end_time|time:"H:i:s" }}',
                color: '#ff9f89',
                display: 'background'
            },
            {% endfor %}
        ],
        eventClick: function(info) {
            // Show candidate details in a modal or alert
            if (info.event.title !== 'Break') {
                alert('Candidate: ' + info.event.title +
                      '\nDate: ' + info.event.start.toLocaleDateString() +
                      '\nTime: ' + info.event.extendedProps.time +
                      '\nEmail: ' + info.event.extendedProps.email);
            }
            info.jsEvent.preventDefault();
        }
    });
    calendar.render();
});
</script>
{% endblock %}