<!-- En-tête avec solde courant -->
<div class="row mb-4">
    <div class="col-md-6">
        <p class="text-muted mb-0">Owner Running Account — Compagnie Maritime</p>
    </div>
    <div class="col-md-6">
        <div class="row g-2">
            <div class="col-4">
                <div class="card bg-success text-white shadow-sm">
                    <div class="card-body py-2 px-3 text-center">
                        <small class="opacity-75">Avances reçues</small>
                        <div class="h6 mb-0 fw-bold"><?= number_format($totals->total_credits ?? 0, 0, ',', ' '); ?></div>
                    </div>
                </div>
            </div>
            <div class="col-4">
                <div class="card bg-danger text-white shadow-sm">
                    <div class="card-body py-2 px-3 text-center">
                        <small class="opacity-75">Dépenses imputées</small>
                        <div class="h6 mb-0 fw-bold"><?= number_format($totals->total_debits ?? 0, 0, ',', ' '); ?></div>
                    </div>
                </div>
            </div>
            <div class="col-4">
                <div class="card bg-primary text-white shadow-sm">
                    <div class="card-body py-2 px-3 text-center">
                        <small class="opacity-75">Solde courant</small>
                        <div class="h5 mb-0 fw-bold"><?= number_format($current_balance, 0, ',', ' '); ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Filtres -->
<div class="card shadow-sm mb-4">
    <div class="card-body py-2">
        <?= form_open('statements', ['method' => 'get', 'class' => 'row g-2 align-items-end']); ?>
            <div class="col-md-2">
                <label class="form-label small mb-1">Type</label>
                <select name="type" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <option value="advance"    <?= ($filters['type'] ?? '') == 'advance'    ? 'selected' : ''; ?>>Avances</option>
                    <option value="expense"    <?= ($filters['type'] ?? '') == 'expense'    ? 'selected' : ''; ?>>Dépenses</option>
                    <option value="adjustment" <?= ($filters['type'] ?? '') == 'adjustment' ? 'selected' : ''; ?>>Ajustements</option>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Du</label>
                <input type="date" name="date_from" class="form-control form-control-sm" value="<?= $filters['date_from'] ?? ''; ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Au</label>
                <input type="date" name="date_to" class="form-control form-control-sm" value="<?= $filters['date_to'] ?? ''; ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Recherche</label>
                <input type="text" name="search" class="form-control form-control-sm" placeholder="Réf, description..." value="<?= htmlspecialchars($filters['search'] ?? ''); ?>">
            </div>
            <div class="col-md-4 d-flex gap-2">
                <button class="btn btn-sm btn-primary"><i class="bi bi-search"></i> Filtrer</button>
                <a href="<?= site_url('statements'); ?>" class="btn btn-sm btn-outline-secondary">Reset</a>

                <?php if ($this->auth_lib->has_permission('partner_manage')): ?>
                <div class="ms-auto d-flex gap-1">
                    <a href="<?= site_url('statements/cash_advance'); ?>" class="btn btn-sm btn-success">
                        <i class="bi bi-plus-circle"></i> Cash Advance
                    </a>
                    <a href="<?= site_url('statements/adjustment'); ?>" class="btn btn-sm btn-outline-warning">
                        <i class="bi bi-pencil-square"></i> Ajustement
                    </a>
                </div>
                <?php endif; ?>
            </div>
        <?= form_close(); ?>
    </div>
</div>

<!-- Relevé de compte -->
<div class="card shadow-sm">
    <div class="card-header bg-white">
        <h6 class="mb-0"><i class="bi bi-journal-text"></i> Relevé du Compte Partenaire</h6>
    </div>
    <div class="table-responsive">
        <table class="table table-sm table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>Date</th>
                    <th>Référence</th>
                    <th>Description</th>
                    <th>Type</th>
                    <th class="text-end">Débit</th>
                    <th class="text-end">Crédit</th>
                    <th class="text-end">Solde</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($transactions)): ?>
                <tr><td colspan="7" class="text-center text-muted py-4">
                    Aucune transaction enregistrée.
                    <?php if ($this->auth_lib->has_permission('partner_manage')): ?>
                    <br><a href="<?= site_url('statements/cash_advance'); ?>">Enregistrer le premier cash advance</a>
                    <?php endif; ?>
                </td></tr>
                <?php else: ?>
                <?php foreach ($transactions as $tx): ?>
                <?php
                    $type_map = [
                        'advance'    => ['success', 'arrow-down-circle', 'Avance'],
                        'expense'    => ['danger',  'receipt',           'Dépense'],
                        'adjustment' => ['warning', 'pencil-square',    'Ajust.'],
                    ];
                    $t = $type_map[$tx->type] ?? ['secondary', 'circle', $tx->type];
                ?>
                <tr>
                    <td class="small"><?= date('d/m/Y', strtotime($tx->transaction_date)); ?></td>
                    <td>
                        <a href="<?= site_url('statements/view/' . $tx->id); ?>" class="text-decoration-none">
                            <code><?= htmlspecialchars($tx->reference); ?></code>
                        </a>
                    </td>
                    <td class="small"><?= htmlspecialchars($tx->description); ?></td>
                    <td>
                        <span class="badge bg-<?= $t[0]; ?> bg-opacity-10 text-<?= $t[0]; ?>">
                            <i class="bi bi-<?= $t[1]; ?>"></i> <?= $t[2]; ?>
                        </span>
                    </td>
                    <td class="text-end <?= $tx->debit > 0 ? 'text-danger fw-semibold' : 'text-muted'; ?>">
                        <?= $tx->debit > 0 ? number_format($tx->debit, 0, ',', ' ') : '—'; ?>
                    </td>
                    <td class="text-end <?= $tx->credit > 0 ? 'text-success fw-semibold' : 'text-muted'; ?>">
                        <?= $tx->credit > 0 ? number_format($tx->credit, 0, ',', ' ') : '—'; ?>
                    </td>
                    <td class="text-end fw-bold <?= $tx->running_balance < 0 ? 'text-danger' : ''; ?>">
                        <?= number_format($tx->running_balance, 0, ',', ' '); ?>
                    </td>
                </tr>
                <?php endforeach; ?>

                <!-- Ligne de total -->
                <tr class="table-light fw-bold">
                    <td colspan="4" class="text-end">TOTAUX (période affichée)</td>
                    <td class="text-end text-danger"><?= number_format($totals->total_debits ?? 0, 0, ',', ' '); ?></td>
                    <td class="text-end text-success"><?= number_format($totals->total_credits ?? 0, 0, ',', ' '); ?></td>
                    <td class="text-end"><?= number_format($current_balance, 0, ',', ' '); ?> KMF</td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
