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

{% block title %}{% if object %}Edit Leave Request{% else %}New Leave Request{% endif %} - 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">{% if object %}Edit Leave Request{% else %}New Leave Request{% endif %}</h1>
            <p class="text-muted">{% if object %}Modify existing leave request{% else %}Submit a new leave request{% endif %}</p>
        </div>
        <a href="{% url 'staff:leave_list' %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-2"></i>Back to Leave Requests
        </a>
    </div>

    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card shadow">
                <div class="card-header py-3 bg-primary text-white">
                    <h6 class="m-0 font-weight-bold"><i class="fas fa-calendar-times me-2"></i>Leave Request Details</h6>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        {% if form.errors %}
                        <div class="alert alert-danger">
                            <i class="fas fa-exclamation-circle me-2"></i>Please correct the errors below.
                        </div>
                        {% endif %}

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Leave Type <span class="text-danger">*</span></label>
                                {{ form.leave_type }}
                                {% if form.leave_type.errors %}<div class="text-danger small">{{ form.leave_type.errors }}</div>{% endif %}
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Status</label>
                                {{ form.status }}
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Start Date <span class="text-danger">*</span></label>
                                {{ form.start_date }}
                                {% if form.start_date.errors %}<div class="text-danger small">{{ form.start_date.errors }}</div>{% endif %}
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">End Date <span class="text-danger">*</span></label>
                                {{ form.end_date }}
                                {% if form.end_date.errors %}<div class="text-danger small">{{ form.end_date.errors }}</div>{% endif %}
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Days Requested</label>
                            {{ form.days_requested }}
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Reason <span class="text-danger">*</span></label>
                            {{ form.reason }}
                            {% if form.reason.errors %}<div class="text-danger small">{{ form.reason.errors }}</div>{% endif %}
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Remarks (Admin)</label>
                            {{ form.remarks }}
                        </div>

                        <div class="d-flex justify-content-end gap-2 mt-4">
                            <a href="{% url 'staff:leave_list' %}" class="btn btn-secondary">Cancel</a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save me-2"></i>{% if object %}Update Request{% else %}Submit Request{% endif %}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{% block extra_js %}
<script>
// Auto-calculate days when dates change
document.addEventListener('DOMContentLoaded', function() {
    const startDate = document.querySelector('[name="start_date"]');
    const endDate = document.querySelector('[name="end_date"]');
    const daysField = document.querySelector('[name="days_requested"]');

    function calcDays() {
        if (startDate.value && endDate.value) {
            const start = new Date(startDate.value);
            const end = new Date(endDate.value);
            const diff = Math.round((end - start) / (1000 * 60 * 60 * 24)) + 1;
            if (diff > 0) daysField.value = diff;
        }
    }
    if (startDate) startDate.addEventListener('change', calcDays);
    if (endDate) endDate.addEventListener('change', calcDays);
});
</script>
{% endblock %}
{% endblock %}
