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

{% block title %}{{ title }} | {{ hospital.name }}{% endblock %}

{% block extra_css %}
<style>
:root {
    --hh-navy: #005696;
    --hh-blue: #007bbd;
    --hh-light: #eef6fb;
    --hh-slate: #64748b;
}

.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);
}

.section-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
    margin-bottom: 1.5rem;
}

.section-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.text-navy {
    color: #005696;
}

.text-blue {
    color: #007bbd;
}

.text-slate {
    color: #64748b;
}

.bg-navy {
    background-color: #005696;
}

.bg-blue {
    background-color: #007bbd;
}

.bg-light {
    background-color: #eef6fb;
}

.hover\:bg-blue-50:hover {
    background-color: #eff6ff;
}

.hover\:bg-green-50:hover {
    background-color: #f0fdf4;
}

.hover\:bg-slate-100:hover {
    background-color: #f1f5f9;
}

/* Form Styles */
form input[type="text"],
form input[type="email"],
form input[type="number"],
form select,
form textarea {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    transition: all 0.2s ease;
    background-color: white;
}

form input[type="text"]:focus,
form input[type="email"]:focus,
form input[type="number"]:focus,
form select:focus,
form textarea:focus {
    outline: none;
    border-color: #005696;
    box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
}

form input[type="checkbox"] {
    width: 1.25rem;
    height: 1.25rem;
    accent-color: #005696;
    cursor: pointer;
}
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold mb-1">
                <i data-lucide="settings-2" class="w-6 h-6 inline-block mr-2"></i>
                {{ title }}
            </h1>
            <p class="text-blue-100 text-sm">{% trans "Configure SLA parameters for complaint resolution" %}</p>
        </div>
        <a href="{% url 'complaints:sla_management' %}" 
           class="inline-flex items-center px-4 py-2.5 bg-white/10 border-2 border-white/30 text-white font-medium rounded-xl hover:bg-white hover:text-navy transition">
            <i data-lucide="arrow-left" class="w-4 h-4 mr-2"></i>
            {% trans "Back to List" %}
        </a>
    </div>
</div>

