<?php
    session_start();
    if(!isset($_SESSION['user_ID'])) {
        header("Location: ./Log In.php");
    }
    
    require './connect.php';

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

        $res = mysqli_query($conn, "SELECT users_information.*, users.username, users.email FROM users_information INNER JOIN users ON users_information.user_id = users.user_ID WHERE 
            users.user_id = {$_SESSION['user_ID']};
        ");

        if(mysqli_num_rows($res) == 0) {
            mysqli_query($conn, "INSERT INTO users_information(user_id, name, surname, phone_number, city, address, postal_code) VALUES (
                {$_SESSION['user_ID']}, '{$_POST['name']}', '{$_POST['surname']}', '{$_POST['phone_number']}', '{$_POST['city']}', '{$_POST['address']}', '{$_POST['postal_code']}');
            ");
        }
        else {
            mysqli_query($conn, "UPDATE users_information SET name = '{$_POST['name']}', surname = '{$_POST['surname']}', phone_number = '{$_POST['phone_number']}',
                city = '{$_POST['city']}', address = '{$_POST['address']}', postal_code = '{$_POST['postal_code']}' WHERE user_id = {$_SESSION['user_ID']};
            ");
        }

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

    $res = mysqli_query($conn, "SELECT users_information.*, users.username, users.email FROM users_information INNER JOIN users ON users_information.user_id = users.user_ID WHERE 
        users.user_id = {$_SESSION['user_ID']};
    ");

    $user_data = $res->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="./CSS/EditProfile.css">
        <title>Edit Profile</title>
    </head>
    <body>
            <form action="./EditProfile.php" method="POST">
                <div class="form-section">
                    <div class="form-card">
                        <label for="name">Име:</label>
                        <input type="text" name="name" required value="<?php
                            if(mysqli_num_rows($res) != 0) {
                                echo $user_data['name'];
                            }
                        ?>">
                    </div>
                    <div class="form-card">
                        <label for="surname">Презиме:</label>
                        <input type="text" name="surname" required value="<?php
                            if(mysqli_num_rows($res) != 0) {
                                echo $user_data['surname'];
                            }
                        ?>">
                    </div>
                </div>
                <div class="form-section">
                    <div class="form-card">
                        <label for="phone-number">Тел. Број:</label>
                        <input type="tel" name="phone_number" required value="<?php
                            if(mysqli_num_rows($res) != 0) {
                                echo $user_data['phone_number'];
                            }
                        ?>">
                    </div>
                    <div class="form-card">
                        <label for="city">City:</label>
                        <input type="text" name="city" required value="<?php
                            if(mysqli_num_rows($res) != 0) {
                                echo $user_data['city'];
                            }
                        ?>">
                    </div>
                </div>
                <div class="form-section">
                    <div class="form-card">
                        <label for="address">Address:</label>
                        <input type="text" name="address" required value="<?php
                            if(mysqli_num_rows($res) != 0) {
                                echo $user_data['address'];
                            }
                        ?>">
                    </div>
                    <div class="form-card">
                        <label for="postal-code">Postal Code:</label>
                        <input type="text" name="postal_code" required value="<?php
                            if(mysqli_num_rows($res) != 0) {
                                echo $user_data['postal_code'];
                            }
                        ?>">
                    </div>
                </div>
                <div class="form-section" style="display: flex; justify-content: center;">
                    <a href="Profile.php">Back</a>
                    <input type="submit" value="Save">
                </div>
            </form>
    </body>
</html>