{# app/templates/store/letzshop.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/headers.html' import page_header_flex %}
{% from 'shared/macros/tables.html' import table_wrapper, table_header %}
{% from 'shared/macros/modals.html' import form_modal, confirm_modal %}

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

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

{% block extra_scripts %}
<script defer src="/static/modules/marketplace/store/js/letzshop.js"></script>
{% endblock %}

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Letzshop Orders', subtitle='Manage orders from Letzshop marketplace') %}
    <button
        @click="importOrders()"
        :disabled="!status.is_configured || importing"
        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="!importing" x-html="$icon('download', 'w-4 h-4 mr-2')"></span>
        <span x-show="importing" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
        <span x-text="importing ? 'Importing...' : 'Import Orders'"></span>
    </button>
    <button
        @click="refreshData()"
        :disabled="loading"
        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"
    >
        <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>
        Refresh
    </button>
{% endcall %}

<!-- Success Message -->
<div x-show="successMessage" x-transition class="mb-6 p-4 bg-green-100 dark:bg-green-900/30 border border-green-400 dark:border-green-600 text-green-700 dark:text-green-300 rounded-lg flex items-start">
    <span x-html="$icon('check-circle', 'w-5 h-5 mr-3 mt-0.5 flex-shrink-0')"></span>
    <div>
        <p class="font-semibold" x-text="successMessage"></p>
    </div>
    <button @click="successMessage = ''" class="ml-auto text-green-700 dark:text-green-300 hover:text-green-900">
        <span x-html="$icon('x', 'w-4 h-4')"></span>
    </button>
</div>

{# noqa: FE-003 - Custom dismissible error with dark mode support not available in error_state macro #}
<!-- Error Message -->
<div x-show="error" x-transition class="mb-6 p-4 bg-red-100 dark:bg-red-900/30 border border-red-400 dark:border-red-600 text-red-700 dark:text-red-300 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</p>
        <p class="text-sm" x-text="error"></p>
    </div>
    <button @click="error = ''" class="ml-auto text-red-700 dark:text-red-300 hover:text-red-900">
        <span x-html="$icon('x', 'w-4 h-4')"></span>
    </button>
</div>

<!-- Tabs -->
<div class="mb-6">
    <div class="flex border-b border-gray-200 dark:border-gray-700">
        <button
            @click="activeTab = 'orders'"
            :class="activeTab === 'orders' ? 'border-purple-600 text-purple-600' : 'border-transparent text-gray-500 hover:text-gray-700 dark:hover:text-gray-300'"
            class="px-4 py-2 text-sm font-medium border-b-2 transition-colors"
        >
            <span class="flex items-center">
                <span x-html="$icon('shopping-cart', 'w-4 h-4 mr-2')"></span>
                Orders
                <span x-show="orders.length > 0" class="ml-2 px-2 py-0.5 text-xs bg-purple-100 dark:bg-purple-900 text-purple-600 dark:text-purple-300 rounded-full" x-text="totalOrders"></span>
            </span>
        </button>
        <button
            @click="activeTab = 'export'"
            :class="activeTab === 'export' ? 'border-purple-600 text-purple-600' : 'border-transparent text-gray-500 hover:text-gray-700 dark:hover:text-gray-300'"
            class="px-4 py-2 text-sm font-medium border-b-2 transition-colors"
        >
            <span class="flex items-center">
                <span x-html="$icon('upload', 'w-4 h-4 mr-2')"></span>
                Export
            </span>
        </button>
        <button
            @click="activeTab = 'settings'"
            :class="activeTab === 'settings' ? 'border-purple-600 text-purple-600' : 'border-transparent text-gray-500 hover:text-gray-700 dark:hover:text-gray-300'"
            class="px-4 py-2 text-sm font-medium border-b-2 transition-colors"
        >
            <span class="flex items-center">
                <span x-html="$icon('cog', 'w-4 h-4 mr-2')"></span>
                Settings
            </span>
        </button>
    </div>
</div>

<!-- Orders Tab -->
<div x-show="activeTab === 'orders'" x-transition>
    <!-- Status Cards -->
    <div class="grid gap-6 mb-8 md:grid-cols-4">
        <!-- Connection Status -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div :class="status.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(status.is_configured ? 'check' : 'x', status.is_configured ? 'w-5 h-5 text-green-500' : 'w-5 h-5 text-gray-400')"></span>
            </div>
            <div>
                <p class="mb-2 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="status.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-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="orderStats.pending"></p>
            </div>
        </div>

        <!-- Confirmed 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-2 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.confirmed"></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-2 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">
        <select
            x-model="filters.sync_status"
            @change="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="confirmed">Confirmed</option>
            <option value="rejected">Rejected</option>
            <option value="shipped">Shipped</option>
        </select>
    </div>

    <!-- Orders Table -->
    {% call table_wrapper() %}
        {{ table_header(['Order', 'Customer', 'Total', 'Status', 'Date', 'Actions']) }}
                <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                    <template x-if="loading && orders.length === 0">
                        <tr>
                            <td colspan="6" 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="!loading && orders.length === 0">
                        <tr>
                            <td colspan="6" 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-show="status.is_configured">Click "Import Orders" to fetch orders from Letzshop</p>
                                <p class="text-sm mt-1" x-show="!status.is_configured">Configure your API key in Settings to get started</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.letzshop_order_number || order.letzshop_order_id"></p>
                                        <p class="text-xs text-gray-500" x-text="'#' + order.id"></p>
                                    </div>
                                </div>
                            </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.sync_status === 'pending',
                                        'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': order.sync_status === 'confirmed',
                                        'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100': order.sync_status === 'rejected',
                                        'text-blue-700 bg-blue-100 dark:bg-blue-700 dark:text-blue-100': order.sync_status === 'shipped'
                                    }"
                                    x-text="order.sync_status.toUpperCase()"
                                ></span>
                            </td>
                            <td class="px-4 py-3 text-sm">
                                <span x-text="formatDate(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.sync_status === 'pending'"
                                        @click="orderToConfirm = order; showConfirmOrderModal = true"
                                        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.sync_status === 'pending'"
                                        @click="orderToReject = order; showRejectOrderModal = 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="Reject Order"
                                    >
                                        <span x-html="$icon('x', 'w-4 h-4')"></span>
                                    </button>
                                    <button
                                        x-show="order.sync_status === 'confirmed'"
                                        @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>
    {% endcall %}
    {# noqa: FE-001 - Uses flat variables (page, limit, totalOrders) instead of pagination object expected by macro #}
    <!-- Pagination -->
    <div x-show="totalOrders > limit" class="grid px-4 py-3 text-xs font-semibold tracking-wide text-gray-500 uppercase border-t dark:border-gray-700 bg-gray-50 sm:grid-cols-9 dark:text-gray-400 dark:bg-gray-800">
        <span class="flex items-center col-span-3">
            Showing <span x-text="((page - 1) * limit) + 1"></span>-<span x-text="Math.min(page * limit, totalOrders)"></span> of <span x-text="totalOrders"></span>
        </span>
        <span class="col-span-2"></span>
        <span class="flex col-span-4 mt-2 sm:mt-auto sm:justify-end">
            <nav aria-label="Table navigation">
                <ul class="inline-flex items-center">
                    <li>
                        <button
                            @click="page--; loadOrders()"
                            :disabled="page <= 1"
                            class="px-3 py-1 rounded-md rounded-l-lg focus:outline-none focus:shadow-outline-purple disabled:opacity-50"
                        >
                            <span x-html="$icon('chevron-left', 'w-4 h-4')"></span>
                        </button>
                    </li>
                    <li>
                        <button
                            @click="page++; loadOrders()"
                            :disabled="page * limit >= totalOrders"
                            class="px-3 py-1 rounded-md rounded-r-lg focus:outline-none focus:shadow-outline-purple disabled:opacity-50"
                        >
                            <span x-html="$icon('chevron-right', 'w-4 h-4')"></span>
                        </button>
                    </li>
                </ul>
            </nav>
        </span>
    </div>
</div>

<!-- Export Tab -->
<div x-show="activeTab === 'export'" x-transition>
    <div class="grid gap-6 md:grid-cols-2">
        <!-- Export Card -->
        <div class="bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-6">
                <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                    Export Products to Letzshop
                </h3>
                <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">
                    Generate a Letzshop-compatible CSV file from your product catalog.
                    The file uses Google Shopping feed format and includes all required fields.
                </p>

                <!-- Language Selection -->
                <div class="mb-6">
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                        Export Language
                    </label>
                    <p class="text-xs text-gray-500 dark:text-gray-400 mb-3">
                        Select the language for product titles and descriptions
                    </p>
                    <div class="flex flex-wrap gap-3">
                        <button
                            @click="exportLanguage = 'fr'"
                            :class="exportLanguage === 'fr'
                                ? 'bg-purple-100 dark:bg-purple-900/30 border-purple-500 text-purple-700 dark:text-purple-300'
                                : 'bg-white dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300'"
                            class="flex items-center gap-2 px-4 py-2 text-sm font-medium border rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
                        >
                            <span class="fi fi-fr"></span>
                            Francais
                        </button>
                        <button
                            @click="exportLanguage = 'de'"
                            :class="exportLanguage === 'de'
                                ? 'bg-purple-100 dark:bg-purple-900/30 border-purple-500 text-purple-700 dark:text-purple-300'
                                : 'bg-white dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300'"
                            class="flex items-center gap-2 px-4 py-2 text-sm font-medium border rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
                        >
                            <span class="fi fi-de"></span>
                            Deutsch
                        </button>
                        <button
                            @click="exportLanguage = 'en'"
                            :class="exportLanguage === 'en'
                                ? 'bg-purple-100 dark:bg-purple-900/30 border-purple-500 text-purple-700 dark:text-purple-300'
                                : 'bg-white dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300'"
                            class="flex items-center gap-2 px-4 py-2 text-sm font-medium border rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
                        >
                            <span class="fi fi-gb"></span>
                            English
                        </button>
                    </div>
                </div>

                <!-- Include Inactive -->
                <div class="mb-6">
                    <label class="flex items-center cursor-pointer">
                        <input
                            type="checkbox"
                            x-model="exportIncludeInactive"
                            class="form-checkbox h-5 w-5 text-purple-600 rounded border-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:ring-purple-500"
                        />
                        <span class="ml-3 text-sm font-medium text-gray-700 dark:text-gray-400">Include inactive products</span>
                    </label>
                    <p class="mt-1 text-xs text-gray-500 dark:text-gray-400 ml-8">
                        Export products that are currently marked as inactive
                    </p>
                </div>

                <!-- Download Button -->
                <div class="flex flex-wrap gap-3">
                    <button
                        @click="downloadExport()"
                        :disabled="exporting"
                        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"
                    >
                        <span x-show="!exporting" x-html="$icon('download', 'w-4 h-4 mr-2')"></span>
                        <span x-show="exporting" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
                        <span x-text="exporting ? 'Generating...' : 'Download CSV'"></span>
                    </button>
                </div>
            </div>
        </div>

        <!-- Export Info Card -->
        <div class="bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-6">
                <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                    CSV Format Information
                </h3>

                <div class="space-y-4 text-sm text-gray-600 dark:text-gray-400">
                    <div>
                        <h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2">File Format</h4>
                        <ul class="list-disc list-inside space-y-1">
                            <li>Tab-separated values (TSV)</li>
                            <li>UTF-8 encoding</li>
                            <li>Google Shopping compatible</li>
                        </ul>
                    </div>

                    <div>
                        <h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2">Included Fields</h4>
                        <div class="flex flex-wrap gap-2">
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">id</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">title</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">description</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">price</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">image_link</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">availability</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">brand</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">gtin</span>
                            <span class="px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 rounded">+30 more</span>
                        </div>
                    </div>

                    <div>
                        <h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2">How to Upload</h4>
                        <ol class="list-decimal list-inside space-y-1">
                            <li>Download the CSV file</li>
                            <li>Log in to your Letzshop merchant portal</li>
                            <li>Navigate to Products > Import</li>
                            <li>Upload the CSV file</li>
                        </ol>
                    </div>
                </div>

                <div class="mt-6 p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg">
                    <div class="flex">
                        <span x-html="$icon('information-circle', 'w-5 h-5 text-blue-500 mr-2 flex-shrink-0')"></span>
                        <div class="text-sm text-blue-700 dark:text-blue-300">
                            <p class="font-medium">Translation Fallback</p>
                            <p class="mt-1">If a product doesn't have a translation in the selected language, the system will use English, then fall back to any available translation.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Settings Tab -->
<div x-show="activeTab === 'settings'" x-transition>
    <div class="bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-6">
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Letzshop API Configuration
            </h3>

            <form @submit.prevent="saveCredentials()">
                <div class="grid gap-6 mb-6 md:grid-cols-2">
                    <!-- API Key -->
                    <div class="md:col-span-2">
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                            API Key <span class="text-red-500">*</span>
                        </label>
                        <div class="relative">
                            <input
                                :type="showApiKey ? 'text' : 'password'"
                                x-model="credentialsForm.api_key"
                                :placeholder="credentials ? credentials.api_key_masked : 'Enter your Letzshop API key'"
                                class="block w-full px-3 py-2 pr-10 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md focus:border-purple-400 focus:outline-none focus:shadow-outline-purple"
                            />
                            <button
                                type="button"
                                @click="showApiKey = !showApiKey"
                                class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-400 hover:text-gray-600"
                            >
                                <span x-html="$icon(showApiKey ? 'eye-off' : 'eye', 'w-4 h-4')"></span>
                            </button>
                        </div>
                        <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                            Get your API key from the Letzshop merchant portal
                        </p>
                    </div>

                    <!-- Auto Sync -->
                    <div>
                        <label class="flex items-center cursor-pointer">
                            <input
                                type="checkbox"
                                x-model="credentialsForm.auto_sync_enabled"
                                class="form-checkbox h-5 w-5 text-purple-600 rounded border-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:ring-purple-500"
                            />
                            <span class="ml-3 text-sm font-medium text-gray-700 dark:text-gray-400">Enable Auto-Sync</span>
                        </label>
                        <p class="mt-1 text-xs text-gray-500 dark:text-gray-400 ml-8">
                            Automatically import new orders periodically
                        </p>
                    </div>

                    <!-- Sync Interval -->
                    <div x-show="credentialsForm.auto_sync_enabled">
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                            Sync Interval (minutes)
                        </label>
                        <select
                            x-model="credentialsForm.sync_interval_minutes"
                            class="block w-full px-3 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-md focus:border-purple-400 focus:outline-none"
                        >
                            <option value="15">Every 15 minutes</option>
                            <option value="30">Every 30 minutes</option>
                            <option value="60">Every hour</option>
                            <option value="120">Every 2 hours</option>
                            <option value="360">Every 6 hours</option>
                        </select>
                    </div>
                </div>

                <!-- Last Sync Info -->
                <div x-show="credentials" class="mb-6 p-4 bg-gray-50 dark:bg-gray-700 rounded-lg">
                    <h4 class="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Last Sync</h4>
                    <div class="grid gap-2 text-sm text-gray-600 dark:text-gray-400">
                        <p>
                            <span class="font-medium">Status:</span>
                            <span
                                class="ml-2 px-2 py-0.5 text-xs rounded-full"
                                :class="{
                                    'bg-green-100 text-green-700': credentials?.last_sync_status === 'success',
                                    'bg-yellow-100 text-yellow-700': credentials?.last_sync_status === 'partial',
                                    'bg-red-100 text-red-700': credentials?.last_sync_status === 'failed',
                                    'bg-gray-100 text-gray-700': !credentials?.last_sync_status
                                }"
                                x-text="credentials?.last_sync_status || 'Never'"
                            ></span>
                        </p>
                        <p x-show="credentials?.last_sync_at">
                            <span class="font-medium">Time:</span>
                            <span x-text="formatDate(credentials?.last_sync_at)"></span>
                        </p>
                        <p x-show="credentials?.last_sync_error" class="text-red-600">
                            <span class="font-medium">Error:</span>
                            <span x-text="credentials?.last_sync_error"></span>
                        </p>
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="flex flex-wrap gap-3">
                    <button
                        type="submit"
                        :disabled="saving"
                        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"
                    >
                        <span x-show="!saving" x-html="$icon('save', 'w-4 h-4 mr-2')"></span>
                        <span x-show="saving" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
                        <span x-text="saving ? 'Saving...' : 'Save Credentials'"></span>
                    </button>

                    <button
                        type="button"
                        @click="testConnection()"
                        :disabled="testing || !status.is_configured"
                        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"
                    >
                        <span x-show="!testing" x-html="$icon('lightning-bolt', 'w-4 h-4 mr-2')"></span>
                        <span x-show="testing" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
                        <span x-text="testing ? 'Testing...' : 'Test Connection'"></span>
                    </button>

                    <button
                        type="button"
                        x-show="credentials"
                        @click="showDeleteCredentialsModal = true"
                        class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-red-600 transition-colors duration-150 bg-white dark:bg-gray-800 border border-red-300 rounded-lg hover:bg-red-50 dark:hover:bg-red-900/20 focus:outline-none"
                    >
                        <span x-html="$icon('trash', 'w-4 h-4 mr-2')"></span>
                        Remove
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Tracking Modal -->
{% call form_modal('trackingModal', 'Set Tracking Information', show_var='showTrackingModal', submit_action='submitTracking()', submit_text='Save Tracking', loading_var='submittingTracking', loading_text='Saving...', size='sm') %}
    <div class="mb-4">
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
            Tracking Number <span class="text-red-500">*</span>
        </label>
        <input
            type="text"
            x-model="trackingForm.tracking_number"
            required
            placeholder="1Z999AA10123456784"
            class="block w-full px-3 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-md focus:border-purple-400 focus:outline-none"
        />
    </div>

    <div class="mb-4">
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
            Carrier <span class="text-red-500">*</span>
        </label>
        <select
            x-model="trackingForm.tracking_carrier"
            required
            class="block w-full px-3 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-md focus:border-purple-400 focus:outline-none"
        >
            <option value="">Select carrier...</option>
            <option value="dhl">DHL</option>
            <option value="ups">UPS</option>
            <option value="fedex">FedEx</option>
            <option value="post_lu">Post Luxembourg</option>
            <option value="dpd">DPD</option>
            <option value="gls">GLS</option>
            <option value="other">Other</option>
        </select>
    </div>
{% endcall %}

<!-- Order Details Modal -->
<div
    x-show="showOrderModal"
    x-transition:enter="transition ease-out duration-150"
    x-transition:enter-start="opacity-0"
    x-transition:enter-end="opacity-100"
    x-transition:leave="transition ease-in duration-150"
    x-transition:leave-start="opacity-100"
    x-transition:leave-end="opacity-0"
    class="fixed inset-0 z-30 flex items-end bg-black bg-opacity-50 sm:items-center sm:justify-center"
    @click.self="showOrderModal = false"
>
    <div
        x-transition:enter="transition ease-out duration-150"
        x-transition:enter-start="opacity-0 transform translate-y-1/2"
        x-transition:enter-end="opacity-100"
        x-transition:leave="transition ease-in duration-150"
        x-transition:leave-start="opacity-100"
        x-transition:leave-end="opacity-0 transform translate-y-1/2"
        class="w-full px-6 py-4 overflow-hidden bg-white rounded-t-lg dark:bg-gray-800 sm:rounded-lg sm:m-4 sm:max-w-2xl max-h-[80vh] overflow-y-auto"
        @click.stop
    >
        <header class="flex justify-between items-center mb-4">
            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">Order Details</h3>
            <button @click="showOrderModal = false" class="text-gray-400 hover:text-gray-600">
                <span x-html="$icon('x', 'w-5 h-5')"></span>
            </button>
        </header>

        <div x-show="selectedOrder" class="space-y-4">
            <div class="grid grid-cols-2 gap-4 text-sm">
                <div>
                    <span class="font-medium text-gray-600 dark:text-gray-400">Order Number:</span>
                    <span class="ml-2 text-gray-700 dark:text-gray-300" x-text="selectedOrder?.letzshop_order_number"></span>
                </div>
                <div>
                    <span class="font-medium text-gray-600 dark:text-gray-400">Status:</span>
                    <span
                        class="ml-2 px-2 py-0.5 text-xs rounded-full"
                        :class="{
                            'bg-orange-100 text-orange-700': selectedOrder?.sync_status === 'pending',
                            'bg-green-100 text-green-700': selectedOrder?.sync_status === 'confirmed',
                            'bg-red-100 text-red-700': selectedOrder?.sync_status === 'rejected',
                            'bg-blue-100 text-blue-700': selectedOrder?.sync_status === 'shipped'
                        }"
                        x-text="selectedOrder?.sync_status?.toUpperCase()"
                    ></span>
                </div>
                <div>
                    <span class="font-medium text-gray-600 dark:text-gray-400">Customer:</span>
                    <span class="ml-2 text-gray-700 dark:text-gray-300" x-text="selectedOrder?.customer_email"></span>
                </div>
                <div>
                    <span class="font-medium text-gray-600 dark:text-gray-400">Total:</span>
                    <span class="ml-2 text-gray-700 dark:text-gray-300" x-text="selectedOrder?.total_amount + ' ' + selectedOrder?.currency"></span>
                </div>
                <div x-show="selectedOrder?.tracking_number">
                    <span class="font-medium text-gray-600 dark:text-gray-400">Tracking:</span>
                    <span class="ml-2 text-gray-700 dark:text-gray-300" x-text="selectedOrder?.tracking_number + ' (' + selectedOrder?.tracking_carrier + ')'"></span>
                </div>
                <div>
                    <span class="font-medium text-gray-600 dark:text-gray-400">Created:</span>
                    <span class="ml-2 text-gray-700 dark:text-gray-300" x-text="formatDate(selectedOrder?.created_at)"></span>
                </div>
            </div>

            <div x-show="selectedOrder?.inventory_units?.length > 0">
                <h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2">Items</h4>
                <div class="bg-gray-50 dark:bg-gray-700 rounded-lg p-3">
                    <template x-for="unit in selectedOrder?.inventory_units || []" :key="unit.id">
                        <div class="flex justify-between text-sm py-1 border-b border-gray-200 dark:border-gray-600 last:border-0">
                            <span class="text-gray-600 dark:text-gray-400" x-text="unit.id"></span>
                            <span
                                class="px-2 py-0.5 text-xs rounded-full"
                                :class="unit.state === 'confirmed' ? 'bg-green-100 text-green-700' : 'bg-orange-100 text-orange-700'"
                                x-text="unit.state"
                            ></span>
                        </div>
                    </template>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Credentials Confirm Modal -->
{{ confirm_modal('deleteCredentialsModal', 'Remove Credentials', 'Are you sure you want to remove your Letzshop API credentials? This will disable order syncing.', 'deleteCredentials()', 'showDeleteCredentialsModal', 'Remove', 'Cancel', 'danger') }}

<!-- Confirm Order Modal -->
{{ confirm_modal('confirmOrderModal', 'Confirm Order', 'Are you sure you want to confirm this order? The order will be marked as accepted.', 'confirmOrder(orderToConfirm)', 'showConfirmOrderModal', 'Confirm', 'Cancel', 'info') }}

<!-- Reject Order Modal -->
{{ confirm_modal('rejectOrderModal', 'Reject Order', 'Are you sure you want to reject this order? This action cannot be undone.', 'rejectOrder(orderToReject)', 'showRejectOrderModal', 'Reject', 'Cancel', 'danger') }}
{% endblock %}
