{# app/templates/store/orders.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% from 'shared/macros/headers.html' import page_header_flex, refresh_button %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/modals.html' import modal_simple %}
{% from 'shared/macros/tables.html' import table_wrapper %}

{% block title %}Orders{% endblock %}

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

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Orders', subtitle='View and manage your orders') %}
    <div class="flex items-center gap-4">
        {{ refresh_button(loading_var='loading', onclick='loadOrders()', variant='secondary') }}
    </div>
{% endcall %}

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

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

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- 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-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
            <span x-html="$icon('document-text', '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.total">0</p>
        </div>
    </div>

    <!-- Pending -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-yellow-500 bg-yellow-100 rounded-full dark:text-yellow-100 dark:bg-yellow-500">
            <span x-html="$icon('clock', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 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="stats.pending">0</p>
        </div>
    </div>

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

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

<!-- Filters -->
<div x-show="!loading" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
    <div class="flex flex-wrap items-center gap-4">
        <!-- Search -->
        <div class="flex-1 min-w-[200px]">
            <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 order #, customer..."
                    class="w-full pl-10 pr-4 py-2 text-sm text-gray-700 placeholder-gray-400 bg-gray-50 border border-gray-200 rounded-lg dark:placeholder-gray-500 dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                />
            </div>
        </div>

        <!-- Status Filter -->
        <select
            x-model="filters.status"
            @change="applyFilter()"
            class="px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        >
            <option value="">All Statuses</option>
            <template x-for="status in statuses" :key="status.value">
                <option :value="status.value" x-text="status.label"></option>
            </template>
        </select>

        <!-- Date From -->
        <input
            type="date"
            x-model="filters.date_from"
            @change="applyFilter()"
            class="px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        />

        <!-- Date To -->
        <input
            type="date"
            x-model="filters.date_to"
            @change="applyFilter()"
            class="px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        />

        <!-- Clear Filters -->
        <button
            x-show="filters.search || filters.status || filters.date_from || filters.date_to"
            @click="clearFilters()"
            class="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200"
        >
            Clear filters
        </button>
    </div>
</div>

<!-- Bulk Actions Bar -->
<div x-show="!loading && selectedOrders.length > 0"
     x-transition:enter="transition ease-out duration-200"
     x-transition:enter-start="opacity-0 -translate-y-2"
     x-transition:enter-end="opacity-100 translate-y-0"
     class="mb-4 p-3 bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-800 rounded-lg flex items-center justify-between">
    <div class="flex items-center gap-2">
        <span class="text-sm font-medium text-purple-700 dark:text-purple-300">
            <span x-text="selectedOrders.length"></span> order(s) selected
        </span>
        <button @click="clearSelection()" class="text-sm text-purple-600 hover:text-purple-800 dark:text-purple-400">
            Clear
        </button>
    </div>
    <div class="flex items-center gap-2">
        <button
            @click="openBulkStatusModal()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-blue-700 bg-blue-100 rounded-lg hover:bg-blue-200 dark:bg-blue-900 dark:text-blue-300 dark:hover:bg-blue-800 disabled:opacity-50"
        >
            <span x-html="$icon('pencil-square', 'w-4 h-4 inline mr-1')"></span>
            Update Status
        </button>
        <button
            @click="exportSelectedOrders()"
            class="px-3 py-1.5 text-sm font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600"
        >
            <span x-html="$icon('arrow-down-tray', 'w-4 h-4 inline mr-1')"></span>
            Export CSV
        </button>
    </div>
</div>

<!-- Orders Table -->
<div x-show="!loading && !error" class="mb-8">
{% call table_wrapper() %}
    <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 w-10">
                <input
                    type="checkbox"
                    :checked="allSelected"
                    :indeterminate="someSelected"
                    @click="toggleSelectAll()"
                    class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600"
                />
            </th>
            <th class="px-4 py-3">Order #</th>
            <th class="px-4 py-3">Customer</th>
            <th class="px-4 py-3">Date</th>
            <th class="px-4 py-3">Total</th>
            <th class="px-4 py-3">Status</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-for="order in orders" :key="order.id">
            <tr class="text-gray-700 dark:text-gray-400" :class="{'bg-purple-50 dark:bg-purple-900/10': isSelected(order.id)}">
                <!-- Checkbox -->
                <td class="px-4 py-3">
                    <input
                        type="checkbox"
                        :checked="isSelected(order.id)"
                        @click="toggleSelect(order.id)"
                        class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600"
                    />
                </td>
                <!-- Order Number -->
                <td class="px-4 py-3">
                    <span class="font-mono font-semibold" x-text="order.order_number || `#${order.id}`"></span>
                </td>
                <!-- Customer -->
                <td class="px-4 py-3">
                    <div class="text-sm">
                        <p class="font-medium" x-text="order.customer_name || 'Guest'"></p>
                        <p class="text-xs text-gray-500 dark:text-gray-400" x-text="order.customer_email || ''"></p>
                    </div>
                </td>
                <!-- Date -->
                <td class="px-4 py-3 text-sm" x-text="formatDate(order.created_at)"></td>
                <!-- Total -->
                <td class="px-4 py-3 text-sm font-semibold" x-text="formatPrice(order.total)"></td>
                <!-- Status -->
                <td class="px-4 py-3 text-xs">
                    <span
                        :class="{
                            'px-2 py-1 font-semibold leading-tight rounded-full': true,
                            'text-yellow-700 bg-yellow-100 dark:bg-yellow-700 dark:text-yellow-100': getStatusColor(order.status) === 'yellow',
                            'text-blue-700 bg-blue-100 dark:bg-blue-700 dark:text-blue-100': getStatusColor(order.status) === 'blue',
                            'text-orange-700 bg-orange-100 dark:bg-orange-700 dark:text-orange-100': getStatusColor(order.status) === 'orange',
                            'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': getStatusColor(order.status) === 'green',
                            'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100': getStatusColor(order.status) === 'red',
                            'text-indigo-700 bg-indigo-100 dark:bg-indigo-700 dark:text-indigo-100': getStatusColor(order.status) === 'indigo',
                            'text-gray-700 bg-gray-100 dark:bg-gray-700 dark:text-gray-100': getStatusColor(order.status) === 'gray'
                        }"
                        x-text="getStatusLabel(order.status)"
                    ></span>
                </td>
                <!-- Actions -->
                <td class="px-4 py-3">
                    <div class="flex items-center space-x-2 text-sm">
                        <button
                            @click="viewOrder(order)"
                            class="p-1 text-gray-500 hover:text-purple-600 dark:text-gray-400 dark:hover:text-purple-400"
                            title="View Details"
                        >
                            <span x-html="$icon('eye', 'w-5 h-5')"></span>
                        </button>
                        <button
                            @click="openStatusModal(order)"
                            class="p-1 text-gray-500 hover:text-blue-600 dark:text-gray-400 dark:hover:text-blue-400"
                            title="Update Status"
                        >
                            <span x-html="$icon('pencil-square', 'w-5 h-5')"></span>
                        </button>
                    </div>
                </td>
            </tr>
        </template>
        <!-- Empty State -->
        <tr x-show="orders.length === 0">
            <td colspan="7" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                <div class="flex flex-col items-center">
                    <span x-html="$icon('document-text', 'w-12 h-12 text-gray-300 dark:text-gray-600 mb-4')"></span>
                    <p class="text-lg font-medium">No orders found</p>
                    <p class="text-sm">Orders will appear here when customers make purchases</p>
                </div>
            </td>
        </tr>
    </tbody>
{% endcall %}
</div>

