<?php 
$pageTitle = 'Client Transaction Report';
include __DIR__ . '/../layouts/header.php'; 

// Compute quick summary from results (optional)
$totalCredits = 0;
$totalDebits  = 0;
foreach ($results as $r) {
    if ($r['trx_type'] == 'credit') $totalCredits += $r['amount'];
    else $totalDebits += $r['amount'];
}
$netFlow = $totalCredits - $totalDebits;
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-funnel me-2"></i>Client Transactions</h2>
    <div class="d-flex gap-2">
  <a href="<?= $this->baseUrl('/reports/client-transactions/pdf?' . http_build_query($_GET)) ?>" class="btn btn-sm btn-outline-danger"><i class="bi bi-file-pdf"></i> PDF</a>
<a href="<?= $this->baseUrl('/reports/client-transactions/excel?' . http_build_query($_GET)) ?>" class="btn btn-sm btn-outline-success"><i class="bi bi-file-excel"></i> Excel</a>
        <a href="<?= $this->baseUrl('/reports') ?>" class="btn btn-outline-secondary"><i class="bi bi-arrow-left"></i> Back</a>
    </div>
</div>

<!-- Summary Cards -->
<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card stat-card" style="background: linear-gradient(135deg, #10b981, #059669);">
            <div class="card-body text-white">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-uppercase small opacity-75">Total Credits</h6>
                        <h4 class="mb-0"><?= number_format($totalCredits, 2) ?></h4>
                    </div>
                    <i class="bi bi-arrow-down-circle fs-1 opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card stat-card" style="background: linear-gradient(135deg, #ef4444, #dc2626);">
            <div class="card-body text-white">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-uppercase small opacity-75">Total Debits</h6>
                        <h4 class="mb-0"><?= number_format($totalDebits, 2) ?></h4>
                    </div>
                    <i class="bi bi-arrow-up-circle fs-1 opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card stat-card" style="background: linear-gradient(135deg, <?= $netFlow >= 0 ? '#3b82f6, #2563eb' : '#f59e0b, #d97706' ?>);">
            <div class="card-body text-white">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-uppercase small opacity-75">Net Flow</h6>
                        <h4 class="mb-0"><?= number_format($netFlow, 2) ?></h4>
                    </div>
                    <i class="bi bi-graph-up fs-1 opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Filter Card -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3 align-items-end">
            <div class="col-md-3">
                <label class="form-label"><i class="bi bi-calendar"></i> From Date</label>
                <input type="date" name="from" class="form-control" value="<?= htmlspecialchars($fromDate) ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label"><i class="bi bi-calendar"></i> To Date</label>
                <input type="date" name="to" class="form-control" value="<?= htmlspecialchars($toDate) ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label"><i class="bi bi-person"></i> Client</label>
                <select name="client_id" class="form-select">
                    <option value="">All Clients</option>
                    <?php foreach ($clients as $c): ?>
                        <option value="<?= $c['id'] ?>" <?= $selectedClient == $c['id'] ? 'selected' : '' ?>>
                            <?= htmlspecialchars($c['full_name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label"><i class="bi bi-tag"></i> Type</label>
                <select name="type" class="form-select">
                    <option value="all" <?= $selectedType == 'all' ? 'selected' : '' ?>>All</option>
                    <option value="credit" <?= $selectedType == 'credit' ? 'selected' : '' ?>>Credit</option>
                    <option value="debit" <?= $selectedType == 'debit' ? 'selected' : '' ?>>Debit</option>
                </select>
            </div>
            <div class="col-md-1">
                <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
            </div>
        </form>
    </div>
</div>

<!-- Results Table -->
<div class="card">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th><i class="bi bi-calendar3"></i> Date</th>
                        <th>Client</th>
                        <th>Type</th>
                        <th class="text-end">Amount (KES)</th>
                        <th>Reference</th>
                        <th>Notes</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($results)): ?>
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                                No transactions found for the selected filters.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($results as $r): ?>
                        <tr>
                            <td><?= date('Y-m-d H:i', strtotime($r['trx_date'])) ?></td>
                            <td>
                                <strong><?= htmlspecialchars($r['full_name']) ?></strong>
                                <div class="small text-muted"><?= $r['client_code'] ?></div>
                            </td>
                            <td>
                                <?php if ($r['trx_type'] == 'credit'): ?>
                                    <span class="badge bg-success-subtle text-success">Credit</span>
                                <?php else: ?>
                                    <span class="badge bg-warning-subtle text-warning">Debit</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end fw-bold <?= $r['trx_type'] == 'credit' ? 'text-success' : 'text-danger' ?>">
                                <?= $r['trx_type'] == 'credit' ? '+' : '-' ?><?= number_format($r['amount'], 2) ?>
                            </td>
                            <td><?= htmlspecialchars($r['reference'] ?? '—') ?></td>
                            <td><?= htmlspecialchars($r['notes'] ?? '—') ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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