<?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']) && !isset($_SESSION['uname']) )

        header("Location: logout.php");

?>

<!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: 120px;"></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 Student</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">

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

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

                                                <option value="" selected disabled>Select Class *</option>

                                                <?php

                                                    $run= mysqli_query($con, "SELECT sclass,sid FROM class WHERE uploadedby = '$_SESSION[uref]'");

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

                                                        echo "

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

                                                        ";

                                                    }

                                                ?>

                                            </select>

                                        </div>

                                    </div>

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

                                    <div class="row">

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

                                    

                                    <div class="row">

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

                                            <label class="badge badge-dark">Student Remarks</label>

                                            <textarea name="remark" class="form-control" placeholder="Enter Remarks*" rows="3"></textarea>

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

                                        </div>

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

                                        <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="fa fa-upload"></i> Upload

                                            </button>

                                        </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>



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

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

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



   

</body>

</html>







<?php



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

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

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

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

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

    

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

    

    $admin_ref = $_SESSION['uref'];

    $chk_ph_count = mysqli_num_rows($chk_ph);

    if ( $chk_ph_count < 1 ) {

        $uref = rand().'_'.time();



        $ins_sql = "INSERT INTO users (uname, saddress, ucontact, dist, pin, uphone, upass, uref, utype, admin_ref, sclass,uremark, ucreated) VALUES ('$uname', '$saddress', '$ucontact', '$dist', '$pin', '$uphone', '$upass', '$uref', 'Student', '$admin_ref', '$sclass', '$remark', '$currentTime')";

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





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

            ?>

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

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

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

                }, 1200);

            </script>

            <?php

        }

    } else {

        ?>

        <script>

            alert('Mobile Number Exists');

            setTimeout(() => {

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

            }, 500);

        </script>

        <?php

    }

}



    

?>