<?php $this->start('body');
    $from = $this->v('from');
    $to   = $this->v('to');
    $p    = $this->data['pnl'] ?? [];
    $rev  = $p['revenue']       ?? [];
    $cogs = $p['cogs']          ?? [];
    $opex = $p['opex']          ?? [];
    $sumR = (float)($p['total_revenue'] ?? 0);
    $sumC = (float)($p['total_cogs']    ?? 0);
    $sumO = (float)($p['total_opex']    ?? 0);
    $gp   = (float)($p['gross_profit']  ?? 0);
    $np   = (float)($p['net_profit']    ?? 0);
    $gm   = (float)($p['gross_margin']  ?? 0);
    $nm   = (float)($p['net_margin']    ?? 0);

    $line = function (array $r): string {
        return '<tr><td class="text-muted small" style="color:#7a6f63;">' . htmlspecialchars($r['account_code']) .
               '</td><td style="color:#4a3f36;">' . htmlspecialchars($r['account_name']) .
               '</td><td class="text-end" style="font-weight:500;color:#4a3f36;">' . number_format((float)$r['amount']) . '</td></tr>';
    };
?>

<?php $q = 'from=' . urlencode($from) . '&to=' . urlencode($to); ?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#4a3f36;"><i class="fas fa-chart-line me-2" style="color:#b56b58;"></i>Profit &amp; Loss</h5>
    <div class="btn-group">
        <a href="<?= PROOT; ?>FinancialStatements/profitAndLossXlsx?<?= $q; ?>" class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;"><i class="fas fa-file-excel me-1" style="color:#4a6b3f;"></i>Excel</a>
        <a href="<?= PROOT; ?>FinancialStatements/profitAndLossPdf?<?= $q; ?>"  class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;"><i class="fas fa-file-pdf   me-1" style="color:#8a4a3a;"></i>PDF</a>
    </div>
</div>

<form method="get" action="<?= PROOT; ?>FinancialStatements/profitAndLoss" class="card border-0 shadow-sm mb-3" style="background:#faf6f0;">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label" style="color:#4a3f36;">From</label><input type="date" name="from" value="<?= htmlspecialchars($from); ?>" class="form-control form-control-sm"></div>
            <div class="col-md-3"><label class="form-label" style="color:#4a3f36;">To</label><input type="date" name="to" value="<?= htmlspecialchars($to); ?>" class="form-control form-control-sm"></div>
            <div class="col-md-6 text-end">
                <button type="submit" class="btn btn-sm" style="background:#b56b58;border-color:#b56b58;color:#fff;font-weight:600;"><i class="fas fa-filter me-1"></i>Refresh</button>
            </div>
        </div>
    </div>
</form>

<!-- ── KPI strip ────────────────────────────────────────────────────────── -->
<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7a9e6e !important;background:#faf6f0;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#7a6f63;">Revenue</div>
        <div style="font-weight:700;font-size:1.4rem;color:#4a6b3f;">+ TZS <?= number_format($sumR); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #c9a875 !important;background:#faf6f0;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#7a6f63;">Gross profit</div>
        <div style="font-weight:700;font-size:1.4rem;color:<?= $gp>=0?'#7a6039':'#8a4a3a'; ?>;">TZS <?= number_format($gp); ?></div>
        <div style="color:#a89a8a;font-size:.72rem;">margin <?= number_format($gm, 1); ?>%</div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #b56b58 !important;background:#faf6f0;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#7a6f63;">Operating expenses</div>
        <div style="font-weight:700;font-size:1.4rem;color:#8a4a3a;">&minus; TZS <?= number_format($sumO); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $np>=0?'#7a9e6e':'#b56b58'; ?> !important;background:#faf6f0;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#7a6f63;">Net profit</div>
        <div style="font-weight:700;font-size:1.5rem;color:<?= $np>=0?'#4a6b3f':'#8a4a3a'; ?>;">TZS <?= number_format($np); ?></div>
        <div style="color:#a89a8a;font-size:.72rem;">margin <?= number_format($nm, 1); ?>%</div>
    </div></div></div>
</div>

<!-- ── P&L statement ────────────────────────────────────────────────────── -->
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white" style="border-bottom:1px solid #eae0d0;">
        <strong style="color:#4a3f36;"><i class="fas fa-file-invoice-dollar me-2" style="color:#c9a875;"></i>P&amp;L statement &middot; <?= htmlspecialchars($from); ?> to <?= htmlspecialchars($to); ?></strong>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#f5efe6;">
                <tr>
                    <th style="color:#4a3f36;">Code</th>
                    <th style="color:#4a3f36;">Account</th>
                    <th class="text-end" style="color:#4a3f36;">Amount</th>
                </tr>
            </thead>
            <tbody>
                <tr style="background:#faf6f0;"><td colspan="3" style="font-weight:600;color:#4a6b3f;text-transform:capitalize;font-size:.78rem;letter-spacing:.3px;">Revenue</td></tr>
                <?php foreach ($rev as $r) echo $line($r); ?>
                <?php if (empty($rev)): ?><tr><td colspan="3" class="text-center py-2" style="color:#a89a8a;">No revenue in period.</td></tr><?php endif; ?>
                <tr style="background:#f5efe6;"><td colspan="2" class="text-end" style="font-weight:600;color:#4a3f36;">Total revenue</td><td class="text-end" style="font-weight:600;color:#4a6b3f;"><?= number_format($sumR); ?></td></tr>

                <tr style="background:#faf6f0;"><td colspan="3" style="font-weight:600;color:#7a6039;text-transform:capitalize;font-size:.78rem;letter-spacing:.3px;">Cost of Sales</td></tr>
                <?php foreach ($cogs as $r) echo $line($r); ?>
                <?php if (empty($cogs)): ?><tr><td colspan="3" class="text-center py-2" style="color:#a89a8a;">No cost of sales in period.</td></tr><?php endif; ?>
                <tr style="background:#f5efe6;"><td colspan="2" class="text-end" style="font-weight:600;color:#4a3f36;">Total cost of sales</td><td class="text-end" style="font-weight:600;color:#7a6039;"><?= number_format($sumC); ?></td></tr>

                <tr style="background:#eae0d0;"><td colspan="2" class="text-end" style="font-weight:700;color:#4a3f36;">Gross profit</td><td class="text-end" style="font-weight:700;color:<?= $gp>=0?'#4a6b3f':'#8a4a3a'; ?>;"><?= number_format($gp); ?></td></tr>

                <tr style="background:#faf6f0;"><td colspan="3" style="font-weight:600;color:#8a4a3a;text-transform:capitalize;font-size:.78rem;letter-spacing:.3px;">Operating expenses</td></tr>
                <?php foreach ($opex as $r) echo $line($r); ?>
                <?php if (empty($opex)): ?><tr><td colspan="3" class="text-center py-2" style="color:#a89a8a;">No operating expenses in period.</td></tr><?php endif; ?>
                <tr style="background:#f5efe6;"><td colspan="2" class="text-end" style="font-weight:600;color:#4a3f36;">Total operating expenses</td><td class="text-end" style="font-weight:600;color:#8a4a3a;"><?= number_format($sumO); ?></td></tr>
            </tbody>
            <tfoot style="background:#eae0d0;">
                <tr>
                    <td colspan="2" class="text-end" style="font-weight:700;color:#4a3f36;font-size:1rem;">Net profit</td>
                    <td class="text-end" style="font-weight:700;color:<?= $np>=0?'#4a6b3f':'#8a4a3a'; ?>;font-size:1rem;">TZS <?= number_format($np); ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

<?php $this->end(); ?>
