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

{% block title %}{% if form.instance.pk %}{% trans "Edit Badge" %}{% else %}{% trans "Add Badge" %}{% endif %} - {% 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;
}
</style>
{% endblock %}

{% block content %}
<div class="p-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="flex flex-wrap items-center gap-2 text-sm">
            <li><a href="{% url 'appreciation:appreciation_list' %}" class="text-blue-600 hover:text-blue-800">{% trans "Appreciation" %}</a></li>
            <li class="text-gray-400"><i data-lucide="chevron-right" class="w-4 h-4"></i></li>
            <li><a href="{% url 'appreciation:badge_list' %}" class="text-blue-600 hover:text-blue-800">{% trans "Badges" %}</a></li>
            <li class="text-gray-400"><i data-lucide="chevron-right" class="w-4 h-4"></i></li>
            <li class="text-gray-600 font-medium" aria-current="page">{% if form.instance.pk %}{% trans "Edit" %}{% else %}{% trans "Add" %}{% endif %}</li>
        </ol>
    </nav>

    <!-- Header -->
    <div class="page-header-gradient">
        <h1 class="text-2xl font-bold flex items-center gap-3">
            <i data-lucide="trophy" class="w-6 h-6"></i>
            {% if form.instance.pk %}{% trans "Edit Badge" %}{% else %}{% trans "Add Badge" %}{% endif %}
        </h1>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Form -->
        <div class="lg:col-span-2">
            <div class="form-section">
                <form method="post">
                    {% csrf_token %}
                    
                    <!-- Name (English) -->
                    <div class="mb-4">
                        <label for="id_name_en" class="form-label">
                            {% trans "Name (English)" %} <span class="text-red-500">*</span>
                        </label>
                        <input 
                            type="text" 
                            class="form-control" 
                            id="id_name_en" 
                            name="name_en" 
                            value="{{ form.name_en.value|default:'' }}"
                            required
                        >
                    </div>

                    <!-- Name (Arabic) -->
                    <div class="mb-4">
                        <label for="id_name_ar" class="form-label">
                            {% trans "Name (Arabic)" %} <span class="text-red-500">*</span>
                        </label>
                        <input 
                            type="text" 
                            class="form-control" 
                            id="id_name_ar" 
                            name="name_ar" 
                            value="{{ form.name_ar.value|default:'' }}"
                            dir="rtl"
                            required
                        >
                    </div>

                    <!-- Description (English) -->
                    <div class="mb-4">
                        <label for="id_description_en" class="form-label">{% trans "Description (English)" %}</label>
                        <textarea 
                            class="form-control" 
                            id="id_description_en" 
                            name="description_en" 
                            rows="3"
                        >{{ form.description_en.value|default:'' }}</textarea>
                    </div>

                    <!-- Description (Arabic) -->
                    <div class="mb-4">
                        <label for="id_description_ar" class="form-label">{% trans "Description (Arabic)" %}</label>
                        <textarea 
                            class="form-control" 
                            id="id_description_ar" 
                            name="description_ar" 
                            rows="3"
                            dir="rtl"
                        >{{ form.description_ar.value|default:'' }}</textarea>
                    </div>

                    <!-- Icon -->
                    <div class="mb-4">
                        <label for="id_icon" class="form-label">{% trans "Icon" %}</label>
                        <input 
                            type="text" 
                            class="form-control" 
                            id="id_icon" 
                            name="icon" 
                            value="{{ form.icon.value|default:'fa-trophy' }}"
                            placeholder="fa-trophy"
                        >
                        <p class="text-sm text-gray-500 mt-1">
                            {% trans "FontAwesome icon class (e.g., fa-trophy, fa-star, fa-medal)" %}
                        </p>
                    </div>

                    <!-- Criteria Type -->
                    <div class="mb-4">
                        <label for="id_criteria_type" class="form-label">{% trans "Criteria Type" %}</label>
                        <select class="form-select" id="id_criteria_type" name="criteria_type">
                            <option value="count" {% if badge.criteria_type == 'count' or not badge %}selected{% endif %}>{% trans "Count" %}</option>
                            <option value="streak" {% if badge.criteria_type == 'streak' %}selected{% endif %}>{% trans "Streak" %}</option>
                            <option value="special" {% if badge.criteria_type == 'special' %}selected{% endif %}>{% trans "Special" %}</option>
                        </select>
                    </div>

                    <!-- Criteria Value -->
                    <div class="mb-4">
                        <label for="id_criteria_value" class="form-label">{% trans "Criteria Value" %}</label>
                        <input 
                            type="number" 
                            class="form-control" 
                            id="id_criteria_value" 
                            name="criteria_value" 
                            value="{{ form.criteria_value.value|default:'' }}"
                            min="1"
                            required
                        >
                        <p class="text-sm text-gray-500 mt-1">
                            {% trans "Number of appreciations required to earn this badge" %}
                        </p>
                    </div>

                    <!-- Is Active -->
                    <div class="mb-6">
                        <div class="flex items-center gap-3">
                            <input 
                                class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500" 
                                type="checkbox" 
                                id="id_is_active" 
                                name="is_active"
                                {% if badge.is_active %}checked{% endif %}
                            >
                            <label class="text-sm font-medium text-gray-700" for="id_is_active">
                                {% trans "Active" %}
                            </label>
                        </div>
                    </div>

                    <!-- Buttons -->
                    <div class="flex flex-wrap gap-3">
                        <a href="{% url 'appreciation:badge_list' %}" class="hh-btn hh-btn-secondary">
                            <i data-lucide="x" class="w-4 h-4"></i>
                            {% trans "Cancel" %}
                        </a>
                        <button type="submit" class="hh-btn hh-btn-primary">
                            <i data-lucide="save" class="w-4 h-4"></i>
                            {% if form.instance.pk %}{% trans "Update" %}{% else %}{% trans "Create" %}{% endif %}
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Sidebar -->
        <div class="lg:col-span-1">
            <!-- Icon Preview -->
            <div class="form-section text-center">
                <h6 class="font-semibold text-gray-800 mb-4">{% trans "Badge Preview" %}</h6>
                <div class="mb-4">
                    <i id="icon-preview" class="{{ form.icon.value|default:'bi-trophy' }} fa-4x text-yellow-500"></i>
                </div>
                <p id="name-preview" class="text-lg font-semibold mb-2">{{ form.name_en.value|default:'Badge Name' }}</p>
                <p class="text-sm text-gray-500">
                    <strong>{% trans "Requires" %}: </strong>
                    <span id="criteria-preview">{{ badge.criteria_value|default:0 }}</span>
                    {% trans "appreciations" %}
                </p>
            </div>

            <!-- Criteria Information -->
            <div class="form-section">
                <h6 class="font-semibold text-gray-800 mb-4 flex items-center gap-2">
                    <i data-lucide="info" class="w-4 h-4 text-blue-500"></i>
                    {% trans "About Badge Criteria" %}
                </h6>
                <ul class="space-y-3 text-sm">
                    <li>
                        <strong class="text-gray-800">{% trans "Count:" %}</strong>
                        <p class="text-gray-500 text-xs">
                            {% trans "Badge is earned after receiving the specified number of appreciations" %}
                        </p>
                    </li>
                    <li>
                        <strong class="text-gray-800">{% trans "Tips:" %}</strong>
                        <ul class="text-gray-500 text-xs mt-1 space-y-1">
                            <li>{% trans "Set achievable criteria to encourage participation" %}</li>
                            <li>{% trans "Use descriptive names and icons" %}</li>
                            <li>{% trans "Create badges for different achievement levels" %}</li>
                            <li>{% trans "Deactivate badges instead of deleting to preserve history" %}</li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
{{ block.super }}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const iconInput = document.getElementById('id_icon');
    const iconPreview = document.getElementById('icon-preview');
    const nameEnInput = document.getElementById('id_name_en');
    const namePreview = document.getElementById('name-preview');
    const criteriaValueInput = document.getElementById('id_criteria_value');
    const criteriaPreview = document.getElementById('criteria-preview');
    
    // Update icon preview
    iconInput.addEventListener('input', function() {
        const iconClass = this.value.trim() || 'bi-trophy';
        iconPreview.className = iconClass + ' fa-4x text-warning';
    });
    
    // Update name preview
    nameEnInput.addEventListener('input', function() {
        const name = this.value.trim() || 'Badge Name';
        namePreview.textContent = name;
    });
    
    // Update criteria preview
    criteriaValueInput.addEventListener('input', function() {
        const value = this.value.trim() || '0';
        criteriaPreview.textContent = value;
    });
});
</script>
{% endblock %}
