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

{% block title %}{% if category %}{% trans "Edit Category" %}{% else %}{% trans "Add Category" %}{% 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:category_list' %}" class="text-blue-600 hover:text-blue-800">{% trans "Categories" %}</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 category %}{% 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="tag" class="w-6 h-6"></i>
            {% if category %}{% trans "Edit Category" %}{% else %}{% trans "Add Category" %}{% 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-heart' }}"
                            placeholder="fa-heart"
                        >
                        <p class="text-sm text-gray-500 mt-1">
                            {% trans "FontAwesome icon class (e.g., fa-heart, fa-star, fa-thumbs-up)" %}
                        </p>
                    </div>

                    <!-- Color -->
                    <div class="mb-4">
                        <label for="id_color" class="form-label">{% trans "Color" %}</label>
                        <select class="form-select" id="id_color" name="color">
                            <option value="primary" {% if category.color == 'primary' or not category %}selected{% endif %}>{% trans "Primary" %}</option>
                            <option value="success" {% if category.color == 'success' %}selected{% endif %}>{% trans "Success" %}</option>
                            <option value="warning" {% if category.color == 'warning' %}selected{% endif %}>{% trans "Warning" %}</option>
                            <option value="danger" {% if category.color == 'danger' %}selected{% endif %}>{% trans "Danger" %}</option>
                            <option value="info" {% if category.color == 'info' %}selected{% endif %}>{% trans "Info" %}</option>
                        </select>
                    </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 form.is_active.value %}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:category_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 "Icon Preview" %}</h6>
                <i id="icon-preview" class="{{ category.icon|default:'bi-heart' }} fa-4x text-blue-600 mb-4"></i>
                <p id="name-preview" class="text-lg font-semibold">{{ category.name_en|default:'Category Name' }}</p>
            </div>

            <!-- Tips -->
            <div class="form-section">
                <h6 class="font-semibold text-gray-800 mb-4 flex items-center gap-2">
                    <i data-lucide="lightbulb" class="w-4 h-4 text-yellow-500"></i>
                    {% trans "Tips" %}
                </h6>
                <ul class="space-y-2 text-sm">
                    <li class="flex items-start gap-2">
                        <i data-lucide="check" class="w-4 h-4 text-green-500 mt-0.5"></i>
                        {% trans "Use descriptive names for categories" %}
                    </li>
                    <li class="flex items-start gap-2">
                        <i data-lucide="check" class="w-4 h-4 text-green-500 mt-0.5"></i>
                        {% trans "Choose appropriate icons for each category" %}
                    </li>
                    <li class="flex items-start gap-2">
                        <i data-lucide="check" class="w-4 h-4 text-green-500 mt-0.5"></i>
                        {% trans "Colors help users quickly identify categories" %}
                    </li>
                    <li class="flex items-start gap-2">
                        <i data-lucide="check" class="w-4 h-4 text-green-500 mt-0.5"></i>
                        {% trans "Deactivate unused categories instead of deleting" %}
                    </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');
    
    // Update icon preview
    iconInput.addEventListener('input', function() {
        const iconClass = this.value.trim() || 'bi-heart';
        iconPreview.className = iconClass + ' fa-4x text-primary';
    });
    
    // Update name preview
    nameEnInput.addEventListener('input', function() {
        const name = this.value.trim() || 'Category Name';
        namePreview.textContent = name;
    });
});
</script>
{% endblock %}
