{# app/templates/admin/inventory.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 store_selector %}

{% block title %}Inventory{% endblock %}

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

{% block content %}
<!-- Page Header with Store Selector -->
{% call page_header_flex(title='Inventory', subtitle='Manage stock levels across all stores') %}
    <div class="flex items-center gap-4">
        <!-- Store Autocomplete (Tom Select) -->
        {{ store_selector(
            ref_name='storeSelect',
            id='inventory-store-select',
            placeholder='Filter by store...',
            width='w-80'
        ) }}
        {{ refresh_button(loading_var='loading', onclick='refresh()', variant='secondary') }}
        <button
            @click="showImportModal = true"
            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"
        >
            <span x-html="$icon('upload', 'w-4 h-4 mr-2')"></span>
            Import
        </button>
    </div>
{% endcall %}

<!-- Selected Store Info -->
<div x-show="selectedStore" x-transition class="mb-6 p-3 bg-purple-50 dark:bg-purple-900/20 rounded-lg border border-purple-200 dark:border-purple-800">
    <div class="flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="w-8 h-8 rounded-full bg-purple-100 dark:bg-purple-900 flex items-center justify-center">
                <span class="text-sm font-semibold text-purple-600 dark:text-purple-300" x-text="selectedStore?.name?.charAt(0).toUpperCase()"></span>
            </div>
            <div>
                <span class="font-medium text-purple-800 dark:text-purple-200" x-text="selectedStore?.name"></span>
                <span class="ml-2 text-xs text-purple-600 dark:text-purple-400 font-mono" x-text="selectedStore?.store_code"></span>
            </div>
        </div>
        <button @click="clearStoreFilter()" class="text-purple-600 dark:text-purple-400 hover:text-purple-800 dark:hover:text-purple-200 text-sm flex items-center gap-1">
            <span x-html="$icon('x', 'w-4 h-4')"></span>
            Clear filter
        </button>
    </div>
</div>

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

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

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

    <!-- Card: Total Quantity -->
    <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('cube', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Stock
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatNumber(stats.total_quantity || 0)">
                0
            </p>
        </div>
    </div>

    <!-- Card: Available Stock -->
    <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">
                Available
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatNumber(stats.total_available || 0)">
                0
            </p>
        </div>
    </div>

    <!-- Card: Low Stock Items -->
    <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:text-red-100 dark:bg-red-500">
            <span x-html="$icon('exclamation', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Low Stock
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.low_stock_count || 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 product title or SKU..."
                    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">
            <!-- Location Filter -->
            <select
                x-model="filters.location"
                @change="pagination.page = 1; loadInventory()"
                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 Locations</option>
                <template x-for="loc in locations" :key="loc">
                    <option :value="loc" x-text="loc"></option>
                </template>
            </select>

            <!-- Low Stock Filter -->
            <select
                x-model="filters.low_stock"
                @change="pagination.page = 1; loadInventory()"
                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 Stock Levels</option>
                <option value="5">Low Stock (< 5)</option>
                <option value="10">Low Stock (< 10)</option>
                <option value="25">Low Stock (< 25)</option>
                <option value="50">Low Stock (< 50)</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 inventory"
            >
                <span x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
                Refresh
            </button>
        </div>
    </div>
</div>

<!-- Inventory 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">Product</th>
                    <th class="px-4 py-3">Store</th>
                    <th class="px-4 py-3">Location</th>
                    <th class="px-4 py-3 text-right">Quantity</th>
                    <th class="px-4 py-3 text-right">Reserved</th>
                    <th class="px-4 py-3 text-right">Available</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">
                <!-- Empty State -->
                <template x-if="inventory.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('archive', 'w-12 h-12 mb-2 text-gray-300')"></span>
                                <p class="font-medium">No inventory entries found</p>
                                <p class="text-xs mt-1" x-text="filters.search || filters.store_id || filters.location || filters.low_stock ? 'Try adjusting your filters' : 'Inventory will appear here when products have stock entries'"></p>
                            </div>
                        </td>
                    </tr>
                </template>

                <!-- Inventory Rows -->
                <template x-for="item in inventory" :key="item.id">
                    <tr class="text-gray-700 dark:text-gray-400">
                        <!-- Product Info -->
                        <td class="px-4 py-3">
                            <div class="flex items-center">
                                <!-- Product Image -->
                                <div class="w-10 h-10 mr-3 rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700 flex-shrink-0">
                                    <template x-if="item.product_image">
                                        <img :src="item.product_image" :alt="item.product_title" class="w-full h-full object-cover" loading="lazy" />
                                    </template>
                                    <template x-if="!item.product_image">
                                        <div class="w-full h-full flex items-center justify-center">
                                            <span x-html="$icon('photograph', 'w-5 h-5 text-gray-400')"></span>
                                        </div>
                                    </template>
                                </div>
                                <!-- Product Details -->
                                <div class="min-w-0">
                                    <p class="font-semibold text-sm truncate max-w-[200px]" x-text="item.product_title || 'Untitled'"></p>
                                    <template x-if="item.product_sku">
                                        <p class="text-xs text-gray-400 font-mono">SKU: <span x-text="item.product_sku"></span></p>
                                    </template>
                                    <template x-if="item.gtin">
                                        <p class="text-xs text-gray-400 font-mono">GTIN: <span x-text="item.gtin"></span></p>
                                    </template>
                                </div>
                            </div>
                        </td>

                        <!-- Store Info -->
                        <td class="px-4 py-3 text-sm">
                            <p class="font-medium" x-text="item.store_name || 'Unknown'"></p>
                        </td>

                        <!-- Location -->
                        <td class="px-4 py-3 text-sm">
                            <span class="px-2 py-1 text-xs font-medium rounded-full bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300" x-text="item.location"></span>
                        </td>

                        <!-- Quantity -->
                        <td class="px-4 py-3 text-sm text-right font-mono">
                            <span x-text="formatNumber(item.quantity)"></span>
                        </td>

                        <!-- Reserved -->
                        <td class="px-4 py-3 text-sm text-right font-mono">
                            <span :class="item.reserved_quantity > 0 ? 'text-orange-600 dark:text-orange-400' : ''" x-text="formatNumber(item.reserved_quantity)"></span>
                        </td>

                        <!-- Available -->
                        <td class="px-4 py-3 text-sm text-right font-mono font-semibold">
                            <span :class="item.available_quantity <= 0 ? 'text-red-600 dark:text-red-400' : item.available_quantity < 10 ? 'text-orange-600 dark:text-orange-400' : 'text-green-600 dark:text-green-400'" x-text="formatNumber(item.available_quantity)"></span>
                        </td>

                        <!-- Status -->
                        <td class="px-4 py-3 text-sm">
                            <template x-if="item.available_quantity <= 0">
                                <span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100">
                                    Out of Stock
                                </span>
                            </template>
                            <template x-if="item.available_quantity > 0 && item.available_quantity < 10">
                                <span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs text-orange-700 bg-orange-100 dark:bg-orange-700 dark:text-orange-100">
                                    Low Stock
                                </span>
                            </template>
                            <template x-if="item.available_quantity >= 10">
                                <span class="px-2 py-1 font-semibold leading-tight rounded-full text-xs text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100">
                                    In Stock
                                </span>
                            </template>
                        </td>

                        <!-- Actions -->
                        <td class="px-4 py-3 text-sm">
                            <div class="flex items-center space-x-2">
                                <button
                                    @click="openAdjustModal(item)"
                                    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="Adjust Stock"
                                >
                                    <span x-html="$icon('adjustments', 'w-4 h-4')"></span>
                                </button>
                                <button
                                    @click="openSetModal(item)"
                                    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="Set Quantity"
                                >
                                    <span x-html="$icon('pencil', 'w-4 h-4')"></span>
                                </button>
                                <button
                                    @click="confirmDelete(item)"
                                    class="flex items-center justify-center px-2 py-1 text-xs font-medium leading-5 text-red-600 rounded-lg dark:text-red-400 focus:outline-none hover:bg-gray-100 dark:hover:bg-gray-700"
                                    title="Delete Entry"
                                >
                                    <span x-html="$icon('delete', 'w-4 h-4')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
            </tbody>
    {% endcall %}

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

<!-- Adjust Stock Modal -->
{% call modal_simple('adjustStockModal', 'Adjust Stock', show_var='showAdjustModal', 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" x-text="selectedItem?.product_title || 'Product'"></p>
        <p class="text-xs">Location: <span x-text="selectedItem?.location || '-'"></span></p>
        <p class="text-xs">Current Stock: <span x-text="formatNumber(selectedItem?.quantity || 0)"></span></p>
    </div>

    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Adjustment Quantity
        </label>
        <div class="flex items-center gap-2">
            <button
                @click="adjustForm.quantity = Math.max(-999999, adjustForm.quantity - 1)"
                class="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 border border-gray-300 dark:border-gray-600 rounded"
            >
                <span x-html="$icon('minus', 'w-4 h-4')"></span>
            </button>
            {# noqa: FE-008 - Custom stepper with negative values for stock adjustments #}
            <input
                type="number"
                x-model.number="adjustForm.quantity"
                class="flex-1 px-3 py-2 text-sm text-center 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="0"
            >
            <button
                @click="adjustForm.quantity = Math.min(999999, adjustForm.quantity + 1)"
                class="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 border border-gray-300 dark:border-gray-600 rounded"
            >
                <span x-html="$icon('plus', 'w-4 h-4')"></span>
            </button>
        </div>
        <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
            Positive = add stock, Negative = remove stock
        </p>
    </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="adjustForm.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., Restocking from supplier"
        >
    </div>

    <div class="p-3 bg-gray-50 dark:bg-gray-700 rounded-lg">
        <p class="text-sm text-gray-600 dark:text-gray-400">
            New quantity will be:
            <span class="font-semibold" :class="(selectedItem?.quantity || 0) + adjustForm.quantity < 0 ? 'text-red-600' : 'text-green-600'" x-text="formatNumber(Math.max(0, (selectedItem?.quantity || 0) + adjustForm.quantity))"></span>
        </p>
    </div>

    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showAdjustModal = 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="executeAdjust()"
            :disabled="saving || adjustForm.quantity === 0"
            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 ? 'Saving...' : 'Adjust Stock'"></span>
        </button>
    </div>
</div>
{% endcall %}

<!-- Set Quantity Modal -->
{% call modal_simple('setQuantityModal', 'Set Quantity', show_var='showSetModal', 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" x-text="selectedItem?.product_title || 'Product'"></p>
        <p class="text-xs">Location: <span x-text="selectedItem?.location || '-'"></span></p>
        <p class="text-xs">Current Stock: <span x-text="formatNumber(selectedItem?.quantity || 0)"></span></p>
    </div>

    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            New Quantity
        </label>
        <input
            type="number"
            x-model.number="setForm.quantity"
            min="0"
            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 new quantity"
        >
    </div>

    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showSetModal = 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="executeSet()"
            :disabled="saving || setForm.quantity < 0"
            class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors disabled:opacity-50"
        >
            <span x-text="saving ? 'Saving...' : 'Set Quantity'"></span>
        </button>
    </div>
</div>
{% endcall %}

<!-- Delete Confirmation Modal -->
{% call modal_simple('deleteModal', 'Delete Inventory Entry', show_var='showDeleteModal', size='sm') %}
<div class="space-y-4">
    <p class="text-sm text-gray-600 dark:text-gray-400">
        Are you sure you want to delete this inventory entry?
    </p>
    <div class="text-sm">
        <p class="font-medium text-gray-700 dark:text-gray-300" x-text="selectedItem?.product_title || 'Product'"></p>
        <p class="text-xs text-gray-500">Location: <span x-text="selectedItem?.location || '-'"></span></p>
        <p class="text-xs text-gray-500">Current Stock: <span x-text="formatNumber(selectedItem?.quantity || 0)"></span></p>
    </div>
    <p class="text-xs text-red-600 dark:text-red-400">
        This action cannot be undone.
    </p>

    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showDeleteModal = 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="executeDelete()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50"
        >
            <span x-text="saving ? 'Deleting...' : 'Delete'"></span>
        </button>
    </div>
</div>
{% endcall %}

<!-- Import Inventory Modal -->
{% call modal_simple('importModal', 'Import Inventory', show_var='showImportModal', size='md') %}
<div class="space-y-4">
    <p class="text-sm text-gray-600 dark:text-gray-400">
        Upload a TSV or CSV file to import inventory. Products are matched by EAN/GTIN.
    </p>

    <!-- File Format Info -->
    <div class="p-3 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-sm">
        <p class="font-medium text-blue-800 dark:text-blue-200 mb-2">File Format:</p>
        <p class="text-blue-700 dark:text-blue-300 text-xs font-mono mb-1">BIN    EAN    PRODUCT    QUANTITY</p>
        <p class="text-blue-600 dark:text-blue-400 text-xs">
            <strong>Required:</strong> BIN, EAN<br>
            <strong>Optional:</strong> PRODUCT (display only), QUANTITY (defaults to 1 per row)
        </p>
    </div>

    <form @submit.prevent="executeImport()">
        <!-- Store Selection -->
        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                Store <span class="text-red-500">*</span>
            </label>
            <select
                x-model="importForm.store_id"
                required
                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="">Select store...</option>
                <template x-for="store in storesList" :key="store.id">
                    <option :value="store.id" x-text="store.name + ' (' + store.store_code + ')'"></option>
                </template>
            </select>
        </div>

        <!-- Warehouse -->
        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                Warehouse
            </label>
            <input
                type="text"
                x-model="importForm.warehouse"
                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="strassen"
            >
        </div>

        <!-- File Upload -->
        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                File <span class="text-red-500">*</span>
            </label>
            <input
                type="file"
                @change="importForm.file = $event.target.files[0]"
                accept=".tsv,.csv,.txt"
                required
                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 file:mr-4 file:py-1 file:px-3 file:rounded file:border-0 file:text-sm file:font-medium file:bg-purple-100 file:text-purple-700 dark:file:bg-purple-900 dark:file:text-purple-300 hover:file:bg-purple-200"
            >
        </div>

        <!-- Clear Existing Option -->
        <div class="mb-4">
            <label class="flex items-center">
                <input
                    type="checkbox"
                    x-model="importForm.clear_existing"
                    class="w-4 h-4 text-purple-600 border-gray-300 rounded focus:ring-purple-500"
                >
                <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">
                    Clear existing inventory for this warehouse before import
                </span>
            </label>
        </div>

        <!-- Import Result -->
        <template x-if="importResult">
            <div class="p-3 rounded-lg" :class="importResult.success ? 'bg-green-50 dark:bg-green-900/20' : 'bg-red-50 dark:bg-red-900/20'">
                <p class="font-medium" :class="importResult.success ? 'text-green-800 dark:text-green-200' : 'text-red-800 dark:text-red-200'">
                    <span x-text="importResult.success ? 'Import Successful!' : 'Import Failed'"></span>
                </p>
                <template x-if="importResult.success">
                    <div class="text-sm text-green-700 dark:text-green-300 mt-1">
                        <p>Rows processed: <span x-text="importResult.total_rows"></span></p>
                        <p>Entries created: <span x-text="importResult.entries_created"></span></p>
                        <p>Entries updated: <span x-text="importResult.entries_updated"></span></p>
                        <p>Total quantity: <span x-text="importResult.quantity_imported"></span></p>
                        <template x-if="importResult.unmatched_gtins?.length > 0">
                            <div class="mt-2">
                                <p class="font-medium text-orange-600 dark:text-orange-400">
                                    Unmatched GTINs (<span x-text="importResult.unmatched_gtins.length"></span>):
                                </p>
                                <ul class="text-xs mt-1 max-h-32 overflow-y-auto">
                                    <template x-for="item in importResult.unmatched_gtins" :key="item.gtin">
                                        <li class="font-mono" x-text="item.gtin + ' (' + item.quantity + ' units) - ' + item.product_name"></li>
                                    </template>
                                </ul>
                            </div>
                        </template>
                    </div>
                </template>
                <template x-if="!importResult.success && importResult.errors?.length > 0">
                    <ul class="text-sm text-red-700 dark:text-red-300 mt-1">
                        <template x-for="error in importResult.errors" :key="error">
                            <li x-text="error"></li>
                        </template>
                    </ul>
                </template>
            </div>
        </template>

        <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
            <button
                type="button"
                @click="closeImportModal()"
                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"
            >
                <span x-text="importResult?.success ? 'Close' : 'Cancel'"></span>
            </button>
            <button
                type="submit"
                :disabled="importing || !importForm.store_id || !importForm.file"
                x-show="!importResult?.success"
                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="importing ? 'Importing...' : 'Import'"></span>
            </button>
        </div>
    </form>
</div>
{% endcall %}
{% endblock %}

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