<div class="row justify-content-center">
    <div class="col-lg-8">
        <a href="<?= site_url('transfers'); ?>" class="text-decoration-none text-muted small">
            <i class="bi bi-arrow-left"></i> Retour aux transferts
        </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-arrow-left-right"></i> Transfert <?= htmlspecialchars($transfer->reference); ?>
                </h5>
                <?php
                    $status_map = [
                        'pending'   => ['warning', 'text-dark', 'hourglass-split', 'En attente de validation'],
                        'validated' => ['success', '',          'check-circle',    'Validé'],
                        'rejected'  => ['danger',  '',          'x-circle',        'Rejeté'],
                    ];
                    $s = $status_map[$transfer->status] ?? ['secondary', '', 'circle', $transfer->status];
                ?>
                <span class="badge bg-<?= $s[0]; ?> <?= $s[1]; ?> fs-6">
                    <i class="bi bi-<?= $s[2]; ?>"></i> <?= $s[3]; ?>
                </span>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" style="width:40%">Référence</td>
                                <td class="fw-semibold"><code><?= $transfer->reference; ?></code></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Date</td>
                                <td><?= date('d/m/Y', strtotime($transfer->transfer_date)); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Créé par</td>
                                <td><?= htmlspecialchars($transfer->created_by_name); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Créé le</td>
                                <td class="small"><?= date('d/m/Y H:i', strtotime($transfer->created_at)); ?></td>
                            </tr>
                            <?php if ($transfer->validated_by_name): ?>
                            <tr>
                                <td class="text-muted"><?= $transfer->status === 'validated' ? 'Validé par' : 'Rejeté par'; ?></td>
                                <td><?= htmlspecialchars($transfer->validated_by_name); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Le</td>
                                <td class="small"><?= date('d/m/Y H:i', strtotime($transfer->validated_at)); ?></td>
                            </tr>
                            <?php endif; ?>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <!-- Visualisation du transfert -->
                        <div class="card bg-light">
                            <div class="card-body text-center py-3">
                                <div class="d-flex align-items-center justify-content-center gap-3">
                                    <div class="text-center">
                                        <div class="badge bg-danger bg-opacity-10 text-danger mb-1 px-3 py-2">
                                            <i class="bi bi-safe2 fs-5"></i>
                                        </div>
                                        <div class="small fw-semibold"><?= htmlspecialchars($transfer->source_name); ?></div>
                                        <code class="small"><?= $transfer->source_code; ?></code>
                                    </div>

                                    <div class="text-center">
                                        <div class="h4 fw-bold text-primary mb-0">
                                            <?= number_format($transfer->amount, 0, ',', ' '); ?>
                                        </div>
                                        <small class="text-muted">KMF</small>
                                        <div><i class="bi bi-arrow-right fs-3 text-primary"></i></div>
                                    </div>

                                    <div class="text-center">
                                        <div class="badge bg-success bg-opacity-10 text-success mb-1 px-3 py-2">
                                            <i class="bi bi-wallet2 fs-5"></i>
                                        </div>
                                        <div class="small fw-semibold"><?= htmlspecialchars($transfer->dest_name); ?></div>
                                        <code class="small"><?= $transfer->dest_code; ?></code>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

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

                <?php if ($transfer->receipt_path): ?>
                <div class="mt-3">
                    <strong>Justificatif :</strong>
                    <a href="<?= base_url($transfer->receipt_path); ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                        <i class="bi bi-file-earmark"></i> Voir le document
                    </a>
                </div>
                <?php endif; ?>

                <!-- Actions de validation -->
                <?php if ($transfer->status === 'pending' && $this->auth_lib->has_permission('transfer_validate')): ?>
                <hr>
                <div class="d-flex gap-2">
                    <a href="<?= site_url('transfers/validate/' . $transfer->id); ?>" 
                       class="btn btn-success"
                       onclick="return confirm('Valider ce transfert ? Les soldes des caisses seront mis à jour.');">
                        <i class="bi bi-check-lg"></i> Valider le transfert
                    </a>
                    <a href="<?= site_url('transfers/reject/' . $transfer->id); ?>" 
                       class="btn btn-outline-danger"
                       onclick="return confirm('Rejeter ce transfert ?');">
                        <i class="bi bi-x-lg"></i> Rejeter
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
