<div>
    <x-layouts.app title="{{ __('Customers') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Customers') }}</h1>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ __('Dashboard') }}</a></li>
                        <li class="breadcrumb-item active">{{ __('Customers') }}</li>
                    </ol>
                </nav>
            </div>
            <div>
                <a href="{{ route('customers.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i>{{ __('Add Customer') }}
                </a>
            </div>
        </div>

        <!-- Filters -->
        <div class="card mb-4">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6">
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-search"></i></span>
                            <input type="text" class="form-control" 
                                   wire:model.live.debounce.300ms="search" 
                                   placeholder="{{ __('Search by name, phone, email...') }}">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" wire:model.live="statusFilter">
                            <option value="">{{ __('All Status') }}</option>
                            <option value="active">{{ __('Active') }}</option>
                            <option value="inactive">{{ __('Inactive') }}</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" wire:model.live="perPage">
                            <option value="15">15 {{ __('per page') }}</option>
                            <option value="25">25 {{ __('per page') }}</option>
                            <option value="50">50 {{ __('per page') }}</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Customers Table -->
        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th wire:click="sortBy('name')" style="cursor: pointer;">
                                    {{ __('Customer') }}
                                    @if($sortField === 'name')
                                        <i class="fas fa-sort-{{ $sortDirection === 'asc' ? 'up' : 'down' }} ms-1"></i>
                                    @endif
                                </th>
                                <th>{{ __('Phone') }}</th>
                                <th>{{ __('Email') }}</th>
                                <th wire:click="sortBy('credit_limit')" style="cursor: pointer;" class="text-end">
                                    {{ __('Credit Limit') }}
                                    @if($sortField === 'credit_limit')
                                        <i class="fas fa-sort-{{ $sortDirection === 'asc' ? 'up' : 'down' }} ms-1"></i>
                                    @endif
                                </th>
                                <th class="text-end">{{ __('Balance') }}</th>
                                <th class="text-center">{{ __('Status') }}</th>
                                <th style="width: 150px;" class="text-center">{{ __('Actions') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($customers as $customer)
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-primary text-white rounded-circle d-flex align-items-center justify-content-center me-2">
                                                {{ strtoupper(substr($customer->name, 0, 1)) }}
                                            </div>
                                            <div>
                                                <a href="{{ route('customers.show', $customer) }}" class="fw-medium text-dark text-decoration-none">
                                                    {{ $customer->name }}
                                                </a>
                                                @if($customer->address)
                                                    <br><small class="text-muted">{{ \Str::limit($customer->address, 40) }}</small>
                                                @endif
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <a href="tel:{{ $customer->phone }}" class="text-decoration-none">
                                            {{ $customer->phone }}
                                        </a>
                                    </td>
                                    <td>
                                        @if($customer->email)
                                            <a href="mailto:{{ $customer->email }}" class="text-decoration-none">
                                                {{ $customer->email }}
                                            </a>
                                        @else
                                            <span class="text-muted">-</span>
                                        @endif
                                    </td>
                                    <td class="text-end">
                                        E£ {{ number_format($customer->credit_limit, 2) }}
                                    </td>
                                    <td class="text-end">
                                        @php
                                            $balance = $customer->credit_balance;
                                            $balanceClass = $balance >= 0 ? 'success' : 'danger';
                                        @endphp
                                        <span class="text-{{ $balanceClass }} fw-medium">
                                            E£ {{ number_format(abs($balance), 2) }}
                                            @if($balance < 0) <small>({{ __('owes') }})</small> @endif
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        @if($customer->is_active)
                                            <span class="badge bg-success">{{ __('Active') }}</span>
                                        @else
                                            <span class="badge bg-secondary">{{ __('Inactive') }}</span>
                                        @endif
                                    </td>
                                    <td class="text-center">
                                        <div class="btn-group btn-group-sm">
                                            <a href="{{ route('customers.show', $customer) }}" class="btn btn-outline-info" 
                                               data-bs-toggle="tooltip" title="{{ __('View') }}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ route('customers.edit', $customer) }}" class="btn btn-outline-primary" 
                                               data-bs-toggle="tooltip" title="{{ __('Edit') }}">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <button type="button" class="btn btn-outline-danger" 
                                                    wire:click="confirmDelete({{ $customer->id }})"
                                                    data-bs-toggle="tooltip" title="{{ __('Delete') }}">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center py-5">
                                        <i class="fas fa-users fa-3x text-muted mb-3"></i>
                                        <p class="text-muted mb-0">{{ __('No customers found.') }}</p>
                                        <a href="{{ route('customers.create') }}" class="btn btn-primary btn-sm mt-3">
                                            <i class="fas fa-plus me-1"></i>{{ __('Add First Customer') }}
                                        </a>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($customers->hasPages())
                <div class="card-footer">
                    {{ $customers->links() }}
                </div>
            @endif
        </div>

        <!-- Delete Modal -->
        @if($showDeleteModal)
            <div class="modal fade show d-block" tabindex="-1" style="background: rgba(0,0,0,0.5);">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">{{ __('Confirm Delete') }}</h5>
                            <button type="button" class="btn-close" wire:click="cancelDelete"></button>
                        </div>
                        <div class="modal-body">
                            <p class="mb-0">{{ __('Are you sure you want to delete this customer?') }}</p>
                            <p class="text-danger small mb-0 mt-2">
                                <i class="fas fa-exclamation-triangle me-1"></i>
                                {{ __('Customers with sales history cannot be deleted.') }}
                            </p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" wire:click="cancelDelete">{{ __('Cancel') }}</button>
                            <button type="button" class="btn btn-danger" wire:click="delete">
                                <i class="fas fa-trash me-1"></i>{{ __('Delete') }}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        @endif
    </x-layouts.app>
</div>
