<?php
    session_start();
    include "../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('jS F Y');

    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"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet"> 
       
 
    
</head>

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

  <!--==========================
  Header
  ============================-->
  
  <?php include "includes/header2.php"; ?>


<div class="container">
            <div class="row mx-0 just ify-content-center">
                <div class="col-12 col-sm-12 col-md-12">
                        <div class="card-body">

                        
                            <h1 class="h3 mb-2 text-gray-800 text-center" style="margin-top: 70px">Second Level Admin Profile</h1>
                            <hr>

                            <?php
                                if ( isset($_GET["edit_id"]) ) {
                                    $edit_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_GET[edit_id]'");
                                    $edit_row = mysqli_fetch_assoc($edit_run);
                                    ?>
                                    <form class="user" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-3 badge badge-secondary">Edit Admin Profile</h4>
                                        <div class="form-group row ">
										
                                            <div class="col-sm-3 col-md-3 col-lg-3">
													Name <br>
                                                <input type="text" class="form-control form-control-sm mb-2" id="uname" placeholder="Name of Level 2 Admin *" name="uname" value="<?php echo $edit_row["uname"]; ?>" required>
                                            </div>
                                     
                                       
                                            <div class="col-sm-3 col-md-3  col-lg-3">
											Mobile<br>
                                                <input type="text" class="form-control form-control-sm mb-2" id="uphone" placeholder="Mobile Number *" name="uphone" value="<?php echo $edit_row["uphone"]; ?>" required>
                                            </div>
                                            <div class="col-sm-3 col-md-3  col-lg-3">
											Password<br>
                                                <input type="password" 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_users">
                                            <i class="far fa-image"></i> Update Admin Profile
                                        </button>
                                        <a href="admin_lvl2.php" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Cancel </a>
                                    </form>
									 	
                                    <?php
                                } else {
                                    ?>
									
									  <?php
                                if ( isset($_GET["see_id"]) ) {
                                    $see_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_GET[see_id]'");
                                    $see_row = mysqli_fetch_assoc($see_run);
                                    ?>
                               
                                        <h4 class="mb-3 badge badge-secondary">see Admin Profile</h4>
										
										
										
                                        
                         
                                        
                                        <div class="form-group row">
										<div class="col-sm-3 mb-1 mb-sm-0">
										</div>
                                            <div class="col-sm-6 mb-1 mb-sm-0">
											
                                            <br> Name - <?php echo $see_row["uname"]; ?>
											 <br>
											 Main Mobile - <a href="tel:+91<?php echo $see_row["uphone"]; ?>"> <?php echo $see_row["uphone"]; ?></a>									 

											 <br>
											   Create Date - <?php echo $see_row["ucreated"]; ?> <br>
											 
											Password-   <?php echo $see_row["upass"]; ?> <br>
											Type of Admin -   <?php echo $see_row["utype"]; ?> <br>

                                            <a href="admin_lvl2.php" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Back </a>
                                            <a href="nav_lv2.php?nav_for=<?php echo $_GET['see_id']; ?>" class="btn btn-success btn-user m-2"> Nav Bar </a>
											
                                     
                                       
									
									  </div>
									</div>
									
									
                                    <?php
                                } else {
                                    ?>
                                    <form class="user" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-3 badge badge-secondary">Add New Admin</h4>
                                        <div class="form-group row">
                                            <div class="col-sm-3 col-md-3  col-lg-3">
                                                <input type="text" class="form-control form-control-sm mb-2" id="uname" placeholder="Name of Admin  *" name="uname" required>
                                            </div>
											<div class="col-sm-3 col-md-3  col-lg-3">
                                                <input type="text" class="form-control form-control-sm mb-2" id="uphone" placeholder="Main Mobile Number*" name="uphone" required>
                                            </div>
											<div class="col-sm-3 col-md-3  col-lg-3">
                                                <input type="text" class="form-control form-control-sm mb-2" id="upass" placeholder="Password*" name="upass" required>
                                            </div>
											
											
                                        </div>
                                       
										
										 
										 
										
                                        
                                        <button type="submit" class="btn btn-primary btn-user m-2" name="new_admin">
                                            <i class="far fa-image"></i> Add New Admin 
                                        </button>
                                    </form>
                                    <?php
                                }
								}
                            ?>

                            <hr>

                            <div class="row m-0">
                                <h4 class="mb-3 badge badge-secondary">Users</h4>

                                <div class="col-md-12 p-0">
                                    <div class="table-responsive">
                                        <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                            <thead>
                                                <tr>
                                                    <th>No.</th>
                                                    <th>Name</th>
                                                 
                                                    <th>User</th>
                                            
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM users WHERE utype = 'basic_lvl_admin' ORDER BY uid DESC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                        echo "
                                                        <tr>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'>$c</span>
																
                                                            </td>
                                                            <td><span class='badge badge-info badge-pill'>Uid-$rows[uid]</span><br>
                                                             <a href='admin_lvl2.php?see_id=$rows[uid]' cla ss='m-2 btn btn-success btn-sm  title='Click to Edit'>     $rows[uname]  </a>
