<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // รับค่าคะแนนจากฟอร์ม
    $spirit = isset($_POST['spirit']) ? intval($_POST['spirit']) : 0;
    $test = isset($_POST['test']) ? intval($_POST['test']) : 0;
    $assignment = isset($_POST['assignment']) ? intval($_POST['assignment']) : 0;
    $finalExam = isset($_POST['final_exam']) ? intval($_POST['final_exam']) : 0;

    // คำนวณคะแนนรวม
    $totalScore = $spirit + $test + $assignment + $finalExam;

    // กำหนดเกรดตามคะแนนรวม
    $grade = '';
    if ($totalScore >= 80) {
        $grade = 'A';
    } elseif ($totalScore >= 75) {
        $grade = 'B+';
    } elseif ($totalScore >= 70) {
        $grade = 'B';
    } elseif ($totalScore >= 65) {
        $grade = 'C+';
    } elseif ($totalScore >= 60) {
        $grade = 'C';
    } elseif ($totalScore >= 55) {
        $grade = 'D+';
    } elseif ($totalScore >= 50) {
        $grade = 'D';
    } else {
        $grade = 'F';
    }

    // แสดงผลลัพธ์
    $result = [
        'spirit' => $spirit,
        'test' => $test,
        'assignment' => $assignment,
        'finalExam' => $finalExam,
        'totalScore' => $totalScore,
        'grade' => $grade
    ];
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ฟอร์มกรอกคะแนนและคำนวณเกรด</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .form-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            max-width: 700px;
            margin: auto;
        }
        h2 {
            text-align: center;
        }
        table {
            width: 100%;
            margin-bottom: 20px;
        }
        table td {
            padding: 10px;
            text-align: left;
        }
        table input {
            width: 100%;
            padding: 8px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .form-group {
            text-align: center;
        }
        .form-group input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 18px;
        }
        .form-group input[type="submit"]:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #eef2f3;
            border: 1px solid #ccc;
            border-radius: 8px;
            text-align: center;
            font-size: 18px;
        }
        .result h3 {
            margin: 0;
        }
        .result p {
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>ฟอร์มกรอกคะแนนและคำนวณเกรด</h2>
        <form id="gradeForm" method="POST">
            <table>
                <tr>
                    <td><label for="spirit">จิตพิสัย (20 คะแนน)</label></td>
                    <td><input type="number" id="spirit" name="spirit" min="0" max="20" required></td>
                </tr>
                <tr>
                    <td><label for="test">แบบทดสอบ (30 คะแนน)</label></td>
                    <td><input type="number" id="test" name="test" min="0" max="30" required></td>
                </tr>
                <tr>
                    <td><label for="assignment">งาน (20 คะแนน)</label></td>
                    <td><input type="number" id="assignment" name="assignment" min="0" max="20" required></td>
                </tr>
                <tr>
                    <td><label for="final_exam">สอบปลายภาค (30 คะแนน)</label></td>
                    <td><input type="number" id="final_exam" name="final_exam" min="0" max="30" required></td>
                </tr>
            </table>

            <div class="form-group">
                <input type="submit" value="คำนวณเกรด">
            </div>
        </form>

        <?php if (isset($result)): ?>
        <!-- แสดงผลคะแนนรวมและเกรด -->
        <div id="result" class="result">
            <hr><p>คะแนนรวม: <?php echo $result['totalScore']; ?> คะแนน</p>
            <p>เกรดที่ได้รับ: <?php echo $result['grade']; ?></p>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>