<?php

    session_start();
    if(!isset($_SESSION['userid'])) {
        header("Location: ./Sign&Log.php");
    }

    require './connect.php';


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

        $sql = "UPDATE users SET username = :username, firstname = :firstname, lastname = :lastname, phone= :phone, address = :address WHERE userid = :userid";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR);
        $stmt->bindParam(':firstname', $_POST['firstName'], PDO::PARAM_STR);
        $stmt->bindParam(':lastname', $_POST['lastName'], PDO::PARAM_STR);
        $stmt->bindParam(':phone', $_POST['phone'], PDO::PARAM_STR);
        $stmt->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
        $stmt->bindParam(':userid', $_SESSION['userid'], PDO::PARAM_INT);
        $stmt->execute();

        header('location: ./Profile.php');
        die();

    }

    $sql = "SELECT users.*, member.* FROM users INNER JOIN member ON member.memberid = users.userid WHERE memberid = :userid";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':userid', $_SESSION['userid'], PDO::PARAM_INT);
    $stmt->execute();


    if($stmt->rowCount() == 0) {
        $sql = "SELECT * FROM users WHERE userid = :userid";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':userid', $_SESSION['userid'], PDO::PARAM_INT);
        $stmt->execute();
        $users = $stmt->fetch(PDO::FETCH_ASSOC);

        $users['address'] = "";
        $users['phone'] = "";
        $users['firstname'] = "";
        $users['lastname'] = "";
    }
    else {
        $users = $stmt->fetch(PDO::FETCH_ASSOC);
    }

?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Profile</title>
    <link rel="stylesheet" href="CSS/EditProfile.css">
</head>
<body>
<div class="container">

        <h1>Edit Profile</h1>
        <form action="./EditProfile.php" method="POST">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" id="username" name="username" required value="<?php echo $users['username'] ?>">
            </div>

            <div class="form-group">
                <label for="firstName">First Name</label>
                <input type="text" id="firstName" name="firstName" required value="<?php
                        echo $users['firstname'];
                    ?>">
            </div>

            <div class="form-group">
                <label for="lastName">Last Name</label>
                <input type="text" id="lastName" name="lastName" required value="<?php echo $users['lastname'] ?>">
            </div>

            <div class="form-group">
                <label for="phone">Phone Number</label>
                <input type="tel" id="phone" name="phone" required value="<?php echo $users['phone'] ?>">
            </div>

            <div class="form-group">
                <label for="address">Address</label>
                <input type="text" id="address" name="address" required value="<?php echo $users['address'] ?>">
            </div>

            <div class="button-group">
                <button type="button" class="btn btn-secondary" onclick="window.history.back()">Cancel</button>
                <button type="submit" class="btn">Save Changes</button>
            </div>
        </form>
    </div>
</body>
</html>