<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Devis <?= htmlspecialchars($devis['numero']) ?></h1>
        <p class="text-muted mb-0"><?= htmlspecialchars($devis['client_nom']) ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=devis" class="btn btn-outline-secondary">Retour</a>
        <a href="index.php?page=devis_edit&id=<?= (int)$devis['id'] ?>" class="btn btn-outline-primary"><i class="bi bi-pencil me-1"></i> Modifier</a>
        <a href="index.php?page=devis_pdf&id=<?= (int)$devis['id'] ?>" target="_blank" class="btn btn-outline-primary"><i class="bi bi-file-earmark-pdf me-1"></i> Générer le PDF</a>
        <a href="index.php?page=document_center_devis_prepare&id=<?= (int)$devis['id'] ?>" class="btn btn-outline-primary"><i class="bi bi-envelope me-1"></i> Envoyer</a>
        <form method="post" action="index.php?page=devis_duplicate" onsubmit="return confirm('Dupliquer ce devis ?');">
            <input type="hidden" name="id" value="<?= (int)$devis['id'] ?>">
            <button type="submit" class="btn btn-outline-secondary">Dupliquer</button>
        </form>
        <form method="post" action="index.php?page=factures_from_devis" onsubmit="return confirm('Créer une facture directement depuis ce devis ?');">
            <input type="hidden" name="devis_id" value="<?= (int)$devis['id'] ?>">
            <button type="submit" class="btn btn-success"><i class="bi bi-receipt me-1"></i> Créer une facture</button>
        </form>
        <form method="post" action="index.php?page=devis_transform_bl" onsubmit="return confirm('Transformer ce devis en bon de livraison ? Le stock sera décrémenté.');">
            <input type="hidden" name="devis_id" value="<?= (int)$devis['id'] ?>">
            <button type="submit" class="btn darlink-btn-primary">Transformer en BL</button>
        </form>
    </div>
</div>

<?php $cycleType = 'DEVIS'; require __DIR__ . '/../partials/document_cycle.php'; ?>


<?php if (!empty($lastMailDelivery)): ?>
<div class="alert alert-info d-flex align-items-center gap-3 mb-4">
    <i class="bi bi-envelope-check fs-4"></i>
    <div>
        <strong>Dernier envoi :</strong>
        <?= htmlspecialchars((string)$lastMailDelivery['date_envoi']) ?>
        à <?= htmlspecialchars((string)$lastMailDelivery['destinataire']) ?>
        par <?= htmlspecialchars((string)$lastMailDelivery['envoye_par_nom']) ?>.
    </div>
</div>
<?php endif; ?>

<div class="darlink-card p-4 mb-4">
    <div class="row g-3">
        <div class="col-md-3"><strong>Date :</strong><br><?= htmlspecialchars($devis['date_devis']) ?></div>
        <div class="col-md-3"><strong>Validité :</strong><br><?= htmlspecialchars($devis['validite'] ?? '-') ?></div>
        <div class="col-md-3"><strong>Statut :</strong><br><span class="badge text-bg-light border"><?= htmlspecialchars($devis['statut']) ?></span></div>
        <div class="col-md-3"><strong>Marge interne :</strong><br><?= number_format((float)$devis['marge_pourcentage'], 2, ',', ' ') ?> %</div>
        <div class="col-12"><strong>Observations :</strong><br><?= nl2br(htmlspecialchars($devis['observations'] ?: '-')) ?></div>
    </div>
</div>

<div class="darlink-card p-4">
    <div class="table-responsive">
        <table class="table table-hover align-middle darlink-table">
            <thead>
            <tr>
                <th>Réf.</th><th>Désignation</th><th>Unité</th><th class="text-end">Qté</th><th class="text-end">Prix HT</th><th class="text-end">Remise</th><th class="text-end">TVA</th><th class="text-end">Total HT</th><th class="text-end">Total TTC</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($lignes as $ligne): ?>
                <tr>
                    <td><?= htmlspecialchars($ligne['reference']) ?></td>
                    <td><?= htmlspecialchars($ligne['designation']) ?></td>
                    <td><?= htmlspecialchars($ligne['unite']) ?></td>
                    <td class="text-end"><?= number_format((float)$ligne['quantite'], 0, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)$ligne['prix_vente_ht'], 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)$ligne['remise'], 2, ',', ' ') ?> %</td>
                    <td class="text-end"><?= number_format((float)$ligne['taux_tva'], 2, ',', ' ') ?> %</td>
                    <td class="text-end"><?= number_format((float)$ligne['total_ht'], 2, ',', ' ') ?></td>
                    <td class="text-end"><?= number_format((float)$ligne['total_ttc'], 2, ',', ' ') ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
            <tfoot>
            <tr><th colspan="7" class="text-end">Total HT</th><th colspan="2" class="text-end"><?= number_format((float)$devis['total_ht'], 2, ',', ' ') ?> MAD</th></tr>
            <tr><th colspan="7" class="text-end">TVA</th><th colspan="2" class="text-end"><?= number_format((float)$devis['total_tva'], 2, ',', ' ') ?> MAD</th></tr>
            <tr><th colspan="7" class="text-end">Total TTC</th><th colspan="2" class="text-end fs-5"><?= number_format((float)$devis['total_ttc'], 2, ',', ' ') ?> MAD</th></tr>
            </tfoot>
        </table>
    </div>
</div>
