<?php
require_once __DIR__ . '/../system/customer/auth.php';
require_once __DIR__ . '/../system/customer/middleware.php';

customer_auth();

$user = customer_user();
$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();

    $name = trim($_POST['name']);
    $phone = trim($_POST['phone']);
    $email = strtolower(trim($_POST['email']));

    $stmt = $pdo->prepare("UPDATE customers_login SET name=?, phone=?, email=? WHERE id=?");
    $stmt->execute([$name, $phone, $email, $user['id']]);

    if (!empty($user['customer_id'])) {
        $stmt = $pdo->prepare("UPDATE customers SET name=?, phone=?, email=? WHERE id=?");
        $stmt->execute([$name, $phone, $email, $user['customer_id']]);
    }

    $_SESSION['customer_login']['name'] = $name;
    $_SESSION['customer_login']['phone'] = $phone;
    $_SESSION['customer_login']['email'] = $email;

    $user = customer_user();
    $message = 'Profile berhasil diperbarui.';
}
?>
<!DOCTYPE html><html lang="id"><head><meta charset="UTF-8"><title>Profile</title><script src="https://cdn.tailwindcss.com"></script></head>
<body class="bg-slate-100 p-6">
<div class="max-w-xl mx-auto bg-white rounded-3xl shadow p-6">
    <div class="flex justify-between mb-6"><h1 class="text-3xl font-black">Profile</h1><a href="dashboard.php" class="text-green-700 font-bold">Dashboard</a></div>
    <?php if ($message): ?><div class="bg-green-100 text-green-700 p-4 rounded-2xl mb-5"><?= e($message) ?></div><?php endif; ?>
    <form method="POST" class="space-y-4">
        <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">
        <input name="name" required value="<?= e($user['name']) ?>" class="w-full border rounded-xl px-4 py-3">
        <input name="phone" value="<?= e($user['phone'] ?? '') ?>" placeholder="Phone" class="w-full border rounded-xl px-4 py-3">
        <input type="email" name="email" required value="<?= e($user['email']) ?>" class="w-full border rounded-xl px-4 py-3">
        <button class="bg-green-600 text-white px-5 py-3 rounded-2xl font-bold w-full">Simpan Profile</button>
    </form>
</div></body></html>
