<?php 
$pageTitle = 'Clients';
include __DIR__ . '/../layouts/header.php'; 

// Quick summary
$totalClients = count($clients ?? []);
$totalBalance = array_sum(array_column($clients ?? [], 'balance'));
?>

<!-- Top Summary -->
<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card stat-card" style="background: linear-gradient(135deg, #0f172a, #1e3a5f);">
            <div class="card-body text-white">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-uppercase small opacity-75">Total Clients</h6>
                        <h3 class="mb-0"><?= $totalClients ?></h3>
                    </div>
                    <i class="bi bi-people icon-bg"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card stat-card" style="background: linear-gradient(135deg, #059669, #10b981);">
            <div class="card-body text-white">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-uppercase small opacity-75">Total Client Balances</h6>
                        <h3 class="mb-0"><?= number_format($totalBalance, 0) ?></h3>
                    </div>
                    <i class="bi bi-wallet2 icon-bg"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4 d-flex align-items-end justify-content-end">
        <div class="d-flex gap-2">
            <a href="<?= $this->baseUrl('/clients/transaction/create') ?>" class="btn btn-outline-primary">
                <i class="bi bi-arrow-left-right"></i> Quick Transaction
            </a>
            <a href="<?= $this->baseUrl('/clients/create') ?>" class="btn btn-primary">
                <i class="bi bi-plus-circle"></i> Add New Client
            </a>
        </div>
    </div>
</div>

<!-- Alerts -->
<?php if (isset($_SESSION['success'])): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <?= htmlspecialchars($_SESSION['success']) ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <?= htmlspecialchars($_SESSION['error']) ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php unset($_SESSION['error']); ?>
<?php endif; ?>

<!-- Clients Table -->
<div class="card">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <h5 class="mb-0"><i class="bi bi-people me-2"></i>Client List</h5>
        <span class="text-muted small"><?= $totalClients ?> clients</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Code</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Balance (KES)</th>
                        <th>Overdraft Limit</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($clients)): ?>
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">
                                <i class="bi bi-people fs-1 d-block mb-2"></i>
                                No clients found. <a href="<?= $this->baseUrl('/clients/create') ?>">Add your first client</a>.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($clients as $c): ?>
                        <tr>
                            <td><span class="badge bg-secondary"><?= htmlspecialchars($c['client_code']) ?></span></td>
                            <td>
                                <strong><?= htmlspecialchars($c['full_name']) ?></strong>
                                <?php if (!empty($c['email'])): ?>
                                    <br><small class="text-muted"><?= htmlspecialchars($c['email']) ?></small>
                                <?php endif; ?>
                            </td>
                            <td><?= htmlspecialchars($c['phone'] ?: '—') ?></td>
                            <td class="<?= $c['balance'] < 0 ? 'text-danger fw-bold' : '' ?>">
                                <?= number_format($c['balance'], 2) ?>
                            </td>
                            <td><?= number_format($c['overdraft_limit'] ?? 0, 2) ?></td>
                            <td class="text-center">
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="<?= $this->baseUrl('/clients/' . $c['id'] . '/deposit') ?>" 
                                       class="btn btn-success" title="Deposit"><i class="bi bi-plus-circle"></i></a>
                                    <a href="<?= $this->baseUrl('/clients/' . $c['id'] . '/transaction') ?>" 
                                       class="btn btn-warning" title="Transaction"><i class="bi bi-arrow-left-right"></i></a>
                                    <a href="<?= $this->baseUrl('/clients/' . $c['id'] . '/overdraft') ?>" 
                                       class="btn btn-secondary" title="Overdraft Limit"><i class="bi bi-sliders"></i></a>
                                    <a href="<?= $this->baseUrl('/reports/client-transactions?client_id=' . $c['id']) ?>" 
                                       class="btn btn-outline-info" title="View Report"><i class="bi bi-file-text"></i></a>
                                    <div class="btn-group" role="group">
                                        <button type="button" class="btn btn-info dropdown-toggle" data-bs-toggle="dropdown" title="Export">
                                            <i class="bi bi-download"></i>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-end">
                                            <li><a class="dropdown-item" href="<?= $this->baseUrl('/clients/' . $c['id'] . '/statement/pdf') ?>"><i class="bi bi-file-pdf"></i> PDF</a></li>
                                            <li><a class="dropdown-item" href="<?= $this->baseUrl('/clients/' . $c['id'] . '/statement/excel') ?>"><i class="bi bi-file-excel"></i> Excel</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../layouts/footer.php'; ?>