<?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: 150px;"></div>



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

<div class="container">

                    <div class="row justify-content-center">

                    <div class="col-md-8">

                        <div class="card shadow mt-3">

                        <h5 class="card-header text-dark  text-center font-weight-bold"> Add Admin</h5>

                            <div class="card-body">

                                <form method="POST">

                                    <div class="row">

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

                                            <label class="badge badge-dark">Name</label>

                                            <input type="text" class="form-control" name="uname" placeholder="Enter Name" required>

                                        </div>

                                    </div>

                                    <div class="row mb-2">

                                        <div class="col-md-6 mt-2">

                                            <label class="badge badge-dark">Phone No</label>

                                            <input type="number" class="form-control" name="ucontact" placeholder="Enter Phone Number*" required>

                                        </div>

                                        <div class="col-md-6 mt-2">

                                        <label class="badge badge-dark">Qualification</label>

                                            <input type="text" class="form-control" name="uqualification" placeholder="Enter Qualification*" required>

                                        </div>

                                    </div>

                                    <h4 class="badge badge-dark">Address</h4>

                                    <div class="row mb-2">

                                        <div class="col-md-6 mb-2">

                                            <input type="text" class="form-control" name="saddress" placeholder="Enter Your Street Address*" required>

                                        </div>

                                        <div class="col-md-4 mb-2">

                                            <input type="text" class="form-control" name="dist" placeholder="District*" required>

                                        </div>

                                        <div class="col-md-2 mb-2">

                                            <input type="number" class="form-control" name="pin" placeholder="Pin*" required>

                                        </div>

                                    </div>

                                    <h4 class="badge badge-dark">Login Details</h4>

                                    <div class="row">

                                        <div class="col-md-6 mb-2">

                                        <input type="text" class="form-control" name="uphone" placeholder="Enter User Name" required>

                                        </div>

                                        <div class="col-md-6 mb-2">

                                        <input type="password" class="form-control"name="upass" placeholder="Enter Password" required>

                                        </div>

                                    </div>

                                    <div class=" row justify-content-center">

                                        <button type="submit" class="btn btn-success text-sm-center mt-4" name="submit">

                                            <i class="fas fa-upload"></i> Upload

                                        </button>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

                <?php



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

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

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

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

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

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

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

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

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

    $admin_ref = $_SESSION['uref'];

    // created user reference code

        $slicedRandom = substr(mt_rand(), 0, 2);

        $customTime = date("s");

        $upperUserStr = strtoupper($uname);

        $firstTwoCharactersOfUser = substr($upperUserStr, 0, 2);

        $ref_code = $firstTwoCharactersOfUser.$customTime.$slicedRandom;



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

    $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,uref_code, ucontact, uqualification, saddress, dist, pin, uphone, upass, uref, utype, admin_ref, ucreated) VALUES ('$uname','$ref_code', '$ucontact', '$uqualification', '$saddress', '$dist', '$pin', '$uphone', '$upass', '$uref', 'Admin', '$admin_ref', '$currentTime')";

        //  echo "<pre>";print_r($ins_sql);exit();





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

            ?>

            <script>window.location = 'view-admin.php';</script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'add-admin.php';

                }, 1200);

            </script>

            <?php

        }

    } else {

        ?>

        <script>

            alert('Mobile Number Exists');

            setTimeout(() => {

                window.location = 'add-admin.php';

            }, 500);

        </script>

        <?php

    }

}



    

?>

</div>

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

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



    <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>







