{# app/templates/admin/orders.html #}
{% extends "admin/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/tables.html' import table_wrapper %}
{% from 'shared/macros/modals.html' import modal_simple %}
{% from 'shared/macros/inputs.html' import entity_selector, entity_selected_badge %}

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

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

{% block content %}
<!-- Page Header with Store Selector -->
{% call page_header_flex(title='Orders', subtitle='Manage orders across all stores') %}
    <div class="flex items-center gap-4">
        <!-- Store Autocomplete (Tom Select) -->
        {{ entity_selector(ref_name='storeSelect', id='store-select', placeholder='Search store...') }}
        {{ refresh_button(loading_var='loading', onclick='refresh()', variant='secondary') }}
    </div>
{% endcall %}

<!-- Selected Store Info -->
{{ entity_selected_badge(entity_var='selectedStore', clear_fn='clearStoreFilter()', code_field='store_code', color='purple') }}

{{ 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">
    <!-- 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-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
            <span x-html="$icon('clipboard-list', '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_orders || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: 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-orange-500 bg-orange-100 rounded-full dark:text-orange-100 dark:bg-orange-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_orders || 0">
                0
            </p>
        </div>
    </div>

    <!-- Card: 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('refresh', '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_orders || 0">
                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-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-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="formatPrice(stats.total_revenue || 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 lg:flex-row lg:items-center lg:justify-between gap-4">
        <!-- Search Input -->
        <div class="flex-1 max-w-xl">
            <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 number or customer..."
                    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.status"
                @change="pagination.page = 1; loadOrders()"
                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 Statuses</option>
                <option value="pending">Pending</option>
                <option value="processing">Processing</option>
                <option value="shipped">Shipped</option>
                <option value="delivered">Delivered</option>
                <option value="cancelled">Cancelled</option>
                <option value="refunded">Refunded</option>
            </select>

            <!-- Channel Filter -->
            <select
                x-model="filters.channel"
                @change="pagination.page = 1; loadOrders()"
                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 Channels</option>
                <option value="direct">Direct</option>
                <option value="letzshop">Letzshop</option>
            </select>

            <!-- Refresh Button -->
            <button
                @click="refresh()"
                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 orders"
            >
                <span x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
                Refresh
            </button>
        </div>
    </div>
</div>

<!-- Orders Table with Pagination -->
<div x-show="!loading">
    {% 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">Order</th>
                    <th class="px-4 py-3">Customer</th>
                    <th class="px-4 py-3">Store</th>
                    <th class="px-4 py-3">Channel</th>
                    <th class="px-4 py-3 text-right">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">
                <!-- Empty State -->
                <template x-if="orders.length === 0">
                    <tr>
                        <td colspan="8" 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('clipboard-list', 'w-12 h-12 mb-2 text-gray-300')"></span>
                                <p class="font-medium">No orders found</p>
                                <p class="text-xs mt-1" x-text="filters.search || filters.store_id || filters.status || filters.channel ? 'Try adjusting your filters' : 'Orders will appear here when customers place orders'"></p>
                            </div>
                        </td>
                    </tr>
                </template>

                <!-- Order Rows -->
                <template x-for="order in orders" :key="order.id">
                    <tr class="text-gray-700 dark:text-gray-400">
                        <!-- Order Info -->
                        <td class="px-4 py-3">
                            <div>
                                <p class="font-semibold text-sm font-mono" x-text="order.order_number"></p>
                                <p class="text-xs text-gray-500" x-text="order.item_count + ' item(s)'"></p>
                            </div>
                        </td>

                        <!-- Customer Info -->
                        <td class="px-4 py-3 text-sm">
                            <p class="font-medium" x-text="order.customer_name || 'Guest'"></p>
                            <p class="text-xs text-gray-500 dark:text-gray-400 truncate max-w-[150px]" x-text="order.customer_email || '-'"></p>
                        </td>

                        <!-- Store Info -->
                        <td class="px-4 py-3 text-sm">
                            <p class="font-medium" x-text="order.store_name || 'Unknown'"></p>
                            <p class="text-xs text-gray-500 dark:text-gray-400 font-mono" x-text="order.store_code || ''"></p>
                        </td>

                        <!-- Channel -->
                        <td class="px-4 py-3 text-sm">
                            <span class="px-2 py-1 text-xs font-medium rounded-full"
                                  :class="order.channel === 'letzshop' ? 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400' : 'bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300'"
                                  x-text="order.channel"></span>
                        </td>

                        <!-- Total -->
                        <td class="px-4 py-3 text-sm text-right font-mono font-semibold">
                            <span x-text="formatPrice(order.total_amount, order.currency)"></span>
                        </td>

                        <!-- Status -->
                        <td class="px-4 py-3 text-sm">
                            <span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs"
                                  :class="getStatusClass(order.status)"
                                  x-text="order.status"></span>
                        </td>

                        <!-- Date -->
                        <td class="px-4 py-3 text-sm">
                            <p x-text="formatDate(order.created_at)"></p>
                            <p class="text-xs text-gray-500" x-text="formatTime(order.created_at)"></p>
                        </td>

                        <!-- Actions -->
                        <td class="px-4 py-3 text-sm">
                            <div class="flex items-center space-x-2">
                                <button
                                    @click="viewOrder(order)"
                                    class="flex items-center justify-center px-2 py-1 text-xs font-medium leading-5 text-purple-600 rounded-lg dark:text-purple-400 focus:outline-none hover:bg-gray-100 dark:hover:bg-gray-700"
                                    title="View Details"
                                >
                                    <span x-html="$icon('eye', 'w-4 h-4')"></span>
                                </button>
                                <button
                                    @click="openStatusModal(order)"
                                    class="flex items-center justify-center px-2 py-1 text-xs font-medium leading-5 text-blue-600 rounded-lg dark:text-blue-400 focus:outline-none hover:bg-gray-100 dark:hover:bg-gray-700"
                                    title="Update Status"
                                >
                                    <span x-html="$icon('pencil', 'w-4 h-4')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
    {% endcall %}

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

<!-- Update Status Modal -->
{% call modal_simple('updateStatusModal', 'Update Order Status', show_var='showStatusModal', size='sm') %}
<div class="space-y-4">
    <div class="text-sm text-gray-600 dark:text-gray-400">
        <p class="font-medium text-gray-700 dark:text-gray-300">Order: <span x-text="selectedOrder?.order_number"></span></p>
        <p class="text-xs">Current Status: <span class="font-medium" x-text="selectedOrder?.status"></span></p>
    </div>

    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            New Status
        </label>
        <select
            x-model="statusForm.status"
            class="w-full px-3 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"
        >
            <option value="pending">Pending</option>
            <option value="processing">Processing</option>
            <option value="shipped">Shipped</option>
            <option value="delivered">Delivered</option>
            <option value="cancelled">Cancelled</option>
            <option value="refunded">Refunded</option>
        </select>
    </div>

    <div x-show="statusForm.status === 'shipped'">
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Tracking Number
        </label>
        <input
            type="text"
            x-model="statusForm.tracking_number"
            class="w-full px-3 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"
            placeholder="Enter tracking number"
        >
    </div>

    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Reason (optional)
        </label>
        <input
            type="text"
            x-model="statusForm.reason"
            class="w-full px-3 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"
            placeholder="e.g., Customer requested cancellation"
        >
    </div>

    <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 || statusForm.status === selectedOrder?.status"
            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"
        >
            <span x-text="saving ? 'Updating...' : 'Update Status'"></span>
        </button>
    </div>
</div>
{% endcall %}

<!-- Order Detail Modal -->
{% call modal_simple('orderDetailModal', 'Order Details', show_var='showDetailModal', size='lg') %}
<div x-show="selectedOrderDetail" class="space-y-4">
    <!-- Order Header -->
    <div class="flex items-center justify-between pb-4 border-b border-gray-200 dark:border-gray-700">
        <div>
            <p class="text-lg font-semibold text-gray-800 dark:text-gray-200" x-text="selectedOrderDetail?.order_number"></p>
            <p class="text-sm text-gray-500" x-text="formatDateTime(selectedOrderDetail?.created_at)"></p>
        </div>
        <span class="px-3 py-1 text-sm font-semibold rounded-full"
              :class="getStatusClass(selectedOrderDetail?.status)"
              x-text="selectedOrderDetail?.status"></span>
    </div>

    <!-- Customer & Store Info -->
    <div class="grid grid-cols-2 gap-4">
        <div>
            <p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Customer</p>
            <p class="text-sm font-medium text-gray-800 dark:text-gray-200" x-text="(selectedOrderDetail?.customer_first_name || '') + ' ' + (selectedOrderDetail?.customer_last_name || '')"></p>
            <p class="text-xs text-gray-500" x-text="selectedOrderDetail?.customer_email"></p>
        </div>
        <div>
            <p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Store</p>
            <p class="text-sm font-medium text-gray-800 dark:text-gray-200" x-text="selectedOrderDetail?.store_name || 'Unknown'"></p>
            <p class="text-xs text-gray-500 font-mono" x-text="selectedOrderDetail?.store_code || ''"></p>
        </div>
    </div>

    <!-- Items -->
    <div>
        <div class="flex items-center justify-between mb-2">
            <p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Items</p>
            <span class="text-xs text-gray-400" x-text="(selectedOrderDetail?.items?.length || 0) + ' item(s)'"></span>
        </div>
        <div class="bg-gray-50 dark:bg-gray-700 rounded-lg overflow-hidden max-h-48 overflow-y-auto">
            <template x-for="item in selectedOrderDetail?.items || []" :key="item.id">
                <div class="flex items-center justify-between px-4 py-2 border-b border-gray-200 dark:border-gray-600 last:border-0">
                    <div>
                        <p class="text-sm font-medium text-gray-800 dark:text-gray-200" x-text="item.product_name"></p>
                        <p class="text-xs text-gray-500" x-text="'SKU: ' + (item.product_sku || '-')"></p>
                    </div>
                    <div class="text-right">
                        <p class="text-sm font-medium text-gray-800 dark:text-gray-200" x-text="formatPrice(item.total_price)"></p>
                        <p class="text-xs text-gray-500" x-text="item.quantity + ' x ' + formatPrice(item.unit_price)"></p>
                    </div>
                </div>
            </template>
        </div>
    </div>

    <!-- Totals -->
    <div class="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
        <div class="flex justify-between text-sm mb-1">
            <span class="text-gray-600 dark:text-gray-400">Subtotal</span>
            <span x-text="formatPrice(selectedOrderDetail?.subtotal)"></span>
        </div>
        <div class="flex justify-between text-sm mb-1">
            <span class="text-gray-600 dark:text-gray-400">Tax</span>
            <span x-text="formatPrice(selectedOrderDetail?.tax_amount)"></span>
        </div>
        <div class="flex justify-between text-sm mb-1">
            <span class="text-gray-600 dark:text-gray-400">Shipping</span>
            <span x-text="formatPrice(selectedOrderDetail?.shipping_amount)"></span>
        </div>
        <template x-if="selectedOrderDetail?.discount_amount > 0">
            <div class="flex justify-between text-sm mb-1 text-green-600">
                <span>Discount</span>
                <span x-text="'-' + formatPrice(selectedOrderDetail?.discount_amount)"></span>
            </div>
        </template>
        <div class="flex justify-between text-sm font-semibold pt-2 border-t border-gray-200 dark:border-gray-600">
            <span>Total</span>
            <span x-text="formatPrice(selectedOrderDetail?.total_amount, selectedOrderDetail?.currency)"></span>
        </div>
    </div>

    <!-- Shipping Address -->
    <div x-show="selectedOrderDetail?.ship_address_line_1">
        <p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase mb-2">Shipping Address</p>
        <div class="text-sm text-gray-700 dark:text-gray-300">
            <p x-text="(selectedOrderDetail?.ship_first_name || '') + ' ' + (selectedOrderDetail?.ship_last_name || '')"></p>
            <p x-text="selectedOrderDetail?.ship_address_line_1"></p>
            <p x-show="selectedOrderDetail?.ship_address_line_2" x-text="selectedOrderDetail?.ship_address_line_2"></p>
            <p x-text="(selectedOrderDetail?.ship_postal_code || '') + ' ' + (selectedOrderDetail?.ship_city || '')"></p>
            <p x-text="selectedOrderDetail?.ship_country_iso"></p>
        </div>
    </div>

    <!-- Shipping & Tracking Info -->
    <div x-show="selectedOrderDetail?.shipment_number || selectedOrderDetail?.tracking_number || selectedOrderDetail?.shipping_carrier">
        <p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase mb-2">Shipping & Tracking</p>
        <div class="bg-gray-50 dark:bg-gray-700 rounded-lg p-3 space-y-2">
            <!-- Shipment Number -->
            <div x-show="selectedOrderDetail?.shipment_number" class="flex items-center justify-between">
                <div>
                    <span class="text-xs text-gray-500 dark:text-gray-400">Shipment #:</span>
                    <span class="ml-2 text-sm font-mono text-gray-800 dark:text-gray-200" x-text="selectedOrderDetail?.shipment_number"></span>
                </div>
                <!-- Download Label Button -->
                <button
                    x-show="selectedOrderDetail?.shipping_carrier"
                    @click="downloadShippingLabel(selectedOrderDetail)"
                    class="flex items-center gap-1 px-2 py-1 text-xs font-medium text-purple-600 dark:text-purple-400 hover:text-purple-800 dark:hover:text-purple-200 bg-purple-50 dark:bg-purple-900/30 rounded"
                    title="Download shipping label"
                >
                    <span x-html="$icon('download', 'w-3 h-3')"></span>
                    Label
                </button>
            </div>
            <!-- Carrier -->
            <div x-show="selectedOrderDetail?.shipping_carrier" class="flex items-center">
                <span class="text-xs text-gray-500 dark:text-gray-400">Carrier:</span>
                <span class="ml-2 px-2 py-0.5 text-xs font-medium rounded capitalize"
                    :class="{
                        'bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200': selectedOrderDetail?.shipping_carrier === 'greco',
                        'bg-yellow-100 dark:bg-yellow-900 text-yellow-800 dark:text-yellow-200': selectedOrderDetail?.shipping_carrier === 'colissimo',
                        'bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200': selectedOrderDetail?.shipping_carrier === 'xpresslogistics',
                        'bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-gray-200': !['greco', 'colissimo', 'xpresslogistics'].includes(selectedOrderDetail?.shipping_carrier)
                    }"
                    x-text="selectedOrderDetail?.shipping_carrier"></span>
            </div>
            <!-- Tracking Number (if different from shipment) -->
            <div x-show="selectedOrderDetail?.tracking_number" class="flex items-center">
                <span class="text-xs text-gray-500 dark:text-gray-400">Tracking #:</span>
                <span class="ml-2 text-sm font-mono text-gray-800 dark:text-gray-200" x-text="selectedOrderDetail?.tracking_number"></span>
            </div>
            <!-- Tracking URL -->
            <div x-show="selectedOrderDetail?.tracking_url" class="flex items-center">
                <span class="text-xs text-gray-500 dark:text-gray-400">Track:</span>
                <a :href="selectedOrderDetail?.tracking_url" target="_blank"
                   class="ml-2 text-xs text-purple-600 dark:text-purple-400 hover:underline flex items-center gap-1">
                    View tracking
                    <span x-html="$icon('external-link', 'w-3 h-3')"></span>
                </a>
            </div>
        </div>
    </div>

    <!-- Notes -->
    <div x-show="selectedOrderDetail?.internal_notes">
        <p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase mb-1">Internal Notes</p>
        <p class="text-sm text-gray-700 dark:text-gray-300 whitespace-pre-wrap" x-text="selectedOrderDetail?.internal_notes"></p>
    </div>

    <div class="flex items-center justify-between pt-4 border-t border-gray-200 dark:border-gray-700">
        <div>
            <!-- Mark as Shipped button (only for processing orders) -->
            <button
                x-show="selectedOrderDetail?.status === 'processing'"
                @click="showDetailModal = false; openMarkAsShippedModal(selectedOrderDetail)"
                class="flex items-center gap-2 px-4 py-2 text-sm font-medium text-white bg-green-600 rounded-lg hover:bg-green-700 transition-colors"
            >
                <span x-html="$icon('truck', 'w-4 h-4')"></span>
                Mark as Shipped
            </button>
        </div>
        <div class="flex items-center gap-3">
            <button
                @click="showDetailModal = 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"
            >
                Close
            </button>
            <button
                @click="showDetailModal = false; openStatusModal(selectedOrderDetail)"
                class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors"
            >
                Update Status
            </button>
        </div>
    </div>
</div>
{% endcall %}

<!-- Mark as Shipped Modal -->
{% call modal_simple('markAsShippedModal', 'Mark Order as Shipped', show_var='showMarkAsShippedModal', size='sm') %}
<div class="space-y-4">
    <p class="text-sm text-gray-600 dark:text-gray-400">
        Mark order <span class="font-mono font-medium" x-text="selectedOrder?.order_number"></span> as shipped.
    </p>

    <!-- Shipment Info (read-only if from Letzshop) -->
    <div x-show="selectedOrder?.shipment_number" class="p-3 bg-gray-50 dark:bg-gray-700 rounded-lg">
        <p class="text-xs text-gray-500 dark:text-gray-400 mb-1">Letzshop Shipment</p>
        <p class="text-sm font-mono text-gray-800 dark:text-gray-200" x-text="selectedOrder?.shipment_number"></p>
        <p x-show="selectedOrder?.shipping_carrier" class="text-xs text-gray-500 dark:text-gray-400 mt-1">
            Carrier: <span class="capitalize" x-text="selectedOrder?.shipping_carrier"></span>
        </p>
    </div>

    <!-- Tracking Number Input -->
    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Tracking Number <span class="text-gray-400">(optional)</span>
        </label>
        <input
            type="text"
            x-model="shipForm.tracking_number"
            placeholder="e.g., 3XYVi85dDE8l6bov97122"
            class="w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-600"
        />
    </div>

    <!-- Tracking URL Input -->
    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Tracking URL <span class="text-gray-400">(optional)</span>
        </label>
        <input
            type="url"
            x-model="shipForm.tracking_url"
            placeholder="https://dispatchweb.fr/Tracky/Home/..."
            class="w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-600"
        />
    </div>

    <!-- Carrier Select (if not already set) -->
    <div x-show="!selectedOrder?.shipping_carrier">
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Carrier <span class="text-gray-400">(optional)</span>
        </label>
        <select
            x-model="shipForm.shipping_carrier"
            class="w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-600"
        >
            <option value="">Select carrier...</option>
            <option value="greco">Greco</option>
            <option value="colissimo">Colissimo</option>
            <option value="xpresslogistics">XpressLogistics</option>
            <option value="dhl">DHL</option>
            <option value="ups">UPS</option>
            <option value="fedex">FedEx</option>
            <option value="other">Other</option>
        </select>
    </div>

    <div class="flex justify-end gap-3 pt-4">
        <button
            @click="showMarkAsShippedModal = 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"
        >
            Cancel
        </button>
        <button
            @click="markAsShipped()"
            :disabled="markingAsShipped"
            class="flex items-center gap-2 px-4 py-2 text-sm font-medium text-white bg-green-600 rounded-lg hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed"
        >
            <span x-show="!markingAsShipped" x-html="$icon('truck', 'w-4 h-4')"></span>
            <span x-show="markingAsShipped" class="animate-spin" x-html="$icon('refresh', 'w-4 h-4')"></span>
            <span x-text="markingAsShipped ? 'Shipping...' : 'Mark as Shipped'"></span>
        </button>
    </div>
</div>
{% endcall %}
{% endblock %}

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