<?php
$formatMoney = static fn($value): string => number_format((float)$value, 2, ',', ' ') . ' DH';
$modes = ['virement' => 'Virement', 'cheque' => 'Chèque', 'especes' => 'Espèces', 'carte' => 'Carte bancaire', 'autre' => 'Autre'];
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Règlement fournisseur</h1>
        <p class="text-muted mb-0">
            Facture <?= htmlspecialchars($facture['numero_interne']) ?> — <?= htmlspecialchars($facture['fournisseur_nom']) ?>
        </p>
    </div>
    <a href="index.php?page=factures_fournisseurs_show&id=<?= (int)$facture['id'] ?>" class="btn btn-darlink-outline-secondary">← Facture</a>
</div>

<div class="row g-3">
    <div class="col-md-8">
        <form method="post" action="index.php?page=factures_fournisseurs_reglement_store" class="darlink-card p-4">
            <input type="hidden" name="facture_fournisseur_id" value="<?= (int)$facture['id'] ?>">

            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label">Date de règlement *</label>
                    <input type="date" name="date_reglement" class="form-control" value="<?= htmlspecialchars($reglement['date_reglement']) ?>" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Montant réglé *</label>
                    <input type="number" step="0.01" min="0" max="<?= htmlspecialchars((string)$facture['reste_du']) ?>" name="montant" class="form-control" value="<?= htmlspecialchars((string)$reglement['montant']) ?>" required>
                    <div class="form-text">Reste dû actuel : <?= $formatMoney($facture['reste_du']) ?></div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Mode de paiement</label>
                    <select name="mode_reglement" class="form-select">
                        <?php foreach ($modes as $key => $label): ?>
                            <option value="<?= htmlspecialchars($key) ?>" <?= ($reglement['mode_reglement'] ?? '') === $key ? 'selected' : '' ?>><?= htmlspecialchars($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Référence du paiement</label>
                    <input type="text" name="reference_reglement" class="form-control" value="<?= htmlspecialchars($reglement['reference_reglement']) ?>" placeholder="N° chèque, référence virement, ticket TPE...">
                    <div class="form-text">Facultatif.</div>
                </div>
                <div class="col-md-12">
                    <label class="form-label">Observations</label>
                    <textarea name="observations" class="form-control" rows="3"><?= htmlspecialchars($reglement['observations']) ?></textarea>
                </div>
            </div>

            <div class="d-flex justify-content-end gap-2 mt-4">
                <a href="index.php?page=factures_fournisseurs_show&id=<?= (int)$facture['id'] ?>" class="btn btn-outline-secondary">Annuler</a>
                <button class="btn btn-success">Enregistrer le règlement</button>
            </div>
        </form>
    </div>

    <div class="col-md-4">
        <div class="darlink-card p-4">
            <h5>Résumé facture</h5>
            <div class="mt-3 d-flex justify-content-between"><span>Facture</span><strong><?= htmlspecialchars($facture['numero_interne']) ?></strong></div>
            <div class="mt-2 d-flex justify-content-between"><span>N° fournisseur</span><strong><?= htmlspecialchars($facture['numero_facture_fournisseur']) ?></strong></div>
            <hr>
            <div class="d-flex justify-content-between"><span>Total TTC</span><strong><?= $formatMoney($facture['montant_ttc']) ?></strong></div>
            <div class="d-flex justify-content-between mt-2"><span>Déjà réglé</span><strong><?= $formatMoney($facture['montant_regle']) ?></strong></div>
            <div class="d-flex justify-content-between h5 mt-3"><span>Reste dû</span><strong><?= $formatMoney($facture['reste_du']) ?></strong></div>
        </div>
    </div>
</div>
