{% include 'header.twig' %}

<style>
/* === Global Container === */
.content__area {
    padding: 20px;
    background-color: #f9fafd;
    min-height: 100vh;
    display: flex;
    justify-content: center;
}

/* === Card Wrapper === */
.analytics-card {
    width: 100%;
    max-width: 1200px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.06);
    padding: 20px;
}

/* === Page Title === */
.page-title {
    font-size: 24px;
    font-weight: 700;
    color: #111827;
    margin-bottom: 6px;
}

.page-subtitle {
    font-size: 14px;
    color: #6b7280;
    margin-bottom: 20px;
}

/* Filters + Search */
.top-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    gap: 10px;
}

.search-container {
    display: flex;
    align-items: center;
    background-color: #f9fafb;
    padding: 6px 10px;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.05);
}

.search-container input {
    border: none;
    background: transparent;
    font-size: 14px;
    padding: 6px;
    outline: none;
    width: 180px;
}

.search-container button {
    border: none;
    background-color: #6366f1;
    color: #fff;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    transition: 0.3s ease;
}

.search-container button:hover {
    background-color: #4f46e5;
}

/* === Cards Grid === */
.grid-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill,minmax(250px,1fr));
    gap: 15px;
    margin-bottom: 20px;
}

.card-item {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    padding: 15px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.card-item-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
}

.card-item-header .badge {
    font-size: 12px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 20px;
}

