<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pole_booking";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Check if form submitted via POST method
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_booking'])) {
    $diameter = $_POST['diameter'];
    $point = $_POST['point'];
    $pole_number = $_POST['pole_number'];
    $requester_name = $_POST['requester_name'];
    $company = $_POST['company'];
    $date = $_POST['date'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];

    // SQL query to insert data into database
    $sql = "INSERT INTO bookings (diameter, point, pole_number, requester_name, company, date, start_time, end_time) 
            VALUES ('$diameter', '$point', '$pole_number', '$requester_name', '$company', '$date', '$start_time', '$end_time')";

    if (mysqli_query($conn, $sql)) {
        // Redirect to the same page to prevent resubmission
        header("Location: " . $_SERVER['PHP_SELF']);
        exit();
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

// Check if form submitted via POST method for delete
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['delete_id'])) {
    $id = $_POST['delete_id'];

    // SQL query to delete data from database
    $sql_delete = "DELETE FROM bookings WHERE id='$id'";

    if (mysqli_query($conn, $sql_delete)) {
        // Redirect to the same page to prevent resubmission
        header("Location: " . $_SERVER['PHP_SELF']);
        exit();
    } else {
        echo "Error: " . $sql_delete . "<br>" . mysqli_error($conn);
    }
}

// Fetch all bookings data
$sql_select = "SELECT * FROM bookings";
$result = mysqli_query($conn, $sql_select);

?>

<!DOCTYPE html>
<html lang="en">
<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;
            background-color: #f0f0f0;
            text-align: center;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
            padding: 8px;
        }
        th {
            background-color: #f2f2f2;
        }
        a {
            text-decoration: none;
            color: #007bff;
            font-weight: bold;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>ข้อมูลการจองเสาไฟฟ้า</h2>
        <?php
        if (mysqli_num_rows($result) > 0) {
            echo "<table>";
            echo "<tr><th>ID</th><th>Diameter</th><th>Point</th><th>Pole Number</th><th>Requester Name</th><th>Company</th><th>Date</th><th>Start Time</th><th>End Time</th><th>Action</th></tr>";
            while($row = mysqli_fetch_assoc($result)) {
                echo "<tr>";
                echo "<td>".$row['id']."</td>";
                echo "<td>".$row['diameter']."</td>";
                echo "<td>".$row['point']."</td>";
                echo "<td>".$row['pole_number']."</td>";
                echo "<td>".$row['requester_name']."</td>";
                echo "<td>".$row['company']."</td>";
                echo "<td>".$row['date']."</td>";
                echo "<td>".$row['start_time']."</td>";
                echo "<td>".$row['end_time']."</td>";
                echo "<td>
                        <form method='POST' action='".$_SERVER['PHP_SELF']."' onsubmit='return confirm(\"Are you sure you want to delete this record?\");'>
                            <input type='hidden' name='delete_id' value='".$row['id']."'>
                            <input type='submit' value='Delete'>
                        </form>
                    </td>";
                echo "</tr>";
            }
            echo "</table>";
        } else {
            echo "ไม่มีข้อมูลการจอง";
        }

        // Close connection
        mysqli_close($conn);
        ?>
        <br>
        <a href="index-admin.html">กลับหน้าแรก</a>
    </div>
</body>
</html>