{# app/modules/marketplace/templates/marketplace/admin/partials/letzshop-orders-tab.html #}
{# Orders tab for admin Letzshop management #}
{% from 'shared/macros/pagination.html' import pagination %}

<!-- Header with Import Buttons -->
<div class="flex items-center justify-between mb-6">
    <div>
        <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">Orders</h3>
        <p class="text-sm text-gray-500 dark:text-gray-400" x-text="selectedStore ? 'Manage Letzshop orders for this store' : 'All Letzshop orders across stores'"></p>
    </div>
    <!-- Import buttons only shown when store is selected -->
    <div x-show="selectedStore" class="flex gap-2">
        <button
            @click="importHistoricalOrders()"
            :disabled="!letzshopStatus.is_configured || importingHistorical"
            class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-gray-700 dark:text-gray-300 transition-colors duration-150 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed"
            title="Import all historical confirmed and declined orders"
        >
            <span x-show="!importingHistorical" x-html="$icon('archive', 'w-4 h-4 mr-2')"></span>
            <span x-show="importingHistorical" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
            <span x-show="!importingHistorical">Import History</span>
            <span x-show="importingHistorical" x-text="historicalImportProgress?.message || 'Starting...'"></span>
        </button>
        <button
            @click="importOrders()"
            :disabled="!letzshopStatus.is_configured || importingOrders"
            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="!importingOrders" x-html="$icon('download', 'w-4 h-4 mr-2')"></span>
            <span x-show="importingOrders" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
            <span x-text="importingOrders ? 'Importing...' : 'Import New'"></span>
        </button>
    </div>
</div>

<!-- Historical Import Progress -->
<div x-show="historicalImportProgress && importingHistorical" x-transition class="mb-6 p-4 bg-purple-50 dark:bg-purple-900/20 rounded-lg border border-purple-200 dark:border-purple-800">
    <div class="flex items-center">
        <span x-html="$icon('spinner', 'w-5 h-5 text-purple-500 mr-3')"></span>
        <div class="flex-1">
            <h4 class="font-medium text-purple-800 dark:text-purple-200">Historical Import in Progress</h4>
            <p class="text-sm text-purple-700 dark:text-purple-300 mt-1" x-text="historicalImportProgress?.message"></p>
            <div class="flex gap-4 mt-2 text-xs text-purple-600 dark:text-purple-400">
                <span x-show="historicalImportProgress?.current_phase">
                    Phase: <strong x-text="historicalImportProgress?.current_phase"></strong>
                </span>
                <span x-show="historicalImportProgress?.shipments_fetched > 0">
                    Fetched: <strong x-text="historicalImportProgress?.shipments_fetched"></strong>
                </span>
                <span x-show="historicalImportProgress?.orders_processed > 0">
                    Processed: <strong x-text="historicalImportProgress?.orders_processed"></strong>
                </span>
            </div>
        </div>
    </div>
</div>

<!-- Historical Import Result -->
<div x-show="historicalImportResult" x-transition class="mb-6 p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg border border-blue-200 dark:border-blue-800">
    <div class="flex items-start justify-between">
        <div class="flex items-start">
            <span x-html="$icon('check-circle', 'w-5 h-5 text-blue-500 mr-3 mt-0.5')"></span>
            <div>
                <h4 class="font-medium text-blue-800 dark:text-blue-200">Historical Import Complete</h4>
                <div class="text-sm text-blue-700 dark:text-blue-300 mt-1">
                    <span x-text="historicalImportResult?.imported + ' imported'"></span> ·
                    <span x-text="historicalImportResult?.updated + ' updated'"></span> ·
                    <span x-text="historicalImportResult?.skipped + ' skipped'"></span>
                </div>
                <div x-show="historicalImportResult?.products_matched > 0 || historicalImportResult?.products_not_found > 0" class="text-sm text-blue-600 dark:text-blue-400 mt-1">
                    <span x-text="historicalImportResult?.products_matched + ' products matched by EAN'"></span> ·
                    <span x-text="historicalImportResult?.products_not_found + ' not found'"></span>
                </div>
            </div>
        </div>
        <button @click="historicalImportResult = null" class="text-blue-500 hover:text-blue-700">
            <span x-html="$icon('x', 'w-4 h-4')"></span>
        </button>
    </div>
</div>

<!-- Status Cards -->
<div class="grid gap-6 mb-8" :class="selectedStore ? 'md:grid-cols-5' : 'md:grid-cols-4'">
    <!-- Connection Status (only when store selected) -->
    <div x-show="selectedStore" class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div :class="letzshopStatus.is_configured ? 'bg-green-100 dark:bg-green-900' : 'bg-gray-100 dark:bg-gray-700'" class="p-3 mr-4 rounded-full">
            <span x-html="$icon(letzshopStatus.is_configured ? 'check' : 'x', letzshopStatus.is_configured ? 'w-5 h-5 text-green-500' : 'w-5 h-5 text-gray-400')"></span>
        </div>
        <div>
            <p class="mb-1 text-sm font-medium text-gray-600 dark:text-gray-400">Connection</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="letzshopStatus.is_configured ? 'Configured' : 'Not Configured'"></p>
        </div>
    </div>

    <!-- Pending 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-orange-500 bg-orange-100 rounded-full dark:bg-orange-900">
            <span x-html="$icon('clock', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-1 text-sm font-medium text-gray-600 dark:text-gray-400">Pending</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="orderStats.pending"></p>
        </div>
    </div>

    <!-- Confirmed/Processing 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:bg-green-900">
            <span x-html="$icon('check-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-1 text-sm font-medium text-gray-600 dark:text-gray-400">Confirmed</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="orderStats.processing"></p>
        </div>
    </div>

    <!-- Declined/Cancelled 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-red-500 bg-red-100 rounded-full dark:bg-red-900">
            <span x-html="$icon('x-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-1 text-sm font-medium text-gray-600 dark:text-gray-400">Declined</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="orderStats.cancelled"></p>
        </div>
    </div>

    <!-- Shipped 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-blue-500 bg-blue-100 rounded-full dark:bg-blue-900">
            <span x-html="$icon('truck', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-1 text-sm font-medium text-gray-600 dark:text-gray-400">Shipped</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="orderStats.shipped"></p>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="mb-4 flex flex-wrap gap-4 items-center">
    <!-- Search input -->
    <div class="relative flex-1 min-w-[200px] max-w-md">
        <span x-html="$icon('search', 'w-4 h-4 absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400')"></span>
        <input
            type="text"
            x-model="ordersSearch"
            @input.debounce.300ms="pagination.page = 1; loadOrders()"
            placeholder="Search by order #, name, or email..."
            class="w-full pl-9 pr-8 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md focus:border-purple-400 focus:outline-none"
        />
        <button
            x-show="ordersSearch"
            @click="ordersSearch = ''; pagination.page = 1; loadOrders()"
            class="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
        >
            <span x-html="$icon('x', 'w-4 h-4')"></span>
        </button>
    </div>

    <!-- Status filter -->
    <select
        x-model="ordersFilter"
        @change="ordersHasDeclinedItems = false; pagination.page = 1; loadOrders()"
        class="px-3 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md focus:border-purple-400 focus:outline-none"
    >
        <option value="">All Status</option>
        <option value="pending">Pending</option>
        <option value="processing">Confirmed</option>
        <option value="cancelled">Declined</option>
        <option value="shipped">Shipped</option>
    </select>

    <!-- Declined items filter -->
    <button
        @click="ordersFilter = ''; ordersHasDeclinedItems = !ordersHasDeclinedItems; pagination.page = 1; loadOrders()"
        :class="ordersHasDeclinedItems ? 'bg-red-100 dark:bg-red-900 border-red-300 dark:border-red-700 text-red-700 dark:text-red-300' : 'bg-white dark:bg-gray-800 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300'"
        class="px-3 py-2 text-sm border rounded-md hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
        :title="ordersHasDeclinedItems ? 'Showing orders with declined items' : 'Show only orders with declined items'"
    >
        <span x-html="$icon('x-circle', 'w-4 h-4 inline mr-1')"></span>
        Has Declined Items
        <span x-show="orderStats.has_declined_items > 0" class="ml-1 px-1.5 py-0.5 text-xs bg-red-200 dark:bg-red-800 rounded-full" x-text="orderStats.has_declined_items"></span>
    </button>
</div>

<!-- Not Configured Warning (only when store selected) -->
<div x-show="selectedStore && !letzshopStatus.is_configured" class="mb-6 p-4 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg border border-yellow-200 dark:border-yellow-800">
    <div class="flex items-center">
        <span x-html="$icon('exclamation', 'w-5 h-5 text-yellow-500 mr-3')"></span>
        <div>
            <h4 class="font-medium text-yellow-800 dark:text-yellow-200">API Not Configured</h4>
            <p class="text-sm text-yellow-700 dark:text-yellow-300">Configure the Letzshop API key in the Settings tab to import and manage orders.</p>
        </div>
    </div>
</div>

<!-- Orders Table -->
<div class="w-full 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</th>
                    <th x-show="!selectedStore" class="px-4 py-3">Store</th>
                    <th class="px-4 py-3">Customer</th>
                    <th class="px-4 py-3">Total</th>
                    <th class="px-4 py-3">Status</th>
                    <th class="px-4 py-3">Date</th>
                    <th class="px-4 py-3">Actions</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                <template x-if="loadingOrders && orders.length === 0">
                    <tr>
                        <td :colspan="selectedStore ? 6 : 7" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                            <span x-html="$icon('spinner', 'w-6 h-6 mx-auto mb-2')"></span>
                            <p>Loading orders...</p>
                        </td>
                    </tr>
                </template>
                <template x-if="!loadingOrders && orders.length === 0">
                    <tr>
                        <td :colspan="selectedStore ? 6 : 7" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                            <span x-html="$icon('inbox', 'w-12 h-12 mx-auto mb-2 text-gray-300')"></span>
                            <p class="font-medium">No orders found</p>
                            <p class="text-sm mt-1" x-text="selectedStore ? 'Click Import Orders to fetch orders from Letzshop' : 'Select a store to import orders'"></p>
                        </td>
                    </tr>
                </template>
                <template x-for="order in orders" :key="order.id">
                    <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700">
                        <td class="px-4 py-3">
                            <div class="flex items-center text-sm">
                                <div>
                                    <p class="font-semibold" x-text="order.external_order_number || order.order_number"></p>
                                    <p class="text-xs text-gray-500" x-text="'#' + order.id"></p>
                                </div>
                            </div>
                        </td>
                        <!-- Store column (only in cross-store view) -->
                        <td x-show="!selectedStore" class="px-4 py-3 text-sm">
                            <p class="font-medium text-gray-700 dark:text-gray-200" x-text="order.store_name || 'N/A'"></p>
                        </td>
                        <td class="px-4 py-3 text-sm">
                            <p x-text="order.customer_email || 'N/A'"></p>
                        </td>
                        <td class="px-4 py-3 text-sm">
                            <span x-text="order.total_amount ? order.total_amount + ' ' + order.currency : 'N/A'"></span>
                        </td>
                        <td class="px-4 py-3 text-xs">
                            <span
                                class="px-2 py-1 font-semibold leading-tight rounded-full"
                                :class="{
                                    'text-orange-700 bg-orange-100 dark:bg-orange-700 dark:text-orange-100': order.status === 'pending',
                                    'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': order.status === 'processing',
                                    'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100': order.status === 'cancelled',
                                    'text-blue-700 bg-blue-100 dark:bg-blue-700 dark:text-blue-100': order.status === 'shipped'
                                }"
                                x-text="order.status === 'cancelled' ? 'DECLINED' : (order.status === 'processing' ? 'CONFIRMED' : order.status.toUpperCase())"
                            ></span>
                        </td>
                        <td class="px-4 py-3 text-sm">
                            <span x-text="formatDate(order.order_date || order.created_at)"></span>
                        </td>
                        <td class="px-4 py-3">
                            <div class="flex items-center space-x-2 text-sm">
                                <button
                                    x-show="order.status === 'pending'"
                                    @click="confirmOrder(order)"
                                    class="flex items-center justify-center px-2 py-1 text-sm text-green-600 transition-colors duration-150 rounded-md hover:bg-green-100 dark:hover:bg-green-900"
                                    title="Confirm Order"
                                >
                                    <span x-html="$icon('check', 'w-4 h-4')"></span>
                                </button>
                                <button
                                    x-show="order.status === 'pending'"
                                    @click="orderToDecline = order; showDeclineOrderModal = true"
                                    class="flex items-center justify-center px-2 py-1 text-sm text-red-600 transition-colors duration-150 rounded-md hover:bg-red-100 dark:hover:bg-red-900"
                                    title="Decline Order"
                                >
                                    <span x-html="$icon('x', 'w-4 h-4')"></span>
                                </button>
                                <button
                                    x-show="order.status === 'processing'"
                                    @click="openTrackingModal(order)"
                                    class="flex items-center justify-center px-2 py-1 text-sm text-blue-600 transition-colors duration-150 rounded-md hover:bg-blue-100 dark:hover:bg-blue-900"
                                    title="Set Tracking"
                                >
                                    <span x-html="$icon('truck', 'w-4 h-4')"></span>
                                </button>
                                <button
                                    @click="viewOrderDetails(order)"
                                    class="flex items-center justify-center px-2 py-1 text-sm text-gray-600 transition-colors duration-150 rounded-md hover:bg-gray-100 dark:hover:bg-gray-700"
                                    title="View Details"
                                >
                                    <span x-html="$icon('eye', 'w-4 h-4')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
        </table>
    </div>
    {{ pagination(show_condition="!loadingOrders && pagination.total > 0") }}
</div>
