<?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>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Admin Panel</title>

    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/alltable.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
</head>
<body id="page-top" class="mt-55">

<div class="overlay"></div>

    <div id="wrapper">

        <div id="content-wrapper" class="d-flex flex-column">

            <div id="content">

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

                <div class="container-fluid px-0">

                    <div class="card shadow mb-4">
                        <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="far fa-image"></i> Update Profile
                                </button>
                                <a href="index.php" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Cancel </a>
                            </form>

                            <hr>

                        </div>
                    </div>

                </div>

            </div>

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

        </div>

    </div>





    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/sb-admin-2.min.js"></script>
    <script src="js/sweetalert2.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap4.min.js"></script>
    <script src="js/datatables.mark.min.js"></script>
    <script src="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>

</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']));
		$chk_ph = mysqli_query($con, "SELECT uphone FROM users WHERE uname != '$uname' AND uphone = '$uphone'");
        $chk_ph_count = mysqli_num_rows($chk_ph);
        if ( $chk_ph_count < 1 ) {

        $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 = 'profile.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'profile.php';
                }, 500);
            </script>
			 <?php
            }
        } else {
            ?>
            <script>
                alert('Mobile Number Exists');
                setTimeout(() => {
                    window.location = 'profile.php';
                }, 500);
            </script>
			
            <?php
			
			
			
        }
		
		
		
		
		
    }
?>