<?php
require_once 'protege.php';
require_once '../config.php';

$cliente_id = (int)$_SESSION['cliente_id'];
$mensagem = '';
$erro = '';

$stmt = $pdo->prepare("SELECT * FROM clientes WHERE id = ? LIMIT 1");
$stmt->execute([$cliente_id]);
$cliente = $stmt->fetch();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nome = trim($_POST['nome'] ?? '');
    $whatsapp = trim($_POST['whatsapp'] ?? '');

    if (empty($nome)) {
        $erro = 'Informe seu nome.';
    } else {
        $stmt = $pdo->prepare("UPDATE clientes SET nome = ?, whatsapp = ? WHERE id = ?");
        $stmt->execute([$nome, $whatsapp, $cliente_id]);

        $_SESSION['cliente_nome'] = $nome;
        $mensagem = 'Perfil atualizado com sucesso.';

        $stmt = $pdo->prepare("SELECT * FROM clientes WHERE id = ? LIMIT 1");
        $stmt->execute([$cliente_id]);
        $cliente = $stmt->fetch();
    }
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Meu Perfil - PromoCreative LED Mapper</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
*{box-sizing:border-box}
body{margin:0;background:#060b16;color:#fff;font-family:Arial,Helvetica,sans-serif}
.topbar{padding:22px 36px;background:#08101f;border-bottom:1px solid rgba(255,255,255,.08);display:flex;justify-content:space-between;align-items:center}
.topbar a{color:#fff;text-decoration:none;background:#13223a;padding:11px 15px;border-radius:12px}
.main{max-width:760px;margin:auto;padding:36px}
.card{background:#0d182b;border:1px solid rgba(255,255,255,.08);border-radius:24px;padding:30px}
label{display:block;margin:16px 0 8px;color:#c8d6f0}
input{width:100%;padding:15px;border-radius:14px;border:1px solid rgba(255,255,255,.12);background:#08111f;color:#fff}
button{width:100%;margin-top:24px;padding:16px;border:0;border-radius:16px;background:linear-gradient(135deg,#ff7a00,#00aaff);color:#fff;font-weight:bold}
.alert{padding:14px;border-radius:14px;margin-bottom:18px}
.ok{background:rgba(0,255,160,.12);color:#b7ffd8}
.erro{background:rgba(255,60,60,.12);color:#ffc1c1}
</style>
</head>
<body>

<div class="topbar">
    <strong>PromoCreative LED Mapper</strong>
    <div>
        <a href="painel.php">Voltar</a>
        <a href="alterar_senha.php">Alterar senha</a>
        <a href="logout.php">Sair</a>
    </div>
</div>

<div class="main">
    <div class="card">
        <h1>Meu perfil</h1>

        <?php if ($mensagem): ?><div class="alert ok"><?= htmlspecialchars($mensagem); ?></div><?php endif; ?>
        <?php if ($erro): ?><div class="alert erro"><?= htmlspecialchars($erro); ?></div><?php endif; ?>

        <form method="POST">
            <label>Nome</label>
            <input type="text" name="nome" value="<?= htmlspecialchars($cliente['nome']); ?>" required>

            <label>E-mail</label>
            <input type="email" value="<?= htmlspecialchars($cliente['email']); ?>" disabled>

            <label>WhatsApp</label>
            <input type="text" name="whatsapp" value="<?= htmlspecialchars($cliente['whatsapp'] ?? ''); ?>">

            <button type="submit">Salvar alterações</button>
        </form>
    </div>
</div>

</body>
</html>