<?php
$con = mysqli_connect("localhost","root","","WSAS");

if (!$con) {
    die("Connection failed");
}

// Get student_id from URL
if (!isset($_GET['student_id'])) {
    echo "No student selected";
    exit;
}

$student_id = $_GET['student_id'];

// Fetch student name
$student_sql = mysqli_query($con, "SELECT f_name, l_name FROM adminstudent WHERE student_id = '$student_id'");
$student = mysqli_fetch_assoc($student_sql);
$student_name = $student ? $student['f_name'] . ' ' . $student['l_name'] : "Unknown Student";

if (isset($_POST['update_status'])) {
    $subject_id = $_POST['subject_id'];
    $new_status = $_POST['new_status'];

    mysqli_query($con, "
        UPDATE student_curriculum 
        SET sc_status = '$new_status'
        WHERE student_id = '$student_id' 
        AND subject_id = '$subject_id'
    ");

    header("Location: ".$_SERVER['REQUEST_URI']);
    exit;
}
// Get student + subjects
$query = mysqli_query($con, "
    SELECT 
        sc.sc_status,
        sc.subject_id,
        a.subject_code,
        a.subject_name,
        a.lecture_unit,
        a.lab_unit,
        a.total_unit,
        cs.year_level,
        cs.semester
    FROM student_curriculum sc
    JOIN adminsubject a ON sc.subject_id = a.subject_id
    JOIN adminstudent st ON sc.student_id = st.student_id
    JOIN course_subject cs 
        ON sc.subject_id = cs.subject_id 
        AND cs.course_id = st.course_id
    WHERE sc.student_id = '$student_id'
    ORDER BY cs.year_level ASC, FIELD(cs.semester,'1st','2nd','Summer')
");

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

<!DOCTYPE html>
<html>
<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>Student Curriculum - Admin</title>
    <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;
    font-family: "Segoe UI", sans-serif;
    background-color: #f4f6f8;
    }

    .mainbar {
    margin-left: 280px;
    width: calc(100% - 280px);
    flex: 1;
    display: flex;
    flex-direction: column;
}
    .open-btn {
    background-color: #4a9c3f;
    color: #fff;
    padding: 10px 18px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    transition: background-color 0.3s;
    }

    .open-btn:hover {
    background-color: #3e8c36;
    }

    /* Main content */
    .mainbar-context {
    flex: 1;
    padding: 20px 25px;
    overflow-y: auto;
    }

    /* Inner containers */
    .container, .container1 {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    margin-bottom: 20px;
    }

    .courses {
    background-color: #4a9c3f;
    color: #fff;
    padding: 20px;
    flex: 1 1 300px;
    border-radius: 10px;
    text-align: center;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    }

    /* Table Styling */
    table {
    width: 100%;
    border-collapse: collapse;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 10px rgba(0,0,0,0.05);
    text-align: center;
    background: #fff;
    }

    table th, table td {
    padding: 12px 15px;
    border-bottom: 1px solid #e0e0e0;
    font-size: 12px;
    color: #333;
    }

    table th {
    background-color: #4a9c3f;
    color: #fff;
    font-weight: 600;
    }

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

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

   .upd_btn{
     display: inline-block;
  padding: 4px 8px;
  background: #1261A0;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}

td form {
  margin: 0;
}
   
    .del_btn {
   display: inline-block;
  padding: 4px 8px;
  background: #EE7A02;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}

td form {
  margin: 0;
}
    /* Search Box */
    .search-container {
    margin-bottom: 15px;
    text-align: right;
    }

    .search-input {
    padding: 8px 12px;
    width: 220px;
    border: 1px solid #ccc;
    border-radius: 6px;
    font-size: 14px;
    }

    .search-input:focus {
    outline: none;
    border-color: #4a9c3f;
    box-shadow: 0 0 3px rgba(74,156,63,0.5);
    }

    </style>
    <style>
    /* Modal Overlay */
    .modal {
    display: none;
    position: fixed;
    z-index: 1000;
    inset: 0; 
    background: rgba(0,0,0,0.5);
    overflow-y: auto;
    padding: 20px;
    transition: opacity 0.3s ease;
    }

    /* Modal Content */
    .modal-content {
    background: #fff;
    max-width: 480px;
    width: 100%;
    margin: 80px auto;
    padding: 30px 25px;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
    animation: pop 0.3s ease;
    position: relative;
    box-sizing: border-box;
    }

    /* Pop Animation */
    @keyframes pop {
    from { transform: scale(0.85); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
    }

    /* Close Button */
    .close-btn {
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 26px;
    font-weight: bold;
    cursor: pointer;
    color: #444;
    transition: color 0.3s;
    }
    .close-btn:hover {
    color: #4a9c3f;
    }

    /* Modal Title */
    .discount-title {
    text-align: center;
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 25px;
    color: #333;
    }

    /* Form Styles */
    .discount-form {
    display: flex;
    flex-direction: column;
    gap: 18px;
    }

    .form-group {
    display: flex;
    flex-direction: column;
    }

    .form-label {
    font-weight: 500;
    margin-bottom: 6px;
    color: #555;
    font-size: 14px;
    }

    .form-input {
    padding: 12px 14px;
    font-size: 15px;
    border-radius: 8px;
    border: 1px solid #ccc;
    outline: none;
    transition: border-color 0.3s, box-shadow 0.3s;
    }

    .form-input:focus {
    border-color: #4a9c3f;
    box-shadow: 0 0 0 2px rgba(74,156,63,0.2);
    }

    /* Submit Button */
    .submit-btn {
    padding: 14px 0;
    background-color: #4a9c3f;
    color: #fff;
    font-weight: 600;
    font-size: 15px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s;
    }

    .submit-btn:hover {
    background-color: #3e8c36;
    }

    /* Responsive for Mobile */
    @media (max-width: 500px) {
    .modal-content {
        margin: 60px 15px;
        padding: 25px 20px;
    }

    .form-input {
        font-size: 14px;
        padding: 10px 12px;
    }

    .submit-btn {
        font-size: 14px;
    }
    }
    </style>
</head>
<body>
<div class="dashboard-container">
    
    <?php include('../adminpanel/adminnav.php'); ?>

   
    <aside class="mainbar">

      <div class="mainbar-context">
        <div class="container1">
        </div>
        <div class="container">
          <div class="courses">

              <?php foreach($curriculum as $year => $semesters): ?>
                <h4><?php echo $year; ?> Year</h4>

                <?php foreach($semesters as $sem => $subjects): ?>
                    <h4><?php echo $sem; ?> Semester</h4>

                    <?php $semester_total_units = 0; ?>

                    <table>
                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Subject</th>
                                <th>Lec</th>
                                <th>Lab</th>
                                <th>Total Units</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($subjects as $row): ?>
                                <?php $semester_total_units += $row['total_unit']; ?>
                                <tr>
                                    <td><?= htmlspecialchars($row['subject_code']); ?></td>
                                    <td><?= htmlspecialchars($row['subject_name']); ?></td>
                                    <td><?= $row['lecture_unit']; ?></td>
                                    <td><?= $row['lab_unit']; ?></td>
                                    <td><?= $row['total_unit']; ?></td>

                                    <td>
                                        <?php
                                        if ($row['sc_status'] == 'completed') {
                                            echo "Passed";
                                        } else {
                                            echo htmlspecialchars($row['sc_status']);
                                        }
                                        ?>
                                    </td>

                                    <td>
                                        <?php if ($row['sc_status'] == 'enrolled'): ?>
                                            <form method="POST" style="display:inline;">
                                                <input type="hidden" name="subject_id" value="<?= $row['subject_id']; ?>">
                                                <input type="hidden" name="new_status" value="completed">
                                                <button type="submit" name="update_status" class="upd_btn">Pass</button>
                                            </form>

                                            <form method="POST" style="display:inline;">
                                                <input type="hidden" name="subject_id" value="<?= $row['subject_id']; ?>">
                                                <input type="hidden" name="new_status" value="failed">
                                                <button type="submit" name="update_status" class="del_btn">Fail</button>
                                            </form>

                                        <?php elseif ($row['sc_status'] == 'completed'): ?>
                                            <span style="color:green; font-weight:bold;">✔ Passed</span>

                                        <?php elseif ($row['sc_status'] == 'failed'): ?>
                                            <span style="color:red; font-weight:bold;">✘ Failed</span>

                                        <?php else: ?>
                                            <span>-</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>

                    <h4>Total Units: <?= $semester_total_units; ?></h4>
                    <br>

                <?php endforeach; ?>
            <?php endforeach; ?>

              


          </div>
        </div>
      </div>
    </aside>
  </div>
    
     <script src="../Jscript/script.js"></script>
     <script src="../Jscript/form.js"></script>
</body>
</html>