<!-- Pagination -->
{{ pagination(show_condition="!loading && pagination.total > 0") }}

<!-- Status Update Modal -->
{% call modal_simple('updateStatusModal', 'Update Order Status', show_var='showStatusModal', size='sm') %}
<div class="space-y-4">
    <template x-if="selectedOrder">
        <div>
            <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
                Update status for order <span class="font-semibold" x-text="selectedOrder.order_number || `#${selectedOrder.id}`"></span>
            </p>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">New Status</label>
            <select
                x-model="newStatus"
                class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
            >
                <template x-for="status in statuses" :key="status.value">
                    <option :value="status.value" x-text="status.label"></option>
                </template>
            </select>
        </div>
    </template>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showStatusModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="updateStatus()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
        >Update</button>
    </div>
</div>
{% endcall %}

<!-- Bulk Status Update Modal -->
{% call modal_simple('bulkUpdateStatusModal', 'Bulk Update Status', show_var='showBulkStatusModal', size='sm') %}
<div class="space-y-4">
    <div>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
            Update status for <span class="font-semibold" x-text="selectedOrders.length"></span> selected order(s)
        </p>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">New Status</label>
        <select
            x-model="bulkStatus"
            class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        >
            <option value="">Select a status...</option>
            <template x-for="status in statuses" :key="status.value">
                <option :value="status.value" x-text="status.label"></option>
            </template>
        </select>
    </div>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showBulkStatusModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="bulkUpdateStatus()"
            :disabled="saving || !bulkStatus"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
        >Update All</button>
    </div>
</div>
{% endcall %}
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('orders_static', path='store/js/orders.js') }}"></script>
{% endblock %}
