<?php 
use App\Models\Settings;
$appConfig = new Settings();
$appName = $appConfig->get('company_name', 'Finance Manager');
$logo = $appConfig->get('logo');
$logoUrl = $logo ? $this->baseUrl('/' . ltrim($logo, '/')) : null;

// Ensure $client is available (fallback from session)
$client = $client ?? null;
$fromDate = $fromDate ?? date('Y-m-01');
$toDate = $toDate ?? date('Y-m-t');
$pageTitle = 'Transaction History';

include __DIR__ . '/../layouts/client_header.php'; 
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h3><i class="bi bi-list-ul me-2"></i>My Transactions</h3>
    <a href="<?= $this->baseUrl('/client/statement/pdf?' . http_build_query($_GET)) ?>" class="btn btn-outline-danger">
        <i class="bi bi-file-pdf"></i> Export PDF
    </a>
</div>

<?php if ($client): ?>
<!-- Balance summary card -->
<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card stat-card" style="background: linear-gradient(135deg, #059669, #10b981);">
            <div class="card-body text-white">
                <h6 class="text-uppercase small opacity-75">Current Balance</h6>
                <h3 class="mb-0"><?= number_format($client['balance'], 2) ?> KES</h3>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Filter form -->
<div class="card mb-4">
    <div class="card-body">
        <form class="row g-3 align-items-end">
            <div class="col-md-3">
                <label class="form-label">From Date</label>
                <input type="date" name="from" class="form-control" value="<?= htmlspecialchars($fromDate) ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">To Date</label>
                <input type="date" name="to" class="form-control" value="<?= htmlspecialchars($toDate) ?>">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100"><i class="bi bi-funnel"></i> Filter</button>
            </div>
        </form>
    </div>
</div>

<!-- Transactions 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>Date & Time</th>
                        <th>Type</th>
                        <th class="text-end">Amount (KES)</th>
                        <th class="text-end">Running Balance</th>
                        <th>Reference / Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($entries)): ?>
                        <tr>
                            <td colspan="5" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                                No transactions found for this period.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($entries as $e): ?>
                        <tr>
                            <td><?= date('Y-m-d H:i', strtotime($e['date'])) ?></td>
                            <td>
                                <span class="badge bg-<?= $e['type'] === 'Credit' || $e['type'] === 'Deposit' ? 'success-subtle text-success' : 'warning-subtle text-warning' ?>">
                                    <?= $e['type'] ?>
                                </span>
                            </td>
                            <td class="text-end fw-bold <?= $e['amount'] >= 0 ? 'text-success' : 'text-danger' ?>">
                                <?= ($e['amount'] >= 0 ? '+' : '') . number_format($e['amount'], 2) ?>
                            </td>
                            <td class="text-end"><?= number_format($e['balance'], 2) ?></td>
                            <td>
                                <?= htmlspecialchars($e['reference'] ?? '') ?>
                                <?php if (!empty($e['notes'])): ?>
                                    <br><small class="text-muted"><?= htmlspecialchars($e['notes']) ?></small>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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