{% 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;
}
.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.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;
}
</style>
{% endblock %}

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

    <!-- Header -->
    <div class="page-header-gradient">
        <h1 class="text-2xl font-bold flex items-center gap-3">
            <i data-lucide="user-cog" class="w-7 h-7"></i>
            {{ title }}
        </h1>
        <p class="text-white/80 mt-1">
            {% trans "Schedule" %}: {% if schedule.hospital %}{{ schedule.hospital.name }}{% else %}{% trans "System-wide" %}{% endif %}
        </p>
    </div>

    <!-- Form -->
    <div class="form-section">
        <form method="post" class="space-y-6">
            {% csrf_token %}
            
            <!-- Admin Selection (only for new) -->
            {% if not on_call_admin %}
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="user" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Select Admin" %}
                </h2>
                
                <div>
                    <label class="form-label">
                        {% trans "On-Call User" %} <span class="text-red-500">*</span>
                    </label>
                    <select name="admin_user" required class="form-select">
                        <option value="">{% trans "Select a user..." %}</option>
                        {% if available_px_admins %}
                        <optgroup label="{% trans 'PX Admins' %}">
                            {% for admin in available_px_admins %}
                            <option value="{{ admin.id }}">
                                {{ admin.get_full_name|default:admin.email }} ({{ admin.email }})
                            </option>
                            {% endfor %}
                        </optgroup>
                        {% endif %}
                        {% if available_px_employee %}
                        <optgroup label="{% trans 'PX Employee' %}">
                            {% for admin in available_px_employee %}
                            <option value="{{ admin.id }}">
                                {{ admin.get_full_name|default:admin.email }} ({{ admin.email }})
                            </option>
                            {% endfor %}
                        </optgroup>
                        {% endif %}
                        {% if available_hospital_admins %}
                        <optgroup label="{% trans 'Hospital Admins' %}">
                            {% for admin in available_hospital_admins %}
                            <option value="{{ admin.id }}">
                                {{ admin.get_full_name|default:admin.email }} ({{ admin.email }})
                            </option>
                            {% endfor %}
                        </optgroup>
                        {% endif %}
                        {% if not available_admins %}
                        <option value="" disabled>{% trans "No available users" %}</option>
                        {% endif %}
                    </select>
                    {% if not available_admins %}
                    <p class="mt-2 text-sm text-amber-600 flex items-center gap-2">
                        <i data-lucide="alert-triangle" class="w-4 h-4"></i>
                        {% trans "All eligible users are already assigned to this schedule." %}
                    </p>
                    {% endif %}
                </div>
            </div>
            {% else %}
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="user" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Admin" %}
                </h2>
                <div class="flex items-center gap-3 p-4 bg-[#f8fafc] rounded-xl border-2 border-[#e2e8f0]">
                    <div class="w-12 h-12 bg-[#005696] rounded-full flex items-center justify-center">
                        <span class="text-white font-medium text-lg">
                            {{ on_call_admin.admin_user.first_name|first|default:on_call_admin.admin_user.email|first|upper }}
                        </span>
                    </div>
                    <div>
                        <p class="font-bold text-[#1e293b]">{{ on_call_admin.admin_user.get_full_name|default:on_call_admin.admin_user.email }}</p>
                        <p class="text-sm text-[#64748b]">{{ on_call_admin.admin_user.email }}</p>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Active Period -->
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="calendar" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Active Period" %}
                </h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="form-label">
                            {% trans "Start Date (Optional)" %}
                        </label>
                        <input type="date" name="start_date" 
                               value="{{ on_call_admin.start_date|date:'Y-m-d'|default:'' }}"
                               class="form-control">
                        <p class="form-text">{% trans "Leave empty for immediate activation" %}</p>
                    </div>
                    <div>
                        <label class="form-label">
                            {% trans "End Date (Optional)" %}
                        </label>
                        <input type="date" name="end_date" 
                               value="{{ on_call_admin.end_date|date:'Y-m-d'|default:'' }}"
                               class="form-control">
                        <p class="form-text">{% trans "Leave empty for permanent assignment" %}</p>
                    </div>
                </div>
            </div>

            <!-- Notification Settings -->
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="bell" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Notification Settings" %}
                </h2>
                
                <div class="space-y-4">
                    <div>
                        <label class="form-label">
                            {% trans "Notification Priority" %}
                        </label>
                        <select name="notification_priority" class="form-select">
                            {% for i in "12345" %}
                            <option value="{{ i }}" {% if on_call_admin.notification_priority == i|add:0 %}selected{% endif %}>
                                {{ i }} - {% if i == "1" %}{% trans "Highest" %}{% elif i == "5" %}{% trans "Lowest" %}{% else %}{% trans "Priority" %} {{ i }}{% endif %}
                            </option>
                            {% endfor %}
                        </select>
                        <p class="form-text">{% trans "Lower numbers = higher priority in notification order" %}</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 on_call_admin or on_call_admin.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 "On-call assignment is active" %}
                        </label>
                    </div>
                </div>
            </div>

            <!-- Contact Preferences -->
            <div class="form-section">
                <h2 class="text-lg font-bold text-[#1e293b] mb-4 flex items-center gap-2">
                    <i data-lucide="mail" class="w-5 h-5 text-[#005696]"></i>
                    {% trans "Contact Preferences" %}
                </h2>
                
                <div class="space-y-4">
                    <div class="flex items-center gap-3 p-3 bg-[#f8fafc] rounded-lg">
                        <input type="checkbox" name="notify_email" id="notify_email" 
                               {% if not on_call_admin or on_call_admin.notify_email %}checked{% endif %}
                               class="w-5 h-5 text-[#005696] border-2 border-[#e2e8f0] rounded focus:ring-[#005696]">
                        <label for="notify_email" class="text-sm font-semibold text-[#1e293b]">
                            {% trans "Send email notifications" %}
                        </label>
                    </div>
                    
                    <div class="flex items-center gap-3 p-3 bg-[#f8fafc] rounded-lg">
                        <input type="checkbox" name="notify_sms" id="notify_sms" 
                               {% if on_call_admin.notify_sms %}checked{% endif %}
                               class="w-5 h-5 text-[#005696] border-2 border-[#e2e8f0] rounded focus:ring-[#005696]">
                        <label for="notify_sms" class="text-sm font-semibold text-[#1e293b]">
                            {% trans "Send SMS notifications (for high priority complaints)" %}
                        </label>
                    </div>
                    
                    <div>
                        <label class="form-label">
                            {% trans "SMS Phone Number (Optional)" %}
                        </label>
                        <input type="tel" name="sms_phone" 
                               value="{{ on_call_admin.sms_phone|default:'' }}"
                               placeholder="+966501234567"
                               class="form-control">
                        <p class="form-text">
                            {% trans "Leave empty to use the user's profile phone number" %}
                        </p>
                    </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 "When are on-call admins notified?" %}</h3>
                        <p class="text-sm text-[#64748b] mt-1">
                            {% trans "On-call admins are notified outside of working hours (as configured in the schedule) via BOTH email and SMS. During working hours, ALL PX Admins are notified via email only." %}
                        </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_detail' schedule.id %}" 
                   class="hh-btn hh-btn-secondary">
                    {% trans "Cancel" %}
                </a>
                <button type="submit" class="hh-btn hh-btn-primary"
                        {% if not on_call_admin and not available_admins %}disabled{% endif %}>
                    <i data-lucide="{% if on_call_admin %}save{% else %}plus{% endif %}" class="w-4 h-4"></i>
                    {% if on_call_admin %}{% trans "Update Assignment" %}{% else %}{% trans "Add Admin" %}{% endif %}
                </button>
            </div>
        </form>
    </div>
</div>

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