<?php

if (!isset($_SESSION['user'])) {
  header('Location: ?p=login');

}else{

$user = $_SESSION['user'];
$ref = $user['ref'];

require_once 'config.inc.php';

$stmt = $dbh->prepare("SELECT * FROM mondialisa_users WHERE ref = ?");
$stmt->execute([$ref]);
$profil = $stmt->fetch(PDO::FETCH_ASSOC);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $nom = trim($_POST['nom']);
  $email = trim($_POST['email']);
  $telephone = trim($_POST['telephone']);
  $pays = trim($_POST['pays']);
  $ville = trim($_POST['ville']);
  $bio = trim($_POST['bio']);
  $photo = $profil['photo'];

  // Upload photo si ajoutée
  if (!empty($_FILES['photo']['name'])) {
    $targetDir = "uploads/profils/";
    if (!file_exists($targetDir)) mkdir($targetDir, 0777, true);

    $filename = uniqid() . "_" . basename($_FILES["photo"]["name"]);
    $targetFile = $targetDir . $filename;

    if (move_uploaded_file($_FILES["photo"]["tmp_name"], $targetFile)) {
      $photo = $filename;
    }
  }

  $update = $pdo->prepare("UPDATE mondialisa_users 
    SET nom=?, email=?, tel=?, country=?, city=?, bio=?, photo=? WHERE ref=?");
  $update->execute([$nom, $email, $telephone, $pays, $ville, $bio, $photo, $ref]);

  $_SESSION['user']['nom'] = $nom;
  $_SESSION['user']['email'] = $email;

  echo "<div class='alert alert-success text-center mt-3'>Profil mis à jour avec succès ✅</div>";
  
  $profil = array_merge($profil, compact('nom','email','tel','country','city','bio','photo'));
}
}
?>
<div class="container my-5">
    <div class="row justify-content-center">
        <div class="col-md-8 col-lg-6">
            <div class="card shadow-lg p-4 rounded-4">
                <h4 class="text-center mb-3">👤 <?= trad('My Profile') ?></h4>

                <div class="text-center mb-3">
                    <img src="<?= !empty($profil['photo']) ? 'uploads/profils/' . htmlspecialchars($profil['photo']) : 'uploads/profils/default-user.png' ?>" 
                         alt="<?= trad('Profile Photo') ?>" 
                         class="rounded-circle shadow" 
                         style="width:100px;height:100px;object-fit:cover;">
                </div>

                <form method="POST" enctype="multipart/form-data">
                    <!-- 💰 SOLDE UTILISATEUR -->
<div class="text-center mb-4">
    <div class="badge bg-success fs-6 px-4 py-2 rounded-pill shadow">
        💰 <?= trad('Balance') ?> :
        <strong>
            <?= number_format((float)$profil['solde'], 2, ',', ' ') ?>           
        </strong>
    </div>
</div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('First Name') ?></label>
                        <input type="text" name="firstname" class="form-control" value="<?= htmlspecialchars($profil['firstname']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('Last Name') ?></label>
                        <input type="text" name="lastname" class="form-control" value="<?= htmlspecialchars($profil['lastname']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('Email') ?></label>
                        <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($profil['email']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('Phone') ?></label>
                        <input type="text" name="telephone" class="form-control" value="<?= htmlspecialchars($profil['tel']) ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('Country') ?></label>
                        <input type="text" name="pays" class="form-control" value="<?= htmlspecialchars($profil['country']) ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('City') ?></label>
                        <input type="text" name="ville" class="form-control" value="<?= htmlspecialchars($profil['city']) ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('Bio / About') ?></label>
                        <textarea name="bio" class="form-control" rows="3"><?= htmlspecialchars($profil['bio']) ?></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label"><?= trad('Profile Photo') ?></label>
                        <input type="file" name="photo" class="form-control">
                    </div>

                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary btn-lg rounded-pill"><?= trad('Update Profile') ?></button>
                    </div>
                </form>

                <div class="text-center mt-4">
                    <a href="logout.php" class="text-danger">🚪 <?= trad('Logout') ?></a>
                </div>
            </div>
        </div>
    </div>
</div>
