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

{% block title %}{% if form.instance.pk %}{% trans "Edit Badge" %}{% else %}{% trans "Add Badge" %}{% 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:badge_list' %}">{% trans "Badges" %}</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-warning text-dark">
                    <h4 class="mb-0">
                        <i class="bi bi-trophy me-2"></i>
                        {% if form.instance.pk %}{% trans "Edit Badge" %}{% else %}{% trans "Add Badge" %}{% 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-trophy' }}"
                                placeholder="fa-trophy"
                            >
                            <div class="form-text">
                                {% trans "FontAwesome icon class (e.g., fa-trophy, fa-star, fa-medal)" %}
                            </div>
                        </div>

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

                        <!-- Criteria Value -->
                        <div class="mb-3">
                            <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
                            >
                            <div class="form-text">
                                {% trans "Number of appreciations required to earn this badge" %}
                            </div>
                        </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:badge_list' %}" class="btn btn-outline-secondary">
                                <i class="bi bi-x me-2"></i>
                                {% trans "Cancel" %}
                            </a>
                            <button type="submit" class="btn btn-warning text-dark">
                                <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 "Badge Preview" %}</h6>
                </div>
                <div class="card-body text-center">
                    <div class="badge-icon-wrapper mb-3">
                        <i id="icon-preview" class="{{ form.icon.value|default:'bi-trophy' }} fa-4x text-warning"></i>
                    </div>
                    <p id="name-preview" class="h5 mb-2">{{ form.name_en.value|default:'Badge Name' }}</p>
                    <p class="small text-muted mb-0">
                        <strong>{% trans "Requires" %}: </strong>
                        <span id="criteria-preview">{{ form.criteria_value.value|default:0 }}</span>
                        {% trans "appreciations" %}
                    </p>
                </div>
            </div>

            <!-- Criteria Information -->
            <div class="card shadow-sm">
                <div class="card-header bg-light">
                    <h6 class="card-title mb-0">
                        <i class="bi bi-info-circle me-2"></i>
                        {% trans "About Badge Criteria" %}
                    </h6>
                </div>
                <div class="card-body">
                    <ul class="list-unstyled mb-0 small">
                        <li class="mb-2">
                            <strong>{% trans "Count:" %}</strong>
                            <p class="mb-0 text-muted">
                                {% trans "Badge is earned after receiving the specified number of appreciations" %}
                            </p>
                        </li>
                        <li>
                            <strong>{% trans "Tips:" %}</strong>
                            <ul class="mb-0 ps-3 text-muted">
                                <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>
</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 %}
