<?php
if (!isset($_SESSION['user'])) {
    header('Location: index.php?p=login');
    exit;
}

require_once 'config.inc.php';

$ref = $_SESSION['user']['ref'];
$devise = $_SESSION['devise'] ?? 'MAD';
$soldeActuel = $_SESSION['user']['solde'] ?? 0;

// Traitement du formulaire
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $montant = (float) ($_POST['montant_recharge'] ?? 0);
    $pieceJointe = null;

    if ($montant <= 0) {
        addMsg('error', trad('Invalid amount'));
    } else {

        // Upload pièce jointe
        if (!empty($_FILES['piece_jointe']['name'])) {

            $uploadDir = 'uploads/recharges/';
            if (!is_dir($uploadDir)) {
                mkdir($uploadDir, 0777, true);
            }

            $ext = pathinfo($_FILES['piece_jointe']['name'], PATHINFO_EXTENSION);
            $filename = uniqid('recharge_') . '.' . strtolower($ext);
            $targetFile = $uploadDir . $filename;

            if (move_uploaded_file($_FILES['piece_jointe']['tmp_name'], $targetFile)) {
                $pieceJointe = $filename;
            }
        }

        // Insertion demande recharge
        $stmt = $dbh->prepare("
            INSERT INTO users_recharge_solde
            (ref, dateajout, solde_actuel, montant_recharge, piece_jointe, statut, action_by)
            VALUES (?, NOW(), ?, ?, ?, 'new', NULL)
        ");

        $stmt->execute([
            $ref,
            $soldeActuel,
            $montant,
            $pieceJointe
        ]);

        addMsg('success', trad('Your recharge request has been sent successfully'));
        header('Location: index.php?p=dashboard');
        exit;
    }
}

showMsg();
?>

<div class="container my-5">
    <div class="row justify-content-center">
        <div class="col-md-6">

            <div class="card shadow-sm rounded-4">
                <div class="card-body p-4">

                    <h4 class="text-center mb-3">
                        💰 <?= trad('Recharge balance') ?>
                    </h4>

                    <!-- Solde actuel -->
                    <div class="alert alert-success text-center">
                        <?= trad('Current balance') ?> :
                        <strong>
                            <?= number_format((float)$soldeActuel, 2, ',', ' ') ?>
                            <?= htmlspecialchars($devise) ?>
                        </strong>
                    </div>

                    <form method="POST" enctype="multipart/form-data">

                        <!-- Montant -->
                        <div class="mb-3">
                            <label class="form-label">
                                <?= trad('Recharge amount') ?>
                            </label>
                            <input type="number"
                                   name="montant_recharge"
                                   class="form-control"
                                   step="0.01"
                                   min="1"
                                   required>
                        </div>

                        <!-- Pièce jointe -->
                        <div class="mb-3">
                            <label class="form-label">
                                <?= trad('Proof of payment (optional)') ?>
                            </label>
                            <input type="file"
                                   name="piece_jointe"
                                   class="form-control"
                                   accept="image/*,.pdf">
                        </div>

                        <!-- Info -->
                        <div class="alert alert-warning small">
                            <?= trad('Your balance will be updated after validation by the administration.') ?>
                        </div>

                        <div class="d-grid">
                            <button type="submit" class="btn btn-success btn-lg rounded-pill">
                                ➕ <?= trad('Send recharge request') ?>
                            </button>
                        </div>

                    </form>

                </div>
            </div>

        </div>
    </div>
</div>
