{# app/templates/vendor/dashboard.html #}
{% extends "vendor/base.html" %}

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

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

{% block content %}
<!-- Page Header with Refresh Button -->
<div class="flex items-center justify-between my-6">
    <h2 class="text-2xl font-semibold text-gray-700 dark:text-gray-200">
        Dashboard
    </h2>
    <button
        @click="refresh()"
        :disabled="loading"
        class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple disabled:opacity-50 disabled:cursor-not-allowed"
    >
        <span x-show="!loading" x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
        <span x-show="loading" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
        <span x-text="loading ? 'Loading...' : 'Refresh'"></span>
    </button>
</div>

<!-- Loading State -->
<div x-show="loading" class="text-center py-12">
    <span x-html="$icon('spinner', 'inline w-8 h-8 text-purple-600')"></span>
    <p class="mt-2 text-gray-600 dark:text-gray-400">Loading dashboard...</p>
</div>

<!-- Error State -->
<div x-show="error && !loading" class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-start">
    <span x-html="$icon('exclamation', 'w-5 h-5 mr-3 mt-0.5 flex-shrink-0')"></span>
    <div>
        <p class="font-semibold">Error loading dashboard</p>
        <p class="text-sm" x-text="error"></p>
    </div>
</div>

<!-- Vendor Info Card -->
{% include 'vendor/partials/vendor_info.html' %}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Card: Total Products -->
    <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('shopping-bag', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Products
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.products_count">
                0
            </p>
        </div>
    </div>

    <!-- Card: Total Orders -->
    <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('shopping-cart', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Orders
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.orders_count">
                0
            </p>
        </div>
    </div>

    <!-- Card: Total Customers -->
    <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('users', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Customers
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.customers_count">
                0
            </p>
        </div>
    </div>

    <!-- Card: Revenue -->
    <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('currency-euro', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Revenue
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatCurrency(stats.revenue)">
                €0
            </p>
        </div>
    </div>
</div>

<!-- Recent Orders Table -->
<div x-show="!loading && recentOrders.length > 0" class="w-full mb-8 overflow-hidden rounded-lg shadow-xs">
    <div class="w-full overflow-x-auto">
        <table class="w-full whitespace-no-wrap">
            <thead>
                <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
                    <th class="px-4 py-3">Order ID</th>
                    <th class="px-4 py-3">Customer</th>
                    <th class="px-4 py-3">Amount</th>
                    <th class="px-4 py-3">Status</th>
                    <th class="px-4 py-3">Date</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                <template x-for="order in recentOrders" :key="order.id">
                    <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">
                            <span class="text-xs" x-text="'#' + order.id"></span>
                        </td>
                        <td class="px-4 py-3 text-sm" x-text="order.customer_name">
                        </td>
                        <td class="px-4 py-3 text-sm font-semibold" x-text="formatCurrency(order.total_amount)">
                        </td>
                        <td class="px-4 py-3 text-xs">
                            <span class="px-2 py-1 font-semibold leading-tight rounded-full"
                                  :class="{
                                      'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': order.status === 'completed',
                                      'text-orange-700 bg-orange-100 dark:text-white dark:bg-orange-600': order.status === 'pending',
                                      'text-blue-700 bg-blue-100 dark:bg-blue-700 dark:text-blue-100': order.status === 'processing'
                                  }"
                                  x-text="order.status"></span>
                        </td>
                        <td class="px-4 py-3 text-sm" x-text="formatDate(order.created_at)">
                        </td>
                    </tr>
                </template>
            </tbody>
        </table>
    </div>
</div>

<!-- Getting Started Section -->
<div x-show="!loading && recentOrders.length === 0" class="w-full mb-8 overflow-hidden rounded-lg shadow-xs">
    <div class="w-full p-8 bg-white dark:bg-gray-800 text-center">
        <div class="text-6xl mb-4">🚀</div>
        <h3 class="text-xl font-semibold text-gray-700 dark:text-gray-200 mb-2">
            Welcome to Your Vendor Dashboard!
        </h3>
        <p class="text-gray-600 dark:text-gray-400 mb-6">
            Start by importing products from the marketplace to build your catalog.
        </p>
        <a href="/vendor/{{ vendor_code }}/marketplace"
           class="inline-flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple">
            <span x-html="$icon('download', 'w-4 h-4 mr-2')"></span>
            Go to Marketplace Import
        </a>
    </div>
</div>
{% endblock %}

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