<?php
include('session_validator.php'); ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>SRMS Admin Manage Subjects</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/prism/prism.css" media="screen" >
        <link rel="stylesheet" href="css/select2/select2.min.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 Exam Type</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> Exam </li>
                                        <li class="">Exam Type</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 Exam Type</h5>
                                                </div>
                                            </div>
                                            <div class="panel-body">

<?php
include("includes/db.php");

// Function to sanitize user inputs
function sanitizeInput($input) {
    return htmlspecialchars(stripslashes(trim($input)));
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
    $exm_year = sanitizeInput($_POST['exm_year']);
    $exam_type = sanitizeInput($_POST['exam_type']);

    // Check if the combination of exm_year and exam_type already exists
    $checkDuplicateQuery = $conn->prepare("SELECT COUNT(*) as count FROM examination_type WHERE exm_year = ? AND exam_type = ?");
    $checkDuplicateQuery->bind_param("ss", $exm_year, $exam_type);
    $checkDuplicateQuery->execute();
    $checkDuplicateResult = $checkDuplicateQuery->get_result();
    $count = $checkDuplicateResult->fetch_assoc()['count'];

    if ($count > 0) {
        echo '<div style="color: red; font-weight: bold;">Error: Duplicate entry. This combination of Exam Year and Exam Type already exists.</div>';
    } else {
        // Prepare the SQL statement to insert data into the examination_type table
        $insertQuery = $conn->prepare("INSERT INTO examination_type (exm_year, exam_type) VALUES (?, ?)");
        $insertQuery->bind_param("ss", $exm_year, $exam_type);

        // Execute the query
        if ($insertQuery->execute()) {
            echo "Examination type added successfully!";
        } else {
            echo "Error adding examination type. Please try again.";
        }

        // Close the statement and database connection
        $insertQuery->close();
    }
    $checkDuplicateQuery->close();
} 
?>




    <h2>Enter Examination Type Information</h2>
  <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
          <div class="form-row">
                <div class="form-group col-md-3">
        <label for="exm_year">Exam Year:</label>
        <select type="number" id="exm_year" name="exm_year" class="form-control" required>
    <option value="">Select Examination Year</option>
    <option value="2079">2079</option>
    <option value="2080">2080</option>
    <option value="2081">2081</option>

</select>
  </div>

                <div class="form-group col-md-3">
        <label for="exam_type">Exam Type:</label>
        <input type="text" id="exam_type" name="exam_type" class="form-control" placeholder="Write Exam Name" required>
      </div>  
      </div>     

<button style=" height: 70%;" type="submit" name="submit" class="btn btn-primary">Save Exam Type</button>
     

</form>
</body>
</div>

</html>

<!DOCTYPE html>
<html>

<head>
    <title>Exam Types</title>
    <link rel="stylesheet" href="bootstrap\css\bootstrap.css">
</head>

<body>
    <div >
        <h2>Exam Types</h2>
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Exam Type</th>
                    <th> Year </th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>

                <?php
                // Include database connection
                include("includes/db.php");

                // Fetch exam types from the database
                $fetchExamTypesQuery = "SELECT id, exam_type, exm_year FROM examination_type";
                $examTypesResult = $conn->query($fetchExamTypesQuery);

                // Check if there are rows returned
                if ($examTypesResult->num_rows > 0) {
                    while ($row = $examTypesResult->fetch_assoc()) {
                        echo '<tr>';
                        echo '<td>' . $row['id'] . '</td>';
                        echo '<td>' . $row['exam_type'] . '</td>';
                         echo '<td>' . $row['exm_year'] . '</td>';
                        echo '<td><a type="button" href="edit_exam_type.php?id=' . $row['id'] . '">Edit</a></td>';
                        echo '</tr>';
                    }
                } else {
                    echo '<tr><td colspan="3">No exam types found</td></tr>';
                }
                ?>

            </tbody>
        </table>
</div>
                                            </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>
