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

{% block title %}Products{% endblock %}

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

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Products', subtitle='Manage your product catalog') %}
    <div class="flex items-center gap-4">
        {{ refresh_button(loading_var='loading', onclick='loadProducts()', variant='secondary') }}
        <button
            @click="createProduct()"
            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('plus', 'w-4 h-4 mr-2')"></span>
            Add Product
        </button>
    </div>
{% endcall %}

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

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

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

    <!-- Active Products -->
    <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">Active</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.active">0</p>
        </div>
    </div>

    <!-- Inactive Products -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-gray-500 bg-gray-100 rounded-full dark:text-gray-100 dark:bg-gray-500">
            <span x-html="$icon('x-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Inactive</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.inactive">0</p>
        </div>
    </div>

    <!-- Featured Products -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-yellow-500 bg-yellow-100 rounded-full dark:text-yellow-100 dark:bg-yellow-500">
            <span x-html="$icon('star', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Featured</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.featured">0</p>
        </div>
    </div>
</div>

<!-- Filters -->
<div x-show="!loading" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
    <div class="flex flex-wrap items-center gap-4">
        <!-- Search -->
        <div class="flex-1 min-w-[200px]">
            <div class="relative">
                <span class="absolute inset-y-0 left-0 flex items-center pl-3">
                    <span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
                </span>
                <input
                    type="text"
                    x-model="filters.search"
                    @input="debouncedSearch()"
                    placeholder="Search products..."
                    class="w-full pl-10 pr-4 py-2 text-sm text-gray-700 placeholder-gray-400 bg-gray-50 border border-gray-200 rounded-lg dark:placeholder-gray-500 dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                />
            </div>
        </div>

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

        <!-- Featured Filter -->
        <select
            x-model="filters.featured"
            @change="applyFilter()"
            class="px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        >
            <option value="">All Products</option>
            <option value="true">Featured Only</option>
            <option value="false">Not Featured</option>
        </select>

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

<!-- Bulk Actions Bar -->
<div x-show="!loading && selectedProducts.length > 0"
     x-transition:enter="transition ease-out duration-200"
     x-transition:enter-start="opacity-0 -translate-y-2"
     x-transition:enter-end="opacity-100 translate-y-0"
     class="mb-4 p-3 bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-800 rounded-lg flex items-center justify-between">
    <div class="flex items-center gap-2">
        <span class="text-sm font-medium text-purple-700 dark:text-purple-300">
            <span x-text="selectedProducts.length"></span> product(s) selected
        </span>
        <button @click="clearSelection()" class="text-sm text-purple-600 hover:text-purple-800 dark:text-purple-400">
            Clear
        </button>
    </div>
    <div class="flex items-center gap-2">
        <button
            @click="bulkActivate()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-green-700 bg-green-100 rounded-lg hover:bg-green-200 dark:bg-green-900 dark:text-green-300 dark:hover:bg-green-800 disabled:opacity-50"
        >
            <span x-html="$icon('check-circle', 'w-4 h-4 inline mr-1')"></span>
            Activate
        </button>
        <button
            @click="bulkDeactivate()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 disabled:opacity-50"
        >
            <span x-html="$icon('x-circle', 'w-4 h-4 inline mr-1')"></span>
            Deactivate
        </button>
        <button
            @click="bulkSetFeatured()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-yellow-700 bg-yellow-100 rounded-lg hover:bg-yellow-200 dark:bg-yellow-900 dark:text-yellow-300 dark:hover:bg-yellow-800 disabled:opacity-50"
        >
            <span x-html="$icon('star', 'w-4 h-4 inline mr-1')"></span>
            Feature
        </button>
        <button
            @click="bulkRemoveFeatured()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 disabled:opacity-50"
        >
            Unfeature
        </button>
        <button
            @click="confirmBulkDelete()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-red-700 bg-red-100 rounded-lg hover:bg-red-200 dark:bg-red-900 dark:text-red-300 dark:hover:bg-red-800 disabled:opacity-50"
        >
            <span x-html="$icon('trash', 'w-4 h-4 inline mr-1')"></span>
            Delete
        </button>
    </div>
</div>

