<!-- edit_marks_sub.php -->
<?php
include('session_validator.php');
include("includes/db.php");

$marksheetResult = 0;

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
    $studentId = $_POST["studentId"];
    $studentDetailsQuery = "SELECT students.name, students.symbol_no, classes.class_name, classes.section FROM students 
                            INNER JOIN classes ON students.class = classes.Class_id 
                            WHERE student_id = '$studentId'";
    $studentDetailsResult = $conn->query($studentDetailsQuery);

    if ($studentDetailsResult->num_rows > 0) {
        $studentDetails = $studentDetailsResult->fetch_assoc();

        $marksheetQuery = "SELECT marks.id, subjects.subject_name, marks.marks, marks.full_marks, subjects.theory_full_marks, 
                            subjects.internal_full_marks, subjects.theory_credit_hour, subjects.internal_credit_hour
                            FROM marks
                            INNER JOIN subjects ON marks.subject_id = subjects.id
                            WHERE 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 marks SET 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>

<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">
                        <!-- Your existing code for page title and breadcrumbs -->
                    </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>Edit Marks</h5>
                                            
                                        </div>
                                    </div>
                                    <div class="panel-body">
                                        <form action="edit_marks_sub.php" method="POST">
                                            <table>
                                                <thead>
                                                    <tr>
                                                        <th>Enter Student ID:</th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td>
                                                            <input class="form-control" type="text" name="studentId" placeholder="Student ID" required>
                                                        </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>

                                            <?php
                                            if (isset($studentDetails)) {
                                                echo "<p> <b>Name: {$studentDetails['name']} | Symbol Number: {$studentDetails['symbol_no']} | Class: {$studentDetails['class_name']} - {$studentDetails['section']}</b></p>";
                                            }
                                            ?>


                                        <!-- Display marks and field to update -->
                                        <?php if (isset($marksheetResult) && $marksheetResult !== false && is_object($marksheetResult)) { ?>
                                            <?php if ($marksheetResult->num_rows > 0) { ?>
                                                <!-- Existing code for displaying the mark sheet data -->
                                                <form action="edit_marks_sub.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['theory_full_marks']}</center></td>
                                                                        <td style='margin-top:3px'><center>{$row['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['marks']}' required></center>
                                                                        </td>
                                                                    </tr>";
                                                                }
                                                            } else { ?>
                                                                <p>No data found for the selected student.</p>
                                                            <?php } ?>
                                                        <?php } else { ?>
                                                            <p>Enter Student ID and click 'Show Obtained Mark'</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>
                    <!-- /.content-container -->
                </div>
                <!-- /.content-wrapper -->
            </div>
        </div>
    </body>

    </html>

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