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

{% block title %}{{ title }} - {% translate "Complaint Thresholds" %} - 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;
}
</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="gauge" class="w-6 h-6"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold">{{ title }}</h1>
                <p class="text-white/80">
                    {% if threshold %}{% translate "Edit complaint threshold" %}{% else %}{% translate "Create new complaint threshold" %}{% endif %}
                </p>
            </div>
        </div>
        <a href="{% url 'complaints:complaint_threshold_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 "Threshold 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., Daily Complaint Limit' %}">
                    {% 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_threshold_type" class="form-label">
                            {% translate "Threshold Type" %} <span class="text-red-500">*</span>
                        </label>
                        <select name="threshold_type" id="id_threshold_type" class="form-select" required>
                            <option value="">{% translate "Select Type" %}</option>
                            {% for value, label in form.threshold_type.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.threshold_type.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.threshold_type.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.threshold_type.errors.0 }}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div>
                        <label for="id_metric_type" class="form-label">
                            {% translate "Metric Type" %} <span class="text-red-500">*</span>
                        </label>
                        <select name="metric_type" id="id_metric_type" class="form-select" required>
                            <option value="">{% translate "Select Metric" %}</option>
                            {% for value, label in form.metric_type.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.metric_type.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.metric_type.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.metric_type.errors.0 }}
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="id_threshold_value" class="form-label">
                            {% translate "Threshold Value" %} <span class="text-red-500">*</span>
                        </label>
                        <input type="number" 
                               name="threshold_value" 
                               id="id_threshold_value" 
                               class="form-control" 
                               value="{{ form.threshold_value.value|default:'' }}" 
                               min="1" 
                               required
                               placeholder="{% translate 'e.g., 10' %}">
                        {% if form.threshold_value.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.threshold_value.errors.0 }}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div>
                        <label for="id_action" class="form-label">
                            {% translate "Action to Take" %} <span class="text-red-500">*</span>
                        </label>
                        <select name="action" id="id_action" class="form-select" required>
                            <option value="">{% translate "Select Action" %}</option>
                            {% for value, label in form.action.field.choices %}
                            <option value="{{ value }}" 
                                    {% if form.action.value == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if form.action.errors %}
                        <div class="invalid-feedback">
                            <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                            {{ form.action.errors.0 }}
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div>
                    <label for="id_complaint_category" class="form-label">
                        {% translate "Complaint Category (Optional)" %}
                    </label>
                    <select name="complaint_category" id="id_complaint_category" class="form-select">
                        <option value="">{% translate "All Categories" %}</option>
                        {% for category in form.complaint_category.field.queryset %}
                        <option value="{{ category.id }}" 
                                {% if form.complaint_category.value == category.id|stringformat:"s" %}selected{% endif %}>
                            {{ category.name }}
                        </option>
                        {% endfor %}
                    </select>
                    {% if form.complaint_category.errors %}
                    <div class="invalid-feedback">
                        <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                        {{ form.complaint_category.errors.0 }}
                    </div>
                    {% else %}
                    <div class="form-text">
                        {% translate "Leave empty to apply to all complaint categories" %}
                    </div>
                    {% endif %}
                </div>
                
                <div>
                    <label for="id_notify_emails" class="form-label">
                        {% translate "Notify Emails (Optional)" %}
                    </label>
                    <input type="text" 
                           name="notify_emails" 
                           id="id_notify_emails" 
                           class="form-control" 
                           value="{{ form.notify_emails.value|default:'' }}" 
                           placeholder="{% translate 'email1@example.com, email2@example.com' %}">
                    {% if form.notify_emails.errors %}
                    <div class="invalid-feedback">
                        <i data-lucide="alert-circle" class="w-4 h-4 inline"></i>
                        {{ form.notify_emails.errors.0 }}
                    </div>
                    {% else %}
                    <div class="form-text">
                        {% translate "Comma-separated list of email addresses to notify when threshold is reached" %}
                    </div>
                    {% endif %}
                </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 thresholds will be monitored" %}
                    </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 threshold' %}">{{ 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:complaint_threshold_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 Complaint Thresholds" %}
            </h6>
            <p class="text-sm text-gray-600 mb-4">
                {% translate "Thresholds monitor complaint metrics and trigger actions when limits are exceeded." %}
            </p>
            
            <h6 class="text-sm font-semibold text-gray-500 mb-3">
                {% translate "Threshold Types" %}
            </h6>
            <ul class="space-y-2 text-sm text-gray-600">
                <li class="flex items-center gap-2">
                    <i data-lucide="calendar" class="w-4 h-4 text-[#005696]"></i>
                    <strong>{% translate "Daily" %}</strong> - {% translate "Monitor daily complaint volume" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="calendar-days" class="w-4 h-4 text-green-500"></i>
                    <strong>{% translate "Weekly" %}</strong> - {% translate "Monitor weekly complaint volume" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="calendar-range" class="w-4 h-4 text-orange-500"></i>
                    <strong>{% translate "Monthly" %}</strong> - {% translate "Monitor monthly complaint volume" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="tags" class="w-4 h-4 text-purple-500"></i>
                    <strong>{% translate "By Category" %}</strong> - {% translate "Monitor specific complaint categories" %}
                </li>
            </ul>
            
            <hr class="my-4 border-gray-200">
            
            <h6 class="text-sm font-semibold text-gray-500 mb-3">
                {% translate "Metric Types" %}
            </h6>
            <ul class="space-y-2 text-sm text-gray-600">
                <li class="flex items-center gap-2">
                    <i data-lucide="list-ordered" class="w-4 h-4 text-gray-500"></i>
                    {% translate "Count" %} - {% translate "Number of complaints" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="percent" class="w-4 h-4 text-gray-500"></i>
                    {% translate "Percentage" %} - {% translate "Percentage of total complaints" %}
                </li>
            </ul>
            
            <hr class="my-4 border-gray-200">
            
            <h6 class="text-sm font-semibold text-gray-500 mb-3">
                {% translate "Actions" %}
            </h6>
            <ul class="space-y-2 text-sm text-gray-600">
                <li class="flex items-center gap-2">
                    <i data-lucide="bell" class="w-4 h-4 text-orange-500"></i>
                    {% translate "Send Alert" %} - {% translate "Notify administrators" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="mail" class="w-4 h-4 text-blue-500"></i>
                    {% translate "Send Email" %} - {% translate "Send email notifications" %}
                </li>
                <li class="flex items-center gap-2">
                    <i data-lucide="file-text" class="w-4 h-4 text-green-500"></i>
                    {% translate "Generate Report" %} - {% translate "Create detailed report" %}
                </li>
            </ul>
        </div>
    </div>
</div>

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