<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('M j, Y g:i a');



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {

        header("Location: logout.php");

        exit();

    }

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {

        header("Location: logout.php");

        exit();

    }

?>

<!DOCTYPE html>

<html lang="en">

<head>

 <?php include "includes/meta.php"; ?>        

    <?php include "includes/css.php"; ?>  

  

</head>

<body id="page-top" class="mt-55">



    <div style="margin-top: 70px;"></div>



      <?php include "includes/header2.php"; ?>

<div class="container">

            <div class="row mx-0">

                <div class="col-12 col-sm-12 col-md-12">

                        <div class="card-body">



                                <h1 class="h3 mb-2 text-gray-800 text-center">Profile</h1>

                                <hr>



                                <?php

                                    $edit_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]'");

                                    $edit_row = mysqli_fetch_assoc($edit_run);

                                ?>

                                <form class="user" method="post" enctype="multipart/form-data">

                                    <h4 class="mb-3 badge badge-secondary">User Name</h4>

                                    <div class="form-group row">

                                        <div class="col-sm-12 mb-1 mb-sm-0">

                                            <input type="text" class="form-control form-control-sm mb-2" id="uname"

                                                placeholder="Name of Master Admin *" name="uname"

                                                value="<?php echo $edit_row["uname"]; ?>" required>

                                        </div>

                                    </div>

                                    <h4 class="mb-3 badge badge-secondary">Login Details</h4>

                                    <div class="form-group row">

                                        <div class="col-sm-6 mb-1 mb-sm-0">

                                            <input type="text" class="form-control form-control-sm mb-2" id="uphone"

                                                placeholder="Phone Number" name="uphone"

                                                value="<?php echo $edit_row["uphone"]; ?>" required>

                                        </div>

                                        <div class="col-sm-6 mb-1 mb-sm-0">

                                            <input type="text" class="form-control form-control-sm mb-2" id="upass"

                                                placeholder="Password *" name="upass"

                                                value="<?php echo $edit_row["upass"]; ?>" required>

                                        </div>

                                    </div>

                                    <button type="submit" class="btn btn-success btn-user m-2" name="edit_cat">

                                        <i class="fa fa-image"></i> Update Profile

                                    </button>

                                    <a href="index.php" class="btn btn-warning btn-user m-2"> <i class="fa fa-ban"></i>

                                        Cancel </a>

                                </form>



                                <hr>



                            </div>

                        </div>

                    </div>



                </div>



            </div>

<div style="margin-top: 135px;"></div>

              <?php include "includes/ft.php"; ?>

              <?php include "includes/js.php"; ?>



</body>

</html>







<?php

    if(isset($_POST['edit_cat'])){

        $uname = mysqli_real_escape_string($con, strip_tags($_POST['uname']));

        $uphone = mysqli_real_escape_string($con, strip_tags($_POST['uphone']));

        $upass = mysqli_real_escape_string($con, strip_tags($_POST['upass']));



        $up_sql = "UPDATE users SET uname = '$uname', uphone = '$uphone', upass = '$upass' WHERE uid = '$_SESSION[uid]'";



        $_SESSION["uname"] = $uname;



        if(mysqli_query($con, $up_sql)){

            ?>

            <script>window.location = 'index.php';</script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'profile.php';

                }, 500);

            </script>

            <?php

        }

    }

?>