<?php
include('session_validator.php'); 
include("includes/db.php");
$marksheetResult=0;

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
    $studentId = $_POST["studentSelect"];
    $marksheetQuery = "SELECT int_marks.id, subjects.subject_name, int_marks.internal_marks, int_marks.int_full_marks, subjects.internal_full_marks, 
                subjects.internal_full_marks, subjects.theory_credit_hour, subjects.internal_credit_hour
                       FROM int_marks
                       INNER JOIN subjects ON int_marks.subject_id = subjects.id 
                       WHERE int_marks.student_id = '$studentId'";
    $marksheetResult = $conn->query($marksheetQuery);
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["update"])) {
    $markIds = $_POST["mark_id"];
    $newMarks = $_POST["new_marks"];

    // Loop through the submitted marks and update them in the database
    for ($i = 0; $i < count($markIds); $i++) {
        $markId = $conn->real_escape_string($markIds[$i]);
        $newMark = $conn->real_escape_string($newMarks[$i]);

        // Update marks in the database
        $updateQuery = "UPDATE int_marks SET internal_marks = '$newMark' WHERE id = '$markId'";
        if ($conn->query($updateQuery) !== TRUE) {
            echo "<script>alert('Error updating marks. Please try again.');</script>";
            // Handle the error as needed
        }
    }

    echo "<script>alert('Marks updated successfully!');</script>";
    // Redirect or display a success message as needed
}
?>

<!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" >
        <link rel="stylesheet" href="css/tablestyle.css" type="text/css">
        <script src="js/modernizr/modernizr.min.js"></script>
        <script src="js/jquery/jquery-2.2.4.min.js"></script>
    <!-- DataTables JavaScript -->
    <script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>



</head>
    <style>
        body1 {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 100%;
            margin: 0 auto;
        }

        h2 {
            background-color: skyblue;
            color: white;
            padding: 10px;
            text-align: center;
        }

        form {
            margin: 20px 0;
        }

        select {
            padding: 8px;
            font-size: 16px;
        }

        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 5px;
            text-align: left;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }
    </style>




       <script>
    // Event listener to populate students based on selected class
    $(document).on("change", "#classSelect", function () {
        var classId = $(this).val();
        $("#studentSelect").html("<option value=''>Select Student</option>");

        // Fetch students based on the selected class via AJAX
        $.ajax({
            type: "POST",
            url: "edit_markspr.php",
            data: { classId: classId },
            success: function (response) {
                $("#studentSelect").html(response); // Use html() instead of append()
            }
        });
    });
</script>


    <?php
if (isset($_POST["classId"])) {
    $classId = $conn->real_escape_string($_POST["classId"]);

    // Fetch students based on the selected class
    $query = "SELECT * FROM students WHERE class = '$classId'";
    $result = $conn->query($query);

    while ($row = $result->fetch_assoc()) {

        $options .= "<option value='{$row['student_id']}'>{$row['name']}</option>";
    }

    echo $options;
}

?>
    <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="active" >Select Class to edit mark</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> Marks </li>
                                        <li class="">Edit Mark</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>Select Class to edit mark</h5>
                                                </div>
                                            </div> 
                                            <div class="panel-body">
         <form action="edit_markspr.php" method="POST">
        <table id="example" class="table nowrap" style="width:100%">
            <thead>
                <tr>
                <th> Select Class: </th>
                <th>Select Student:</th>
                <th></th>
            </tr>
        </thead>

        <tbody>
       <tr>
        <td><!-- Select Class -->
  
       <select id="classSelect" class="form-control col-md-6 ">
        <option value="" >Select Class</option>
        <?php
        // Include database connection file
        include("includes/db.php");

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

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

</td>

<td>
    <!-- Select Students from the Chosen Class -->

    <select id="studentSelect" name="studentSelect" class="form-control col-md-6">
        <option value="">Select Student</option>
    </select>
</td>




          <td>  <button type="submit"  class="btn-btn-primary" name="submit">Show Obtained Mark</button> </td>
          </tr>
            </tbody>
</table>

        </form>
       <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>
<!-- display marks and field to update -->
                                                     <!-- display marks and field to update -->
                                        <?php if ($marksheetResult !== false && is_object($marksheetResult)) { ?>
    <?php if ($marksheetResult->num_rows > 0) { ?>
        <!-- Existing code for displaying the mark sheet data -->
    

                                                <form action="edit_markspr.php" method="POST">
                                                    <div class="container">
                                                        <h2>Mark Details</h2>
                                                        <table>
                                                            <thead>
                                                                <tr>
                                                                    <th>Subject</th>
                                                                    <th><center>Full Marks</center></th>
                                                                    <th><center>Marks Obtained</center></th>
                                                                    <th><center>Edit Marks</center></th>
                                                                </tr>
                                                            </thead>
                                                            <tbody>
                                                                <?php
                                                                // Display the mark sheet data for editing
                                                                while ($row = $marksheetResult->fetch_assoc()) {
                                                                    echo "<tr>
                                <td style='margin-top:3px'>{$row['subject_name']}</td>
                                <td style='margin-top:3px'><center>{$row['internal_full_marks']}</center></td>
                                <td style='margin-top:3px'><center>{$row['internal_marks']}</center></td>
                                <td style='margin-top:3px'>
                                    <input style='width:20px' type='hidden' name='mark_id[]' value='{$row['id']}'>
                                    <center><input style='width:100px' type='number' name='new_marks[]' value='{$row['internal_marks']}' required></center>
                                </td>
                              </tr>";
                    }
                
                   } else { ?>
        <p>No data found for the selected student.</p>
    <?php } ?>
<?php } else { ?>
    <p>Select Class and Student <?php echo $conn->error; ?></p>
<?php } ?>
         <tr> <td colspan="4">    
            <button type="submit" class="btn-btn-primary"  name="update">Update Marks</button> </td></tr>
      </tbody>
            </table>  


</form>

</div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.col-md-12 -->
                                </div>
                    </div>
                </div>
                <!-- /.content-container -->
            </div>
            <!-- /.content-wrapper -->
        </div>
        
    </body>
</html>

<?php

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