<?php
// Fichier: profile.php
require 'includes/db.php';
require 'includes/auth.php';
require_login();

$user_id = $_SESSION['user_id'];
$entreprise_id = $_SESSION['entreprise_id'];
$msg = "";

// 1. Mise à jour Infos CSE
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'update_cse') {
    $adresse = trim($_POST['adresse']);
    $contact = trim($_POST['contact']);
    $tel = trim($_POST['telephone']);
    
    $pdo->prepare("UPDATE entreprises SET adresse_livraison=?, nom_contact=?, telephone_contact=? WHERE id=?")
        ->execute([$adresse, $contact, $tel, $entreprise_id]);
    $msg = "<div class='alert alert-success'>✅ Informations mises à jour !</div>";
}

// 2. Changement MDP (Reste identique)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_pass'])) {
    $new_pass = password_hash($_POST['new_pass'], PASSWORD_DEFAULT);
    $pdo->prepare("UPDATE utilisateurs SET mot_de_passe_hash = ? WHERE id = ?")->execute([$new_pass, $user_id]);
    $msg = "<div class='alert alert-success'>✅ Mot de passe modifié !</div>";
}

// Récupération Données
$sql = "SELECT u.nom as u_nom, u.prenom as u_prenom, u.email, e.* FROM utilisateurs u JOIN entreprises e ON u.entreprise_id = e.id WHERE u.id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$user_id]);
$infos = $stmt->fetch();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mon Profil - Naïta</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
    <nav class="navbar navbar-dark bg-dark mb-4">
        <div class="container-fluid px-4">
            <span class="navbar-brand">Mon Compte</span>
            <a href="dashboard.php" class="btn btn-outline-light btn-sm">Retour</a>
        </div>
    </nav>
    <div class="container">
        <?= $msg ?>
        <div class="row">
            <div class="col-md-6">
                <div class="card shadow-sm mb-4">
                    <div class="card-header bg-gold text-white fw-bold">🏢 Mes Coordonnées (Modifiables)</div>
                    <div class="card-body">
                        <form method="POST">
                            <input type="hidden" name="action" value="update_cse">
                            <div class="mb-3">
                                <label>Société / CSE</label>
                                <input type="text" class="form-control" value="<?= htmlspecialchars($infos['nom']) ?>" disabled>
                            </div>
                            <div class="mb-3">
                                <label>Adresse de livraison</label>
                                <textarea name="adresse" class="form-control" rows="2" required><?= htmlspecialchars($infos['adresse_livraison']) ?></textarea>
                            </div>
                            <div class="mb-3">
                                <label>Nom Contact (Secrétaire)</label>
                                <input type="text" name="contact" class="form-control" value="<?= htmlspecialchars($infos['nom_contact']) ?>" required>
                            </div>
                            <div class="mb-3">
                                <label>Téléphone</label>
                                <input type="text" name="telephone" class="form-control" value="<?= htmlspecialchars($infos['telephone_contact']) ?>">
                            </div>
                            <button type="submit" class="btn btn-primary btn-sm">Enregistrer modifications</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card shadow-sm">
                    <div class="card-header bg-dark text-white">🔒 Connexion</div>
                    <div class="card-body">
                         <form method="POST">
                            <label class="form-label">Nouveau mot de passe :</label>
                            <div class="input-group mb-3">
                                <input type="password" name="new_pass" class="form-control" required>
                                <button class="btn btn-outline-secondary" type="submit">Changer</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>