<?php
$servername = "localhost";
$username = "u66301280010";
$password = "@2566";
$dbname = "u66301280010";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$studentData = "";
$message = ""; // เพิ่มตัวแปรข้อความแสดงสถานะ

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['studentCode'])) {
    $studentCode = $_POST['studentCode'];
    
    $sql = "SELECT studentCode, firstname, surname FROM student WHERE studentCode = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $studentCode);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $studentData = "<tr>
                                <td>" . $row["studentCode"] . "</td>
                                <td>" . $row["firstname"] . " " . $row["surname"] . "</td>
                                <td>
                                    <input type='radio' name='attendance' value='มา' required> มา
                                    <input type='radio' name='attendance' value='มาสาย' required> มาสาย
                                    <input type='radio' name='attendance' value='ลา' required> ลา
                                    <input type='radio' name='attendance' value='ขาด' required> ขาด
                                </td>
                            </tr>
                            <input type='hidden' name='studentCode' value='" . $studentCode . "'>";
        }
    } else {
        $studentData = "<tr><td colspan='3'>No results found</td></tr>";
    }
    
    $stmt->close();
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['attendance']) && isset($_POST['studentCode'])) {
    $attendanceStatus = $_POST['attendance'];
    $studentCode = $_POST['studentCode']; // เก็บ studentCode ที่ส่งมาจากฟอร์ม
    
    // เรียกข้อมูลชื่อ-นามสกุลอีกครั้ง
    $sql = "SELECT firstname, surname FROM student WHERE studentCode = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $studentCode);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    $firstname = $row["firstname"];
    $surname = $row["surname"];
    $date = date("Y-m-d"); // บันทึกวันที่ปัจจุบัน
    
    // บันทึกข้อมูลลงในตาราง attendance
    $sql = "INSERT INTO attendance (name, surname, status, date) VALUES (?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssss", $firstname, $surname, $attendanceStatus, $date);
    
    if ($stmt->execute()) {
        $message = "ข้อมูลถูก Upload ไปยัง Database แล้ว!"; // แสดงข้อความยืนยัน
    } else {
        $message = "เกิดข้อผิดพลาดในการ Upload ข้อมูล: " . $stmt->error; // แสดงข้อความข้อผิดพลาด
    }
    
    $stmt->close();
}

$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .navbar {
            width: 100%;
            background-color: red;
            overflow: hidden;
            padding: 10px 0;
            position: fixed; /* ทำให้แถบนำทางอยู่บนสุด */
            top: 0;
            left: 0;
        }
        .navbar a {
            float: left;
            display: block;
            color: white;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
        }
        .navbar a:hover {
            background-color: darkred;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
            margin-top: 100px; /* เพิ่มการเว้นระยะด้านบนเพื่อไม่ให้ติดกับแถบนำทาง */
            text-align: center; /* จัดเนื้อหาให้อยู่ตรงกลาง */
        }
        h1 {
            text-align: center;
            color: #333;
        }
        form {
            display: flex;
            flex-direction: column;
            align-items: center; /* จัดฟอร์มให้อยู่ตรงกลาง */
        }
        label {
            margin: 10px 0 5px;
            font-weight: bold;
        }
        input[type="text"], input[type="submit"] {
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: 80%; /* เพิ่มความกว้างของ input */
        }
        input[type="submit"] {
            background-color: #007BFF;
            color: white;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f2f2f2;
        }
        .message {
            text-align: center;
            color: green;
            font-weight: bold;
            border: 1px solid #4CAF50;
            padding: 10px;
            border-radius: 5px;
            background-color: #d4edda;
            margin-top: 20px;
            display: inline-block;
        }
        .center {
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="navbar">
        <a href="index.php">หน้าแรก</a>
        <a href="https://www.example.com/about">เกี่ยวกับเรา</a>
        <a href="https://www.example.com/contact">ติดต่อเรา</a>
    </div>
    <div class="container">
        <h1>เช็คชื่อนักเรียน</h1>
        <form method="post">
            <label for="studentCode">รหัสนักเรียน:</label>
            <input type="text" id="studentCode" name="studentCode" required>
            <input type="submit" value="ค้นหา">
        </form>
        
        <?php if (!empty($studentData)) { ?>
            <form method="post">
                <table>
                    <tr>
                        <th>รหัสนักเรียน</th>
                        <th>ชื่อ-สกุล</th>
                        <th>สถานะการเช็คชื่อ</th>
                    </tr>
                    <?php echo $studentData; ?>
                </table>
                <br>
                <input type="submit" value="บันทึกการเช็คชื่อ">
            </form>
        <?php } ?>
        
        <?php if (!empty($message)) { ?>
            <div class="center">
                <p class="message"><?php echo $message; ?></p>
            </div>
        <?php } ?>
    </div>
</body>
</html>
