<?php
session_start();

if(!isset($_SESSION['login_id'])){
    header("Location: ../login/login.php");
    exit();
}

header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
?>

<?php
$con = mysqli_connect("localhost","root","","WSAS");
if (!$con) die("Database connection failed");

if (!isset($_GET['course_id'])) header("location: ../adminpanel/admincourse.php");

$course_id = $_GET['course_id'];
$course = mysqli_query($con, "SELECT * FROM admincourse WHERE course_id='$course_id'");
$course_data = mysqli_fetch_assoc($course);
?>

<!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.0">
    <title>View Course's Subjects - Admin</title>
    <link rel="shortcut icon" type="x-icon" href="../images/logo.png">
    <link rel="shortcut icon" type="x-icon" href="../images/logo.png">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0" />
    <link rel="stylesheet" href="../Style/navstyle.css">

<style>

.dashboard-container {
  display: flex;
  height: 100vh;
}

.mainbar {
    margin-left: 280px;
    width: calc(100% - 280px);
    flex: 1;
    display: flex;
    flex-direction: column;
}

.mainbar-context {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.container {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.subject {
  background-color: #4a9c3f;
  color: white;
  padding: 20px;
  flex: 1;
  min-width: 200px;
  border-radius: 10px;
  text-align: center;
}

.container1 {
  display: flex;
  padding: 10px;
  gap: 20px;
  flex-wrap: wrap;
  align-items: center;
}

table {
  width: 100%;
  border-collapse: collapse; 
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  overflow: hidden;
  font-family: "Segoe UI", sans-serif;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); 
  text-align: center;
}


table th {
  background-color: #2b5820; 
  color: white;
  font-weight: 600;
  padding: 12px;
  border: 1px solid #2b5820; 
}


table td {
  border: 1px solid #ccc; 
  padding: 10px;
  color: #222;
}


table tr:nth-child(even) {
  background-color: #f8f8f8;
}


table tr:hover {
  background-color: rgba(74, 156, 63, 0.1);
  transition: background-color 0.3s ease;
}

@media (max-width: 1024px) {
  .mainbar {
    margin-left: 0; 
    margin-top: 70px;
  }

  .dashboard-container {
    flex-direction: column;
  }
}

.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
}

.modal-content {
  background: white;
  width: 400px;
  margin: 10% auto;
  padding: 20px;
  border-radius: 10px;
  animation: pop 0.3s ease;
}

.close-btn {
  float: right;
  cursor: pointer;
  font-size: 25px;
  padding: 5px;
}

.open-btn {
  background: #4a9c3f;
  color: white;
  padding: 10px 18px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

@keyframes pop {
  from { transform: scale(0.7); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.upd_btn {
 display: inline-block;
  padding: 4px 8px;
  color: #f1f7ed;
  text-decoration: none;
  border-radius: 6px;
  font-size: 12px;
  transition: 0.3s ease;
}

.upd_btn {
  background: #1261A0;
  color: #fff;
}

.upd_btn:hover {
  background: #0f4d80;
}

body {
  font-size: 12px;
}

/* Keep titles slightly bigger */
h1 {
  font-size: 25px;
  margin-bottom: 10px;
}

h2 {
  background-color: #4a9c3f;
  color: white;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 19px;
  margin-bottom: 20px;
  display: inline-block;
  align-self: flex-start; /* 🔥 THIS FIXES IT */
}


h3 {
  font-size: 18px;
}

/* Table text */
table th,
table td {
  font-size: 12px;
}

/* Buttons */
.open-btn,
.upd_btn {
  font-size: 12px;
}

</style>

<body> 
<?php include('../adminpanel/adminnav.php'); ?>

<aside class="mainbar">
    <h1><?php echo $course_data['course_code']; ?> - <?php echo $course_data['course_name']; ?> - Curriculum</h1>

<?php
$query = mysqli_query($con, "
    SELECT cs.year_level, cs.semester, s.subject_id, s.subject_code, s.subject_name, 
           s.lecture_unit, s.lab_unit, s.total_unit
    FROM course_subject cs
    JOIN adminsubject s ON cs.subject_id = s.subject_id
    WHERE cs.course_id='$course_id'
    ORDER BY cs.year_level ASC, FIELD(cs.semester,'1st','2nd','Summer'), s.subject_code ASC
");

$curriculum = [];
while($row = mysqli_fetch_assoc($query)) {
    $year = $row['year_level'];
    $sem = $row['semester'];
    $curriculum[$year][$sem][] = $row;
}

foreach($curriculum as $year => $semesters) {
    echo "<h2>$year Year</h2>";
    foreach($semesters as $sem => $subjects) {
        echo "<h3>$sem Semester</h3>";
        echo "<table>";
        echo "<thead>
                <tr>
                    <th>Subject Code</th>
                    <th>Subject Name</th>
                    <th>Lecture Unit</th>
                    <th>Lab Unit</th>
                    <th>Total Unit</th>
                    <th>Action</th>
                    
                </tr>
              </thead>";
        echo "<tbody>";
                    foreach($subjects as $sub) {
                        echo "<tr>
                    <td>{$sub['subject_code']}</td>
                    <td>{$sub['subject_name']}</td>
                    <td>{$sub['lecture_unit']}</td>
                    <td>{$sub['lab_unit']}</td>
                    <td>{$sub['total_unit']}</td>
                    <td><a class='upd_btn' href='../view/subject_view.php?subject_id={$sub['subject_id']}'>View</a></td>
                  </tr>";
        }
        echo "</tbody>";
        echo "</table><br>";
    }
}
?>
</aside>

    <!-- Script -->
     <script src="../Jscript/script.js"></script>
     <script src="../Jscript/form.js"></script>

    
</body>
</html>