<?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>Add Class</title>
       <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/toastr/toastr.min.css" media="screen">
    <link rel="stylesheet" href="css/icheck/skins/line/blue.css">
    <link rel="stylesheet" href="css/icheck/skins/line/red.css">
    <link rel="stylesheet" href="css/icheck/skins/line/green.css">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></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">Add Class, Section and Class Teacher</h2>
                                
                                </div>
                                
                                <!-- /.col-md-6 text-right -->
                            </div>
                            <!-- /.row -->
                            <div class="row breadcrumb-div">
                                <div class="col-md-6">
                                    <ul class="breadcrumb">
                                        <li><a href="dashboard.php"><i class="fa fa-home"></i> Home</a></li>
                                        <li> Class </li>
                                        <li class="">Create Class<li>
                                    </ul>
                                </div>
                             
                            </div>
                            <!-- /.row -->
                        </div>
                        <div class="container-fluid">
                           
                        <div class="row">
                                    <div class="col-md-12">
                                        <div class="panel">
                                            <div class="panel-heading">
                                                <div class="panel-title">
                                                    <h5>Add Class, Section and Class Teacher</h5>
                                                </div>
                                            </div>
                                            <div class="panel-body">
        
        <form method="POST" action="">
            <div class="form-row">
    <div class="form-group col-md-3">
    
                <!-- <label for="className">Class Name:</label> -->
                <input type="text" id="className" name="className" placeholder="Class Name" class="form-control" required>
            </div>
             <div class="form-group col-md-3">
                <!-- <label for="section">Section:</label> -->
                <input type="text" id="section" name="section" placeholder="Section" class="form-control" required>
            </div>
       <div class="form-group col-md-3">
                <!-- <label for="class_teacher">Class Teacher:</label> -->
                <input type="text" id="class_teacher" name="class_teacher" placeholder="Class Teacher" class="form-control" required>
            </div>
                <div class="form-group col-md-3">
            <button type="submit" class="btn btn-primary" name="submit">Add Class</button>
        </div>
        </div>
        </form>

        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
            // Include the database configuration file
            include("includes/db.php");

            // Collect data from the form
            $className = $conn->real_escape_string($_POST["className"]);
            $section = $conn->real_escape_string($_POST["section"]);
            $class_teacher = $conn->real_escape_string($_POST["class_teacher"]);

            // SQL query to insert data into the 'classes' table
            $query = "INSERT INTO classes (class_name, section, class_teacher) VALUES ('$className', '$section', '$class_teacher')";

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

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

        <h2>Class Details</h2>

        <table>
            <thead>
                <tr>
                    <th>Class Name</th>
                    <th>Section</th>
                    <th>Class Teacher</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                // Include the database configuration file
                include("includes/db.php");

                // Fetch class data from the database
                $query = "SELECT * FROM classes";
                $result = $conn->query($query);

                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        echo "<tr>";
                        echo "<td>" . $row['class_name'] . "</td>";
                        echo "<td>" . $row['section'] . "</td>";
                        echo "<td>" . $row['class_teacher'] . "</td>";
                        echo "<td><a href='edit_class.php?id=" . $row['Class_id'] . "' class='edit-btn'>Edit</a></td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='4'>No classes found</td></tr>";
                }

                // Close the database connection
                $conn->close();
                ?>
            </tbody>
        </table>
                                </div>
                                        </div>
                                    </div>
                                    <!-- /.col-md-12 -->
                                </div>
                    </div>
                </div>
                <!-- /.content-container -->
            </div>
            <!-- /.content-wrapper -->
        </div>
        <!-- /.main-wrapper -->
        <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/select2/select2.min.js"></script>
        <script src="js/main.js"></script>

    </body>
</html>