<?php $editing = !empty($expense); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6"><a href="?page=expenses" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a><h1 class="text-2xl font-bold text-gray-900"><?= $editing ? 'Edit Expense' : 'Add Expense' ?></h1></div>
    <form method="POST" action="?page=expenses&action=<?= $editing ? 'update&id='.$expense['id'] : 'store' ?>" class="space-y-6"><?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="md:col-span-2"><label class="block text-sm font-medium text-gray-700 mb-1">Description <span class="text-red-500">*</span></label><input type="text" name="description" value="<?= htmlspecialchars(old('description', $expense['description'] ?? '')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Amount <span class="text-red-500">*</span></label><input type="number" name="amount" value="<?= old('amount', $expense['amount'] ?? '') ?>" required step="0.01" min="0" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Date <span class="text-red-500">*</span></label><input type="date" name="expense_date" value="<?= old('expense_date', $expense['expense_date'] ?? date('Y-m-d')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Category</label>
                    <select name="category" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach (['maintenance'=>'Maintenance','repairs'=>'Repairs','utilities'=>'Utilities','insurance'=>'Insurance','taxes'=>'Taxes','management'=>'Management','mortgage'=>'Mortgage','supplies'=>'Supplies','landscaping'=>'Landscaping','cleaning'=>'Cleaning','legal'=>'Legal','advertising'=>'Advertising','other'=>'Other'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('category', $expense['category'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Property</label>
                    <select name="property_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">None</option>
                    <?php foreach ($properties ?? [] as $p): ?><option value="<?= $p['id'] ?>" <?= old('property_id', $expense['property_id'] ?? '') == $p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Vendor</label>
                    <select name="vendor_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">None</option>
                    <?php foreach ($vendors ?? [] as $vn): ?><option value="<?= $vn['id'] ?>" <?= old('vendor_id', $expense['vendor_id'] ?? '') == $vn['id'] ? 'selected' : '' ?>><?= htmlspecialchars($vn['company_name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Payment Method</label>
                    <select name="payment_method" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach (['check'=>'Check','ach'=>'ACH','credit_card'=>'Credit Card','cash'=>'Cash','wire'=>'Wire Transfer'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('payment_method', $expense['payment_method'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Reference #</label><input type="text" name="reference_number" value="<?= htmlspecialchars(old('reference_number', $expense['reference_number'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
            <div class="mt-4"><label class="block text-sm font-medium text-gray-700 mb-1">Notes</label><textarea name="notes" rows="2" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><?= htmlspecialchars(old('notes', $expense['notes'] ?? '')) ?></textarea></div>
        </div>
        <div class="flex justify-end gap-3"><a href="?page=expenses" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"><?= $editing ? 'Update' : 'Add Expense' ?></button></div>
    </form>
</div>
