<?php
require_once __DIR__ . '/../functions.php';
ensure_logged_in();
$user = pdo()->prepare('SELECT id,name,email,role,status,profile_picture FROM users WHERE id = :id LIMIT 1');
$user->execute(['id' => current_user()['id']]);
$user = $user->fetch();
if (!$user) {
    header('Location: ' . BASE_URL . 'admin/index.php');
    exit;
}
?>
<?php include __DIR__ . '/includes/header.php'; ?>
    <section class="panel">
        <header>
            <h2>My Profile</h2>
        </header>
        <form method="post" action="actions.php" enctype="multipart/form-data" class="settings-form">
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>">
            <input type="hidden" name="action" value="update_profile">
            <input type="hidden" name="id" value="<?= clean($user['id']) ?>">
            <div class="form-grid">
                <div>
                    <label>Name</label>
                    <input type="text" name="name" value="<?= clean($user['name']) ?>" required>
                    <label>Email</label>
                    <input type="email" name="email" value="<?= clean($user['email']) ?>" required>
                    <label>Profile Picture</label>
                    <?php if (!empty($user['profile_picture'])): ?>
                        <img src="<?= BASE_URL . UPLOAD_URL . '/' . clean($user['profile_picture']) ?>" alt="Profile picture" class="image-preview">
                    <?php endif; ?>
                    <input type="file" name="profile_picture" accept="image/*,image/svg+xml">
                </div>
                <div>
                    <label>Role</label>
                    <input type="text" value="<?= clean($user['role']) ?>" disabled>
                    <label>Status</label>
                    <input type="text" value="<?= clean($user['status']) ?>" disabled>
                </div>
            </div>
            <div class="panel">
                <header><h3>Security</h3></header>
                <label>New Password</label>
                <input type="password" name="password" placeholder="Leave blank to keep current password">
                <label>Confirm Password</label>
                <input type="password" name="confirm_password" placeholder="Repeat new password">
            </div>
            <button type="submit" class="btn btn-primary">Save Profile</button>
        </form>
    </section>
<?php include __DIR__ . '/includes/footer.php'; ?>
