{# app/templates/admin/dashboard.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/headers.html' import page_header %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/tables.html' import table_wrapper, table_header %}

{% block title %}Dashboard{% endblock %}

{% block alpine_data %}adminDashboard(){% endblock %}

{% block content %}
{{ page_header('Dashboard', action_label='Refresh', action_onclick='refresh()', action_icon='refresh') }}

{{ loading_state('Loading dashboard...') }}

{{ error_state('Error loading dashboard') }}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Card: Total Stores -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-orange-500 bg-orange-100 rounded-full dark:text-orange-100 dark:bg-orange-500">
            <span x-html="$icon('user-group', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Stores
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.totalStores">
                0
            </p>
        </div>
    </div>

    <!-- Card: Active Users -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-500">
            <span x-html="$icon('users', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Active Users
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.activeUsers">
                0
            </p>
        </div>
    </div>

    <!-- Card: Verified Stores -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
            <span x-html="$icon('badge-check', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Verified Stores
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.verifiedStores">
                0
            </p>
        </div>
    </div>

    <!-- Card: Import Jobs -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-teal-500 bg-teal-100 rounded-full dark:text-teal-100 dark:bg-teal-500">
            <span x-html="$icon('download', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Import Jobs
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.importJobs">
                0
            </p>
        </div>
    </div>
</div>

<!-- Recent Stores Table -->
<div x-show="!loading">
    {% call table_wrapper() %}
            {{ table_header(['Store', 'Status', 'Created', 'Actions']) }}
            <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                <template x-if="recentStores.length === 0">
                    <tr>
                        <td colspan="4" class="px-4 py-8 text-center text-gray-600 dark:text-gray-400">
                            <div class="flex flex-col items-center">
                                <span x-html="$icon('user-group', 'w-12 h-12 mb-2 text-gray-300')"></span>
                                <p>No stores yet.</p>
                            </div>
                        </td>
                    </tr>
                </template>

                <template x-for="store in recentStores" :key="store.store_code">
                    <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
                        <td class="px-4 py-3">
                            <div class="flex items-center text-sm">
                                <div class="relative hidden w-8 h-8 mr-3 rounded-full md:block">
                                    <div class="absolute inset-0 rounded-full bg-purple-100 dark:bg-purple-600 flex items-center justify-center">
                                        <span class="text-xs font-semibold text-purple-600 dark:text-purple-100" x-text="store.name?.charAt(0).toUpperCase() || '?'"></span>
                                    </div>
                                </div>
                                <div>
                                    <p class="font-semibold" x-text="store.name"></p>
                                    <p class="text-xs text-gray-600 dark:text-gray-400" x-text="store.store_code"></p>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-xs">
                            <span class="inline-flex items-center px-2 py-1 font-semibold leading-tight rounded-full"
                                  :class="store.is_verified ? 'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100' : 'text-orange-700 bg-orange-100 dark:text-white dark:bg-orange-600'">
                                <span x-show="store.is_verified" x-html="$icon('badge-check', 'w-3 h-3 mr-1')"></span>
                                <span x-text="store.is_verified ? 'Verified' : 'Pending'"></span>
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm" x-text="formatDate(store.created_at)">
                        </td>
                        <td class="px-4 py-3">
                            <div class="flex items-center space-x-2 text-sm">
                                <button
                                    @click="viewStore(store.store_code)"
                                    class="flex items-center justify-center p-2 text-purple-600 rounded-lg hover:bg-purple-50 dark:text-gray-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="View store"
                                >
                                    <span x-html="$icon('eye', 'w-5 h-5')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
    {% endcall %}
</div>
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('core_static', path='admin/js/dashboard.js') }}"></script>
{% endblock %}