<!-- Products Table -->
<div x-show="!loading && !error" class="w-full mb-8 overflow-hidden rounded-lg shadow-xs">
    <div class="w-full overflow-x-auto">
        <table class="w-full whitespace-no-wrap">
            <thead>
                <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
                    <th class="px-4 py-3 w-10">
                        <input
                            type="checkbox"
                            :checked="allSelected"
                            :indeterminate="someSelected"
                            @click="toggleSelectAll()"
                            class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600"
                        />
                    </th>
                    <th class="px-4 py-3">Product</th>
                    <th class="px-4 py-3">SKU</th>
                    <th class="px-4 py-3">Price</th>
                    <th class="px-4 py-3">Status</th>
                    <th class="px-4 py-3">Featured</th>
                    <th class="px-4 py-3">Actions</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                <template x-for="product in products" :key="product.id">
                    <tr class="text-gray-700 dark:text-gray-400" :class="{'bg-purple-50 dark:bg-purple-900/10': isSelected(product.id)}">
                        <!-- Checkbox -->
                        <td class="px-4 py-3">
                            <input
                                type="checkbox"
                                :checked="isSelected(product.id)"
                                @click="toggleSelect(product.id)"
                                class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600"
                            />
                        </td>
                        <!-- Product Info -->
                        <td class="px-4 py-3">
                            <div class="flex items-center text-sm">
                                <div class="relative w-10 h-10 mr-3 rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700">
                                    <img
                                        x-show="product.image_url"
                                        :src="product.image_url"
                                        :alt="product.name"
                                        class="object-cover w-full h-full"
                                    />
                                    <div
                                        x-show="!product.image_url"
                                        class="flex items-center justify-center w-full h-full text-gray-400"
                                    >
                                        <span x-html="$icon('photo', 'w-5 h-5')"></span>
                                    </div>
                                </div>
                                <div>
                                    <p class="font-semibold" x-text="product.name"></p>
                                    <p class="text-xs text-gray-500 dark:text-gray-400" x-text="product.category || 'No category'"></p>
                                </div>
                            </div>
                        </td>
                        <!-- SKU -->
                        <td class="px-4 py-3 text-sm font-mono" x-text="product.sku || '-'"></td>
                        <!-- Price -->
                        <td class="px-4 py-3 text-sm font-semibold" x-text="formatPrice(product.price)"></td>
                        <!-- Status -->
                        <td class="px-4 py-3 text-xs">
                            <button
                                @click="toggleActive(product)"
                                :class="product.is_active
                                    ? 'px-2 py-1 font-semibold leading-tight text-green-700 bg-green-100 rounded-full dark:bg-green-700 dark:text-green-100'
                                    : 'px-2 py-1 font-semibold leading-tight text-gray-700 bg-gray-100 rounded-full dark:bg-gray-700 dark:text-gray-100'"
                                x-text="product.is_active ? 'Active' : 'Inactive'"
                            ></button>
                        </td>
                        <!-- Featured -->
                        <td class="px-4 py-3">
                            <button
                                @click="toggleFeatured(product)"
                                :class="product.is_featured ? 'text-yellow-500' : 'text-gray-300 hover:text-yellow-500'"
                            >
                                <span x-html="$icon('star', 'w-5 h-5')"></span>
                            </button>
                        </td>
                        <!-- Actions -->
                        <td class="px-4 py-3">
                            <div class="flex items-center space-x-2 text-sm">
                                <button
                                    @click="viewProduct(product)"
                                    class="p-1 text-gray-500 hover:text-purple-600 dark:text-gray-400 dark:hover:text-purple-400"
                                    title="View"
                                >
                                    <span x-html="$icon('eye', 'w-5 h-5')"></span>
                                </button>
                                <button
                                    @click="editProduct(product)"
                                    class="p-1 text-gray-500 hover:text-blue-600 dark:text-gray-400 dark:hover:text-blue-400"
                                    title="Edit"
                                >
                                    <span x-html="$icon('pencil', 'w-5 h-5')"></span>
                                </button>
                                <button
                                    @click="confirmDelete(product)"
                                    class="p-1 text-gray-500 hover:text-red-600 dark:text-gray-400 dark:hover:text-red-400"
                                    title="Delete"
                                >
                                    <span x-html="$icon('trash', 'w-5 h-5')"></span>
                                </button>
                            </div>
                        </td>
                    </tr>
                </template>
                <!-- Empty State -->
                <tr x-show="products.length === 0">
                    <td colspan="7" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                        <div class="flex flex-col items-center">
                            <span x-html="$icon('cube', 'w-12 h-12 text-gray-300 dark:text-gray-600 mb-4')"></span>
                            <p class="text-lg font-medium">No products found</p>
                            <p class="text-sm">Add your first product to get started</p>
                            <button
                                @click="createProduct()"
                                class="mt-4 px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700"
                            >
                                Add Product
                            </button>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

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

<!-- Delete Confirmation Modal -->
{% call modal_simple('deleteProductModal', 'Delete Product', show_var='showDeleteModal', size='sm') %}
<div class="space-y-4">
    <template x-if="selectedProduct">
        <p class="text-sm text-gray-600 dark:text-gray-400">
            Are you sure you want to delete <span class="font-semibold" x-text="selectedProduct.name"></span>?
            This action cannot be undone.
        </p>
    </template>
    <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="deleteProduct()"
            :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"
        >Delete</button>
    </div>
</div>
{% endcall %}

<!-- Bulk Delete Confirmation Modal -->
{% call modal_simple('bulkDeleteProductModal', 'Delete Selected Products', show_var='showBulkDeleteModal', 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 <span class="font-semibold" x-text="selectedProducts.length"></span> selected product(s)?
        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="showBulkDeleteModal = 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="bulkDelete()"
            :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"
        >Delete All</button>
    </div>
</div>
{% endcall %}
{% endblock %}

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