<!--Body Starts Here-->
         <div class="container">
    <div class="title">Registration</div>
    <div class="content">
                    <a href="<?php echo SITEURL; ?>admin/index.php?page=students"><img src="../box/assets/img/unnamed.png" alt="" class="header__img"></a>
                    <form method="post" action="" class="forms">
                        
                        <?php 
                            if(isset($_SESSION['validation']))
                            {
                                echo $_SESSION['validation'];
                                unset($_SESSION['validation']);
                            }
                            if(isset($_SESSION['add']))
                            {
                                echo $_SESSION['add'];
                                unset($_SESSION['add']);
                            }
                        ?>
                        <div class="user-details">
                            <div class="input-box">
                        <span class="details">First Name</span> 
                        <input type="text" name="first_name" placeholder="First Name" required="true" /> <br />
                        </div>

                             
                        <div class="input-box">
                        <span class="details">Last Name</span>
                        <input type="text" name="last_name" placeholder="Last Name" required="true" /><br />
                        </div>

                        <div class="input-box">
                        <span class="details">Email</span>
                        <input type="email" name="email" placeholder="Email Address" required="true" /><br />
                        </div>

                        <div class="input-box">
                        <span class="details">Username</span>
                        <input type="text" name="username" placeholder="Username" required="true" /><br />
                        </div>

                        <div class="input-box">
                        <span class="details">Password</span>
                        <input type="text" name="password" placeholder="Password" required="true" /><br />
                        </div>

                        <div class="input-box">
                        <span class="details">Contact</span>
                        <input type="tel" name="contact" placeholder="Contact Number" /><br />
                        </div> </div>
           <div class="gender-details">
          <input type="radio" name="gender" id="dot-1">
          <input type="radio" name="gender" id="dot-2">
          <input type="radio" name="gender" id="dot-3">
          <span class="gender-title">Gender</span>
          <div class="category">
            <label for="dot-1">
            <span class="dot one"></span>
            <span class="gender">Male</span>
          </label>
          <label for="dot-2">
            <span class="dot two"></span>
            <span class="gender">Female</span>
          </label>
          <label for="dot-3">
            <span class="dot three"></span>
            <span class="gender">Prefer not to say</span>
            </label>
          </div>
        </div>
        <div class="user">
                        
                        <span class="details">Faculty</span>
                        <select name="faculty">
                            <?php 
                                //Get Faculty from database
                                $tbl_name="tbl_class";
                                $where="is_active='yes'";
                                $query=$obj->select_data($tbl_name,$where);
                                $res=$obj->execute_query($conn,$query);
                                $count_rows=$obj->num_rows($res);
                                if($count_rows>0)
                                {
                                    while($row=$obj->fetch_data($res))
                                    {
                                        $faculty_id=$row['faculty_id'];
                                        $faculty_name=$row['faculty_name'];
                                        ?>
                                        <option value="<?php echo $faculty_id; ?>"><?php echo $faculty_name; ?></option>
                                        <?php
                                    }
                                }
                                else
                                {
                                    echo("<option value='none'>NONE</option>");
                                }
                            ?>
                        </select>
                        <br />
                    </div>
<div class="user-detailss">
                            <div class="input-box"><br>
                        <span class="details">Is Active?</span>
                        <div class="category">
         
                        Yes 
                        <input type="checkbox" name="is_active" value="yes"id="dot-1" /> 
                        No<input type="checkbox" name="is_active" value="no"id="dot-2" /> 
                        <br />
                        </div></div></div>
                         <div class="button">
                        <input type="submit" name="submit" value="Add Student"/></div>
                        
                    </form>
                     </div>
  </div>
                    <?php 
                        if(isset($_POST['submit']))
                        {
                            //Getting Values from the form
                            $first_name=$obj->sanitize($conn,$_POST['first_name']);
                            $last_name=$obj->sanitize($conn,$_POST['last_name']);
                            $email=$obj->sanitize($conn,$_POST['email']);
                            $username=$obj->sanitize($conn,$_POST['username']);
                            $password=$obj->sanitize($conn,$_POST['password']);
                            $contact=$obj->sanitize($conn,$_POST['contact']);
                            if(isset($_POST['gender']))
                            {
                                $gender=$obj->sanitize($conn,$_POST['gender']);
                            }
                            else
                            {
                                $gender='male';
                            }
                            
                            $faculty=$obj->sanitize($conn,$_POST['faculty']);
                            if(isset($_POST['is_active']))
                            {
                                $is_active=$_POST['is_active'];
                            }
                            else
                            {
                                $is_active='yes';
                            }
                            $added_date=date('Y-m-d');
                            
                            //Backend Validation, Checking whether the input fields are empty or not
                            if(($first_name||$last_name||$email||$username||$password)==null)
                            {
                                //SET SSESSION Message
                                $_SESSION['validation']="<div class='error'>First Name or Last Name, or Email or Username or Password is Empty.</div>";
                                header('location:'.SITEURL.'admin/index.php?page=add_student');
                            }
                            
                            //Addding to the database
                            $tbl_name='tbl_student';
                            $data="first_name='$first_name',
                                    last_name='$last_name',
                                    email='$email',
                                    username='$username',
                                    password='$password',
                                    contact='$contact',
                                    gender='$gender',
                                    faculty='$faculty',
                                    is_active='$is_active',
                                    added_date='$added_date',
                                    updated_date=''";
                            $query=$obj->insert_data($tbl_name,$data);
                            $res=$obj->execute_query($conn,$query);
                            if($res===true)
                            {
                                $_SESSION['add']="<div class='success'>New student successfully added.</div>";
                                header('location:'.SITEURL.'admin/index.php?page=students');
                            }
                            else
                            {
                                $_SESSION['add']="<div class='error'>Failed to add new student. Try again.</div>";
                                header('location:'.SITEURL.'admin/index.php?page=add_student');
                            }
                        }
                    ?>
                </div>
            </div>
        </div>
        <!--Body Ends Here-->