<?php
include('session_validator.php');
?>
<?php

include("includes/db.php");

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
    $subjectId = $_POST["subject_id"];
    $subjectname = $_POST["subject_name"];
    $subjectcode = $_POST["subject_code"];
    $theoryFullMarks = $_POST["theory_full_marks"];
    $internalFullMarks = $_POST["internal_full_marks"];
    $theoryCreditHour = $_POST["theory_credit_hour"];
    $internalCreditHour = $_POST["internal_credit_hour"];

    // Update the subjects table with the new data
    $updateQuery = "UPDATE subjects SET theory_full_marks = '$theoryFullMarks', 
                    internal_full_marks = '$internalFullMarks', 
                    theory_credit_hour = '$theoryCreditHour', 
                    internal_credit_hour = '$internalCreditHour', subject_name ='$subjectname', subject_code = '$subjectcode'
                    WHERE id = '$subjectId'";

    if ($conn->query($updateQuery) === TRUE) {
                header("Location: subjects.php");
        echo "Record updated successfully";

    } else {
        echo "Error updating record: " . $conn->error;
    }
}

// Fetch subject details for editing
if (isset($_GET['subject_id'])) {
    $subjectId = $_GET['subject_id'];
    $fetchQuery = "SELECT * FROM subjects WHERE id = '$subjectId'";
    $result = $conn->query($fetchQuery);
    $subject = $result->fetch_assoc();
}
?>



<!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" type="text/css" href="js/DataTables/datatables.min.css" />
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></script>
    <style>
        .errorWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #fff;
            border-left: 4px solid #dd3d36;
            -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
            box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
        }

        .succWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #fff;
            border-left: 4px solid #5cb85c;
            -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
            box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
        }
    </style>
</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <?php include('includes/topbar.php'); ?>
        <div class="content-wrapper">
            <?php include('includes/leftbar.php'); ?>
            <div class="main-page">
                <div class="container-fluid">
                    <div class="row page-title-div">
                        <div class="col-md-6">
                            <h2 class="title">Manage Subjects</h2>
                        </div>
                    </div>
                    <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">Manage Subjects</li>
                                <form method="POST" action="edit_subject.php">
        <input type="hidden" name="subject_id" value="<?php echo $subject['id']; ?>">

        <label for="subject_name">Subject Name:</label>
        <input type="" class="form-control" style="width: 300px;" name="subject_name" value="<?php echo $subject['subject_name']; ?>">

        <label for="subject_code">Subject Code:</label>
        <input type="" class="form-control" style="width: 300px;" name="subject_code" value="<?php echo $subject['subject_code']; ?>">

        <label for="theory_full_marks">Theory Full Marks:</label>
        <input type="number" step="0.01"  class="form-control" style="width: 300px;" id="theory_full_marks" name="theory_full_marks" value="<?php echo $subject['theory_full_marks']; ?>" required>

        <label for="internal_full_marks">Internal Full Marks:</label>
        <input type="number" step="0.01" class="form-control" style="width: 300px;" id="internal_full_marks" name="internal_full_marks" value="<?php echo $subject['internal_full_marks']; ?>" required>

        <label for="theory_credit_hour">Theory Credit Hour:</label>
        <input type="number" step="0.01" class="form-control" style="width: 300px;" id="theory_credit_hour" name="theory_credit_hour" value="<?php echo $subject['theory_credit_hour']; ?>" required>

        <label for="internal_credit_hour">Internal Credit Hour:</label>
        <input type="number" step="0.01" class="form-control" style="width: 300px;" id="internal_credit_hour" name="internal_credit_hour" value="<?php echo $subject['internal_credit_hour']; ?>" required>
<br>
        <button type="submit" class="btn btn-primary" name="submit">Update Subject</button>
    </form>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </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/DataTables/datatables.min.js"></script>
    <script src="js/main.js"></script>
    <script>
        $(function ($) {
            $('#example').DataTable();
        });
    </script>
</body>

</html>

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