<?php require dirname(__DIR__) . '/layouts/app.php'; ?>
<?php $isEdit = !empty($expense['id']); ?>
<section class="page-heading"><div><p class="eyebrow">Charges</p><h1><?= $isEdit ? 'Modifier depense' : 'Ajouter depense' ?></h1></div><a class="button-link secondary" href="<?= url('/expenses') ?>">Retour</a></section>
<?php if (!empty($errors)): ?><div class="alert alert-error"><?php foreach ($errors as $error): ?><div><?= e($error) ?></div><?php endforeach; ?></div><?php endif; ?>
<form class="form-panel" method="post" action="<?= url($isEdit ? '/expenses/update' : '/expenses/store') ?>">
    <input type="hidden" name="_token" value="<?= csrf_token() ?>"><?php if ($isEdit): ?><input type="hidden" name="id" value="<?= (int) $expense['id'] ?>"><?php endif; ?>
    <div class="form-grid">
        <label>Date <input type="date" name="expense_date" value="<?= e($expense['expense_date']) ?>" required></label>
        <label>Categorie <select name="category"><?php foreach (expense_categories() as $key => $label): ?><option value="<?= e($key) ?>" <?= $expense['category'] === $key ? 'selected' : '' ?>><?= e($label) ?></option><?php endforeach; ?></select></label>
        <label>Montant <input type="number" min="0" step="0.01" name="amount" value="<?= e((string) $expense['amount']) ?>" required></label>
        <label>Bien concerne <select name="property_id"><option value="">Optionnel</option><?php foreach ($properties as $property): ?><option value="<?= (int) $property['id'] ?>" <?= (int) ($expense['property_id'] ?? 0) === (int) $property['id'] ? 'selected' : '' ?>><?= e($property['title']) ?></option><?php endforeach; ?></select></label>
    </div>
    <label class="full-field">Description <textarea name="description" rows="5"><?= e($expense['description']) ?></textarea></label>
    <div class="form-actions"><button type="submit"><?= $isEdit ? 'Enregistrer' : 'Ajouter' ?></button></div>
</form>
<?php require dirname(__DIR__) . '/layouts/app_footer.php'; ?>