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

{% block title %}{{ title }} - {% translate "Escalation Rules" %} - PX360{% 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;
}

.help-card {
    background: #f8fafc;
    border: 2px solid #e2e8f0;
    border-radius: 1rem;
    padding: 1.5rem;
}

.form-switch {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
.form-switch-input {
    width: 3rem;
    height: 1.5rem;
    appearance: none;
    background: #cbd5e1;
    border-radius: 1rem;
    position: relative;
    cursor: pointer;
    transition: all 0.2s ease;
}
.form-switch-input:checked {
    background: #005696;
}
.form-switch-input::after {
    content: '';
    position: absolute;
    width: 1.25rem;
    height: 1.25rem;
    background: white;
    border-radius: 50%;
    top: 0.125rem;
    left: 0.125rem;
    transition: all 0.2s ease;
}
.form-switch-input:checked::after {
    left: 1.625rem;
}

.invalid-feedback {
    color: #dc2626;
    font-size: 0.875rem;
    margin-top: 0.25rem;
}
.form-text {
    color: #64748b;
    font-size: 0.875rem;
    margin-top: 0.25rem;
}

.alert-info {
    background: #dbeafe;
    border: 1px solid #93c5fd;
    border-radius: 0.75rem;
    padding: 1rem;
}
.alert-info ol {
    color: #1e40af;
    font-size: 0.875rem;
    padding-left: 1.25rem;
}
.alert-info li {
    margin-bottom: 0.25rem;
}
</style>
{% endblock %}

{% block content %}
<!-- Gradient Header -->
<div class="page-header-gradient">
    <div class="flex items-center justify-between">
        <div class="flex items-center gap-4">
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                <i data-lucide="arrow-up-circle" class="w-6 h-6"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold">{{ title }}</h1>
                <p class="text-white/80">
                    {% if escalation_rule %}{% translate "Edit escalation rule" %}{% else %}{% translate "Create new escalation rule" %}{% endif %}
                </p>
            </div>
        </div>
        <a href="{% url 'complaints:escalation_rule_list' %}" class="hh-btn hh-btn-secondary">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            {% translate "Back to List" %}
        </a>
    </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <div class="lg:col-span-2">
        <div class="form-section">
            <form method="post" class="space-y-6">
                {% csrf_token %}
                
                {% if not form.hospital.is_hidden %}
                <div>
                    <label for="id_hospital" class="form-label">
                        {% translate "Hospital" %} <span class="text-red-500">*</span>
                    </label>
                    <select name="hospital" id="id_hospital" class="form-select" required>
                        <option value="">{% translate "Select Hospital" %}</option>
                        {% for hospital in form.hospital.field.queryset %}
                        <option value="{{ hospital.id }}" 
                                {% if form.hospital.value == hospital.id|stringformat:"s" %}selected{% endif %}>
                            {{ hospital.name }}
                        </option>
                        {% endfor %}
                    </select>
                    {% if form.hospital.errors %}
                    <div class="invalid-feedback">
                        <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                        {{ form.hospital.errors.0 }}
                    </div>
                    {% endif %}
                </div>
                {% endif %}
                
                <div>
                    <label for="id_name" class="form-label">
                        {% translate "Rule Name" %} <span class="text-red-500">*</span>
                    </label>
                    <input type="text" 
                           name="name" 
                           id="id_name" 
                           class="form-control" 
                           value="{{ form.name.value|default:'' }}" 
                           required
                           placeholder="{% translate 'e.g., Level 1 Escalation - High Priority' %}">
                    {% if form.name.errors %}
                    <div class="invalid-feedback">
                        <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                        {{ form.name.errors.0 }}
                    </div>
                    {% endif %}
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="id_escalation_level" class="form-label">
                            {% translate "Escalation Level" %} <span class="text-red-500">*</span>
                        </label>
                        <select name="escalation_level" id="id_escalation_level" class="form-select" required>
                            <option value="">{% translate "Select Level" %}</option>
                            {% for value, label in form.escalation_level.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.escalation_level.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.escalation_level.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.escalation_level.errors.0 }}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div>
                        <label for="id_trigger_hours" class="form-label">
                            {% translate "Trigger Hours" %} <span class="text-red-500">*</span>
                        </label>
                        <input type="number" 
                               name="trigger_hours" 
                               id="id_trigger_hours" 
                               class="form-control" 
                               value="{{ form.trigger_hours.value|default:'' }}" 
                               min="1" 
                               step="0.5" 
                               required
                               placeholder="{% translate 'e.g., 24' %}">
                        {% if form.trigger_hours.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.trigger_hours.errors.0 }}
                        </div>
                        {% else %}
                        <div class="form-text">
                            {% translate "Hours after complaint creation to trigger escalation" %}
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="id_escalate_to_role" class="form-label">
                            {% translate "Escalate To Role" %}
                        </label>
                        <select name="escalate_to_role" id="id_escalate_to_role" class="form-select">
                            <option value="">{% translate "Select Role" %}</option>
                            {% for value, label in form.escalate_to_role.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.escalate_to_role.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.escalate_to_role.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.escalate_to_role.errors.0 }}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div>
                        <label for="id_escalate_to_user" class="form-label">
                            {% translate "Escalate To Specific User" %}
                        </label>
                        <select name="escalate_to_user" id="id_escalate_to_user" class="form-select">
                            <option value="">{% translate "Select User (Optional)" %}</option>
                            {% for user in users %}
                            <option value="{{ user.id }}" 
                                    {% if form.escalate_to_user.value == user.id|stringformat:"s" %}selected{% endif %}>
                                {{ user.get_full_name }} ({{ user.email }})
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.escalate_to_user.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.escalate_to_user.errors.0 }}
                        </div>
                        {% else %}
                        <div class="form-text">
                            {% translate "Overrides role if specified" %}
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="id_severity" class="form-label">
                            {% translate "Severity (Optional)" %}
                        </label>
                        <select name="severity" id="id_severity" class="form-select">
                            <option value="">{% translate "All Severities" %}</option>
                            {% for value, label in form.severity.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.severity.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.severity.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.severity.errors.0 }}
                        </div>
                        {% else %}
                        <div class="form-text">
                            {% translate "Leave empty to apply to all severities" %}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div>
                        <label for="id_priority" class="form-label">
                            {% translate "Priority (Optional)" %}
                        </label>
                        <select name="priority" id="id_priority" class="form-select">
                            <option value="">{% translate "All Priorities" %}</option>
                            {% for value, label in form.priority.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.priority.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.priority.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.priority.errors.0 }}
                        </div>
                        {% else %}
                        <div class="form-text">
                            {% translate "Leave empty to apply to all priorities" %}
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="flex items-center gap-3 p-4 bg-gray-50 rounded-xl">
                    <div class="form-switch">
                        <input type="checkbox" 
                               name="is_active" 
                               id="id_is_active" 
                               class="form-switch-input"
                               {% if form.is_active.value == 'on' or not form.is_active.value %}checked{% endif %}>
                        <label for="id_is_active" class="text-sm font-semibold text-gray-700">
                            {% translate "Active" %}
                        </label>
                    </div>
                    <div class="form-text ml-2">
                        {% translate "Only active rules will be triggered" %}
                    </div>
                </div>
                
                <div>
                    <label for="id_description" class="form-label">
                        {% translate "Description" %}
                    </label>
                    <textarea name="description" 
                              id="id_description" 
                              class="form-control resize-none" 
                              rows="3"
                              placeholder="{% translate 'Optional notes about this escalation rule' %}">{{ form.description.value|default:'' }}</textarea>
                    {% if form.description.errors %}
                    <div class="invalid-feedback">
                        <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                        {{ form.description.errors.0 }}
                    </div>
                    {% endif %}
                </div>
                
                <div class="flex items-center gap-3 pt-6 border-t border-gray-100">
                    <button type="submit" class="hh-btn hh-btn-primary">
                        <i data-lucide="save" class="w-4 h-4"></i>
                        {{ action }}
                    </button>
                    <a href="{% url 'complaints:escalation_rule_list' %}" class="hh-btn hh-btn-secondary">
                        <i data-lucide="x" class="w-4 h-4"></i>
                        {% translate "Cancel" %}
                    </a>
                </div>
            </form>
        </div>
    </div>
    
    <div class="lg:col-span-1">
        <div class="help-card">
            <h5 class="text-lg font-bold text-gray-800 mb-4 flex items-center gap-2">
                <i data-lucide="info" class="w-5 h-5 text-[#005696]"></i>
                {% translate "Help" %}
            </h5>
            <h6 class="text-sm font-semibold text-gray-500 mb-3">
                {% translate "Understanding Escalation Rules" %}
            </h6>
            <p class="text-sm text-gray-600 mb-4">
                {% translate "Escalation rules automatically reassign complaints to higher-level staff when they exceed specified time thresholds." %}
            </p>
            <ul class="space-y-2 text-sm text-gray-600">
                <li class="flex items-center gap-2">
                    <i data-lucide="check-circle" class="w-4 h-4 text-green-500"></i>
                    {% translate "Level 1: Escalate to department head" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="check-circle" class="w-4 h-4 text-green-500"></i>
                    {% translate "Level 2: Escalate to hospital admin" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="check-circle" class="w-4 h-4 text-green-500"></i>
                    {% translate "Level 3: Escalate to PX admin" %}
                </li>
            </ul>
            
            <hr class="my-4 border-gray-200">
            
            <h6 class="text-sm font-semibold text-gray-500 mb-3">
                {% translate "Escalation Flow" %}
            </h6>
            <div class="alert-info">
                <ol>
                    <li>{% translate "Complaint created" %}</li>
                    <li>{% translate "Trigger hours pass" %}</li>
                    <li>{% translate "Rule checks severity/priority" %}</li>
                    <li>{% translate "Complaint reassigned automatically" %}</li>
                    <li>{% translate "Notification sent to new assignee" %}</li>
                </ol>
            </div>
        </div>
    </div>
</div>

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