<!-- templates/interviews/schedule_interviews.html -->
{% extends "base.html" %}

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

    <div class="card mt-4">
        <div class="card-body">
            <form method="post" id="schedule-form">
                {% csrf_token %}

                <div class="row">
                    <div class="col-md-6">
                        <h5>Select Candidates</h5>
                        <div class="form-group">
                            {{ form.candidates }}
                        </div>
                    </div>

                    <div class="col-md-6">
                        <h5>Schedule Details</h5>

                        <div class="form-group mb-3">
                            <label for="{{ form.start_date.id_for_label }}">Start Date</label>
                            {{ form.start_date }}
                        </div>

                        <div class="form-group mb-3">
                            <label for="{{ form.end_date.id_for_label }}">End Date</label>
                            {{ form.end_date }}
                        </div>

                        <div class="form-group mb-3">
                            <label>Working Days</label>
                            {{ form.working_days }}
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.start_time.id_for_label }}">Start Time</label>
                                    {{ form.start_time }}
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.end_time.id_for_label }}">End Time</label>
                                    {{ form.end_time }}
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.interview_duration.id_for_label }}">Interview Duration (minutes)</label>
                                    {{ form.interview_duration }}
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.buffer_time.id_for_label }}">Buffer Time (minutes)</label>
                                    {{ form.buffer_time }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row mt-4">
                    <div class="col-12">
                        <h5>Break Times</h5>
                        <div id="break-times-container">
                            {{ break_formset.management_form }}
                            {% for form in break_formset %}
                                <div class="break-time-form row mb-2">
                                    <div class="col-md-5">
                                        <label>Start Time</label>
                                        {{ form.start_time }}
                                    </div>
                                    <div class="col-md-5">
                                        <label>End Time</label>
                                        {{ form.end_time }}
                                    </div>
                                    <div class="col-md-2">
                                        <label>&nbsp;</label><br>
                                        {{ form.DELETE }}
                                        <button type="button" class="btn btn-danger btn-sm remove-break">Remove</button>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                        <button type="button" id="add-break" class="btn btn-secondary btn-sm mt-2">Add Break</button>
                    </div>
                </div>

                <div class="mt-4">
                    <button type="submit" class="btn btn-primary">Preview Schedule</button>
                    <a href="{% url 'job_detail' pk=job.id %}" class="btn btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const addBreakBtn = document.getElementById('add-break');
    const breakTimesContainer = document.getElementById('break-times-container');
    const totalFormsInput = document.getElementById('id_breaks-TOTAL_FORMS');

    addBreakBtn.addEventListener('click', function() {
        const formCount = parseInt(totalFormsInput.value);
        const newFormHtml = `
            <div class="break-time-form row mb-2">
                <div class="col-md-5">
                    <label>Start Time</label>
                    <input type="time" name="breaks-${formCount}-start_time" class="form-control" id="id_breaks-${formCount}-start_time">
                </div>
                <div class="col-md-5">
                    <label>End Time</label>
                    <input type="time" name="breaks-${formCount}-end_time" class="form-control" id="id_breaks-${formCount}-end_time">
                </div>
                <div class="col-md-2">
                    <label>&nbsp;</label><br>
                    <input type="checkbox" name="breaks-${formCount}-DELETE" id="id_breaks-${formCount}-DELETE" style="display:none;">
                    <button type="button" class="btn btn-danger btn-sm remove-break">Remove</button>
                </div>
            </div>
        `;

        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = newFormHtml;
        const newForm = tempDiv.firstChild;

        breakTimesContainer.appendChild(newForm);
        totalFormsInput.value = formCount + 1;
    });

    // Handle remove button clicks
    breakTimesContainer.addEventListener('click', function(e) {
        if (e.target.classList.contains('remove-break')) {
            const form = e.target.closest('.break-time-form');
            const deleteCheckbox = form.querySelector('input[name$="-DELETE"]');
            deleteCheckbox.checked = true;
            form.style.display = 'none';
        }
    });
});
</script>
{% endblock %}