{# app/templates/admin/merchants.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% 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 %}
{% from 'shared/macros/modals.html' import confirm_modal_dynamic %}

{% block title %}Merchants{% endblock %}

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

{% block content %}
{{ page_header('Merchant Management', action_label='Create Merchant', action_url='/admin/merchants/create') }}

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

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

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

    <!-- Card: Verified Merchants -->
    <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('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
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.verified || 0">
                0
            </p>
        </div>
    </div>

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

    <!-- 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('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 Stores
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.totalStores || 0">
                0
            </p>
        </div>
    </div>
</div>

<!-- Search and Filters Bar -->
<div x-show="!loading" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
        <!-- Search Input -->
        <div class="flex-1 max-w-md">
            <div class="relative">
                <span class="absolute inset-y-0 left-0 flex items-center pl-3">
                    <span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
                </span>
                <input
                    type="text"
                    x-model="filters.search"
                    @input="debouncedSearch()"
                    placeholder="Search by name or email..."
                    class="w-full pl-10 pr-4 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300"
                >
            </div>
        </div>

        <!-- Filters -->
        <div class="flex flex-wrap gap-3">
            <!-- Status Filter -->
            <select
                x-model="filters.is_active"
                @change="pagination.page = 1; loadMerchants()"
                class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
            >
                <option value="">All Status</option>
                <option value="true">Active</option>
                <option value="false">Inactive</option>
            </select>

            <!-- Verification Filter -->
            <select
                x-model="filters.is_verified"
                @change="pagination.page = 1; loadMerchants()"
                class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
            >
                <option value="">All Verification</option>
                <option value="true">Verified</option>
                <option value="false">Unverified</option>
            </select>

            <!-- Refresh Button -->
            <button
                @click="loadMerchants()"
                class="flex items-center px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                title="Refresh merchants"
            >
                <span x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
                Refresh
            </button>
        </div>
    </div>
</div>

<!-- Merchants Table -->
<div x-show="!loading">
    {% call table_wrapper() %}
            {{ table_header(['Merchant', 'Owner', 'Stores', 'Status', 'Created', 'Actions']) }}
            <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                <!-- Empty State -->
                <template x-if="paginatedMerchants.length === 0">
                    <tr>
                        <td colspan="6" 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('office-building', 'w-12 h-12 mb-2 text-gray-300')"></span>
                                <p class="font-medium">No merchants found</p>
                                <p class="text-xs mt-1" x-text="filters.search || filters.is_active || filters.is_verified ? 'Try adjusting your search or filters' : 'Create your first merchant to get started'"></p>
                            </div>
                        </td>
                    </tr>
                </template>

                <!-- Merchant Rows -->
                <template x-for="merchant in paginatedMerchants" :key="merchant.id">
                    <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
                        <!-- Merchant Info -->
                        <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-blue-100 dark:bg-blue-600 flex items-center justify-center">
                                        <span class="text-xs font-semibold text-blue-600 dark:text-blue-100"
                                              x-text="merchant.name?.charAt(0).toUpperCase() || '?'"></span>
                                    </div>
                                </div>
                                <div>
                                    <p class="font-semibold" x-text="merchant.name"></p>
                                    <p class="text-xs text-gray-600 dark:text-gray-400" x-text="merchant.contact_email"></p>
                                </div>
                            </div>
                        </td>

                        <!-- Owner Email -->
                        <td class="px-4 py-3 text-sm">
                            <p x-text="merchant.owner_email || 'N/A'"></p>
                        </td>

                        <!-- Store Count -->
                        <td class="px-4 py-3 text-sm">
                            <span class="px-2 py-1 font-semibold leading-tight text-blue-700 bg-blue-100 rounded-full dark:bg-blue-700 dark:text-blue-100"
                                  x-text="merchant.store_count || 0">
                                0
                            </span>
                        </td>

                        <!-- Status Badges -->
                        <td class="px-4 py-3 text-xs">
                            <div class="flex gap-1">
                                <span class="inline-flex items-center px-2 py-1 font-semibold leading-tight rounded-full"
                                      :class="merchant.is_active ? 'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100' : 'text-gray-700 bg-gray-100 dark:text-gray-100 dark:bg-gray-700'">
                                    <span x-text="merchant.is_active ? 'Active' : 'Inactive'"></span>
                                </span>
                                <span x-show="merchant.is_verified" class="inline-flex items-center px-2 py-1 font-semibold leading-tight text-blue-700 bg-blue-100 rounded-full dark:bg-blue-700 dark:text-blue-100">
                                    <span x-html="$icon('badge-check', 'w-3 h-3 mr-1')"></span>
                                    Verified
                                </span>
                            </div>
                        </td>

                        <!-- Created Date -->
                        <td class="px-4 py-3 text-sm" x-text="formatDate(merchant.created_at)"></td>

                        <!-- Actions -->
                        <td class="px-4 py-3">
                            <div class="flex items-center space-x-2 text-sm">
                                <!-- View Button -->
                                <a
                                    :href="'/admin/merchants/' + merchant.id"
                                    class="flex items-center justify-center p-2 text-blue-600 rounded-lg hover:bg-blue-50 dark:text-blue-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="View merchant"
                                >
                                    <span x-html="$icon('eye', 'w-5 h-5')"></span>
                                </a>

                                <!-- Edit Button -->
                                <button
                                    @click="editMerchant(merchant.id)"
                                    class="flex items-center justify-center p-2 text-purple-600 rounded-lg hover:bg-purple-50 dark:text-purple-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="Edit merchant"
                                >
                                    <span x-html="$icon('edit', 'w-5 h-5')"></span>
                                </button>

                                <!-- Delete Button -->
                                <button
                                    @click="promptDeleteMerchant(merchant)"
                                    class="flex items-center justify-center p-2 text-red-600 rounded-lg hover:bg-red-50 dark:text-red-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="Delete merchant"
                                    :disabled="merchant.store_count > 0"
                                    :class="merchant.store_count > 0 ? 'opacity-50 cursor-not-allowed' : ''"
                                >
                                    <span x-html="$icon('delete', 'w-5 h-5')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
    {% endcall %}

    {{ pagination() }}
</div>

{{ confirm_modal_dynamic(
    'deleteMerchantModal',
    'Delete Merchant',
    "'Are you sure you want to delete \"' + (merchantToDelete?.name || '') + '\"? This action cannot be undone.'",
    'deleteMerchant(merchantToDelete)',
    'showDeleteMerchantModal',
    'Delete',
    'Cancel',
    'danger'
) }}
{% endblock %}

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