<?php
    $type_labels = ['bank' => 'Compte Bancaire', 'central' => 'Caisse Centrale', 'petty' => 'Petite Caisse'];
    $type_colors = ['bank' => 'primary', 'central' => 'success', 'petty' => 'warning'];
    $color = $type_colors[$cashbox->type] ?? 'secondary';
?>

<!-- En-tête caisse -->
<div class="row mb-4">
    <div class="col-md-8">
        <a href="<?= site_url('cashboxes'); ?>" class="text-decoration-none text-muted small">
            <i class="bi bi-arrow-left"></i> Retour aux caisses
        </a>
        <h4 class="mt-1 mb-0"><?= htmlspecialchars($cashbox->name); ?></h4>
        <span class="badge bg-<?= $color; ?>"><?= $type_labels[$cashbox->type]; ?></span>
        <?php if ($cashbox->location_name): ?>
        <span class="badge bg-light text-dark"><i class="bi bi-geo-alt"></i> <?= $cashbox->location_name; ?></span>
        <?php endif; ?>
        <span class="badge bg-light text-dark"><code><?= $cashbox->code; ?></code></span>
    </div>
    <div class="col-md-4 text-end">
        <div class="card border-<?= $color; ?> border-2">
            <div class="card-body py-2">
                <small class="text-muted">Solde actuel</small>
                <div class="h3 fw-bold mb-0 <?= $cashbox->current_balance < 0 ? 'text-danger' : 'text-' . $color; ?>">
                    <?= number_format($cashbox->current_balance, 0, ',', ' '); ?> KMF
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Info dernière clôture -->
<?php if ($last_closing): ?>
<div class="alert alert-info py-2">
    <i class="bi bi-info-circle"></i>
    Dernière clôture confirmée : <strong><?= date('d/m/Y', strtotime($last_closing->period_end)); ?></strong>
    — Solde : <strong><?= number_format($last_closing->closing_balance, 0, ',', ' '); ?> KMF</strong>
    <?php if ($last_closing->difference !== null && $last_closing->difference != 0): ?>
    — Écart physique : <span class="text-<?= $last_closing->difference < 0 ? 'danger' : 'success'; ?>">
        <?= number_format($last_closing->difference, 0, ',', ' '); ?> KMF</span>
    <?php endif; ?>
</div>
<?php endif; ?>

<!-- Filtre de période -->
<div class="card shadow-sm mb-4">
    <div class="card-body py-2">
        <?= form_open('cashboxes/view/' . $cashbox->id, ['method' => 'get', 'class' => 'row g-2 align-items-end']); ?>
            <div class="col-md-3">
                <label class="form-label small mb-1">Du</label>
                <input type="date" name="date_from" class="form-control form-control-sm" 
                       value="<?= $date_from; ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label small mb-1">Au</label>
                <input type="date" name="date_to" class="form-control form-control-sm" 
                       value="<?= $date_to; ?>">
            </div>
            <div class="col-md-3">
                <button class="btn btn-sm btn-primary"><i class="bi bi-search"></i> Afficher</button>
            </div>
        <?= form_close(); ?>
    </div>
</div>

<!-- Tableau des mouvements -->
<div class="card shadow-sm">
    <div class="card-header bg-white">
        <h6 class="mb-0"><i class="bi bi-list-ul"></i> Mouvements du <?= date('d/m/Y', strtotime($date_from)); ?> au <?= date('d/m/Y', strtotime($date_to)); ?></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>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($movements)): ?>
                <tr><td colspan="6" class="text-center text-muted py-4">Aucun mouvement sur cette période.</td></tr>
                <?php else: ?>
                <?php foreach ($movements as $mv): ?>
                <?php
                    $type_badges = [
                        'expense'      => ['danger',  'Dépense',      'receipt'],
                        'transfer_in'  => ['success', 'Transfert +',  'arrow-down-circle'],
                        'transfer_out' => ['warning', 'Transfert -',  'arrow-up-circle'],
                    ];
                    $badge = $type_badges[$mv['move_type']] ?? ['secondary', $mv['move_type'], 'circle'];
                ?>
                <tr>
                    <td class="small"><?= date('d/m/Y', strtotime($mv['move_date'])); ?></td>
                    <td><code class="small"><?= htmlspecialchars($mv['reference']); ?></code></td>
                    <td class="small"><?= htmlspecialchars($mv['description']); ?></td>
                    <td>
                        <span class="badge bg-<?= $badge[0]; ?> bg-opacity-10 text-<?= $badge[0]; ?>">
                            <i class="bi bi-<?= $badge[2]; ?>"></i> <?= $badge[1]; ?>
                        </span>
                    </td>
                    <td class="text-end fw-semibold <?= $mv['direction'] === 'debit' ? 'text-danger' : ''; ?>">
                        <?= $mv['direction'] === 'debit' ? number_format($mv['amount'], 0, ',', ' ') : ''; ?>
                    </td>
                    <td class="text-end fw-semibold <?= $mv['direction'] === 'credit' ? 'text-success' : ''; ?>">
                        <?= $mv['direction'] === 'credit' ? number_format($mv['amount'], 0, ',', ' ') : ''; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
