<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="card shadow-sm">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-receipt"></i> Dépense pour <?= $request->reference; ?></h5>
                <a href="<?= site_url('requests/view/' . $request->id); ?>" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i> Retour à la demande
                </a>
            </div>
            <div class="card-body">

                <!-- Résumé de la demande -->
                <div class="alert alert-info py-2 d-flex justify-content-between align-items-center">
                    <div>
                        <i class="bi bi-link-45deg"></i>
                        <strong>Demande liée :</strong> 
                        <code><?= $request->reference; ?></code> — 
                        <?= $request->category_label; ?> — 
                        <?= $request->location_name; ?> —
                        Estimé : <strong><?= number_format($request->estimated_amount, 0, ',', ' '); ?> KMF</strong>
                    </div>
                </div>

                <?= form_open_multipart('expenses/create_from_request/' . $request->id); ?>

                <div class="row">
                    <!-- Date -->
                    <div class="col-md-3 mb-3">
                        <label class="form-label">Date de facturation <span class="text-danger">*</span></label>
                        <input type="date" name="expense_date" class="form-control" required
                               value="<?= set_value('expense_date', date('Y-m-d')); ?>">
                        <div class="form-text">Date de saisie / paiement</div>
                    </div>

                    <div class="col-md-3 mb-3">
                        <label class="form-label">Date opération <span class="text-danger">*</span></label>
                        <input type="date" name="operation_date" class="form-control" required
                               value="<?= set_value('operation_date', date('Y-m-d')); ?>">
                        <div class="form-text">Date du service / prestation</div>
                    </div>

                    <!-- Catégorie (affichée, non modifiable) -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Catégorie</label>
                        <input type="text" class="form-control" disabled
                               value="<?= $request->category_label; ?>">
                    </div>

                    <!-- Localisation (affichée, non modifiable) -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Localisation</label>
                        <input type="text" class="form-control" disabled
                               value="<?= $request->location_name; ?>">
                    </div>

                    <!-- Caisse -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Caisse <span class="text-danger">*</span></label>
                        <select name="cashbox_id" class="form-select" required>
                            <option value="">-- Choisir --</option>
                            <?php foreach ($cashboxes as $cb): ?>
                            <option value="<?= $cb->id; ?>" <?= set_select('cashbox_id', $cb->id); ?>>
                                <?= $cb->name; ?> (<?= number_format($cb->current_balance, 0, ',', ' '); ?>)
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <!-- Fournisseur -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Fournisseur <span class="text-danger">*</span></label>
                        <input type="text" name="supplier" class="form-control" required
                               value="<?= set_value('supplier', $request->supplier); ?>"
                               placeholder="Nom du fournisseur">
                    </div>

                    <!-- Montant -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Montant réel <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <input type="number" name="amount" class="form-control"
                                   step="1" min="1" required 
                                   value="<?= set_value('amount', $request->estimated_amount); ?>">
                            <span class="input-group-text">KMF</span>
                        </div>
                        <div class="form-text">Estimé : <?= number_format($request->estimated_amount, 0, ',', ' '); ?> KMF</div>
                    </div>

                    <!-- N° facture -->
                    <div class="col-md-4 mb-3">
                        <label class="form-label">N° Facture</label>
                        <input type="text" name="invoice_number" class="form-control"
                               value="<?= set_value('invoice_number'); ?>" placeholder="Optionnel">
                    </div>

                    <!-- Description -->
                    <div class="col-12 mb-3">
                        <label class="form-label">Description <span class="text-danger">*</span></label>
                        <textarea name="description" class="form-control" rows="2" required
                                  placeholder="Détails de la dépense..."><?= set_value('description', $request->category_label . ' - ' . $request->description); ?></textarea>
                    </div>

                    <!-- Facture (upload) -->
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Facture / Justificatif</label>
                        <input type="file" name="invoice_file" class="form-control" accept=".jpg,.jpeg,.png,.pdf">
                        <div class="form-text">JPG, PNG ou PDF. Max 5 Mo.</div>
                    </div>
                </div>

                <hr>
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg"></i> Créer et lier la dépense
                    </button>
                    <a href="<?= site_url('requests/view/' . $request->id); ?>" class="btn btn-outline-secondary">Annuler</a>
                </div>

                <?= form_close(); ?>
            </div>
        </div>
    </div>
</div>