<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $totals = $this->data['totals'] ?? [];
    $from   = $this->v('from');
    $to     = $this->v('to');

    $marginColor = function (float $pct): string {
        if ($pct < 0)  return '#ef4444';
        if ($pct < 20) return '#f59e0b';
        if ($pct < 30) return '#3b82f6';
        return '#22c55e';
    };
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-chart-pie me-2 text-muted"></i>Project P&amp;L</h5>
</div>

<form method="get" action="<?= PROOT; ?>ProjectPnl" class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label">Period 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">Period to</label>  <input type="date" name="to"   value="<?= htmlspecialchars($to); ?>"   class="form-control form-control-sm"></div>
            <div class="col-md-6 text-end">
                <?php $today = date('Y-m-d'); $mStart = date('Y-m-01'); ?>
                <a href="<?= PROOT; ?>ProjectPnl?from=<?= $mStart; ?>&amp;to=<?= $today; ?>" class="btn btn-sm btn-outline-secondary">This month</a>
                <a href="<?= PROOT; ?>ProjectPnl?from=<?= date('Y-01-01'); ?>&amp;to=<?= $today; ?>" class="btn btn-sm btn-outline-secondary">YTD</a>
                <a href="<?= PROOT; ?>ProjectPnl" class="btn btn-sm btn-outline-secondary">All time</a>
                <button type="submit" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;"><i class="fas fa-filter me-1"></i>Apply</button>
            </div>
        </div>
    </div>
</form>

<!-- Portfolio KPIs -->
<?php
    $pMargin = ((float)$totals['revenue'] > 0) ? (((float)$totals['gross_margin']) / (float)$totals['revenue']) * 100 : 0;
    $pPlannedMargin = ((float)$totals['planned_revenue'] > 0) ? (((float)$totals['planned_gross_margin']) / (float)$totals['planned_revenue']) * 100 : 0;
?>
<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body p-3"><div class="text-muted small">Revenue (actual)</div><div style="font-weight:700;font-size:1.35rem;color:#3b82f6;">TZS <?= number_format((float)$totals['revenue']); ?></div><div class="text-muted small mt-1">Planned <?= number_format((float)$totals['planned_revenue']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body p-3"><div class="text-muted small">Cost of Sales</div><div style="font-weight:700;font-size:1.35rem;color:#ef4444;">TZS <?= number_format((float)$totals['cogs']); ?></div><div class="text-muted small mt-1">Planned <?= number_format((float)$totals['planned_cog']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body p-3"><div class="text-muted small">OpEx</div><div style="font-weight:700;font-size:1.35rem;color:#f59e0b;">TZS <?= number_format((float)$totals['opex']); ?></div><div class="text-muted small mt-1">not planned</div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $marginColor($pMargin); ?> !important;"><div class="card-body p-3"><div class="text-muted small">Gross margin</div><div style="font-weight:700;font-size:1.5rem;color:<?= $marginColor($pMargin); ?>;"><?= number_format($pMargin, 1); ?>%</div><div class="text-muted small mt-1">Planned <?= number_format($pPlannedMargin, 1); ?>%</div></div></div></div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Per project</strong></div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Project</th><th>Customer</th><th>Type</th><th>Status</th>
                    <th class="text-end">Revenue</th>
                    <th class="text-end">CoGS</th>
                    <th class="text-end">OpEx</th>
                    <th class="text-end">Gross margin</th>
                    <th class="text-end">Margin %</th>
                    <th class="text-end">Planned %</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $rev = (float)$r['revenue'];
                    $cog = (float)$r['cogs'];
                    $opx = (float)$r['opex'];
                    $gm  = $rev - $cog;
                    $mgn = $rev > 0 ? ($gm / $rev) * 100 : 0;
                    $planRev = (float)$r['planned_revenue'];
                    $planCog = (float)$r['planned_cog'];
                    $planMgn = $planRev > 0 ? (($planRev - $planCog) / $planRev) * 100 : 0;
                    $variance = $mgn - $planMgn;
                    $sc = ['OPEN'=>'bg-info text-white','CLOSED'=>'bg-secondary','SUSPENDED'=>'bg-warning text-dark','CANCELLED'=>'bg-danger'][$r['project_status']] ?? 'bg-light text-muted';
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>ProjectPnl/view/<?= (int)$r['project_id']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['project_no']); ?></strong></a>
                            <div class="text-muted small"><?= htmlspecialchars($r['project_name']); ?></div>
                        </td>
                        <td><?= htmlspecialchars($r['project_customer']); ?></td>
                        <td style="text-transform:capitalize;"><?= htmlspecialchars($r['project_type']); ?></td>
                        <td><span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($r['project_status'])); ?></span></td>
                        <td class="text-end"><?= number_format($rev); ?></td>
                        <td class="text-end text-muted"><?= number_format($cog); ?></td>
                        <td class="text-end text-muted"><?= number_format($opx); ?></td>
                        <td class="text-end" style="font-weight:600;"><?= number_format($gm); ?></td>
                        <td class="text-end" style="font-weight:700;color:<?= $marginColor($mgn); ?>;"><?= $rev > 0 ? number_format($mgn, 1).'%' : '—'; ?></td>
                        <td class="text-end text-muted"><?= $planRev > 0 ? number_format($planMgn, 1).'%' : '—'; ?><?php if ($rev > 0 && $planRev > 0): ?>
                            <div class="text-muted" style="font-size:.62rem;color:<?= $variance>=0?'#22c55e':'#ef4444'; ?> !important;">
                                <?= $variance>=0?'+':''; ?><?= number_format($variance,1); ?>pp
                            </div>
                        <?php endif; ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>ProjectPnl/view/<?= (int)$r['project_id']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="11" class="text-center text-muted py-4">No projects yet.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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