<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<h5 class="mb-4">Enregistrer un Paiement</h5>

<div class="row">
    <div class="col-lg-6">
        <div class="table-card">
            <h6 class="mb-3">Facture: <?= $invoice['invoice_number'] ?></h6>
            <div class="alert alert-info">
                <div class="d-flex justify-content-between">
                    <span>Total TTC:</span>
                    <span class="fw-bold"><?= number_format($invoice['total_ttc'], 2, ',', ' ') ?> TND</span>
                </div>
                <div class="d-flex justify-content-between">
                    <span>Net a payer:</span>
                    <span class="fw-bold"><?= number_format($invoice['net_payable'], 2, ',', ' ') ?> TND</span>
                </div>
                <div class="d-flex justify-content-between">
                    <span>Reste a payer:</span>
                    <span class="fw-bold text-danger"><?= number_format($remaining, 2, ',', ' ') ?> TND</span>
                </div>
            </div>

            <form method="POST" action="/payments/create/<?= $invoice['id'] ?>">
                <?= csrf_field() ?>
                <div class="mb-3">
                    <label class="form-label">Montant *</label>
                    <input type="number" step="0.01" name="amount" class="form-control" value="<?= $remaining ?>" max="<?= $remaining ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Date de paiement *</label>
                    <input type="date" name="payment_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Mode de paiement</label>
                    <select name="payment_method" class="form-select">
                        <option value="espece">Espece</option>
                        <option value="cheque">Cheque</option>
                        <option value="virement" selected>Virement</option>
                        <option value="carte">Carte bancaire</option>
                        <option value="autre">Autre</option>
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label">Reference / N cheque</label>
                    <input type="text" name="reference" class="form-control" placeholder="Ex: Virement n 12345">
                </div>
                <div class="mb-3">
                    <label class="form-label">Notes</label>
                    <textarea name="notes" class="form-control" rows="2"></textarea>
                </div>
                <div class="d-flex justify-content-between">
                    <a href="/invoices/view/<?= $invoice['id'] ?>" class="btn btn-outline-secondary">Annuler</a>
                    <button type="submit" class="btn btn-success">
                        <i class="bi bi-check-lg"></i> Enregistrer le paiement
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
