<div class="row justify-content-center">
    <div class="col-xl-9">
        <div class="card">
            <form method="post" action="/accounting/update" enctype="multipart/form-data"><input type="hidden"
                    name="csrf" value="<?= h(Auth::csrf()) ?>"><input type="hidden" name="id"
                    value="<?= (int)$invoice['id'] ?>">
                <div class="card-header">
                    <h3 class="card-title">Modifier <?= h($invoice['reference']) ?></h3>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-4"><label class="form-label">Référence *</label><input class="form-control"
                                name="reference" value="<?= h($invoice['reference']) ?>" required></div>
                        <div class="col-md-4"><label class="form-label">Structure</label><select class="form-select"
                                name="vendor_contact_id">
                                <option value="">—</option><?php foreach ($vendors as $v):?><option
                                    value="<?= (int)$v['id'] ?>"
                                    <?= (int)$v['id'] === (int)$invoice['vendor_contact_id'] ? 'selected' : ''?>>
                                    <?= h($v['name']) ?></option><?php endforeach?>
                            </select></div>
                        <div class="col-md-2"><label class="form-label">Date *</label><input class="form-control"
                                type="date" name="invoice_date" value="<?= h($invoice['invoice_date']) ?>" required>
                        </div>
                        <div class="col-md-2"><label class="form-label">Échéance</label><input class="form-control"
                                type="date" name="due_date" value="<?= h($invoice['due_date'] ?? '') ?>"></div>
                        <div class="col-md-3"><label class="form-label">Type</label><select class="form-select"
                                name="document_type"><?php foreach (['invoice' => 'Facture','credit' => 'Avoir','adjustment' => 'Régularisation'] as $k => $v):?>
                                <option value="<?= $k ?>" <?= $invoice['document_type'] === $k ? 'selected' : ''?>>
                                    <?= $v ?></option><?php endforeach?></select></div>
                        <div class="col-md-3"><label class="form-label">Montant HT</label><input class="form-control"
                                name="total_ht"
                                value="<?= $invoice['total_ht_cents'] === null ? '' : h(number_format($invoice['total_ht_cents'] / 100, 2, ',', '')) ?>">
                        </div>
                        <div class="col-md-3"><label class="form-label">Montant TTC *</label><input class="form-control"
                                name="total_ttc"
                                value="<?= h(number_format($invoice['total_ttc_cents'] / 100, 2, ',', '')) ?>" required>
                        </div>
                        <div class="col-md-3"><label class="form-label">Reste dû</label><input class="form-control"
                                name="amount_due"
                                value="<?= $invoice['amount_due_cents'] === null ? '' : h(number_format($invoice['amount_due_cents'] / 100, 2, ',', '')) ?>">
                        </div>
                        <div class="col-md-4"><label class="form-label">Statut</label><select class="form-select"
                                name="status"><?php foreach (['to_pay' => 'À payer','paid' => 'Payée','partial' => 'Partiellement payée','disputed' => 'Contestée','cancelled' => 'Annulée','credit' => 'Avoir'] as $k => $v):?>
                                <option value="<?= $k ?>" <?= $invoice['status'] === $k ? 'selected' : ''?>><?= $v ?>
                                </option><?php endforeach?></select></div>
                        <div class="col-md-4"><label class="form-label">Règlement</label><select class="form-select"
                                name="payment_method">
                                <option value="">—</option>
                                <?php foreach (['transfer' => 'Virement','direct_debit' => 'Prélèvement','card' => 'Carte','check' => 'Chèque','cash' => 'Espèces','other' => 'Autre'] as $k => $v):?>
                                <option value="<?= $k ?>" <?= $invoice['payment_method'] === $k ? 'selected' : ''?>>
                                    <?= $v ?></option><?php endforeach?>
                            </select></div>
                        <div class="col-md-4"><label class="form-label">Remplacer le justificatif</label><input
                                class="form-control" type="file" name="document"
                                accept="application/pdf,image/jpeg,image/png,image/webp"></div>
                        <div class="col-12"><label class="form-label">Notes</label><textarea class="form-control"
                                name="notes" rows="3"><?= h($invoice['notes'] ?? '') ?></textarea></div>
                    </div>
                </div>
                <div class="card-footer d-flex justify-content-between"><a class="btn btn-link"
                        href="/accounting?year=0">Annuler</a><button class="btn btn-primary">Enregistrer les
                        modifications</button></div>
            </form>
        </div>
        <form method="post" action="/accounting/delete" class="mt-3 text-end"
            onsubmit="return confirm('Mettre cette facture à la corbeille ? Elle ne sera plus comptée dans les totaux.');">
            <input type="hidden" name="csrf" value="<?= h(Auth::csrf()) ?>"><input type="hidden" name="id"
                value="<?= (int)$invoice['id'] ?>"><button class="btn btn-outline-danger">Mettre à la corbeille</button>
        </form>
    </div>
</div>
