<?php
include('session_validator.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Students</title>

    <!-- Stylesheets -->
    <link rel="stylesheet" href="css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/prism/prism.css" media="screen">
    <link rel="stylesheet" href="css/select2/select2.min.css">
    <link rel="stylesheet" href="nepali.datepicker.v4.0.1/css/main.css" media="screen">
    <link href="nepali.datepicker.v4.0.1/css/nepali.datepicker.v4.0.1.min.css" rel="stylesheet" type="text/css"/>

    <!-- Scripts -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="nepali.datepicker.v4.0.1/js/nepali.datepicker.v4.0.1.min.js"></script>
    <script src="js/modernizr/modernizr.min.js"></script>
    

    <script>
        window.onload = function () {
            var mainInput1 = document.getElementById("studentDOB");
            var mainInput2 = document.getElementById("lastpaiddate");
            // var mainInput3 = document.getElementById("joining_date");
            mainInput1.nepaliDatePicker({
                ndpYear: true,
                ndpMonth: true,
                ndpYearCount: 100,
            });

            mainInput2.nepaliDatePicker({
                ndpYear: true,
                ndpMonth: true,
                ndpYearCount: 100,
            });

            // mainInput3.nepaliDatePicker({
            //     ndpYear: true,
            //     ndpMonth: true,
            //     ndpYearCount: 100,
            // });
        };
    </script>
</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <!-- ========== TOP NAVBAR ========== -->
        <?php include('includes/topbar.php'); ?>
        <!-- ========== WRAPPER FOR BOTH SIDEBARS & MAIN CONTENT ========== -->
        <div class="content-wrapper">
            <div class="content-container">
                <!-- ========== LEFT SIDEBAR ========== -->
                <?php include('includes/leftbar.php'); ?>
                <!-- /.left-sidebar -->
                <div class="main-page">
                    <div class="container-fluid">
                        <div class="row page-title-div">
                            <div class="col-md-6">
                                <h2 class="title">Student Admission</h2>
                            </div>

<center>  Click here to add student in bulk.
   <a style="margin-left:20px" href="upload_csv.php" class="btn btn-primary" Upload>Add student in bulk</a> 
<br> </center>
                            
                        </div>

                        <!--<form method="POST" action="" class="needs-validation" novalidate>-->
                       
            <form action="students.php" method="post">
            <div class="form-row">
    <div class="form-group col-md-3">
            <label for="regd_id">Registration ID:</label>
        <input type="text" id="regd_id" name="regd_id" class="form-control" required>
            </div>

             <div class="form-group col-md-3">
                <label for="studentName">Student Name:</label>
                <input type="text" id="studentName" name="studentName" class="form-control" required>
                <!-- <div class="invalid-feedback">Please enter the student's name.</div> -->
            </div>

        <div class="form-group col-md-3">
                <label for="studentDOB">Date of Birth:</label>
                <input type="" id="studentDOB" name="studentDOB" class="form-control" required>
                <div class="invalid-feedback">Please enter the date of birth.</div>
            </div>

              <div class="form-group col-md-3">
<label for="gender">Gender:<b style="color:red"> Required *</b></label>
                <select id="gender" name="gender" required class="form-control">
        <OPTION VALUE="">SELECT </OPTION>
        <OPTION VALUE="Male">MALE</OPTION>
        <OPTION VALUE="Female">FEMALE</OPTION>
        <OPTION VALUE="Others">OTHERS</OPTION>
</select>
<div class="invalid-feedback">Please Select gender</div>
</div>

<div class="form-group col-md-3">
    <label for="group">Ethnic Group:</label>
    <select id="group" name="group" required class="form-control">
        <option value="">SELECT</option>
        <?php
        // Include database connection code
        include("includes/db.php");

        // Fetch options for Ethnic Group dropdown from the 'ent_group' table
        $query_group = "SELECT id, group_name FROM ent_group";
        $result_group = $conn->query($query_group);

        // Populate the dropdown with options from the 'ent_group' table
        while ($row_group = $result_group->fetch_assoc()) {
            echo "<option value='" . $row_group['id'] . "'>" . $row_group['group_name'] . "</option>";
        }

        // Close the database connection
        $conn->close();
        ?>
    </select>


                <div class="invalid-feedback">Please select appropiate enthic group.</div>
            </div>

        <div class="form-group col-md-3">
                <label for="studentAddress">Address:</label>
                <textarea id="studentAddress" name="studentAddress" class="form-control" required></textarea>
                <!-- <div class="invalid-feedback">Please enter the address.</div> -->
            </div>

              <div class="form-group col-md-3">
                <label for="studentPhone">Phone Number:</label>
                <input type="tel" id="studentPhone" name="studentPhone" class="form-control" pattern="[0-9]{10}" required>
                <div class="invalid-feedback">phone number (10 digits).</div>
            </div>

             <div class="form-group col-md-3">
                <label for="studentEmail">Email:</label>
                <input type="email" id="studentEmail" name="studentEmail" class="form-control" required>
                <div class="invalid-feedback">Please enter a valid email address.</div>
            </div>

              <div class="form-group col-md-3">
                <label for="parentName">Parent's Name:</label>
                <input type="text" id="parentName" name="parentName" class="form-control" required>
                <div class="invalid-feedback">Please enter the parent's name.</div>
            </div>

            <div class="form-group col-md-3">
                <label for="parentPhone">Parent's Phone Number:</label>
                <input type="tel" id="parentPhone" name="parentPhone" class="form-control" pattern="[0-9]{10}" required>
                <div class="invalid-feedback">Please enter a valid parent's phone number (10 digits).</div>
            </div>


     
       <div class="form-group col-md-3">
            <label for="studentClass">Select Class:</label>
                    <select id="studentClass" name="studentClass" class="form-control" required>
                    <option value="">Select Class</option>
                    <?php
                    // Include the database configuration file
                    include("includes/db.php");

                    // Fetch class names from the 'classes' table
                    $query = "SELECT class_id, class_name, section FROM classes";
                    $result = $conn->query($query);

                    // Populate the dropdown with class names and sections
                    while ($row = $result->fetch_assoc()) {
                        echo "<option value='" . $row['class_id'] . "'>" . $row['class_name'] . " - " . $row['section'] . "</option>";
                    }

                    // Close the database connection
                    $conn->close();
                    ?>
                </select>
        </div>


                <!-- <div class="invalid-feedback">Please enter the student's class.</div> -->
<!-- 
        <div class="form-group col-md-3">
                <label for="studentSection">Section:</label>
                <input type="text" id="classID" name="studentSection" class="form-control" required>
                <div class="invalid-feedback">Please enter the student's section.</div>
            </div> -->
</div>
</div>
            <button style="margin-left:30px" type="submit" name="addStudent" class="btn btn-primary">Add Student</button>
                       </div>      
                        </form>
                 
      
        <script>
            $(function($) {
                $('#example').DataTable();
            });
        </script>


        <?php
// Include the database configuration file
include("includes/db.php");

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["addStudent"])) {
    $studentName = $conn->real_escape_string($_POST["studentName"]);
    $studentDOB = $conn->real_escape_string($_POST["studentDOB"]);
    $parentName = $conn->real_escape_string($_POST["parentName"]);

    // Check if a student with the same name and class ID already exists
    $checkQuery = "SELECT * FROM students WHERE name = '$studentName' AND dob = '$studentDOB'  "; //And parent_name='$parentName'
    $result = $conn->query($checkQuery);

    if ($result->num_rows > 0) {
        // Duplicate entry found
        // header("Location: students.php?error=duplicate");
        echo '<script>alert("Error: Duplicate entry! The student with the same name and class already exists.");</script>';
        exit();
    } else {

    // Collect data from the form
    $regd_id = $conn->real_escape_string($_POST["regd_id"]);
    $studentName = strtoupper($conn->real_escape_string($_POST["studentName"]));
    $studentDOB = $conn->real_escape_string($_POST["studentDOB"]);
    $studentAddress = $conn->real_escape_string($_POST["studentAddress"]);
    $studentPhone = $conn->real_escape_string($_POST["studentPhone"]);
    $studentEmail = $conn->real_escape_string($_POST["studentEmail"]);
    $parentName = strtoupper($conn->real_escape_string($_POST["parentName"]));
    $parentPhone = $conn->real_escape_string($_POST["parentPhone"]);
    $studentClass = $conn->real_escape_string($_POST["studentClass"]);
    // $studentSection = $conn->real_escape_string($_POST["studentSection"]);
    $gender = $conn->real_escape_string($_POST["gender"]);
    $std_group = $conn->real_escape_string($_POST["group"]);
   

    // SQL query to insert data into the 'students' table
    $query = "INSERT INTO students (regd_id, name, dob, address, phone, email, parent_name, parent_phone, class, Class_id,gender, std_group, section ) 
              VALUES ('$regd_id','$studentName', '$studentDOB', '$studentAddress', '$studentPhone', '$studentEmail', '$parentName', '$parentPhone', '$studentClass', '$studentClass','$gender','$std_group','' )";

    if ($conn->query($query) === TRUE) {
        // Student added successfully
        echo "Student added successfully!";
    } else {
        // Error occurred while adding student
        echo "Error: " . $query . "<br>" . $conn->error;
    }

    // Close the database connection
    $conn->close();
}
}

