<?php
/**
 * @var string $startDate
 * @var string $endDate
 * @var array $report
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-balance-scale me-2 text-gold"></i>Profit & Loss Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/profit-loss') ?>">
        <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/profit_loss?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
$revenue = $report['revenue'] ?? [];
$expenses = $report['expenses'] ?? [];
$otherIncome = $report['other_income'] ?? [];
$totalRevenue = array_sum(array_column($revenue, 'amount'));
$totalExpenses = array_sum(array_column($expenses, 'amount'));
$totalOtherIncome = array_sum(array_column($otherIncome, 'amount'));
$netProfit = ($totalRevenue + $totalOtherIncome) - $totalExpenses;
?>

<div class="row g-4 mb-4">
    <div class="col-md-3">
        <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-up"></i>
            </div>
            <div class="stats-info">
                <span class="stats-label">Total Revenue</span>
                <span class="stats-value"><?= currency($totalRevenue) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <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-down"></i>
            </div>
            <div class="stats-info">
                <span class="stats-label">Total Expenses</span>
                <span class="stats-value"><?= currency($totalExpenses) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(13,202,240,0.15);border-color:rgba(13,202,240,0.3);color:#0dcaf0;">
                <i class="fas fa-plus-circle"></i>
            </div>
            <div class="stats-info">
                <span class="stats-label">Other Income</span>
                <span class="stats-value"><?= currency($totalOtherIncome) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:<?= $netProfit >= 0 ? 'rgba(25,135,84,0.15)' : 'rgba(220,53,69,0.15)'; ?>;border-color:<?= $netProfit >= 0 ? 'rgba(25,135,84,0.3)' : 'rgba(220,53,69,0.3)'; ?>;color:<?= $netProfit >= 0 ? '#198754' : '#dc3545'; ?>;">
                <i class="fas fa-chart-pie"></i>
            </div>
            <div class="stats-info">
                <span class="stats-label">Net Profit</span>
                <span class="stats-value"><?= currency($netProfit) ?></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-up text-success card-icon"></i>
                    <h6 class="card-title">Revenue</h6>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-dark table-hover mb-0">
                    <thead>
                        <tr><th>Category</th><th class="text-end">Amount</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($revenue as $r): ?>
                            <tr>
                                <td><?= e($r->type ?? 'N/A') ?></td>
                                <td class="text-end text-success"><?= currency($r->amount ?? 0) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($revenue)): ?>
                            <tr><td colspan="2" class="text-center text-muted py-3">No revenue 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-down text-danger card-icon"></i>
                    <h6 class="card-title">Expenses</h6>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-dark table-hover mb-0">
                    <thead>
                        <tr><th>Category</th><th class="text-end">Amount</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($expenses as $e): ?>
                            <tr>
                                <td><?= e($e->type ?? 'N/A') ?></td>
                                <td class="text-end text-danger"><?= currency($e->amount ?? 0) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($expenses)): ?>
                            <tr><td colspan="2" class="text-center text-muted py-3">No expenses data</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="glass-card p-0 mt-4">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-plus-circle card-icon"></i>
            <h6 class="card-title">Other Income</h6>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr><th>Description</th><th class="text-end">Amount</th></tr>
            </thead>
            <tbody>
                <?php foreach ($otherIncome as $oi): ?>
                    <tr>
                        <td><?= e($oi->type ?? 'N/A') ?></td>
                        <td class="text-end text-success"><?= currency($oi->amount ?? 0) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($otherIncome)): ?>
                    <tr><td colspan="2" class="text-center text-muted py-3">No other income</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
