<?php
/**
 * @var object $expense
 * @var array $categories
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-edit me-2 text-gold"></i>Edit Expense</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-wallet card-icon"></i>
            <h6 class="card-title">Expense Information</h6>
        </div>
        <div>
            <span class="text-muted small"><span class="text-danger">*</span> Required fields</span>
        </div>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= url('expenses/update/' . $expense->id) ?>" enctype="multipart/form-data">
            <?= csrf_field() ?>

            <div class="row g-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Category <span class="text-danger">*</span></label>
                        <select name="category_id" class="form-select bg-transparent <?= errorClass('category_id') ?>">
                            <option value="">Select Category</option>
                            <?php foreach ($categories as $cat): ?>
                                <option value="<?= $cat->id ?>" <?= (old('category_id', $expense->category_id) == $cat->id) ? 'selected' : '' ?>>
                                    <?= e($cat->name) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <?= errorMsg('category_id') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Amount <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent border-gold"><?= APP_CURRENCY_SYMBOL ?></span>
                            <input type="number" step="0.01" name="amount"
                                   class="form-control bg-transparent <?= errorClass('amount') ?>"
                                   value="<?= e(old('amount', $expense->amount)) ?>" required min="0">
                        </div>
                        <?= errorMsg('amount') ?>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="mb-3">
                        <label class="form-label">Date <span class="text-danger">*</span></label>
                        <input type="date" name="date" class="form-control bg-transparent <?= errorClass('date') ?>"
                               value="<?= e(old('date', $expense->date)) ?>" required>
                        <?= errorMsg('date') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-12">
                    <div class="mb-3">
                        <label class="form-label">Description <span class="text-danger">*</span></label>
                        <textarea name="description" class="form-control bg-transparent <?= errorClass('description') ?>"
                                  rows="3" maxlength="500"><?= e(old('description', $expense->description)) ?></textarea>
                        <?= errorMsg('description') ?>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Payment Method <span class="text-danger">*</span></label>
                        <select name="payment_method" class="form-select bg-transparent <?= errorClass('payment_method') ?>">
                            <option value="cash" <?= (old('payment_method', $expense->payment_method) === 'cash') ? 'selected' : '' ?>>Cash</option>
                            <option value="card" <?= (old('payment_method', $expense->payment_method) === 'card') ? 'selected' : '' ?>>Card</option>
                            <option value="bank_transfer" <?= (old('payment_method', $expense->payment_method) === 'bank_transfer') ? 'selected' : '' ?>>Bank Transfer</option>
                            <option value="cheque" <?= (old('payment_method', $expense->payment_method) === 'cheque') ? 'selected' : '' ?>>Cheque</option>
                        </select>
                        <?= errorMsg('payment_method') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Reference Number</label>
                        <input type="text" name="reference_number" class="form-control bg-transparent <?= errorClass('reference_number') ?>"
                               value="<?= e(old('reference_number', $expense->reference_number)) ?>" maxlength="100">
                        <?= errorMsg('reference_number') ?>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="mb-3">
                        <label class="form-label">Receipt</label>
                        <?php if (!empty($expense->receipt)): ?>
                            <div class="mb-2">
                                <a href="<?= uploadUrl($expense->receipt) ?>" target="_blank" class="btn btn-sm btn-outline-info">
                                    <i class="fas fa-file me-1"></i>View Current Receipt
                                </a>
                            </div>
                        <?php endif; ?>
                        <input type="file" name="receipt" class="form-control bg-transparent <?= errorClass('receipt') ?>"
                               accept=".jpg,.jpeg,.png,.gif,.pdf">
                        <small class="text-muted">Allowed: JPG, PNG, PDF (max 5MB). Leave empty to keep current.</small>
                        <?= errorMsg('receipt') ?>
                    </div>
                </div>
            </div>

            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-gold">
                    <i class="fas fa-save me-1"></i>Update Expense
                </button>
                <a href="<?= url('expenses') ?>" class="btn btn-outline-secondary">
                    <i class="fas fa-times me-1"></i>Cancel
                </a>
            </div>
        </form>
    </div>
</div>
