<?php

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

$conn = getDBConnection();



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



// If no class specified, use 'ALL' as fallback

if (empty($class_name)) {

    $class_name = 'ALL';

}



// Get class-specific boundaries, fallback to ALL

$query = "

    SELECT * FROM grade_boundaries 

    WHERE class_name = ? 

    UNION

    SELECT * FROM grade_boundaries 

    WHERE class_name = 'ALL' AND grade NOT IN (

        SELECT grade FROM grade_boundaries WHERE class_name = ?

    )

    ORDER BY 

        CASE grade

            WHEN 'A' THEN 1

            WHEN 'B' THEN 2

            WHEN 'C' THEN 3

            WHEN 'D' THEN 4

            WHEN 'E' THEN 5

            WHEN 'W' THEN 6

            ELSE 7

        END

";



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

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

$stmt->execute();

$result = $stmt->get_result();



$boundaries = [];

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

    $boundaries[$row['grade']] = $row;

}



// Ensure all grades exist

$grades = ['A', 'B', 'C', 'D', 'E', 'W'];

$output = '';



foreach ($grades as $grade) {

    $boundary = $boundaries[$grade] ?? [

        'grade' => $grade,

        'min_score' => 0,

        'max_score' => 0,

        'remark' => ''

    ];

    

    $gradeClass = 'grade-' . $grade;

    

    // Set default values for missing grades

    if (!isset($boundaries[$grade])) {

        switch($grade) {

            case 'A': $boundary['min_score'] = 90; $boundary['max_score'] = 100; $boundary['remark'] = 'EXCELLENT'; break;

            case 'B': $boundary['min_score'] = 80; $boundary['max_score'] = 89; $boundary['remark'] = 'VERY GOOD'; break;

            case 'C': $boundary['min_score'] = 70; $boundary['max_score'] = 79; $boundary['remark'] = 'GOOD'; break;

            case 'D': $boundary['min_score'] = 60; $boundary['max_score'] = 69; $boundary['remark'] = 'SATISFACTORY'; break;

            case 'E': $boundary['min_score'] = 50; $boundary['max_score'] = 59; $boundary['remark'] = 'PASS'; break;

            case 'W': $boundary['min_score'] = 0; $boundary['max_score'] = 49; $boundary['remark'] = 'WEAK'; break;

        }

    }

    

    $output .= '

    <div class="mb-3 grade-boundary-row" data-grade="' . $grade . '">

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

            <div class="grade-badge ' . $gradeClass . ' me-2">' . $grade . '</div>

            <div class="flex-grow-1">

                <div class="row g-2">

                    <div class="col-5">

                        <input type="number" class="form-control form-control-sm min-score" 

                               value="' . htmlspecialchars($boundary['min_score']) . '" 

                               min="0" max="100" step="0.01"

                               onchange="updateGradeBoundary(\'' . $grade . '\', \'min_score\', this.value)"

                               placeholder="Min">

                    </div>

                    <div class="col-5">

                        <input type="number" class="form-control form-control-sm max-score" 

                               value="' . htmlspecialchars($boundary['max_score']) . '" 

                               min="0" max="100" step="0.01"

                               onchange="updateGradeBoundary(\'' . $grade . '\', \'max_score\', this.value)"

                               placeholder="Max">

                    </div>

                </div>

            </div>

        </div>

        <input type="text" class="form-control form-control-sm remark" 

               value="' . htmlspecialchars($boundary['remark']) . '"

               onchange="updateGradeBoundary(\'' . $grade . '\', \'remark\', this.value)"

               placeholder="Remark (e.g., EXCELLENT, VERY GOOD)">

    </div>';

}



$stmt->close();

$conn->close();



echo $output;