<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

if (empty($_SESSION['user_id']))
{
    header('Location: login.php');
    exit();
}

$userId = (int)$_SESSION['user_id'];

$servername = "localhost";
$port = 3306;
$username = "FSST";
$password = "L9wUNZZ9Qkbt";
$db = "FSST";

$conn = mysqli_connect($servername, $username, $password, $db, $port);
if (!$conn)
{
    http_response_code(500);
    die("Datenbankfehler");
}

$stmt = mysqli_prepare($conn, "SELECT userID, displayName, email, profilePicture FROM users WHERE userID = ? LIMIT 1");
if (!$stmt)
{
    http_response_code(500);
    die("Datenbankfehler");
}

mysqli_stmt_bind_param($stmt, "i", $userId);
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);

if ($result)
{
    $user = mysqli_fetch_assoc($result);
}
else
{
    $user = null;
}

mysqli_stmt_close($stmt);
mysqli_close($conn);

if (!$user)
{
    session_unset();
    session_destroy();
    header('Location: login.php');
    exit();
}

include 'header.php';
?>

<link rel="stylesheet" href="assets/css/login.css">

<main class="auth" role="main">
    <section class="auth__grid auth__card__side" aria-label="Account Bereich">
        <div class="auth__card auth__card__side__picture">
            <header class="auth__header">
                <img src="<?php echo htmlspecialchars($user['profilePicture']); ?>"
                     alt="Profilbild"
                     width="240">
            </header>
        </div>

        <div class="auth__card">
            <header class="auth__header">
                <p class="auth__title">Username: <?php echo htmlspecialchars($user['displayName'], ENT_QUOTES, 'UTF-8'); ?></p>
                <p class="auth__title">UserID: <?php echo (int)$user['userID']; ?></p>
                <p class="auth__title">Email: <?php echo htmlspecialchars($user['email']); ?></p>
            </header>

            <?php if (isset($_GET['upload']) && $_GET['upload'] === 'ok'): ?>
                <p class="auth__alert__sucess" role="status">Upload erfolgreich.</p>
            <?php endif; ?>

            <?php if (isset($_GET['upload']) && $_GET['upload'] === 'err'): ?>
                <p class="auth__alert__error" role="alert">Upload fehlgeschlagen. Bitte eine gültige Bilddatei auswählen.</p>
            <?php endif; ?>
        </div>

        <div class="auth__card">

            <form class="auth__form" action="upload.php" method="post" enctype="multipart/form-data">
                <div class="auth__field">
                    <label for="file">Profilbild auswählen</label>
                    <input type="file" name="uploadFile" id="file" accept="image/*" required>
                    <p class="auth__tip">Erlaubt: JPG/PNG. max. 20MB.</p>
                </div>

                <div class="auth__actions">
                    <button class="auth__submit" type="submit">Hochladen</button>
                </div>
            </form>
        </div>
        <div class="auth__card">
            <form>
                <a href="productAdder.php" class="auth__actions"> <br>
                <button class="auth__submit" type="button"">Produkt hinzufügen</button>
                </a>
            </form>
        </div>
    </section>
</main>

<?php include 'footer.php'; ?>