<?php

require_once '../../../config.php';

$conn = getDBConnection();



$class_name = $_POST['class_name'] ?? '';



if (empty($class_name)) {

    echo '<tr><td colspan="6" class="text-center py-4">No class selected</td></tr>';

    exit;

}



$query = "

    SELECT cs.*, s.subject_name, s.subject_code 

    FROM class_subjects cs

    JOIN subjects s ON cs.subject_id = s.id

    WHERE cs.class_name = ?

    ORDER BY cs.display_order, s.subject_name

";



$stmt = $conn->prepare($query);

$stmt->bind_param("s", $class_name);

$stmt->execute();

$result = $stmt->get_result();



$output = '';

if ($result->num_rows === 0) {

    $output = '<tr><td colspan="6" class="text-center py-4">No subjects configured for this class. Click "Add Subject" to add.</td></tr>';

} else {

    while ($row = $result->fetch_assoc()) {

        $compulsoryChecked = $row['is_compulsory'] === 'yes' ? 'checked' : '';

        

        $output .= '

        <tr class="subject-row" data-subject-id="' . $row['id'] . '">

            <td>

                <strong>' . htmlspecialchars($row['subject_name']) . '</strong>

                <br><small class="text-muted">' . htmlspecialchars($row['subject_code']) . '</small>

            </td>

            <td>

                <div class="d-flex align-items-center">

                    <input type="number" class="form-control form-control-sm class_score_percent" 

                           value="' . $row['class_score_percent'] . '" min="0" max="100" step="1" 

                           style="width:70px" onchange="updateClassSubject(' . $row['id'] . ', \'class_score_percent\', this.value)">

                    <span class="mx-1">%</span>

                </div>

                <div class="mt-1">

                    <small>Max: </small>

                    <input type="number" class="form-control form-control-sm max_class_score" 

                           value="' . $row['max_class_score'] . '" min="0" step="0.5" 

                           style="width:70px" onchange="updateClassSubject(' . $row['id'] . ', \'max_class_score\', this.value)">

                </div>

            </td>

            <td>

                <div class="d-flex align-items-center">

                    <input type="number" class="form-control form-control-sm exam_score_percent" 

                           value="' . $row['exam_score_percent'] . '" min="0" max="100" step="1" 

                           style="width:70px" onchange="updateClassSubject(' . $row['id'] . ', \'exam_score_percent\', this.value)">

                    <span class="mx-1">%</span>

                </div>

                <div class="mt-1">

                    <small>Max: </small>

                    <input type="number" class="form-control form-control-sm max_exam_score" 

                           value="' . $row['max_exam_score'] . '" min="0" step="0.5" 

                           style="width:70px" onchange="updateClassSubject(' . $row['id'] . ', \'max_exam_score\', this.value)">

                </div>

            </td>

            <td>

                <div class="form-check">

                    <input class="form-check-input" type="checkbox" ' . $compulsoryChecked . '

                           onchange="updateClassSubject(' . $row['id'] . ', \'is_compulsory\', this.checked ? \'yes\' : \'no\')">

                </div>

            </td>

            <td>

                <input type="number" class="form-control form-control-sm display_order" 

                       value="' . $row['display_order'] . '" min="0" style="width:70px"

                       onchange="updateClassSubject(' . $row['id'] . ', \'display_order\', this.value)">

            </td>

            <td>

                <button class="btn btn-sm btn-outline-danger" onclick="removeClassSubject(' . $row['id'] . ')">

                    <i class="fas fa-trash"></i>

                </button>

            </td>

        </tr>';

    }

}



$stmt->close();

$conn->close();



echo $output;