<?php
    $status_map = [
        'draft'     => ['secondary', 'pencil',          'Brouillon'],
        'pending'   => ['warning',   'hourglass-split', 'En attente de validation'],
        'validated' => ['success',   'check-circle',    'Validée'],
        'rejected'  => ['danger',    'x-circle',        'Rejetée'],
        'batched'   => ['info',      'collection',      'Incluse dans un batch'],
    ];
    $s = $status_map[$expense->status] ?? ['secondary', 'circle', $expense->status];
?>

<div class="row justify-content-center">
    <div class="col-lg-9">
        <a href="<?= site_url('expenses'); ?>" class="text-decoration-none text-muted small">
            <i class="bi bi-arrow-left"></i> Retour aux dépenses
        </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-receipt"></i> <?= $expense->expense_id; ?>
                </h5>
                <span class="badge bg-<?= $s[0]; ?> <?= $s[0] == 'warning' ? 'text-dark' : ''; ?> fs-6">
                    <i class="bi bi-<?= $s[1]; ?>"></i> <?= $s[2]; ?>
                </span>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-7">
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" style="width:35%">ID Dépense</td>
                                <td class="fw-semibold"><code><?= $expense->expense_id; ?></code></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Date de facturation</td>
                                <td><?= date('d/m/Y', strtotime($expense->expense_date)); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Date opération</td>
                                <td><?= $expense->operation_date ? date('d/m/Y', strtotime($expense->operation_date)) : '—'; ?></td>
                            </tr>
                           
                            <tr>
                                <td class="text-muted">Catégorie</td>
                                <td><span class="badge bg-light text-dark"><?= $expense->category_label; ?></span></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Localisation</td>
                                <td><i class="bi bi-geo-alt"></i> <?= $expense->location_name; ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Fournisseur</td>
                                <td class="fw-semibold"><?= htmlspecialchars($expense->supplier); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Description</td>
                                <td><?= nl2br(htmlspecialchars($expense->description)); ?></td>
                            </tr>
                            <?php if ($expense->invoice_number): ?>
                            <tr>
                                <td class="text-muted">N° Facture</td>
                                <td><code><?= htmlspecialchars($expense->invoice_number); ?></code></td>
                            </tr>
                            <?php endif; ?>
                            <tr>
                                <td class="text-muted">Caisse</td>
                                <td><?= $expense->cashbox_name ?? '—'; ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Créé par</td>
                                <td><?= htmlspecialchars($expense->created_by_name); ?> <small class="text-muted">(<?= date('d/m/Y H:i', strtotime($expense->created_at)); ?>)</small></td>
                            </tr>
                            <?php if ($expense->validated_by_name): ?>
                            <tr>
                                <td class="text-muted"><?= $expense->status === 'rejected' ? 'Rejeté par' : 'Validé par'; ?></td>
                                <td><?= htmlspecialchars($expense->validated_by_name); ?> <small class="text-muted">(<?= date('d/m/Y H:i', strtotime($expense->validated_at)); ?>)</small></td>
                            </tr>
                            <?php endif; ?>
                        </table>
                    </div>
                    <div class="col-md-5">
                        <div class="card bg-light mb-3">
                            <div class="card-body text-center py-3">
                                <small class="text-muted">Montant</small>
                                <div class="h2 fw-bold mb-0"><?= number_format($expense->amount, 0, ',', ' '); ?> <small class="fs-5">KMF</small></div>
                            </div>
                        </div>

                        <?php if ($expense->invoice_path): ?>
                        <div class="card mb-3">
                            <div class="card-body py-2">
                                <strong><i class="bi bi-paperclip"></i> Justificatif</strong>
                                <div class="mt-1">
                                    <a href="<?= base_url($expense->invoice_path); ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-file-earmark"></i> Voir le document
                                    </a>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>

                        <?php if ($expense->status === 'rejected' && $expense->rejection_reason): ?>
                        <div class="alert alert-danger py-2">
                            <strong><i class="bi bi-x-circle"></i> Motif du rejet :</strong><br>
                            <?= nl2br(htmlspecialchars($expense->rejection_reason)); ?>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <?php if ($expense->status === 'pending'): ?>
                <hr>
                <div class="d-flex gap-2 flex-wrap">
                    <?php if ($this->auth_lib->has_permission('expense_validate')): ?>
                    <a href="<?= site_url('expenses/validate/' . $expense->id); ?>"
                       class="btn btn-success"
                       onclick="return confirm('Valider cette dépense ? La caisse sera débitée de <?= number_format($expense->amount, 0, ',', ' '); ?> KMF.');">
                        <i class="bi bi-check-lg"></i> Valider
                    </a>

                    <button class="btn btn-outline-danger" data-bs-toggle="collapse" data-bs-target="#rejectForm">
                        <i class="bi bi-x-lg"></i> Rejeter
                    </button>
                    <?php endif; ?>

                    <?php if ($this->auth_lib->has_permission('expense_edit')): ?>
                    <a href="<?= site_url('expenses/edit/' . $expense->id); ?>" class="btn btn-outline-primary">
                        <i class="bi bi-pencil"></i> Modifier
                    </a>
                    <?php endif; ?>
                </div>

                <?php if ($this->auth_lib->has_permission('expense_validate')): ?>
                <div class="collapse mt-3" id="rejectForm">
                    <div class="card border-danger">
                        <div class="card-body">
                            <?= form_open('expenses/reject/' . $expense->id); ?>
                                <div class="mb-2">
                                    <label class="form-label">Motif du rejet <span class="text-danger">*</span></label>
                                    <textarea name="rejection_reason" class="form-control" rows="2" required
                                              placeholder="Raison du rejet..."></textarea>
                                </div>
                                <button type="submit" class="btn btn-danger btn-sm"
                                        onclick="return confirm('Confirmer le rejet ?');">
                                    <i class="bi bi-x-lg"></i> Confirmer le rejet
                                </button>
                            <?= form_close(); ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                <?php endif; ?>

                <?php if ($expense->status === 'draft' && $this->auth_lib->has_permission('expense_edit')): ?>
                <hr>
                <a href="<?= site_url('expenses/edit/' . $expense->id); ?>" class="btn btn-outline-primary">
                    <i class="bi bi-pencil"></i> Modifier
                </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
