<?php include("../database/auth_check.php"); ?>

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

if (!isset($_GET['subject_id'])) {
    header("Location: ../adminpanel/adminsubject.php");
    exit;
}

$subject_id = $_GET['subject_id'];

$sql = "
    SELECT s.*, f.fee_name, f.fee_amount
    FROM adminsubject s
    LEFT JOIN subject_fee sf ON s.subject_id = sf.subject_id
    LEFT JOIN adminfee f ON sf.fee_id = f.fee_id
    WHERE s.subject_id = '$subject_id'
";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);

if (isset($_POST['update_subject'])) {
    $subject_id = $_POST['subject_id'];
    $subject_code = $_POST['subject_code'];
    $subject_name = $_POST['subject_name'];
    $lecture_unit = $_POST['lecture_unit'];
    $lab_unit = $_POST['lab_unit'];
    $total_unit = $lecture_unit + $lab_unit; 
    $pre_req = $_POST['pre_req'];

    $update_sql = "UPDATE adminsubject 
                  SET subject_code='$subject_code', 
                      subject_name='$subject_name', 
                      lecture_unit='$lecture_unit',
                      lab_unit='$lab_unit', 
                      total_unit='$total_unit', 
                      pre_req='$pre_req',
                  WHERE subject_id='$subject_id'";

    if (mysqli_query($con, $update_sql)) {
        echo "<script>alert('Subject Updated Successfully!'); window.location='subject_view.php?subject_id=$subject_id';</script>";
        exit();
    } else {
        echo "Error updating subject: " . mysqli_error($con);
    }
}

$course_q = mysqli_query($con, "
    SELECT course_id 
    FROM course_subject 
    WHERE subject_id = '$subject_id' 
    LIMIT 1
");

$course_data = mysqli_fetch_assoc($course_q);
$course_id = $course_data['course_id'];
?>

<!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>Subject Details - 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>

/* GLOBAL */
body {
  font-family: "Segoe UI", sans-serif;
  background: #f4f6f8;
  color: #333;
}

/* LAYOUT */
.dashboard-container {
  display: flex;
  min-height: 100vh;
}

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

.mainbar-context {
  padding: 25px;
}

/* TITLE */
h2 {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 15px;
}

/* CARD CONTAINER */
.container {
  background: #fff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

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

table th {
  background: #4a9c3f;
  color: #fff;
  font-size: 14px;
  padding: 12px;
}

table td {
  padding: 12px;
  font-size: 14px;
  border-bottom: 1px solid #eee;
}

table tr:hover {
  background: rgba(74,156,63,0.08);
}

/* BUTTONS */
.open-btn {
  background: #4a9c3f;
  color: #fff;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  margin-top: 15px;
}

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

/* LINK BUTTON */
a.manage-btn {
  display: inline-block;
  margin-top: 15px;
  margin-left: 10px;
  padding: 10px 16px;
  background: #1261A0;
  color: #fff;
  border-radius: 8px;
  text-decoration: none;
  font-size: 14px;
}

a.manage-btn:hover {
  background: #0f4d80;
}

/* MODAL */
.modal {
  display: none;
  position: fixed;
  z-index: 2000;
  inset: 0;
  background: rgba(0,0,0,0.5);
}

/* MODAL BOX */
.modal-content {
  background: #fff;
  width: 420px;
  max-width: 90%;
  margin: 8% auto;
  padding: 25px;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
  animation: pop 0.25s ease;
}

/* FORM */
.modal-content h2 {
  text-align: center;
  margin-bottom: 15px;
}

.modal-content label {
  font-size: 13px;
  margin-bottom: 5px;
  display: block;
}

.modal-content input {
  width: 100%;
  padding: 10px;
  margin-bottom: 12px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 13px;
}

.modal-content input:focus {
  border-color: #4a9c3f;
  outline: none;
}

/* SUBMIT BUTTON */
.modal-content input[type="submit"] {
  background: #4a9c3f;
  color: #fff;
  border: none;
  cursor: pointer;
}

.modal-content input[type="submit"]:hover {
  background: #3e8c36;
}

/* CLOSE BUTTON */
.close-btn {
  float: right;
  font-size: 20px;
  cursor: pointer;
}

/* ANIMATION */
@keyframes pop {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
</style>
<body> 
   <div class="dashboard-container">
    <!-- Left Sidebar -->
    <?php include('../adminpanel/adminnav.php'); ?>

    <!-- Mainbar / Main Content -->
    <aside class="mainbar">

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

        <h2><?php echo $row['subject_code'] . " - " . $row['subject_name']; ?></h2>
          <table>
            <thead>
              <tr>
                <th>Lecture Unit</th>
                <th>Lab Unit</th>
                <th>Total Unit</th>
                <th>Pre-requisites</th>
                <th>Additional Fee</th>

              </tr>
            </thead>
            <tbody>
              <tr>
                <td><?php echo $row['lecture_unit']; ?></td>
                <td><?php echo $row['lab_unit']; ?></td>
                <td><?php echo $row['total_unit']; ?></td>
                <td><?php echo $row['pre_req']; ?></td>
                <td>
                  <?php 
                  if($row['fee_name']){
                      echo $row['fee_name'] . " - ₱" . $row['fee_amount'];
                  }else{
                      echo "None";
                  }
                  ?>
                </td>
                
              </tr>
            </tbody>
          </table>
          <br>
          <button id="openModal" class="open-btn">UPDATE</button>
         <a class="manage-btn" href="../adminpanel/manage_prerequisite.php?subject_id=<?php echo $row['subject_id']; ?>&course_id=<?php echo $course_id; ?>">MANAGE PREREQUISITE</a>
        </div>
        
      </div>
    </aside>
  </div>

<div id="courseModal" class="modal">
    <div class="modal-content">
      <span id="closeModal" class="close-btn">&times;</span>

      <h2>Update Subject</h2>

      <form method="POST">
        <input type="hidden" name="subject_id" value="<?php echo $row['subject_id']; ?>">

        <p>
          <label>Subject Code:</label>
          <input type="text" name="subject_code"
                value="<?php echo $row['subject_code']; ?>" required>
        </p><br>

        <p>
          <label>Subject Name:</label>
          <input type="text" name="subject_name"
                value="<?php echo $row['subject_name']; ?>" required>
        </p><br>

        <p>
          <label>Lecture Unit:</label>
          <input type="number" name="lecture_unit" id="lecture_unit"
                value="<?php echo $row['lecture_unit']; ?>" required>
        </p><br>

        <p>
          <label>Lab Unit:</label>
          <input type="number" name="lab_unit" id="lab_unit"
                value="<?php echo $row['lab_unit']; ?>" required>
        </p><br>

        <p>
          <label>Total Unit:</label>
          <input type="number" name="total_unit" id="total_unit"
                value="<?php echo $row['total_unit']; ?>" readonly>
        </p><br>

        <input type="submit" name="update_subject" value="Update">
      </form>

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

    <script>
      const lectureInput = document.getElementById('lecture_unit');
      const labInput = document.getElementById('lab_unit');
      const totalInput = document.getElementById('total_unit');

      function updateTotal() {
          totalInput.value =
            (parseInt(lectureInput.value) || 0) +
            (parseInt(labInput.value) || 0);
      }

      lectureInput.addEventListener('input', updateTotal);
      labInput.addEventListener('input', updateTotal);
    </script>

    
</body>
</html>