<?php
/**
 * @var object $customers
 * @var string $search
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-users me-2 text-gold"></i>Customers</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Customers</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('customers_create')): ?>
                <a href="<?= url('customers/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Customer
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-6">
                <form method="GET" action="<?= url('customers') ?>" class="d-flex gap-2">
                    <div class="input-group">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-search text-gold"></i>
                        </span>
                        <input type="text" name="search" class="form-control form-control-sm bg-transparent" 
                               placeholder="Search by name, email or phone..." value="<?= e($search) ?>">
                        <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        <?php if (!empty($search)): ?>
                            <a href="<?= url('customers') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th class="text-center">Total Visits</th>
                        <th class="text-end">Total Spent</th>
                        <th>Last Visit</th>
                        <th>Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($customers->data)): ?>
                        <tr>
                            <td colspan="9" class="text-center text-muted py-4">
                                <i class="fas fa-user-slash fa-2x mb-2"></i>
                                <p class="mb-0">No customers found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($customers->currentPage - 1) * $customers->perPage + 1; ?>
                        <?php foreach ($customers->data as $customer): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('customers/show/' . $customer->id) ?>" class="text-gold text-decoration-none">
                                        <div class="d-flex align-items-center gap-2">
                                            <?php if (!empty($customer->photo)): ?>
                                                <img src="<?= uploadUrl($customer->photo) ?>" alt="" class="rounded-circle" width="32" height="32" style="object-fit:cover;">
                                            <?php else: ?>
                                                <div class="avatar-placeholder rounded-circle d-flex align-items-center justify-content-center" 
                                                     style="width:32px;height:32px;background:var(--gold);color:#000;font-size:14px;font-weight:600;">
                                                    <?= strtoupper(substr($customer->first_name ?? '?', 0, 1) . substr($customer->last_name ?? '', 0, 1)) ?>
                                                </div>
                                            <?php endif; ?>
                                            <strong><?= e($customer->first_name . ' ' . $customer->last_name) ?></strong>
                                        </div>
                                    </a>
                                </td>
                                <td><?= e($customer->email ?? '-') ?></td>
                                <td><?= e($customer->phone ?? '-') ?></td>
                                <td class="text-center"><?= (int)($customer->total_visits ?? 0) ?></td>
                                <td class="text-end"><?= currency($customer->total_spent ?? 0) ?></td>
                                <td><?= !empty($customer->last_visit) ? formatDateTime($customer->last_visit) : '-' ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($customer->status ?? 'active') ?>">
                                        <?= e(ucfirst($customer->status ?? 'active')) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('customers/show/' . $customer->id) ?>" class="btn btn-outline-info" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <?php if (can('customers_edit')): ?>
                                            <a href="<?= url('customers/edit/' . $customer->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('customers_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete" 
                                                    data-id="<?= $customer->id ?>" 
                                                    data-name="<?= e($customer->first_name . ' ' . $customer->last_name) ?>"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($customers->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($customers->currentPage - 1) * $customers->perPage) + 1 ?> 
                    to <?= min($customers->currentPage * $customers->perPage, $customers->total) ?> 
                    of <?= $customers->total ?> entries
                </div>
                <?= paginateLinks($customers, url('customers' . (!empty($search) ? '?search=' . urlencode($search) : ''))) ?>
            </div>
        <?php endif; ?>
    </div>
</div>


