<?php

//include html header
require_once("includes/html-header.php");


// Code for change password	
if (isset($_POST['submit'])) {
    $final_file = "";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobileno = $_POST['mobile'];
    $idedit = $_POST['editid'];
    $image = "image.jpg";
    //if (move_uploaded_file($file_loc, $folder . $final_file)) {
    $image = $final_file;
    //}

    $sql = "UPDATE users SET name=(:name), email=(:email), mobile=(:mobileno), Image=(:image) WHERE id=(:idedit)";
    $query = $dbh->prepare($sql);
    $query->bindParam(':name', $name, PDO::PARAM_STR);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->bindParam(':mobileno', $mobileno, PDO::PARAM_STR);
    $query->bindParam(':image', $image, PDO::PARAM_STR);
    $query->bindParam(':idedit', $idedit, PDO::PARAM_STR);
    $query->execute();
    $_SESSION['Success'] = "প্রোফাইল আপলোড হয়েছে।";
}

?>

<body>
<?php
    $email = $_SESSION['alogin'];
    $sql = "SELECT * from users where email = (:email);";
    $query = $dbh->prepare($sql);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->execute();
    $result = $query->fetch(PDO::FETCH_OBJ);
?>

    <!-- Body Wrapper -->
    <div class="page-wrapper" id="main-wrapper" data-layout="vertical" data-navbarbg="skin6" data-sidebartype="full" data-sidebar-position="fixed" data-header-position="fixed">
        <?php
        //include sidebar
        require_once("includes/sidebar.php");
        ?>

        <!-- Main wrapper -->
        <div class="body-wrapper">
            <?php
            //include header
            require_once("includes/header.php");
            ?>

            <div class="container-fluid">
                <!-- notice -->
                <marquee class="alert alert-dark" onmouseover="this.stop()" onmouseout="this.start()" role="alert" style="font-size: 1rem;">
                    <?php
                        $se = "SELECT * FROM notice";
                        $data = mysqli_query($link, $se);
                        $d = mysqli_fetch_assoc($data);
                        echo $d['notice'];
                    ?>
                </marquee>
                <!-- notice end -->

                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title fw-semibold mb-4 text-center">প্রোফাইল আপডেট করুন।</h5>
                        <div class="card">
                            <div class="card-body">
                                <form name="chngpwd" onSubmit="return valid();" method="POST">
                                    <?php
                                        if (isset($_SESSION['Error'])) {
                                    ?>
                                        <div class="alert alert-danger" role="alert">
                                            <?php 
                                            echo $_SESSION['Error'];
                                            $_SESSION['Error'] = null;
                                            ?>
                                        </div>
                                    <?php
                                        } elseif (isset($_SESSION['Success'])) {
                                    ?>
                                        <div class="alert alert-success" role="alert">
                                            <?php 
                                            echo $_SESSION['Success'];
                                            $_SESSION['Success'] = null;
                                            ?>
                                        </div>
                                    <?php
                                        }
                                    ?>
                                    
                                    <div class="mb-3 text-center">
                                        <label for="name" class="form-label">নামঃ</label>
                                        <input type="text" name="name" class="form-control" id="name" required value="<?php echo htmlentities($result->name); ?>">
                                    </div>
                                    
                                    <div class="mb-3 text-center">
                                        <label class="form-label">ইমেইলঃ</label>
                                        <input type="text" class="form-control" name="email" id="email" required value="<?php echo htmlentities($result->email); ?>">
                                    </div>

                                    <div class="mb-3 text-center">
                                        <label class="form-label">মোবাইল নাম্বারঃ</label>
                                        <input type="number" class="form-control" name="mobile" id="mobile" required value="<?php echo htmlentities($result->mobile); ?>">
                                    </div>

                                    <input type="hidden" name="editid" class="form-control" required value="<?php echo htmlentities($result->id); ?>">

                                    <div class="mb-3 text-center">
                                        <button type="submit" name="submit" class="btn btn-outline-primary m-1">পরিবর্তন।</button>
                                    </div>
                                    
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                    <li class="mb-3 text-center">
                        <a class="mb-3 text-link" href="https://t.me/Owner_Of_BFT" aria-expanded="false">
                        <span>
                            <i class="ti-info" style="color: green"></i>
                        </span>
                       <span class="hide-menu" style="font-size: 12px;"> সাপোর্ট ✍️</span>
                     </a>
                    </li>
                </ul>
            </nav>
        </div>
    </aside>
                <?php
                // Function to get the user's real IP address
                function getRealIP() {
                    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                        // If the user is behind a proxy, return the first IP in the forwarded list
                        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                    } else {
                        // Otherwise, return the remote address
                        $ip = $_SERVER['REMOTE_ADDR'];
                    }
                    return $ip;
                }

                // Function to check if the user is using a proxy
                function isUsingProxy() {
                    return !empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['HTTP_CLIENT_IP']);
                }

                // Get the real user's IP and check if they are using a proxy
                $user_ip = getRealIP();
                $proxy_message = '';
                if (isUsingProxy()) {
                    $proxy_message = "Your  IP is: $user_ip";
                }
                ?>

                <?php if ($user_ip): ?>
                    <div class="alert alert-info text-center">
                        <p>Your IP Address: <strong><?php echo $user_ip; ?></strong></p>
                    </div>
                <?php endif; ?>


            </div>
        </div>
    </div>
    

    <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/sidebarmenu.js"></script>
    <script src="assets/js/app.min.js"></script>
    <script src="assets/libs/simplebar/dist/simplebar.js"></script>

</body>
</html>
