<!-- En-tête avec total -->
<div class="row mb-4">
    <div class="col-md-8">
        <p class="text-muted mb-0">Position de trésorerie globale</p>
    </div>
    <div class="col-md-4 text-end">
        <div class="card bg-primary text-white shadow">
            <div class="card-body py-2 px-3">
                <small class="opacity-75">Trésorerie totale</small>
                <div class="h4 mb-0 fw-bold"><?= number_format($total_balance, 0, ',', ' '); ?> KMF</div>
            </div>
        </div>
    </div>
</div>

<!-- Cartes des caisses -->
<div class="row g-3 mb-4">
    <?php foreach ($cashboxes as $cb): ?>
    <?php
        $type_icons  = ['bank' => 'bank', 'central' => 'safe2', 'petty' => 'wallet2'];
        $type_colors = ['bank' => 'primary', 'central' => 'success', 'petty' => 'warning'];
        $type_labels = ['bank' => 'Compte Bancaire', 'central' => 'Caisse Centrale', 'petty' => 'Petite Caisse'];
        $icon  = $type_icons[$cb->type]  ?? 'cash-stack';
        $color = $type_colors[$cb->type] ?? 'secondary';
    ?>
    <div class="col-md-6 col-lg-3">
        <div class="card shadow-sm border-start border-<?= $color; ?> border-4 h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <div>
                        <span class="badge bg-<?= $color; ?> bg-opacity-10 text-<?= $color; ?> mb-1">
                            <?= $type_labels[$cb->type]; ?>
                        </span>
                        <h6 class="mb-0"><?= htmlspecialchars($cb->name); ?></h6>
                        <?php if ($cb->location_name): ?>
                        <small class="text-muted"><i class="bi bi-geo-alt"></i> <?= $cb->location_name; ?></small>
                        <?php endif; ?>
                    </div>
                    <i class="bi bi-<?= $icon; ?> fs-3 text-<?= $color; ?> opacity-50"></i>
                </div>
                <div class="h4 fw-bold mb-2 <?= $cb->current_balance < 0 ? 'text-danger' : ''; ?>">
                    <?= number_format($cb->current_balance, 0, ',', ' '); ?>
                    <small class="fs-6 fw-normal text-muted">KMF</small>
                </div>
                <div class="d-flex gap-1">
                    <a href="<?= site_url('cashboxes/view/' . $cb->id); ?>" class="btn btn-sm btn-outline-<?= $color; ?>">
                        <i class="bi bi-eye"></i> Détails
                    </a>
                    <?php if ($this->auth_lib->has_permission('cashbox_close')): ?>
                    <a href="<?= site_url('cashboxes/close/' . $cb->id); ?>" class="btn btn-sm btn-outline-secondary">
                        <i class="bi bi-lock"></i> Clôturer
                    </a>
                    <?php endif; ?>
                    <?php if ($this->auth_lib->has_permission('cashbox_manage')): ?>
                    <a href="<?= site_url('cashboxes/adjust_balance/' . $cb->id); ?>" class="btn btn-sm btn-outline-warning" title="Ajuster le solde">
                        <i class="bi bi-pencil-square"></i>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<!-- Actions rapides -->
<div class="row g-3">
    <?php if ($this->auth_lib->has_permission('transfer_create')): ?>
    <div class="col-md-4">
        <a href="<?= site_url('transfers/create'); ?>" class="card shadow-sm text-decoration-none h-100">
            <div class="card-body text-center py-3">
                <i class="bi bi-arrow-left-right fs-2 text-info"></i>
                <h6 class="mt-2 mb-0">Nouveau Transfert</h6>
                <small class="text-muted">Approvisionner une caisse</small>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if ($this->auth_lib->has_permission('cashbox_close')): ?>
    <div class="col-md-4">
        <a href="<?= site_url('cashboxes/closings'); ?>" class="card shadow-sm text-decoration-none h-100">
            <div class="card-body text-center py-3">
                <i class="bi bi-journal-check fs-2 text-success"></i>
                <h6 class="mt-2 mb-0">Clôtures</h6>
                <small class="text-muted">Historique des clôtures</small>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if ($this->auth_lib->has_permission('transfer_view')): ?>
    <div class="col-md-4">
        <a href="<?= site_url('transfers'); ?>" class="card shadow-sm text-decoration-none h-100">
            <div class="card-body text-center py-3">
                <i class="bi bi-list-check fs-2 text-primary"></i>
                <h6 class="mt-2 mb-0">Transferts</h6>
                <small class="text-muted">Voir tous les transferts</small>
            </div>
        </a>
    </div>
    <?php endif; ?>
</div>
