<?php
include('session_validator.php');
include("includes/db.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="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
        <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">Enter Marks</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> Subjects</li>
                                        <li class="active">Bulk Marks</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>Bulk Marks</h5>
                                                </div>
                                            </div>
                                            <div class="panel-body">

        <form method="POST" action="process_bulk_marks.php" enctype="multipart/form-data">
            <!-- Class and Subject selection -->
            <div class="form-row">
                 <div class="form-group col-md-3">
                <label for="exam_id">Select Exam:</label>
                    <select id="exam_id" name="exam_id" class="form-control" required>
                        <option value="">Select Exam</option>
                        <?php
                        $queryexam = "SELECT id, exm_year, exam_type FROM examination_type";
                        $resultexam = $conn->query($queryexam);

                        while ($row = $resultexam->fetch_assoc()) {
                            echo "<option value='" . $row['id'] . "'>" . $row['id'] . "-" . $row['exam_type'] . " - " . $row['exm_year'] . "</option>";
                        }
                        ?>
                    </select>
</div>
                <div class="form-group col-md-3">

                    <label for="class_id">Select Class:</label>
                    <select id="class_id" name="class_id" class="form-control" required>
                        <option value="">Select Class</option>
                        <!-- Fetch class names from the 'classes' table -->
                        <?php
                        $query = "SELECT class_id, class_name, section FROM classes";
                        $result = $conn->query($query);

                        while ($row = $result->fetch_assoc()) {
                            echo "<option value='" . $row['class_id'] . "'>" . $row['class_name'] . " - " . $row['section'] . "</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="form-group col-md-4">
                    <label for="subject_id">Select Subject:</label>
                    <select id="subject_id" name="subject_id" class="form-control" required>
                        <option value="">Select Subject</option>
                    </select>
                </div>

                <div class="form-group col-md-4">
                    <label for="csvFile">Upload CSV File:</label>
                    <input type="file" class="form-control-file" id="csvFile" name="csv_file" accept=".csv" required>
                </div>
            </div>

            <button type="submit" name="submit" class="btn btn-primary">Submit Marks</button>
        </form>

        <br><br>

        <form method="POST" action="generate_csv.php">
            <h2>Download student data</h2>
            <div class="form-row">
                <div class="form-group col-md-3">
                    <label for="class_id1">Select Class:</label>
                    <select id="class_id1" name="class_id1" class="form-control" required>
                        <option value="">Select Class</option>
                        <!-- Fetch class names from the 'classes' table -->
                        <?php
                        $result = $conn->query($query);

                        while ($row = $result->fetch_assoc()) {
                            echo "<option value='" . $row['class_id'] . "'>" . $row['class_name'] . " - " . $row['section'] . "</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="form-group col-md-3">
                    <label for="csvDownload">Download CSV file:</label>
                    <button  id="csvDownload" class="btn btn-secondary">Download CSV</button>
                </div>
            </div>
        </form>
    </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/main.js"></script>
    <script src="js/modernizr/modernizr.min.js"></script>
    
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        // Function to trigger CSV download
        $('#csvDownload').click(function () {
            var classId = $('#class_id1').val();

            // AJAX request to fetch student details for the selected class
            $.ajax({
                type: 'POST',
                url: 'generate_csv.php',
                data: { classId: classId },
                success: function (response) {
                    var link = document.createElement('a');
                    link.href = 'data:text/csv;charset=utf-8,' + encodeURI(response);
                    link.target = '_blank';
                    link.download = 'student_details.csv';
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                }
            });
        });
    </script>

    <script>
        // ... Previous JavaScript code for class and subject selection ...


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

document.getElementById("class_id").addEventListener("change", function() {
    var classId = this.value;
    var subjectDropdown = document.getElementById("subject_id");

    // Clear existing options
    subjectDropdown.innerHTML = "<option value=''>Select Subject</option>";

    // Fetch subjects for the selected class via AJAX
    fetch(`getSubjects.php?class_id=${classId}`)
        .then(response => response.json())
        .then(data => {
            // Populate subject dropdown with fetched subjects
            data.forEach(subject => {
                var option = document.createElement("option");
                option.value = subject.id;
                option.textContent = subject.subject_name;
                subjectDropdown.appendChild(option);
            });
        })
        .catch(error => {
            console.error('Error:', error);
        });
});

    </script>
    <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/main.js"></script>
    <script src="js/modernizr/modernizr.min.js"></script>
    
</body>

</html>


