<div class="row justify-content-center">
    <div class="col-lg-8">
        <a href="<?= site_url('cashboxes/closings'); ?>" class="text-decoration-none text-muted small">
            <i class="bi bi-arrow-left"></i> Retour aux clôtures
        </a>

        <div class="card shadow-sm mt-2">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="bi bi-lock"></i> Clôture #<?= $closing->id; ?>
                </h5>
                <?php if ($closing->status === 'confirmed'): ?>
                <span class="badge bg-success fs-6"><i class="bi bi-check-circle"></i> Confirmée</span>
                <?php else: ?>
                <span class="badge bg-warning text-dark fs-6"><i class="bi bi-pencil"></i> Brouillon</span>
                <?php endif; ?>
            </div>
            <div class="card-body">
                <!-- Infos générales -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" style="width:40%">Caisse</td>
                                <td class="fw-semibold"><?= htmlspecialchars($closing->cashbox_name); ?> <code class="small"><?= $closing->cashbox_code; ?></code></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Période</td>
                                <td><?= date('d/m/Y', strtotime($closing->period_start)); ?> → <?= date('d/m/Y', strtotime($closing->period_end)); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Clôturé par</td>
                                <td><?= htmlspecialchars($closing->closed_by_name); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Créé le</td>
                                <td><?= date('d/m/Y H:i', strtotime($closing->created_at)); ?></td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <!-- Résumé financier -->
                        <div class="card bg-light">
                            <div class="card-body py-2">
                                <table class="table table-sm table-borderless mb-0">
                                    <tr>
                                        <td class="text-muted">Solde initial</td>
                                        <td class="text-end fw-semibold"><?= number_format($closing->opening_balance, 0, ',', ' '); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-success"><i class="bi bi-plus-circle"></i> Entrées</td>
                                        <td class="text-end text-success">+ <?= number_format($closing->total_in, 0, ',', ' '); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-danger"><i class="bi bi-dash-circle"></i> Sorties</td>
                                        <td class="text-end text-danger">- <?= number_format($closing->total_out, 0, ',', ' '); ?></td>
                                    </tr>
                                    <tr class="border-top">
                                        <td class="fw-bold">Solde final</td>
                                        <td class="text-end fw-bold fs-5"><?= number_format($closing->closing_balance, 0, ',', ' '); ?> KMF</td>
                                    </tr>
                                    <?php if ($closing->physical_count !== null): ?>
                                    <tr class="border-top">
                                        <td class="text-muted">Comptage physique</td>
                                        <td class="text-end"><?= number_format($closing->physical_count, 0, ',', ' '); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Écart</td>
                                        <td class="text-end fw-semibold <?= $closing->difference < 0 ? 'text-danger' : ($closing->difference > 0 ? 'text-warning' : 'text-success'); ?>">
                                            <?php if ($closing->difference == 0): ?>
                                                <i class="bi bi-check-circle text-success"></i> Aucun écart
                                            <?php else: ?>
                                                <?= ($closing->difference > 0 ? '+' : '') . number_format($closing->difference, 0, ',', ' '); ?> KMF
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endif; ?>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>

                <?php if ($closing->notes): ?>
                <div class="alert alert-secondary py-2">
                    <strong>Notes :</strong> <?= nl2br(htmlspecialchars($closing->notes)); ?>
                </div>
                <?php endif; ?>

                <?php if ($closing->status === 'draft' && $this->auth_lib->has_permission('cashbox_close')): ?>
                <hr>
                <div class="d-flex gap-2">
                    <a href="<?= site_url('cashboxes/confirm_closing/' . $closing->id); ?>" 
                       class="btn btn-success"
                       onclick="return confirm('Confirmer définitivement cette clôture ?');">
                        <i class="bi bi-check-lg"></i> Confirmer la clôture
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