?>




    <!-- Bootstrap JS and dependencies -->
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

    <script>
        // JavaScript form validation using Bootstrap's custom validation classes
        (function() {
            'use strict';
            window.addEventListener('load', function() {
                // Fetch all the forms we want to apply custom Bootstrap validation styles to
                var forms = document.getElementsByClassName('needs-validation');
                // Loop over them and prevent submission
                var validation = Array.prototype.filter.call(forms, function(form) {
                    form.addEventListener('submit', function(event) {
                        if (form.checkValidity() === false) {
                            event.preventDefault();
                            event.stopPropagation();
                        }
                        form.classList.add('was-validated');
                    }, false);
                });
            }, false);
        })();
    </script>


    <script>
            // Fetch classes from the database and populate the class select option
            $(document).ready(function () {
                // Handle class selection change
                $('#studentClass').change(function () {
                    var classId = $(this).val();

                    // Update class ID span with the selected class ID
                    $('#classID').text(classId);

                    // Add additional logic here to fetch and display students based on the selected class
                });

    });
        </script>
</div>
            </div>
       
        <script src="js/jquery/jquery-2.2.4.min.js"></script>
        <script src="js/bootstrap/bootstrap.min.js"></script>
        <script src="js/pace/pace.min.js"></script>
        <script src="js/lobipanel/lobipanel.min.js"></script>
        <script src="js/iscroll/iscroll.js"></script>
        <script src="js/prism/prism.js"></script>
        <script src="js/DataTables/datatables.min.js"></script>
        <script src="js/main.js"></script>
</body>
</html>


