<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-receipt me-2 text-primary"></i><?= $expense ? 'Edit Expense' : 'New Expense' ?></h5>
    <a href="<?= url('/Expenses') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<form method="post" action="<?= url('/Expenses/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="expense_id" value="<?= (int) ($expense['expense_id'] ?? 0) ?>">

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Details</h6>
        <div class="row g-2">
            <div class="col-md-8"><label class="form-label">Title *</label><input type="text" class="form-control form-control-sm" name="title" required value="<?= e($expense['title'] ?? '') ?>"></div>
            <div class="col-md-4"><label class="form-label">Code</label><input type="text" class="form-control form-control-sm" name="code" value="<?= e($expense['code'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Expense Date</label><input type="date" class="form-control form-control-sm" name="expense_date" value="<?= e($expense['expense_date'] ?? date('Y-m-d')) ?>"></div>
            <div class="col-md-3"><label class="form-label">Category</label><select name="category_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($categories as $c): ?><option value="<?= (int) $c['category_id'] ?>" <?= (int) ($expense['category_id'] ?? 0) === (int) $c['category_id'] ? 'selected' : '' ?>><?= e($c['name']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-3"><label class="form-label">Amount *</label><input type="number" step="0.01" class="form-control form-control-sm" name="amount" required value="<?= e((string) ($expense['amount'] ?? '')) ?>"></div>
            <div class="col-md-3"><label class="form-label">Currency</label><input type="text" class="form-control form-control-sm" name="currency" value="<?= e($expense['currency'] ?? 'TZS') ?>" maxlength="5"></div>
            <div class="col-md-4"><label class="form-label">Vendor</label><input type="text" class="form-control form-control-sm" name="vendor" value="<?= e($expense['vendor'] ?? '') ?>"></div>
            <div class="col-md-4"><label class="form-label">Receipt Ref</label><input type="text" class="form-control form-control-sm" name="receipt_ref" value="<?= e($expense['receipt_ref'] ?? '') ?>"></div>
            <div class="col-md-4"><label class="form-label">Payment Method</label><input type="text" class="form-control form-control-sm" name="payment_method" value="<?= e($expense['payment_method'] ?? '') ?>" placeholder="cash / mobile / card"></div>
            <div class="col-md-3"><label class="form-label">Status</label><select name="status" class="form-select form-select-sm"><?php foreach (['draft','submitted','approved','rejected','paid','cancelled'] as $s): ?><option <?= ($expense['status'] ?? 'draft') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?></select></div>
            <div class="col-12"><label class="form-label">Description</label><textarea class="form-control form-control-sm" name="description" rows="2"><?= e($expense['description'] ?? '') ?></textarea></div>
        </div>
    </div>

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Attribution</h6>
        <div class="row g-2">
            <div class="col-md-4"><label class="form-label">Work Order</label><select name="work_order_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($workOrders as $w): ?><option value="<?= (int) $w['work_order_id'] ?>" <?= (int) ($expense['work_order_id'] ?? 0) === (int) $w['work_order_id'] ? 'selected' : '' ?>><?= e($w['code'] ?? '#' . $w['work_order_id']) ?> · <?= e($w['title']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-4"><label class="form-label">Site</label><select name="site_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($sites as $s): ?><option value="<?= (int) $s['site_id'] ?>" <?= (int) ($expense['site_id'] ?? 0) === (int) $s['site_id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-4"><label class="form-label">Asset</label><select name="asset_id" class="form-select form-select-sm"><option value="">— none —</option><?php foreach ($assets as $a): ?><option value="<?= (int) $a['asset_id'] ?>" <?= (int) ($expense['asset_id'] ?? 0) === (int) $a['asset_id'] ? 'selected' : '' ?>><?= e($a['name']) ?></option><?php endforeach; ?></select></div>
        </div>
    </div>

    <div class="d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
        <?php if (!empty($expense['expense_id'])): ?><a class="btn btn-sm btn-outline-danger" href="<?= url('/Expenses/delete/' . (int) $expense['expense_id']) ?>" onclick="return confirm('Delete?')"><i class="fas fa-trash me-1"></i>Delete</a><?php endif; ?>
    </div>
</form>
