{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}

{% block title %}{{ title }} - PX 360{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.form-section {
    background: #fff;
    border: 2px solid #e2e8f0;
    border-radius: 1rem;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    transition: all 0.3s ease;
}
.form-section:hover {
    border-color: #005696;
    box-shadow: 0 4px 12px rgba(0, 86, 150, 0.1);
}

.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 0.5rem;
}

.form-control, .form-select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    transition: all 0.2s ease;
}
.form-control:focus, .form-select:focus {
    outline: none;
    border-color: #005696;
    box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
}

.hh-btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: #005696;
    color: white;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
    border: none;
    cursor: pointer;
}
.hh-btn-primary:hover {
    background: #007bbd;
}

.hh-btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: white;
    color: #64748b;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
}
.hh-btn-secondary:hover {
    background: #f1f5f9;
    border-color: #005696;
}

.form-text {
    font-size: 0.75rem;
    color: #64748b;
    margin-top: 0.25rem;
}

.info-box {
    background: #eef6fb;
    border-radius: 0.75rem;
    padding: 1rem;
}

.day-checkbox {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border: 2px solid #e2e8f0;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
}
.day-checkbox:hover {
    border-color: #005696;
    background: #f8fafc;
}
.day-checkbox input {
    width: 1rem;
    height: 1rem;
    accent-color: #005696;
}
</style>
{% endblock %}

{% block content %}
<div class="max-w-3xl mx-auto">
    <!-- Back Button -->
    <div class="mb-6">
        <a href="{% url 'complaints:oncall_schedule_list' %}" class="hh-btn hh-btn-secondary">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            {% trans "Back to Schedules" %}
        </a>
    </div>

    <!-- Header -->
    <div class="page-header-gradient">
        <h1 class="text-2xl font-bold flex items-center gap-3">
            <i data-lucide="calendar-clock" class="w-7 h-7"></i>
            {{ title }}
        </h1>
        <p class="text-white/80 mt-1">
            {% if schedule %}{% trans "Update on-call schedule settings" %}{% else %}{% trans "Create a new on-call schedule" %}{% endif %}
        </p>
    </div>

    <!-- Form -->
    <div class="form-section">
        <form method="post" class="space-y-6">
            {% csrf_token %}
            
            <!-- Scope Section -->
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="building" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Schedule Scope" %}
                </h2>
                
                <div class="space-y-4">
                    <div>
                        <label class="form-label">
                            {% trans "Hospital (Optional)" %}
                        </label>
                        <select name="hospital" class="form-select">
                            <option value="">{% trans "System-wide (All Hospitals)" %}</option>
                            {% for hospital in hospitals %}
                            <option value="{{ hospital.id }}" {% if schedule.hospital_id == hospital.id %}selected{% endif %}>
                                {{ hospital.name }}
                            </option>
                            {% endfor %}
                        </select>
                        <p class="form-text">
                            {% trans "Leave empty for system-wide configuration, or select a specific hospital." %}
                        </p>
                    </div>
                    
                    <div class="flex items-center gap-3 p-3 bg-[#f8fafc] rounded-lg">
                        <input type="checkbox" name="is_active" id="is_active" 
                               {% if not schedule or schedule.is_active %}checked{% endif %}
                               class="w-5 h-5 text-[#005696] border-2 border-[#e2e8f0] rounded focus:ring-[#005696]">
                        <label for="is_active" class="text-sm font-semibold text-[#1e293b]">
                            {% trans "Schedule is active" %}
                        </label>
                    </div>
                </div>
            </div>

            <!-- Working Hours Section -->
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="clock" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Working Hours Configuration" %}
                </h2>
                
                <div class="space-y-4">
                    <!-- Working Days -->
                    <div>
                        <label class="form-label">
                            {% trans "Working Days" %}
                        </label>
                        <div class="flex flex-wrap gap-3">
                            {% with working_days=schedule.get_working_days_list|default:"0,1,2,3,4"|slice:":" %}
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_0" value="0" 
                                       {% if schedule and 0 in schedule.get_working_days_list %}checked{% elif not schedule %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Mon" %}</span>
                            </label>
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_1" value="1" 
                                       {% if schedule and 1 in schedule.get_working_days_list %}checked{% elif not schedule %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Tue" %}</span>
                            </label>
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_2" value="2" 
                                       {% if schedule and 2 in schedule.get_working_days_list %}checked{% elif not schedule %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Wed" %}</span>
                            </label>
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_3" value="3" 
                                       {% if schedule and 3 in schedule.get_working_days_list %}checked{% elif not schedule %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Thu" %}</span>
                            </label>
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_4" value="4" 
                                       {% if schedule and 4 in schedule.get_working_days_list %}checked{% elif not schedule %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Fri" %}</span>
                            </label>
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_5" value="5" 
                                       {% if schedule and 5 in schedule.get_working_days_list %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Sat" %}</span>
                            </label>
                            <label class="day-checkbox">
                                <input type="checkbox" name="working_day_6" value="6" 
                                       {% if schedule and 6 in schedule.get_working_days_list %}checked{% endif %}>
                                <span class="text-sm font-medium">{% trans "Sun" %}</span>
                            </label>
                            {% endwith %}
                        </div>
                        <p class="form-text">
                            {% trans "Select the days that are considered working days. Outside these days, only on-call admins will be notified." %}
                        </p>
                    </div>
                    
                    <!-- Time Range -->
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="form-label">
                                {% trans "Work Start Time" %}
                            </label>
                            <input type="time" name="work_start_time" 
                                   value="{{ schedule.work_start_time|time:'H:i'|default:'08:00' }}"
                                   class="form-control">
                        </div>
                        <div>
                            <label class="form-label">
                                {% trans "Work End Time" %}
                            </label>
                            <input type="time" name="work_end_time" 
                                   value="{{ schedule.work_end_time|time:'H:i'|default:'17:00' }}"
                                   class="form-control">
                        </div>
                    </div>
                    
                    <!-- Timezone -->
                    <div>
                        <label class="form-label">
                            {% trans "Timezone" %}
                        </label>
                        <select name="timezone" class="form-select">
                            {% for tz in timezones %}
                            <option value="{{ tz }}" {% if schedule.timezone == tz %}selected{% elif not schedule and tz == 'Asia/Riyadh' %}selected{% endif %}>
                                {{ tz }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>

            <!-- Info Box -->
            <div class="info-box">
                <div class="flex items-start gap-3">
                    <i data-lucide="info" class="w-5 h-5 text-[#005696] flex-shrink-0 mt-0.5"></i>
                    <div>
                        <h3 class="font-semibold text-[#005696]">{% trans "How it works" %}</h3>
                        <p class="text-sm text-[#64748b] mt-1">
                            {% trans "During working hours, ALL PX Admins are notified of new complaints via email only. Outside working hours (after work end time, before work start time, or on non-working days), only the on-call admins assigned to this schedule will be notified via BOTH email and SMS." %}
                        </p>
                    </div>
                </div>
            </div>

            <!-- Actions -->
            <div class="flex items-center justify-end gap-3 pt-6 border-t-2 border-[#e2e8f0]">
                <a href="{% url 'complaints:oncall_schedule_list' %}" 
                   class="hh-btn hh-btn-secondary">
                    {% trans "Cancel" %}
                </a>
                <button type="submit" class="hh-btn hh-btn-primary">
                    <i data-lucide="{% if schedule %}save{% else %}plus{% endif %}" class="w-4 h-4"></i>
                    {% if schedule %}{% trans "Update Schedule" %}{% else %}{% trans "Create Schedule" %}{% endif %}
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        lucide.createIcons();
    });
</script>
{% endblock %}