<br>
														   
                                                          
															                                                          
														  </td>
                                                          
                                                            <td class='align-middle'>
                                                                 <a href='tel:$rows[uphone]' class='small text-dark' title='Click here to Call'> <i class='fas fa-phone fa-rotate-90'></i> $rows[uphone] </a>
                                                                <p class='small text-dark mb-0'> <i class='fas fa-lock'></i> $rows[upass] </p>  </td>
                                                           
                                                            <td class='text-center align-middle'>
                                                                <a href='admin_lvl2.php?edit_id=$rows[uid]' class='m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a>

                                                                <a href='admin_lvl2.php?del_id=$rows[uid]' class='delete m-2 btn btn-danger btn-sm rounded-circle' title='Click to Delete'> <i class='fas fa-trash'></i> </a>
                                                            </td>
                                                        </tr>
                                                        ";
                                                        $c++;
                                                    }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </div>

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

 <!--==========================
    Footer
  ============================-->

  
  <!-- #footer -->

    
 
     <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="vendor/croppie/croppie.js"></script>
    <script src="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>
     <script src="js/mobile-nav/mobile-nav.js"></script>
    
	

</body>
</html>


<?php

    if(isset($_POST['new_admin'])){
        $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 uphone = '$uphone'");
        $chk_ph_count = mysqli_num_rows($chk_ph);
        if ( $chk_ph_count < 1 ) {
            $uref = rand().'_'.time();

            $ins_sql = "INSERT INTO users (uname, uphone, upass, uref, utype, ucreated) VALUES ('$uname', '$uphone', '$upass', '$uref', 'basic_lvl_admin', '$currentTime')";

            if(mysqli_query($con, $ins_sql)){
                ?>
                <script>window.location = 'admin_lvl2.php';</script>
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'admin_lvl2.php';
                    }, 1200);
                </script>
                <?php
            }
        } else {
            ?>
            <script>
                alert('Mobile Number Exists');
                setTimeout(() => {
                    window.location = 'admin_lvl2.php';
                }, 500);
            </script>
            <?php
        }
    }




    if ( isset($_GET["del_id"]) ) {

        $del_sql = "DELETE FROM users WHERE uid = '$_GET[del_id]'";

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = "admin_lvl2.php";</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'admin_lvl2.php';
                }, 1200);
            </script>
            <?php
        }
        
    }





    if(isset($_POST['edit_users'])){
        $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='$_GET[edit_id]' ";
   
		 if(mysqli_query($con, $up_sql)){

       
            ?>
            <script>
                alert("Data Updated Successfully");
                window.location = 'admin_lvl2.php';
            </script>

        
               
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'admin_lvl2.php';
                    }, 1200);
                </script>
                <?php
            }

        
    }

?>