<?php 
$pageTitle = 'Daily Float';
include __DIR__ . '/../layouts/header.php'; 
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-cash-stack me-2"></i>Daily Float</h2>
    <a href="<?= $this->baseUrl('/daily-float/create') ?>" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> Record Today's Float
    </a>
</div>

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

<?php if ($latest): ?>
<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="bi bi-calendar-check me-2"></i>Latest Record: <?= date('F j, Y', strtotime($latest['record_date'])) ?>
                </h5>
                <span class="badge bg-primary">KES Equivalent: <?= number_format($latest['total_kes_equivalent'], 2) ?></span>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h6 class="text-muted"><i class="bi bi-cash me-2"></i>Cash Holdings</h6>
                        <table class="table table-sm">
                            <tr><th>KES</th><td class="text-end"><?= number_format($latest['cash_kes'], 2) ?></td></tr>
                            <tr><th>USD</th><td class="text-end"><?= number_format($latest['cash_usd'], 2) ?></td></tr>
                            <tr><th>EUR</th><td class="text-end"><?= number_format($latest['cash_eur'], 2) ?></td></tr>
                            <tr><th>GBP</th><td class="text-end"><?= number_format($latest['cash_gbp'], 2) ?></td></tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <h6 class="text-muted"><i class="bi bi-bank me-2"></i>Bank & Mobile</h6>
                        <table class="table table-sm">
                            <tr><th>M‑Pesa Float</th><td class="text-end"><?= number_format($latest['mpesa_float'], 2) ?></td></tr>
                            <tr><th>Bank KES</th><td class="text-end"><?= number_format($latest['bank_kes'], 2) ?></td></tr>
                            <tr><th>Bank USD</th><td class="text-end"><?= number_format($latest['bank_usd'], 2) ?></td></tr>
                        </table>
                    </div>
                </div>
                <?php if (!empty($latest['notes'])): ?>
                <hr>
                <p class="text-muted mb-0"><i class="bi bi-pencil me-2"></i><?= htmlspecialchars($latest['notes']) ?></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-pie-chart me-2"></i>Summary</h5>
            </div>
            <div class="card-body">
                <div class="d-flex justify-content-between border-bottom pb-2 mb-2">
                    <span>Total Cash (KES eq.)</span>
                    <strong><?= number_format($latest['cash_kes'] + ($latest['cash_usd'] * 130) + ($latest['cash_eur'] * 140) + ($latest['cash_gbp'] * 165), 2) ?></strong>
                </div>
                <div class="d-flex justify-content-between border-bottom pb-2 mb-2">
                    <span>Total Bank</span>
                    <strong><?= number_format($latest['bank_kes'] + ($latest['bank_usd'] * 130), 2) ?></strong>
                </div>
                <div class="d-flex justify-content-between border-bottom pb-2 mb-2">
                    <span>M‑Pesa Float</span>
                    <strong><?= number_format($latest['mpesa_float'], 2) ?></strong>
                </div>
                <div class="d-flex justify-content-between mt-2 pt-2 bg-light p-2 rounded">
                    <span class="fw-bold">Total Float (KES)</span>
                    <span class="fw-bold text-primary"><?= number_format($latest['total_kes_equivalent'], 2) ?></span>
                </div>
            </div>
        </div>
    </div>
</div>
<?php else: ?>
<div class="card">
    <div class="card-body text-center py-5">
        <i class="bi bi-cash-stack fs-1 text-muted mb-3 d-block"></i>
        <h5 class="text-muted">No daily float records yet</h5>
        <p class="text-muted">Start tracking your daily cash and bank positions.</p>
        <a href="<?= $this->baseUrl('/daily-float/create') ?>" class="btn btn-primary">
            <i class="bi bi-plus-circle"></i> Record Today's Float
        </a>
    </div>
</div>
<?php endif; ?>

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