<?php session_start();
include_once('includes/config.php');
include_once('includes/settings.php');
if (strlen($_SESSION['id']) == 0) {
    header('location:logout.php');
} else {

    //For updating User  Profile
    if (isset($_POST['update'])) {
        $name = $_POST['fullname'];
        $uid = $_SESSION['id'];
        $contactno = $_POST['contactnumber'];
        $query = mysqli_query($con, "update users set name='$name',contactno='$contactno' where id='$uid'");
        if ($query) {
            echo "<script>alert('Profile Updated successfully');</script>";
            echo "<script type='text/javascript'> document.location ='my-profile.php'; </script>";
        } else {
            echo "<script>alert('Something went wrong. Please try again.');</script>";
            echo "<script type='text/javascript'> document.location ='my-profile.php'; </script>";
        }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <?php include('includes/head.php'); ?>
    <style type="text/css"></style>

    <body class="cnt-home">
        <header class="header-style-1">
            <?php include('includes/top-header.php'); ?>
            <?php include('includes/main-header.php'); ?>
            <?php include('includes/menu-bar.php'); ?>
        </header>

        <!-- Breadcrumb -->
        <div class="breadcrumb mt-2">
            <div class="container">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                        <li class="breadcrumb-item active">My Profile</li>
                    </ol>
                </nav>
            </div>
        </div>

        <!-- Main Content -->
        <div class="body-content outer-top-xs">
            <div class="container">

                <?php
                $uid = $_SESSION['id'];
                $query = mysqli_query($con, "select * from users where id='$uid'");
                while ($result = mysqli_fetch_array($query)) {
                    ?>
                    <!-- Section-->
                    <section class="py-5">
                        <div class="container px-4  mt-5">

                            <form method="post" name="profile">
                                <div class="row">
                                    <div class="col-2">Full Name</div>
                                    <div class="col-6"><input type="text" name="fullname"
                                            value="<?php echo htmlentities($result['name']); ?>" class="form-control" required>
                                    </div>
                                </div>
                                <div class="row mt-3">
                                    <div class="col-2">Email Id</div>
                                    <div class="col-6"><input type="email" name="emailid" id="emailid" class="form-control"
                                            value="<?php echo htmlentities($result['email']); ?>" readonly>
                                    </div>

                                </div>

                                <div class="row mt-3">
                                    <div class="col-2">Contact Number</div>
                                    <div class="col-6"><input type="text" name="contactnumber"
                                            value="<?php echo htmlentities($result['contactno']); ?>" pattern="[0-9]{10}"
                                            title="10 numeric characters only" class="form-control" required></div>
                                </div>



                                <div class="row mt-3">
                                    <div class="col-4">&nbsp;</div>
                                    <div class="col-6"><input type="submit" name="update" id="update" class="btn btn-primary"
                                            value="Update" required></div>
                                </div>
                            </form>

                        </div>
                    <?php } ?>

            </div>
        </div><!-- /.container -->
        </div><!-- /.body-content -->

        <!-- Footer -->
        <?php include('includes/footer.php'); ?>
        <?php include('includes/foot.php'); ?>

    </html>
<?php } ?>