Files
ppr/action.php

269 lines
7.2 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Row Details</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
width: 80%;
margin: 20px auto;
background-color: #fff;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h2 {
color: #333;
text-align: center;
margin-bottom: 20px;
}
.details {
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
}
.details p {
margin: 10px 0;
font-size: 16px;
}
.details p strong {
color: #333;
}
.back-link {
display: inline-block;
margin-top: 20px;
text-decoration: none;
color: #fff;
background-color: #007BFF;
padding: 10px 15px;
border-radius: 5px;
}
.back-link:hover {
background-color: #0056b3;
}
button {
padding: 10px 20px;
font-size: 1rem;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background-color: #0056b3;
}
.editable {
cursor: pointer;
padding: 5px;
border-radius: 3px;
transition: background 0.3s;
}
.editable:hover {
background:rgb(226, 43, 43);
}
</style>
</head>
<body>
<script>
document.addEventListener("DOMContentLoaded", function () {
let id = "<?php echo $_GET['id'];?>";
// Select all editable elements
document.querySelectorAll(".editable").forEach((element) => {
let oldValue = element.textContent.trim(); // Store initial value
element.addEventListener("focus", function () {
oldValue = this.textContent.trim(); // Store old value when focused
});
element.addEventListener("blur", function () {
let newValue = this.textContent.trim();
let column = this.getAttribute("data-column");
if (newValue !== oldValue) {
sendUpdate(id, column, oldValue, newValue, element);
}
});
});
});
/**
* Sends the updated data to the server
*/
function sendUpdate(flightId, column, oldValue, newValue, element) {
fetch("update_data.php", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: `id=${flightId}&column=${encodeURIComponent(column)}&old_value=${encodeURIComponent(oldValue)}&new_value=${encodeURIComponent(newValue)}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
console.log(`Updated: ${data.column} changed from '${data.old_value}' to '${data.new_value}'`);
oldValue = newValue; // ✅ Update oldValue to prevent reverting
} else {
alert("Error updating data");
element.textContent = oldValue; // ❌ Revert ONLY if update fails
}
})
.catch(error => {
console.error("Fetch error:", error);
element.textContent = oldValue; // ❌ Revert only on network failure
});
}
</script>
<?php
include("functions.php");
require_db_auth();
function opCancel() {
$conn = connectDb();
$sql = "UPDATE submitted SET status = 'CANCELED' where id = " . $_GET['id'];
$result = $conn->query($sql);
logJournal($conn, $_GET['id'], "Marked Canceled");
$conn->close();
}
function opLanded() {
$date = date('Y-m-d');
$time = urldecode($_GET['time']);
$landed_dt = $date . ' ' . $time;
$conn = connectDb();
$sql = "UPDATE submitted SET status = 'LANDED', landed_dt = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $landed_dt, $_GET['id']);
$stmt->execute();
$stmt->close();
logJournal($conn, $_GET['id'], "Marked Landed at time " . $time);
$conn->close();
}
function opDeparted() {
$date = date('Y-m-d');
$time = urldecode($_GET['time']);
$departed_dt = $date . ' ' . $time;
$conn = connectDb();
$sql = "UPDATE submitted SET status = 'DEPARTED', departed_dt = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $departed_dt, $_GET['id']);
$stmt->execute();
$stmt->close();
logJournal($conn, $_GET['id'], "Marked Departed at time " . $time);
$conn->close();
}
function opDelete() {
$conn = connectDb();
$sql = "UPDATE submitted SET status = 'DELETED' where id = " . $_GET['id'];
$result = $conn->query($sql);
logJournal($conn, $_GET['id'], "Marked Deleted");
$conn->close();
}
function opDetail() {
$conn = connectDb();
$sql = "SELECT * FROM submitted WHERE id = " . $_GET['id'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of the row
$row = $result->fetch_assoc();
echo '<div class="container">';
echo '<div class="details"><p><strong>Aircraft Reg: </strong>' . $row['ac_reg'] . "</p>";
echo "<p><strong>Aircraft Type:</strong> " . $row['ac_type'] . "</p>";
echo "<p><strong>Callsign:</strong> " . $row['ac_call'] . "</p>";
echo "<p><strong>Captain's Name:</strong> " . $row['captain'] . "</p>";
echo "<p><strong>Arriving From:</strong> " . $row['in_from'] . "</p>";
echo "<p><strong>POB IN:</strong> " . $row['pob_in'] . "</p>";
echo "<p><strong>ETA:</strong> " . $row['eta'] . "</p>";
echo "<p><strong>Fuel Required:</strong> " . $row['fuel'] . "</p>";
echo "<p><strong>POB OUT:</strong><span class=\"editable\" data-column=\"pob_out\" contenteditable=\"true\">" . $row['pob_out'] . "</p>";
echo "<p><strong>Outbound To:</strong><span class=\"editable\" data-column=\"out_to\" contenteditable=\"true\">" . $row['out_to'] . "</p>";
echo "<p><strong>ETD:</strong><span class=\"editable\" data-column=\"etd\" contenteditable=\"true\">" . $row['etd'] . "</p>";
echo "<p><strong>Email Address:</strong> " . $row['email'] . "</p>";
echo "<p><strong>Phone:</strong> " . $row['phone'] . "</p>";
echo "<p><strong>Notes:</strong> " . $row['notes'] . "</p>";
echo "<p><i>PPR created at:</strong> " . $row['submitted_dt'] . " by " . $row['created_by'] . "</p></div>";
} else {
echo "No details found for the given ID.";
}
$conn->close();
}
switch($_GET['op']) {
case "cancel":
opCancel();
break;
case "landed":
opLanded();
break;
case "departed":
opDeparted();
break;
case "delete":
opDelete();
break;
case "detail":
opDetail();
break;
default:
}
?>
<p><center><button onclick="window.close()">Close Window</button></center>