<?php
/**
 * @var string $startDate
 * @var string $endDate
 * @var array $report
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-money-bill-wave me-2 text-gold"></i>Cash Flow Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/cash-flow') ?>">
        <div class="col-md-3">
            <label class="form-label">Start Date</label>
            <input type="date" name="start_date" class="form-control" value="<?= e($startDate) ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label">End Date</label>
            <input type="date" name="end_date" class="form-control" value="<?= e($endDate) ?>">
        </div>
        <div class="col-md-4">
            <button type="submit" class="btn btn-gold"><i class="fas fa-search me-1"></i>Generate</button>
            <a href="<?= url('reports/export/cash_flow?start_date=' . $startDate . '&end_date=' . $endDate) ?>" class="btn btn-outline-gold ms-2">
                <i class="fas fa-download me-1"></i>CSV
            </a>
        </div>
    </form>
</div>

<?php
$inflow = $report['inflow'] ?? [];
$outflow = $report['outflow'] ?? [];
$totalInflow = array_sum(array_column($inflow, 'amount'));
$totalOutflow = array_sum(array_column($outflow, 'amount'));
$netCash = $totalInflow - $totalOutflow;
?>
<div class="row g-4 mb-4">
    <div class="col-md-4">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(25,135,84,0.15);border-color:rgba(25,135,84,0.3);color:#198754;"><i class="fas fa-arrow-down"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Inflow</span>
                <span class="stats-value text-success"><?= currency($totalInflow) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(220,53,69,0.15);border-color:rgba(220,53,69,0.3);color:#dc3545;"><i class="fas fa-arrow-up"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Outflow</span>
                <span class="stats-value text-danger"><?= currency($totalOutflow) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:<?= $netCash >= 0 ? 'rgba(25,135,84,0.15)' : 'rgba(220,53,69,0.15)' ?>;border-color:<?= $netCash >= 0 ? 'rgba(25,135,84,0.3)' : 'rgba(220,53,69,0.3)' ?>;color:<?= $netCash >= 0 ? '#198754' : '#dc3545' ?>;"><i class="fas fa-calculator"></i></div>
            <div class="stats-info">
                <span class="stats-label">Net Cash Flow</span>
                <span class="stats-value"><?= currency($netCash) ?></span>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <div class="col-md-6">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-arrow-down text-success card-icon"></i>
                    <h6 class="card-title">Inflow (Income)</h6>
                </div>
                <span class="badge bg-success"><?= count($inflow) ?> entries</span>
            </div>
            <div class="table-responsive" style="max-height:400px;">
                <table class="table table-dark table-hover mb-0">
                    <thead>
                        <tr><th>Date</th><th>Category</th><th>Payment</th><th class="text-end">Amount</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($inflow as $i): ?>
                            <tr>
                                <td><?= formatDate($i->date ?? '') ?></td>
                                <td><?= e($i->category ?? 'N/A') ?></td>
                                <td><?= e(ucwords(str_replace('_', ' ', $i->payment_method ?? ''))) ?></td>
                                <td class="text-end text-success"><?= currency($i->amount ?? 0) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($inflow)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-3">No inflow data</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-arrow-up text-danger card-icon"></i>
                    <h6 class="card-title">Outflow (Expenses)</h6>
                </div>
                <span class="badge bg-danger"><?= count($outflow) ?> entries</span>
            </div>
            <div class="table-responsive" style="max-height:400px;">
                <table class="table table-dark table-hover mb-0">
                    <thead>
                        <tr><th>Date</th><th>Category</th><th>Payment</th><th class="text-end">Amount</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($outflow as $o): ?>
                            <tr>
                                <td><?= formatDate($o->date ?? '') ?></td>
                                <td><?= e($o->category ?? 'N/A') ?></td>
                                <td><?= e(ucwords(str_replace('_', ' ', $o->payment_method ?? ''))) ?></td>
                                <td class="text-end text-danger"><?= currency($o->amount ?? 0) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($outflow)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-3">No outflow data</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
