<?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"> Edit Student</h5>

                            

                                <?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);

                                        ?>

                                            <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" value="<?php echo $edit_row['uname']; ?>">

                                                        </div>

                                                    </div>

                                                    <div class="row mb-2 mt-2">

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

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

                                                            <input type="number" class="form-control" name="ucontact"

                                                                placeholder="Enter Phone Number*" value="<?php echo $edit_row['ucontact']; ?>">

                                                        </div>

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

                                                            <a href="add-class.php"

                                                                class="text-decoration-none badge badge-dark mb-2"><i

                                                                    class="fa fa-user-plus"></i> Add Class</a>

                                                            <select name="sclass" id="sclass" class="form-control">

                                                                <option value="" selected disabled>Selected Class: <?php echo $edit_row["sclass"]?>

                                                                </option>

                                                                <?php

                                                                    $run= mysqli_query($con, "SELECT sclass FROM class");

                                                                    while ($rows = mysqli_fetch_assoc($run)) {

                                                                        echo "

                                                                            <option value='$rows[sclass]'>$rows[sclass]</option>

                                                                        ";

                                                                    }

                                                                ?>

                                                            </select>

                                                        </div>

                                                    </div>

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

                                                    <div class="row mb-2">

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

                                                            <input type="text" class="form-control" name="saddress"

                                                                placeholder="Enter Your Street Address*" value="<?php echo $edit_row['saddress']?>">

                                                        </div>

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

                                                            <input type="text" class="form-control" name="dist"

                                                                placeholder="District*" value="<?php echo $edit_row['dist']?>">

                                                        </div>

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

                                                            <input type="number" class="form-control" name="pin"

                                                                placeholder="Pin*" value="<?php echo $edit_row['pin']?>">

                                                        </div>

                                                    </div>

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

                                                    <div class="row">

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

                                                            <input type="text" class="form-control" name="uphone"

                                                                placeholder="Enter User Name" value="<?php echo $edit_row['uphone']?>">

                                                        </div>

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

                                                            <input type="password" class="form-control" name="upass"

                                                                placeholder="Enter Password" value="<?php echo $edit_row['upass']?>">

                                                        </div>

                                                    </div>

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

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

                                                        <button type="submit" class="btn btn-success btn-user m-2"      name="edit_users"><i class="fa fa-edit"></i> UPDATE DETAILS

                                                        </button>

                                                    </div>

                                                </form>

                                            </div>

                                        <?php

                                    }

                                ?>

                    

                            </div>

                        </div>

                    </div>

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



<?php



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

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

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

        $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']));

        $chk_ph = mysqli_query($con, "SELECT uphone FROM users WHERE uname != '$uname' AND uphone = '$uphone'");

        $chk_ph_count = mysqli_num_rows($chk_ph);

        echo $chk_ph_count;

        if ( $chk_ph_count < 1 ) {

            $up_sql = "UPDATE users SET uname = '$uname', ucontact = '$ucontact', saddress = '$saddress', dist = '$dist', pin = '$pin', uphone = '$uphone', upass = '$upass', ucreated = '$currentTime' ";

    

            

            if ( isset($_POST["sclass"]) ) {

                $sclass = $_POST["sclass"];

                $up_sql .= ", sclass = '$sclass'";

            }

            $up_sql .= " WHERE uid = '$_GET[edit_id]' ";



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

                ?>

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

                <?php

            }

            else{

                ?>

                <script>

                    alert('OOPS !!!!');

                    setTimeout(() => {

                        window.location = 'edit-student.php?edit_id=<?php echo $_GET["edit_id"]; ?>';

                    }, 1200);

                </script>

                <?php

            }

        } else {

            ?>

            <script>

                alert('Mobile Number Exists');

                setTimeout(() => {

                    window.location = 'edit-student.php?edit_id=<?php echo $_GET["edit_id"]; ?>';

                }, 500);

            </script>

            <?php

        }

    }



?>