<?php
// =============================================
// VUE RECHERCHE EXPEDITIONS FRET
// application/views/fret/recherche.php
// =============================================
?>

<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card mb-4">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-10">
                        <div class="input-group input-group-lg">
                            <span class="input-group-text"><i class="bi bi-search"></i></span>
                            <input type="text" class="form-control" name="q" value="<?= htmlspecialchars($term ?? '') ?>" 
                                   placeholder="N° expédition, nom, téléphone..." autofocus>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary btn-lg w-100">Rechercher</button>
                    </div>
                </form>
            </div>
        </div>

        <?php if ($term): ?>
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    Résultats pour "<?= htmlspecialchars($term) ?>"
                    <span class="badge bg-secondary"><?= count($results) ?></span>
                </h5>
            </div>
            <div class="card-body p-0">
                <?php if (empty($results)): ?>
                <div class="text-center py-5 text-muted">
                    <i class="bi bi-search" style="font-size: 3rem;"></i>
                    <p class="mt-2">Aucune expédition trouvée</p>
                </div>
                <?php else: ?>
                <div class="list-group list-group-flush">
                    <?php foreach ($results as $exp): ?>
                    <a href="<?= base_url('fret/details/' . $exp->id) ?>" class="list-group-item list-group-item-action">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="mb-1"><?= $exp->numero_expedition ?></h6>
                                <p class="mb-1">
                                    <strong>Exp:</strong> <?= $exp->expediteur_nom ?> (<?= $exp->expediteur_telephone ?>)
                                    <br>
                                    <strong>Dest:</strong> <?= $exp->destinataire_nom ?> (<?= $exp->destinataire_telephone ?>)
                                </p>
                                <small class="text-muted">
                                    <?= date('d/m/Y', strtotime($exp->date_voyage)) ?> - 
                                    <?= $exp->port_depart ?> → <?= $exp->port_arrivee ?>
                                </small>
                            </div>
                            <div class="text-end">
                                <?php
                                $badge_class = 'secondary';
                                switch ($exp->statut) {
                                    case 'PAYE': $badge_class = 'success'; break;
                                    case 'ENREGISTRE': $badge_class = 'warning'; break;
                                    case 'CHARGE': $badge_class = 'info'; break;
                                    case 'LIVRE': $badge_class = 'success'; break;
                                    case 'ANNULE': $badge_class = 'danger'; break;
                                }
                                ?>
                                <span class="badge bg-<?= $badge_class ?>"><?= $exp->statut ?></span>
                                <br>
                                <strong class="text-success"><?= number_format($exp->tarif_total, 0, ',', ' ') ?> FC</strong>
                            </div>
                        </div>
                    </a>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
