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

{% block title %}{% trans "Appreciation" %} - {% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Page Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h2 class="h4 mb-0">
                                <i class="bi bi-heart-fill text-danger me-2"></i>
                                {% trans "Appreciation" %}
                            </h2>
                            <p class="text-muted mb-0 mt-2">
                                {% trans "Send appreciation to colleagues and celebrate achievements" %}
                            </p>
                        </div>
                        <a href="{% url 'appreciation:appreciation_send' %}" class="btn btn-primary">
                            <i class="bi bi-send me-2"></i>
                            {% trans "Send Appreciation" %}
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="bg-primary bg-opacity-10 p-3 rounded-circle me-3">
                            <i class="bi bi-inbox text-primary fa-lg"></i>
                        </div>
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Received" %}</h6>
                            <h3 class="mb-0" id="receivedCount">{{ stats.received }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="bg-success bg-opacity-10 p-3 rounded-circle me-3">
                            <i class="bi bi-send text-success fa-lg"></i>
                        </div>
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Sent" %}</h6>
                            <h3 class="mb-0" id="sentCount">{{ stats.sent }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="bg-warning bg-opacity-10 p-3 rounded-circle me-3">
                            <i class="bi bi-award text-warning fa-lg"></i>
                        </div>
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Badges Earned" %}</h6>
                            <h3 class="mb-0" id="badgesCount">{{ stats.badges_earned }}</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="bg-info bg-opacity-10 p-3 rounded-circle me-3">
                            <i class="bi bi-trophy text-info fa-lg"></i>
                        </div>
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Leaderboard" %}</h6>
                            <a href="{% url 'appreciation:leaderboard_view' %}" class="btn btn-sm btn-outline-info">
                                <i class="bi bi-arrow-right me-1"></i>
                                {% trans "View" %}
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="row mb-4">
        <div class="col-md-4">
            <a href="{% url 'appreciation:leaderboard_view' %}" class="text-decoration-none">
                <div class="card shadow-sm border-warning mb-3">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="bg-warning bg-opacity-10 p-3 rounded-circle me-3">
                                <i class="bi bi-trophy text-warning fa-lg"></i>
                            </div>
                            <div>
                                <h6 class="mb-0">{% trans "Leaderboard" %}</h6>
                                <small class="text-muted">{% trans "See top performers" %}</small>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="{% url 'appreciation:my_badges_view' %}" class="text-decoration-none">
                <div class="card shadow-sm border-primary mb-3">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="bg-primary bg-opacity-10 p-3 rounded-circle me-3">
                                <i class="bi bi-award text-primary fa-lg"></i>
                            </div>
                            <div>
                                <h6 class="mb-0">{% trans "My Badges" %}</h6>
                                <small class="text-muted">{% trans "View earned badges" %}</small>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="{% url 'appreciation:appreciation_send' %}" class="text-decoration-none">
                <div class="card shadow-sm border-success mb-3">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="bg-success bg-opacity-10 p-3 rounded-circle me-3">
                                <i class="bi bi-send text-success fa-lg"></i>
                            </div>
                            <div>
                                <h6 class="mb-0">{% trans "Send Appreciation" %}</h6>
                                <small class="text-muted">{% trans "Share appreciation" %}</small>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    </div>

    <!-- Filter Bar -->
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label for="tab" class="form-label">{% trans "View" %}</label>
                    <select class="form-select" id="tab" name="tab" onchange="this.form.submit()">
                        <option value="received" {% if current_tab == 'received' %}selected{% endif %}>
                            {% trans "My Appreciations" %}
                        </option>
                        <option value="sent" {% if current_tab == 'sent' %}selected{% endif %}>
                            {% trans "Sent by Me" %}
                        </option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="status" class="form-label">{% trans "Status" %}</label>
                    <select class="form-select" id="status" name="status" onchange="this.form.submit()">
                        <option value="">{% trans "All Status" %}</option>
                        {% for choice in status_choices %}
                            <option value="{{ choice.0 }}" {% if filters.status == choice.0 %}selected{% endif %}>
                                {{ choice.1 }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="category" class="form-label">{% trans "Category" %}</label>
                    <select class="form-select" id="category" name="category" onchange="this.form.submit()">
                        <option value="">{% trans "All Categories" %}</option>
                        {% for cat in categories %}
                            <option value="{{ cat.id }}" {% if filters.category == cat.id|stringformat:"s" %}selected{% endif %}>
                                {{ cat.name_en }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="search" class="form-label">{% trans "Search" %}</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="search" name="search" 
                               value="{{ filters.search|default:'' }}" placeholder="{% trans 'Search messages...' %}">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-search"></i>
                        </button>
                    </div>
                </div>
                <div class="col-12">
                    <a href="{% url 'appreciation:appreciation_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-clockwise me-2"></i>
                        {% trans "Clear Filters" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Appreciations List -->
    <div class="card shadow-sm">
        <div class="card-body">
            {% if page_obj %}
                <div class="list-group">
                    {% for appreciation in page_obj %}
                        <a href="{% url 'appreciation:appreciation_detail' appreciation.id %}" 
                           class="list-group-item list-group-item-action">
                            <div class="d-flex w-100 justify-content-between align-items-start">
                                <div class="flex-grow-1">
                                    <div class="d-flex align-items-center mb-2">
                                        {% if appreciation.category %}
                                            <span class="badge bg-primary me-2">
                                                <i class="{{ appreciation.category.icon|default:'fa-heart' }} me-1"></i>
                                                {{ appreciation.category.name_en }}
                                            </span>
                                        {% endif %}
                                        <h6 class="mb-0">
                                            {% if current_tab == 'received' %}
                                                {{ appreciation.sender.get_full_name }}
                                            {% else %}
                                                {{ appreciation.recipient_name }}
                                            {% endif %}
                                        </h6>
                                    </div>
                                    <p class="mb-2 text-muted">{{ appreciation.message_en|truncatewords:15 }}</p>
                                    <small class="text-muted">
                                        <i class="bi bi-clock me-1"></i>
                                        {{ appreciation.sent_at|date:"F j, Y, g:i A" }}
                                        {% if appreciation.department %}
                                            <span class="mx-2">•</span>
                                            <i class="bi bi-building me-1"></i>
                                            {{ appreciation.department.name }}
                                        {% endif %}
                                    </small>
                                </div>
                                <div class="text-end">
                                    <span class="badge bg-{% if appreciation.status == 'acknowledged' %}success{% else %}primary{% endif %}">
                                        {{ appreciation.get_status_display }}
                                    </span>
                                    <div class="small text-muted mt-1">
                                        {{ appreciation.get_visibility_display }}
                                    </div>
                                </div>
                            </div>
                        </a>
                    {% endfor %}
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                    <nav aria-label="Page navigation" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}&tab={{ current_tab }}{% if filters.urlencode %}&{{ filters.urlencode }}{% endif %}">
                                        {% trans "Previous" %}
                                    </a>
                                </li>
                            {% else %}
                                <li class="page-item disabled">
                                    <span class="page-link">{% trans "Previous" %}</span>
                                </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ num }}&tab={{ current_tab }}{% if filters.urlencode %}&{{ filters.urlencode }}{% endif %}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}&tab={{ current_tab }}{% if filters.urlencode %}&{{ filters.urlencode }}{% endif %}">
                                        {% trans "Next" %}
                                    </a>
                                </li>
                            {% else %}
                                <li class="page-item disabled">
                                    <span class="page-link">{% trans "Next" %}</span>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                {% endif %}
            {% else %}
                <div class="text-center py-5">
                    <i class="bi bi-heart text-muted fa-4x mb-3"></i>
                    <h4 class="text-muted">
                        {% if current_tab == 'received' %}
                            {% trans "No appreciations received yet" %}
                        {% else %}
                            {% trans "No appreciations sent yet" %}
                        {% endif %}
                    </h4>
                    <p class="text-muted mb-3">
                        {% trans "Start sharing appreciation with your colleagues!" %}
                    </p>
                    <a href="{% url 'appreciation:appreciation_send' %}" class="btn btn-primary">
                        <i class="bi bi-send me-2"></i>
                        {% trans "Send Your First Appreciation" %}
                    </a>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
{{ block.super }}
<script>
// Refresh summary statistics periodically (every 30 seconds)
function refreshSummary() {
    fetch("{% url 'appreciation:appreciation_summary_ajax' %}")
        .then(response => response.json())
        .then(data => {
            document.getElementById('receivedCount').textContent = data.total_received || 0;
            document.getElementById('sentCount').textContent = data.total_sent || 0;
            document.getElementById('badgesCount').textContent = data.badges_earned || 0;
        })
        .catch(error => {
            console.error('Error refreshing summary:', error);
        });
}

// Refresh summary every 30 seconds
setInterval(refreshSummary, 30000);

// Initial summary refresh after page load
setTimeout(refreshSummary, 1000);
</script>
{% endblock %}
