<?php
$isEdit = ($mode ?? 'create') === 'edit';
$current = $devis ?? [];
$formAction = $isEdit ? 'index.php?page=devis_update' : 'index.php?page=devis_store';
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1"><?= $isEdit ? 'Modifier le devis ' . htmlspecialchars((string)$numero) : 'Nouveau devis' ?></h1>
        <p class="text-muted mb-0"><?= $isEdit ? 'Modifier les informations et les lignes du devis' : 'Créer un devis sans impact sur le stock' ?></p>
    </div>
    <a href="index.php?page=<?= $isEdit ? 'devis_show&id=' . (int)$current['id'] : 'devis' ?>" class="btn btn-outline-secondary">Retour</a>
</div>

<form method="post" action="<?= htmlspecialchars($formAction) ?>" id="devisForm">
    <?php if ($isEdit): ?><input type="hidden" name="id" value="<?= (int)$current['id'] ?>"><?php endif; ?>
    <div class="darlink-card p-4 mb-4">
        <div class="row g-3">
            <div class="col-md-3"><label class="form-label">Numéro</label><input type="text" name="numero" class="form-control" value="<?= htmlspecialchars((string)$numero) ?>" readonly></div>
            <div class="col-md-3"><label class="form-label">Date</label><input type="date" name="date_devis" class="form-control" value="<?= htmlspecialchars((string)$today) ?>" required></div>
            <div class="col-md-3"><label class="form-label">Validité</label><input type="date" name="validite" class="form-control" value="<?= htmlspecialchars((string)$validite) ?>"></div>
            <div class="col-md-3"><label class="form-label">Client</label><select name="client_id" class="form-select" required><option value="">Sélectionner...</option><?php foreach ($clients as $client): ?><option value="<?= (int)$client['id'] ?>" <?= $isEdit && (int)$current['client_id'] === (int)$client['id'] ? 'selected' : '' ?>><?= htmlspecialchars($client['raison_sociale'] . ' — ' . $client['code_client']) ?></option><?php endforeach; ?></select></div>
            <div class="col-12"><label class="form-label">Observations</label><textarea name="observations" class="form-control" rows="2"><?= htmlspecialchars((string)($current['observations'] ?? '')) ?></textarea></div>
        </div>
    </div>

    <div class="darlink-card p-4 mb-4">
        <div class="d-flex justify-content-between align-items-center mb-3"><h5 class="mb-0">Lignes du devis</h5><button type="button" class="btn btn-outline-primary" id="addLineBtn">+ Ajouter une ligne</button></div>
        <div class="row mb-3"><div class="col-md-8"><input type="text" id="articleQuickSearch" class="form-control" placeholder="Rechercher un article par référence ou désignation..."><div id="articleResults" class="list-group mt-2 shadow-sm" style="display:none; position:absolute; z-index:20; max-width:720px;"></div></div></div>
        <div class="table-responsive"><table class="table table-hover align-middle darlink-table" id="devisLinesTable"><thead><tr><th style="width:120px;">Réf.</th><th>Désignation</th><th style="width:80px;">Unité</th><th class="text-end" style="width:100px;">Qté</th><th class="text-end" style="width:120px;">Prix HT</th><th class="text-end" style="width:90px;">Remise</th><th class="text-end" style="width:90px;">TVA</th><th class="text-end" style="width:120px;">Total HT</th><th style="width:50px;"></th></tr></thead><tbody></tbody></table></div>
    </div>

    <div class="darlink-card p-4 mb-4"><div class="row text-end"><div class="col-md-8"></div><div class="col-md-4"><div class="d-flex justify-content-between mb-2"><strong>Total HT</strong><span id="totalHt">0,00</span></div><div class="d-flex justify-content-between mb-2"><strong>Total TVA</strong><span id="totalTva">0,00</span></div><div class="d-flex justify-content-between fs-5"><strong>Total TTC</strong><strong id="totalTtc">0,00</strong></div><div class="d-flex justify-content-between text-muted small mt-2"><span>Marge interne estimée</span><span id="totalMarge">0,00 %</span></div></div></div></div>
    <div class="d-flex justify-content-end gap-2"><a href="index.php?page=<?= $isEdit ? 'devis_show&id=' . (int)$current['id'] : 'devis' ?>" class="btn btn-outline-secondary">Annuler</a><button type="submit" class="btn darlink-btn-primary"><?= $isEdit ? 'Enregistrer les modifications' : 'Enregistrer le devis' ?></button></div>
</form>
<script>
window.DARLINK_ARTICLES = <?= json_encode($articles, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE) ?>;
window.DARLINK_DEVIS_LINES = <?= json_encode($lignes ?? [], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE) ?>;
</script>
<script src="assets/js/devis.js"></script>
