{# app/templates/store/customers.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 %}Customers{% endblock %}

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

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

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

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

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

    <!-- Active Customers -->
    <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>

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

<!-- Filters -->
<div x-show="!loading" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
    <div class="flex flex-wrap items-center gap-4">
        <!-- Search -->
        <div class="flex-1 min-w-[200px]">
            <div class="relative">
                <span class="absolute inset-y-0 left-0 flex items-center pl-3">
                    <span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
                </span>
                <input
                    type="text"
                    x-model="filters.search"
                    @input="debouncedSearch()"
                    placeholder="Search by name or email..."
                    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>

        <!-- Clear Filters -->
        <button
            x-show="filters.search || filters.status"
            @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>

<!-- Customers Table -->
<div x-show="!loading && !error" class="mb-8">
{% call table_wrapper() %}
    <thead>
        <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
            <th class="px-4 py-3">Customer</th>
            <th class="px-4 py-3">Email</th>
            <th class="px-4 py-3">Joined</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="customer in customers" :key="customer.id">
            <tr class="text-gray-700 dark:text-gray-400">
                <!-- Customer Info -->
                <td class="px-4 py-3">
                    <div class="flex items-center text-sm">
                        <div class="relative w-10 h-10 mr-3 rounded-full overflow-hidden 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="getInitials(customer)"></span>
                        </div>
                        <div>
                            <p class="font-semibold" x-text="`${customer.first_name || ''} ${customer.last_name || ''}`.trim() || 'Unknown'"></p>
                            <p class="text-xs text-gray-500 dark:text-gray-400" x-text="customer.phone || ''"></p>
                        </div>
                    </div>
                </td>
                <!-- Email -->
                <td class="px-4 py-3 text-sm" x-text="customer.email || '-'"></td>
                <!-- Joined -->
                <td class="px-4 py-3 text-sm" x-text="formatDate(customer.created_at)"></td>
                <!-- Actions -->
                <td class="px-4 py-3">
                    <div class="flex items-center space-x-2 text-sm">
                        <button
                            @click="viewCustomer(customer)"
                            class="p-1 text-gray-500 hover:text-purple-600 dark:text-gray-400 dark:hover:text-purple-400"
                            title="View Details"
                        >
                            <span x-html="$icon('eye', 'w-5 h-5')"></span>
                        </button>
                        <button
                            @click="messageCustomer(customer)"
                            class="p-1 text-gray-500 hover:text-green-600 dark:text-gray-400 dark:hover:text-green-400"
                            title="Send Message"
                        >
                            <span x-html="$icon('chat-bubble-left-right', 'w-5 h-5')"></span>
                        </button>
                    </div>
                </td>
            </tr>
        </template>
        <!-- Empty State -->
        <tr x-show="customers.length === 0">
            <td colspan="4" 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('users', 'w-12 h-12 text-gray-300 dark:text-gray-600 mb-4')"></span>
                    <p class="text-lg font-medium">No customers found</p>
                    <p class="text-sm">Customers will appear here when they make purchases</p>
                </div>
            </td>
        </tr>
    </tbody>
{% endcall %}
</div>

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

<!-- Customer Detail Modal -->
{% call modal_simple('customerDetailModal', 'Customer Details', show_var='showDetailModal', size='md') %}
<div x-show="selectedCustomer">
    <div class="flex items-center mb-4">
        <div class="w-16 h-16 rounded-full bg-purple-100 dark:bg-purple-900 flex items-center justify-center mr-4">
            <span class="text-xl font-semibold text-purple-600 dark:text-purple-300" x-text="getInitials(selectedCustomer)"></span>
        </div>
        <div>
            <p class="text-lg font-semibold text-gray-800 dark:text-gray-200" x-text="`${selectedCustomer?.first_name || ''} ${selectedCustomer?.last_name || ''}`.trim() || 'Unknown'"></p>
            <p class="text-sm text-gray-500 dark:text-gray-400" x-text="selectedCustomer?.email"></p>
        </div>
    </div>
    <div class="grid grid-cols-2 gap-4 text-sm">
        <div>
            <p class="text-gray-500 dark:text-gray-400">Phone</p>
            <p class="font-medium text-gray-800 dark:text-gray-200" x-text="selectedCustomer?.phone || '-'"></p>
        </div>
        <div>
            <p class="text-gray-500 dark:text-gray-400">Joined</p>
            <p class="font-medium text-gray-800 dark:text-gray-200" x-text="formatDate(selectedCustomer?.created_at)"></p>
        </div>
        <div>
            <p class="text-gray-500 dark:text-gray-400">Customer #</p>
            <p class="font-medium font-mono text-gray-800 dark:text-gray-200" x-text="selectedCustomer?.customer_number || '-'"></p>
        </div>
        <div>
            <p class="text-gray-500 dark:text-gray-400">Status</p>
            <p class="font-medium" :class="selectedCustomer?.is_active ? 'text-green-600' : 'text-red-600'" x-text="selectedCustomer?.is_active ? 'Active' : 'Inactive'"></p>
        </div>
    </div>
</div>
<div class="flex justify-end gap-2 pt-4 mt-4 border-t dark:border-gray-700">
    <button @click="showDetailModal = false" class="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200">
        Close
    </button>
    <a :href="`/store/${storeCode}/customers/${selectedCustomer?.id}`" class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700">
        View Full Profile
    </a>
</div>
{% endcall %}
{% endblock %}

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