<!-- Form -->
<form method="post" class="space-y-6">
    {% csrf_token %}
    
    <!-- Hospital (Hidden) -->
    <input type="hidden" name="hospital" value="{{ hospital.id }}">
    
    <!-- Configuration Type -->
    <div class="section-card">
        <div class="section-header p-6">
            <h2 class="text-lg font-bold text-navy m-0">{% trans "Configuration Type" %}</h2>
        </div>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4 p-6">
            <!-- Source-Based -->
            <div class="border-2 border-blue-200 bg-blue-50 rounded-xl p-4">
                <div class="flex items-start gap-3">
                    <div class="w-10 h-10 bg-blue-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i data-lucide="layers" class="w-5 h-5 text-blue-600"></i>
                    </div>
                    <div class="flex-1">
                        <label class="block text-sm font-bold text-navy mb-2">
                            {{ form.source.label }}
                        </label>
                        <div class="mb-1">
                            {{ form.source }}
                        </div>
                        <p class="text-xs text-slate">
                            {% trans "Select a source for source-based SLA, or leave empty for severity/priority-based" %}
                        </p>
                        {% if form.source.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.source.errors.0 }}</p>
                        {% endif %}
                    </div>
                </div>
            </div>
            
            <!-- Severity/Priority-Based -->
            <div class="border-2 border-purple-200 bg-purple-50 rounded-xl p-4">
                <div class="flex items-start gap-3">
                    <div class="w-10 h-10 bg-purple-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i data-lucide="tag" class="w-5 h-5 text-purple-600"></i>
                    </div>
                    <div class="flex-1">
                        <label class="block text-sm font-bold text-navy mb-2">
                            {% trans "Severity & Priority" %}
                        </label>
                        <div class="grid grid-cols-2 gap-2 mb-1">
                            {{ form.severity }}
                            {{ form.priority }}
                        </div>
                        <p class="text-xs text-slate">
                            {% trans "Required for severity/priority-based SLA" %}
                        </p>
                        {% if form.severity.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.severity.errors.0 }}</p>
                        {% endif %}
                        {% if form.priority.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.priority.errors.0 }}</p>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- SLA Deadline -->
    <div class="section-card">
        <div class="section-header p-6">
            <h2 class="text-lg font-bold text-navy m-0">
                <i data-lucide="clock" class="w-5 h-5 inline-block mr-2"></i>
                {% trans "SLA Deadline" %}
            </h2>
        </div>
        
        <div class="max-w-md p-6">
            <label class="block text-sm font-bold text-navy mb-2">
                {{ form.sla_hours.label }}
            </label>
            <div class="relative">
                {{ form.sla_hours }}
                <span class="absolute right-4 top-1/2 -translate-y-1/2 text-slate font-medium">{% trans "hours" %}</span>
            </div>
            <p class="text-sm text-slate mt-2">
                {{ form.sla_hours.help_text }}
            </p>
            {% if form.sla_hours.errors %}
                <p class="mt-1 text-sm text-red-600">{{ form.sla_hours.errors.0 }}</p>
            {% endif %}
        </div>
    </div>

    <!-- Reminder Configuration -->
    <div class="section-card">
        <div class="section-header p-6">
            <h2 class="text-lg font-bold text-navy m-0">
                <i data-lucide="bell" class="w-5 h-5 inline-block mr-2"></i>
                {% trans "Reminder Configuration" %}
            </h2>
        </div>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 p-6">
            <!-- First Reminder -->
            <div>
                <label class="block text-sm font-bold text-navy mb-2">
                    {% trans "First Reminder" %}
                </label>
                <div class="relative">
                    {{ form.first_reminder_hours_after }}
                    <span class="absolute right-4 top-1/2 -translate-y-1/2 text-slate text-sm">{% trans "hours after creation" %}</span>
                </div>
                <p class="text-xs text-slate mt-2">
                    {% trans "Leave as 0 to use legacy timing (hours before deadline)" %}
                </p>
                {% if form.first_reminder_hours_after.errors %}
                    <p class="mt-1 text-xs text-red-600">{{ form.first_reminder_hours_after.errors.0 }}</p>
                {% endif %}
            </div>
            
            <!-- Second Reminder -->
            <div>
                <label class="block text-sm font-bold text-navy mb-2">
                    {% trans "Second Reminder" %}
                </label>
                <div class="relative">
                    {{ form.second_reminder_hours_after }}
                    <span class="absolute right-4 top-1/2 -translate-y-1/2 text-slate text-sm">{% trans "hours after creation" %}</span>
                </div>
                <p class="text-xs text-slate mt-2">
                    {% trans "Leave as 0 to disable or use legacy timing" %}
                </p>
                {% if form.second_reminder_hours_after.errors %}
                    <p class="mt-1 text-xs text-red-600">{{ form.second_reminder_hours_after.errors.0 }}</p>
                {% endif %}
            </div>
        </div>
        
        <!-- Legacy Timing -->
        <div class="mt-6 pt-6 px-6 pb-6 border-t-2 border-slate-200">
            <div class="flex items-center gap-2 mb-4">
                <i data-lucide="clock-3" class="w-5 h-5 text-slate"></i>
                <span class="text-sm font-bold text-slate">{% trans "Legacy Timing (hours before deadline)" %}</span>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <label class="block text-xs font-bold text-slate mb-1">
                        {% trans "First Reminder (Legacy)" %}
                    </label>
                    <div class="relative">
                        {{ form.reminder_hours_before }}
                        <span class="absolute right-4 top-1/2 -translate-y-1/2 text-slate text-sm">{% trans "hours before" %}</span>
                    </div>
                </div>
                
                <div>
                    <div class="flex items-center gap-2 mb-2">
                        {{ form.second_reminder_enabled }}
                        <label class="text-xs font-bold text-slate">
                            {% trans "Enable Second Reminder (Legacy)" %}
                        </label>
                    </div>
                    <div class="relative">
                        {{ form.second_reminder_hours_before }}
                        <span class="absolute right-4 top-1/2 -translate-y-1/2 text-slate text-sm">{% trans "hours before" %}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Escalation Configuration -->
    <div class="section-card">
        <div class="section-header p-6">
            <h2 class="text-lg font-bold text-navy m-0">
                <i data-lucide="trending-up" class="w-5 h-5 inline-block mr-2"></i>
                {% trans "Escalation Configuration" %}
            </h2>
        </div>

        <div class="p-6">
            <div class="max-w-md">
            <label class="block text-sm font-bold text-navy mb-2">
                {{ form.escalation_hours_after.label }}
            </label>
            <div class="relative">
                {{ form.escalation_hours_after }}
                <span class="absolute right-4 top-1/2 -translate-y-1/2 text-slate font-medium">{% trans "hours after creation" %}</span>
            </div>
            <p class="text-sm text-slate mt-2">
                {{ form.escalation_hours_after.help_text }}
            </p>
            {% if form.escalation_hours_after.errors %}
                <p class="mt-1 text-sm text-red-600">{{ form.escalation_hours_after.errors.0 }}</p>
            {% endif %}
        </div>
    </div>

    <!-- Status -->
    <div class="section-card">
        <div class="flex items-center justify-between p-6">
            <div>
                <h3 class="text-lg font-bold text-navy">{% trans "Configuration Status" %}</h3>
                <p class="text-sm text-slate mt-1">{% trans "Enable or disable this SLA configuration" %}</p>
            </div>
            <div class="flex items-center gap-3">
                <span class="text-sm font-bold text-navy">{% trans "Active" %}</span>
                {{ form.is_active }}
            </div>
        </div>
    </div>

    <!-- Submit Buttons -->
    <div class="flex items-center justify-end gap-4">
        <a href="{% url 'complaints:sla_management' %}" 
           class="px-6 py-3 border-2 border-slate-300 text-slate-700 font-bold rounded-xl hover:bg-slate-50 transition">
            {% trans "Cancel" %}
        </a>
        <button type="submit" 
                class="px-6 py-3 bg-navy text-white font-bold rounded-xl hover:bg-blue transition shadow-lg shadow-navy/20">
            {{ action }} {% trans "Configuration" %}
        </button>
    </div>
</form>

<script>
// Toggle second reminder hours based on checkbox
document.addEventListener('DOMContentLoaded', function() {
    // Initialize Lucide icons
    lucide.createIcons();
    
    const secondReminderCheckbox = document.querySelector('input[name="second_reminder_enabled"]');
    const secondReminderHours = document.querySelector('input[name="second_reminder_hours_before"]');
    
    if (secondReminderCheckbox && secondReminderHours) {
        secondReminderHours.disabled = !secondReminderCheckbox.checked;
        
        secondReminderCheckbox.addEventListener('change', function() {
            secondReminderHours.disabled = !this.checked;
        });
    }
});
</script>
{% endblock %}