.badge.today { background-color: #10b981; color: #fff; }
.badge.upcoming { background-color: #f59e0b; color: #fff; }

.card-item-body .service-name {
    font-weight: 700;
    font-size: 14px;
    margin-bottom: 5px;
}

.card-item-body .info {
    font-size: 12px;
    color: #6b7280;
    display: flex;
    justify-content: space-between;
}

/* === Table Styling === */
.table-container {
    overflow-x: auto;
    border-radius: 10px;
    border: 1px solid #e5e7eb;
    margin-top: 15px;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 700px;
}

thead {
    background-color: #f9fafb;
    border-bottom: 1px solid #e5e7eb;
}

th, td {
    padding: 12px 15px;
    font-size: 14px;
    color: #4b5563;
    border-bottom: 1px solid #f1f1f1;
    text-align: left;
    white-space: nowrap;
}

th {
    font-weight: 700;
    color: #374151;
}

.btn-order {
    background-color: #6366f1;
    color: #fff;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
}

.btn-order:hover {
    background-color: #4f46e5;
}

/* Responsive Design */
@media screen and (max-width: 768px) {
    .page-title { font-size: 20px; }
    .top-actions { flex-direction: column; align-items: stretch; }
    .search-container input { width: 100%; }
    table { min-width: unset; }
}
</style>

<div class="content__area">
    <div class="analytics-card">
        <h1 class="page-title">Order/Service Analytics</h1>
        <p class="page-subtitle">
            Explore recent orders, processing times, and top-performing services on {{ site['site_name'] }}.
        </p>

        <div class="top-actions">
            <div class="search-container">
                <form action="/analytics" method="get">
                    <input type="text" name="search" value="{{ search|default('') }}" placeholder="Search for service or order IDs">
                    <button type="submit"><i class="fas fa-search"></i></button>
                </form>
            </div>
        </div>

        <!-- CARDS VIEW -->
        <div id="cardView" class="grid-cards">
            {% set today = "now"|date("Y-m-d") %}
            {% for order in orders %}
            <div class="card-item">
                <div class="card-item-header">
                    <span class="badge" style="background-color:#1967be;">#{{ order['id'] }}</span>
                    {% if order['date']|date("Y-m-d") == today %}
                        <span class="badge today">Today's order</span>
                    {% endif %}
                    <span class="badge" style="background-color:#1967be;">{{ order['date'] }}</span>
                </div>
                <div class="card-item-body">
                    <div class="service-name">{{ order['service_id'] }} - {{ order['service'] }}</div>
                    <div class="info">
                        <span>Quantity: <b>{{ order['quantity'] }}</b></span>
                        <span>Duration: <b>{{ order['duration'] }}</b></span>
                        <span class="text-indigo-600 font-bold">{{ order['charge'] }}</span>
                    </div>
                </div>
                <a href="{{ site['url'] }}/?category_id={{order['category_id']}}&service_id={{ order['service_id'] }}" class="btn-order mt-2 flex justify-center">Create Order</a>
            </div>
            {% endfor %}
        </div>

        <!-- TABLE VIEW -->
        <div id="tableView" class="table-container" style="display:none;">
            <table>
                <thead>
                    <tr>
                        <th>Order ID</th>
                        <th>Service</th>
                        <th>Date</th>
                        <th>Quantity</th>
                        <th>Duration</th>
                        <th>Charge</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    {% for order in orders %}
                    <tr>
                        <td>#{{ order['id'] }}</td>
                        <td>{{ order['service_id'] }} - {{ order['service'] }}</td>
                        <td>{{ order['date'] }}</td>
                        <td>{{ order['quantity'] }}</td>
                        <td>{{ order['duration'] }}</td>
                        <td>{{ order['charge'] }}</td>
                        <td>
                            <a href="{{ site['url'] }}/?category_id={{order['category_id']}}&service_id={{ order['service_id'] }}" class="btn-order">Order</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
{% if pagination["count"] > 1 %}
<div class="mt-4 flex justify-center">
    <ul class="inline-flex items-center space-x-2 list-none p-0 m-0">
        
        {# Previous button #}
        {% if pagination["current"] > 1 %}
        <li>
            <a class="px-3 py-1 text-sm bg-gray-200 rounded hover:bg-indigo-200"
               href="analytics/{{ status }}/{{ pagination['previous'] }}{% if search %}?search={{ search }}{% endif %}">
               «
            </a>
        </li>
        {% endif %}

        {# First page #}
        {% if pagination["current"] > 3 %}
        <li>
            <a class="px-3 py-1 text-sm bg-gray-200 rounded hover:bg-indigo-200"
               href="analytics/{{ status }}/1{% if search %}?search={{ search }}{% endif %}">1</a>
        </li>
        {% if pagination["current"] > 4 %}
        <li><span class="px-3 py-1 text-sm text-gray-500">…</span></li>
        {% endif %}
        {% endif %}

        {# Pages around current #}
        {% for page in (pagination["current"]-2)..(pagination["current"]+2) %}
            {% if page > 0 and page <= pagination["count"] %}
            <li>
                <a class="px-3 py-1 text-sm rounded 
                   {% if page == pagination['current'] %}
                       bg-indigo-700 text-white
                   {% else %}
                       bg-gray-200 text-gray-700 hover:bg-indigo-200
                   {% endif %}"
                   href="analytics/{{ status }}/{{ page }}{% if search %}?search={{ search }}{% endif %}">
                   {{ page }}
                </a>
            </li>
            {% endif %}
        {% endfor %}

        {# Last page #}
        {% if pagination["current"] < pagination["count"]-2 %}
        {% if pagination["current"] < pagination["count"]-3 %}
        <li><span class="px-3 py-1 text-sm text-gray-500">…</span></li>
        {% endif %}
        <li>
            <a class="px-3 py-1 text-sm bg-gray-200 rounded hover:bg-indigo-200"
               href="analytics/{{ status }}/{{ pagination['count'] }}{% if search %}?search={{ search }}{% endif %}">
               {{ pagination['count'] }}
            </a>
        </li>
        {% endif %}

        {# Next button #}
        {% if pagination["current"] < pagination["count"] %}
        <li>
            <a class="px-3 py-1 text-sm bg-gray-200 rounded hover:bg-indigo-200"
               href="analytics/{{ status }}/{{ pagination['next'] }}{% if search %}?search={{ search }}{% endif %}">
               »
            </a>
        </li>
        {% endif %}

    </ul>
</div>
{% endif %}

<script>
document.addEventListener('DOMContentLoaded', function() {
    const toggleViewButton = document.getElementById('toggleView');
    const cardView = document.getElementById('cardView');
    const tableView = document.getElementById('tableView');
    const savedView = localStorage.getItem('viewPreference');
    
    if (savedView === 'table') {
        cardView.style.display = 'none';
        tableView.style.display = 'block';
    } else {
        cardView.style.display = 'grid';
        tableView.style.display = 'none';
    }
});
</script>

{% include 'footer.twig' %}