<?php 
$pageTitle = 'Forex Profit/Loss';
include __DIR__ . '/../layouts/header.php'; 
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-pie-chart me-2"></i>Profit/Loss Report</h2>
    <a href="<?= $this->baseUrl('/forex/trades') ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> Back to Trades
    </a>
</div>

<!-- Date Filter -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3 align-items-end">
            <div class="col-auto">
                <label class="form-label">From Date</label>
                <input type="date" name="from" class="form-control" value="<?= htmlspecialchars($fromDate) ?>">
            </div>
            <div class="col-auto">
                <label class="form-label">To Date</label>
                <input type="date" name="to" class="form-control" value="<?= htmlspecialchars($toDate) ?>">
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-primary">Apply Filter</button>
            </div>
            <?php if ($lastRateUpdate): ?>
            <div class="col-auto ms-auto">
                <span class="text-muted">
                    <i class="bi bi-clock"></i> Rates updated: <?= date('Y-m-d H:i', strtotime($lastRateUpdate)) ?>
                </span>
            </div>
            <?php endif; ?>
        </form>
    </div>
</div>

<!-- Summary Cards -->
<div class="row g-3 mb-4">
    <div class="col-md-6">
        <div class="card bg-success bg-gradient text-white h-100">
            <div class="card-body">
                <h6 class="text-uppercase small opacity-75">Realised Profit (<?= $fromDate ?> to <?= $toDate ?>)</h6>
                <h2 class="mb-0"><?= number_format($realised, 2) ?> KES</h2>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card bg-primary bg-gradient text-white h-100">
            <div class="card-body">
                <h6 class="text-uppercase small opacity-75">Unrealised Profit (Current Positions)</h6>
                <h2 class="mb-0"><?= number_format($unrealisedTotal, 2) ?> KES</h2>
            </div>
        </div>
    </div>
</div>

<!-- Unrealised P&L Details -->
<div class="card">
    <div class="card-header bg-white">
        <h5 class="mb-0"><i class="bi bi-table me-2"></i>Open Positions & Unrealised P&L</h5>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Currency</th>
                        <th class="text-end">Balance</th>
                        <th class="text-end">Avg Cost (KES/unit)</th>
                        <th class="text-end">Current Rate (KES/unit)</th>
                        <th class="text-end">Market Value (KES)</th>
                        <th class="text-end">Unrealised P&L (KES)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($unrealisedDetails)): ?>
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">
                                <i class="bi bi-currency-exchange fs-1 d-block mb-2"></i>
                                No open positions.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($unrealisedDetails as $d): ?>
                        <tr>
                            <td><span class="badge bg-secondary"><?= htmlspecialchars($d['code']) ?></span></td>
                            <td class="text-end"><?= number_format($d['balance'], 2) ?></td>
                            <td class="text-end"><?= number_format($d['avg_cost'], 4) ?></td>
                            <td class="text-end"><?= number_format($d['current_rate'], 4) ?></td>
                            <td class="text-end"><?= number_format($d['market_value'], 2) ?></td>
                            <td class="text-end fw-bold <?= $d['unrealised'] >= 0 ? 'text-success' : 'text-danger' ?>">
                                <?= number_format($d['unrealised'], 2) ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
                <?php if (!empty($unrealisedDetails)): ?>
                <tfoot class="table-light">
                    <tr>
                        <th colspan="5" class="text-end">Total Unrealised P&L:</th>
                        <th class="text-end <?= $unrealisedTotal >= 0 ? 'text-success' : 'text-danger' ?>">
                            <?= number_format($unrealisedTotal, 2) ?> KES
                        </th>
                    </tr>
                </tfoot>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

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