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

{% block title %}{% if form.instance.pk %}{% trans "Edit Category" %}{% else %}{% trans "Add Category" %}{% endif %} - {% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{% url 'appreciation:appreciation_list' %}">{% trans "Appreciation" %}</a></li>
            <li class="breadcrumb-item"><a href="{% url 'appreciation:category_list' %}">{% trans "Categories" %}</a></li>
            <li class="breadcrumb-item active" aria-current="page">{% if form.instance.pk %}{% trans "Edit" %}{% else %}{% trans "Add" %}{% endif %}</li>
        </ol>
    </nav>

    <!-- Form -->
    <div class="row">
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">
                        <i class="bi bi-tag me-2"></i>
                        {% if form.instance.pk %}{% trans "Edit Category" %}{% else %}{% trans "Add Category" %}{% endif %}
                    </h4>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        
                        <!-- Name (English) -->
                        <div class="mb-3">
                            <label for="id_name_en" class="form-label">
                                {% trans "Name (English)" %} <span class="text-danger">*</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-3">
                            <label for="id_name_ar" class="form-label">
                                {% trans "Name (Arabic)" %} <span class="text-danger">*</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-3">
                            <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-3">
                            <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-3">
                            <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"
                            >
                            <div class="form-text">
                                {% trans "FontAwesome icon class (e.g., fa-heart, fa-star, fa-thumbs-up)" %}
                            </div>
                        </div>

                        <!-- Color -->
                        <div class="mb-3">
                            <label for="id_color" class="form-label">{% trans "Color" %}</label>
                            <select class="form-select" id="id_color" name="color">
                                {% for choice in form.fields.color.choices %}
                                    <option value="{{ choice.0 }}" {% if choice.0 == form.color.value|stringformat:'s' or (not form.color.value and choice.0 == 'primary') %}selected{% endif %}>
                                        {{ choice.1 }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>

                        <!-- Is Active -->
                        <div class="mb-4">
                            <div class="form-check">
                                <input 
                                    class="form-check-input" 
                                    type="checkbox" 
                                    id="id_is_active" 
                                    name="is_active"
                                    {% if form.is_active.value %}checked{% endif %}
                                >
                                <label class="form-check-label" for="id_is_active">
                                    {% trans "Active" %}
                                </label>
                            </div>
                        </div>

                        <!-- Buttons -->
                        <div class="d-flex gap-2">
                            <a href="{% url 'appreciation:category_list' %}" class="btn btn-outline-secondary">
                                <i class="bi bi-x me-2"></i>
                                {% trans "Cancel" %}
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-save me-2"></i>
                                {% if form.instance.pk %}{% trans "Update" %}{% else %}{% trans "Create" %}{% endif %}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Sidebar -->
        <div class="col-lg-4">
            <!-- Icon Preview -->
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h6 class="card-title mb-0">{% trans "Icon Preview" %}</h6>
                </div>
                <div class="card-body text-center">
                    <i id="icon-preview" class="{{ form.icon.value|default:'bi-heart' }} fa-4x text-primary mb-3"></i>
                    <p id="name-preview" class="h5 mb-0">{{ form.name_en.value|default:'Category Name' }}</p>
                </div>
            </div>

            <!-- Tips -->
            <div class="card shadow-sm">
                <div class="card-header bg-light">
                    <h6 class="card-title mb-0">
                        <i class="bi bi-lightbulb me-2"></i>
                        {% trans "Tips" %}
                    </h6>
                </div>
                <div class="card-body">
                    <ul class="list-unstyled mb-0 small">
                        <li class="mb-2">
                            <i class="bi bi-check text-success me-2"></i>
                            {% trans "Use descriptive names for categories" %}
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check text-success me-2"></i>
                            {% trans "Choose appropriate icons for each category" %}
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check text-success me-2"></i>
                            {% trans "Colors help users quickly identify categories" %}
                        </li>
                        <li>
                            <i class="bi bi-check text-success me-2"></i>
                            {% trans "Deactivate unused categories instead of deleting" %}
                        </li>
                    </ul>
                </div>
            </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 %}
