<?php
include('../db/model.php');
$obj = new Model();
// $tableHeight='';
$id = 0;
$user = null;
if (isset($_POST['id']) && is_numeric($_POST['id'])) {
    $id = (int)$_POST['id'];
}

if ($id > 0 && $id != null) {
    $user = $obj->GetUser($id, "id");
}

if ($_POST['type'] == 'up' && $user != null) {
    $fullName = $_POST['fullName'];

    $rr = $obj->UpdateUserProfile($_POST);

    if (isset($_FILES['profilePhoto']) && $_FILES['profilePhoto']['error'] == 0) {

        $photo = $_FILES['profilePhoto'];
        $newImagePath = "../img/user/" . $user['username'] . '.jpg';


        // Move the uploaded image to the target directory
        if (!move_uploaded_file($photo['tmp_name'], $newImagePath)) {
            echo "photo_error";
            exit;
        }
    }
    return $rr;
}

$_userProfileImage = "../img/user/" . $user["username"] . '.jpg';
if (!file_exists($_userProfileImage)) {
    $_userProfileImage = "../img/user/default_user.png";
}

?>
<div class="modal fade" id="profileModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="profileModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header text-center">
                <h1 class="modal-title fs-5 w-100" id="profileModalLabel">Update Profile</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            
            <div id="custom-form-light">
                <div class="modal-body">

                    <input type="hidden" name="mId" id="mId" value="<?php echo $user['id']; ?>">

                    <div class="mb-3">
                        <label for="mUserName">User Name</label>
                        <input type="text" name="mUserName" id="mUserName" value="<?php echo $user['username']; ?>" readonly class="form-control">
                    </div>
                    <div class="mb-3">
                        <label for="mFullName">Full Name</label>
                        <input type="text" name="mFullName" id="mFullName" value="<?php echo $user['full_name']; ?>" class="form-control">
                    </div>

                    <div class="mb-3">
                        <label for="" class="w-100">Profile Photo</label>
                        <img src="<?php echo $_userProfileImage; ?>" data-src="<?php echo $_userProfileImage; ?>" id="profileImage" alt="" width="100" height="100" class="me-5 mb-2" style="border: 1px solid var(--s-text-color);">
                        <input type="file" accept="image/*" name="mProfilePhoto" id="mProfilePhoto" onchange="PreviewImage(event)">
                    </div>

                </div>
                <div class="modal-footer">
                    <div class="validationErrorMsg"></div>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" name="updateProfile" class="btn btn-primary" onclick="UpdateUser()">Update</button>
                </div>
            </div>
            <!-- </form> -->
        </div>
    </div>
</div>