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

// Code for change password    
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $telegram_username = $_POST['telegram_username']; // Get the Telegram username
    $idedit = $_POST['editid'];

    // Update query to include the Telegram username
    $sql = "UPDATE users SET name=(:name), telegram_username=(:telegram_username) WHERE id=(:idedit)";
    $query = $dbh->prepare($sql);
    $query->bindParam(':name', $name, PDO::PARAM_STR);
    $query->bindParam(':telegram_username', $telegram_username, PDO::PARAM_STR); // Bind Telegram username
    $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 text-white" 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" readonly 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" readonly value="<?php echo htmlentities($result->mobile); ?>">
                                </div>

                                <div class="mb-3 text-center">
                                    <label class="form-label">টেলিগ্রাম ইউজারনেমঃ</label>
                                    <input type="text" class="form-control" name="telegram_username" id="telegram_username" value="<?php echo htmlentities($result->telegram_username); ?>">
                                </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>
                                    <button type="button" id="getChatIdBtn" class="btn btn-outline-info m-1" onclick="openTelegramBot()">Chat ID পান</button>
                                </div>
                                
                            </form>
                        </div>
                    </div>
                </div>
            </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>

    <script>
        function openTelegramBot() {
            // Open the @chatIDrobot bot directly
            window.open(`https://t.me/chatIDrobot`, '_blank');
        }
    </script>
</body>

</html>
