<?php
require __DIR__ . '/includes/functions.php';
customer_require_login();
$currentPage = 'account';
$pageTitle = 'My Profile | Furnify';
$customer = customer_current();
$messages = array();
$errors = array();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verify_csrf_token((string) (isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''))) {
        $errors[] = 'Security token expired. Please try again.';
    } else {
        $action = isset($_POST['action']) ? (string) $_POST['action'] : 'profile';
        if ($action === 'profile') {
            $result = customer_update_profile((int) $customer['id'], array(
                'full_name' => isset($_POST['full_name']) ? $_POST['full_name'] : '',
                'phone' => isset($_POST['phone']) ? $_POST['phone'] : '',
                'city' => isset($_POST['city']) ? $_POST['city'] : '',
                'address' => isset($_POST['address']) ? $_POST['address'] : '',
            ));
            if ($result['ok']) { $messages[] = 'Profile updated.'; $customer = customer_current(); } else { $errors[] = $result['error']; }
        } elseif ($action === 'password') {
            $new = isset($_POST['new_password']) ? (string) $_POST['new_password'] : '';
            $confirm = isset($_POST['confirm_password']) ? (string) $_POST['confirm_password'] : '';
            if ($new !== $confirm) {
                $errors[] = 'New password confirmation does not match.';
            } else {
                $result = customer_update_password((int) $customer['id'], isset($_POST['current_password']) ? $_POST['current_password'] : '', $new);
                if ($result['ok']) { $messages[] = 'Password updated.'; } else { $errors[] = $result['error']; }
            }
        }
    }
}
require __DIR__ . '/includes/header.php';
?>
<section class="page-section"><div class="container"><div class="section-heading split-heading"><div><p class="eyebrow">My account</p><h1>My Profile</h1><p class="lead">Update your contact details and password.</p></div><a class="btn btn-outline-primary" href="customer-dashboard.php">Dashboard</a></div><?php foreach ($messages as $msg): ?><div class="alert alert-success"><?= e($msg) ?></div><?php endforeach; ?><?php foreach ($errors as $error): ?><div class="alert alert-danger"><?= e($error) ?></div><?php endforeach; ?><div class="row g-4"><div class="col-lg-7"><div class="premium-panel p-4"><h2 class="h5">Contact information</h2><p class="text-muted">Email cannot be changed here because it is used for verification and order matching.</p><form method="post" class="row g-3"><input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>"><input type="hidden" name="action" value="profile"><div class="col-md-6"><label class="form-label">Full name</label><input class="form-control" name="full_name" required value="<?= e(array_get($customer, 'full_name', '')) ?>"></div><div class="col-md-6"><label class="form-label">Phone</label><input class="form-control" name="phone" required value="<?= e(array_get($customer, 'phone', '')) ?>"></div><div class="col-md-6"><label class="form-label">Email</label><input class="form-control" value="<?= e(array_get($customer, 'email', '')) ?>" disabled><div class="form-text"><?= customer_is_email_verified($customer) ? 'Verified' : 'Not verified yet' ?></div></div><div class="col-md-6"><label class="form-label">City</label><input class="form-control" name="city" value="<?= e(array_get($customer, 'city', '')) ?>"></div><div class="col-12"><label class="form-label">Address / landmark</label><input class="form-control" name="address" value="<?= e(array_get($customer, 'address', '')) ?>"></div><div class="col-12"><button class="btn btn-primary" type="submit">Save profile</button></div></form></div></div><div class="col-lg-5"><div class="premium-panel p-4"><h2 class="h5">Change password</h2><form method="post" class="row g-3"><input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>"><input type="hidden" name="action" value="password"><div class="col-12"><label class="form-label">Current password</label><input class="form-control" type="password" name="current_password" required></div><div class="col-12"><label class="form-label">New password</label><input class="form-control" type="password" name="new_password" minlength="8" required></div><div class="col-12"><label class="form-label">Confirm new password</label><input class="form-control" type="password" name="confirm_password" minlength="8" required></div><div class="col-12"><button class="btn btn-outline-primary" type="submit">Update password</button></div></form></div></div></div></div></section>
<?php require __DIR__ . '/includes/footer.php'; ?>
