<?php
$host = "nitigon.cmtc.ac.th";
$port = "3307";
$username = "root";  // ใส่ชื่อผู้ใช้ของคุณ
$password = "root1234";  // ใส่รหัสผ่านของคุณ
$dbname = "webdb";  // ใส่ชื่อฐานข้อมูลที่ต้องการเชื่อมต่อ

// สร้างการเชื่อมต่อ
$conn = new mysqli($host, $username, $password, $dbname, $port);
// ตรวจสอบการเชื่อมต่อ
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>แสดงข้อมูลในตาราง</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color:rgb(65, 63, 63);
            color: #333;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: rgb(255, 255, 255);
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        h2 {
            text-align: center;
            color: #4CAF50;
        }
        form {
            margin-bottom: 20px;
            text-align: center;
        }
        input[type="date"],
        input[type="time"],
        input[type="text"],
        input[type="submit"] {
            padding: 10px;
            margin: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        input[type="button"] {
            background-color: #2196F3;
            color: white;
            border: none;
            cursor: pointer;
            padding: 10px;
            margin: 5px;
            border-radius: 4px;
            font-size: 16px;
        }
        input[type="button"]:hover {
            background-color: #0b7dda;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        tr:nth-child(even) {
            background-color:rgb(255, 255, 255);
        }
        tr:hover {
            background-color:rgb(180, 180, 180);
        }
        .pagination {
            text-align: center;
        }
        .pagination a {
            margin: 0 5px;
            padding: 8px 16px;
            border: 1px solid #ddd;
            color: #4CAF50;
            text-decoration: none;
            border-radius: 4px;
        }
        .pagination a:hover {
            background-color: #ddd;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>แสดงข้อมูลในตาราง</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            วัน: <input type="date" name="date" value="<?php echo isset($_POST['date']) ? $_POST['date'] : ''; ?>">
            เวลา: <input type="time" name="time" value="<?php echo isset($_POST['time']) ? $_POST['time'] : ''; ?>">
            สถานะ: <input type="text" name="status" value="<?php echo isset($_POST['status']) ? $_POST['status'] : ''; ?>">
            <input type="submit" name="search" value="ค้นหา">
            <input type="button" onclick="window.location.href='<?php echo $_SERVER['PHP_SELF']; ?>'" value="ดูทั้งหมด">
        </form>

        <?php
        // ตั้งค่าตัวแปรสำหรับการแบ่งหน้า
        $limit = 20; // จำนวนรายการต่อหน้า
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $start = ($page - 1) * $limit;

        // สร้างคำสั่ง SQL เริ่มต้น
        $sql = "SELECT * FROM dbtest WHERE 1=1";

        // ถ้ามีการกดปุ่มค้นหา
        $date = isset($_POST['date']) ? $_POST['date'] : (isset($_GET['date']) ? $_GET['date'] : '');
        $time = isset($_POST['time']) ? $_POST['time'] : (isset($_GET['time']) ? $_GET['time'] : '');
        $status = isset($_POST['status']) ? $_POST['status'] : (isset($_GET['status']) ? $_GET['status'] : '');

        if (!empty($date)) {
            $sql .= " AND date = ?";
        }
        if (!empty($time)) {
            $sql .= " AND time = ?";
        }
        if (!empty($status)) {
            $sql .= " AND status LIKE ?";
        }

        // เตรียมคำสั่ง
        $stmt = $conn->prepare($sql);

        // ผูกตัวแปร
        $params = [];
        if (!empty($date)) {
            $params[] = $date;
        }
        if (!empty($time)) {
            $params[] = $time;
        }
        if (!empty($status)) {
            $params[] = "%" . $status . "%";
        }
        if (count($params) > 0) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }

        // ดำเนินการคำสั่ง
        $stmt->execute();
        $result = $stmt->get_result();

        // คำนวณจำนวนทั้งหมดของรายการ
        $total_records = $result->num_rows;
        $total_pages = ceil($total_records / $limit);

        // เพิ่ม LIMIT สำหรับการแบ่งหน้า
        $sql .= " LIMIT $start, $limit";
        
        // เตรียมคำสั่งใหม่สำหรับการแบ่งหน้า
        $stmt = $conn->prepare($sql);
        if (count($params) > 0) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            echo "<table><tr><th>วัน</th><th>เวลา</th><th>สถานะ</th></tr>";
            // แสดงผลข้อมูล
            while($row = $result->fetch_assoc()) {
                echo "<tr><td>" . $row["date"]. "</td><td>" . $row["time"]. "</td><td>" . $row["status"]. "</td></tr>";
            }
            echo "</table>";

            // ปุ่มเปลี่ยนหน้า
            echo "<div class='pagination'>";
            // ปุ่มไปหน้าแรก
            if ($page > 1) {
                echo "<a href='?page=1&date=" . $date . "&time=" . $time . "&status=" . urlencode($status) . "'>&laquo; หน้าแรก</a> ";
                echo "<a href='?page=" . ($page - 1) . "&date=" . $date . "&time=" . $time . "&status=" . urlencode($status) . "'>&lsaquo; ก่อนหน้า</a> ";
            }
            // ปุ่มหน้าถัดไป
            for ($i = 1; $i <= $total_pages; $i++) {
                if ($i == $page) {
                    echo "<strong>" . $i . "</strong> ";
                } else {
                    echo "<a href='?page=" . $i . "&date=" . $date . "&time=" . $time . "&status=" . urlencode($status) . "'>" . $i . "</a> ";
                }
            }
            // ปุ่มไปหน้าสุดท้าย
            if ($page < $total_pages) {
                echo "<a href='?page=" . ($page + 1) . "&date=" . $date . "&time=" . $time . "&status=" . urlencode($status) . "'>ถัดไป &rsaquo;</a> ";
                echo "<a href='?page=" . $total_pages . "&date=" . $date . "&time=" . $time . "&status=" . urlencode($status) . "'>หน้าสุดท้าย &raquo;</a> ";
            }
            echo "</div>";
        } else {
            echo "ไม่พบข้อมูล";
        }

        // ปิดการเชื่อมต่อ
        $conn->close();
        ?>
    </div>
</body>
</html>
