feat: Implement Edit and Delete functionality for missions
This commit is contained in:
parent
933c384e49
commit
1d017848aa
@ -1,4 +1,33 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
// You can add any interactive scripts here in the future.
|
||||
// For example, handling view button clicks to show mission details.
|
||||
});
|
||||
|
||||
const editMissionModal = document.getElementById('editMissionModal');
|
||||
if (editMissionModal) {
|
||||
editMissionModal.addEventListener('show.bs.modal', function (event) {
|
||||
// Button that triggered the modal
|
||||
const button = event.relatedTarget;
|
||||
|
||||
// Extract info from data-* attributes
|
||||
const missionId = button.getAttribute('data-id');
|
||||
const name = button.getAttribute('data-name');
|
||||
const drone = button.getAttribute('data-drone');
|
||||
const status = button.getAttribute('data-status');
|
||||
const date = button.getAttribute('data-date');
|
||||
|
||||
// Update the modal's content.
|
||||
const modalTitle = editMissionModal.querySelector('.modal-title');
|
||||
const missionIdInput = editMissionModal.querySelector('#edit_mission_id');
|
||||
const nameInput = editMissionModal.querySelector('#edit_name');
|
||||
const droneInput = editMissionModal.querySelector('#edit_drone');
|
||||
const statusInput = editMissionModal.querySelector('#edit_status');
|
||||
const dateInput = editMissionModal.querySelector('#edit_mission_date');
|
||||
|
||||
modalTitle.textContent = 'Edit Mission: ' + name;
|
||||
missionIdInput.value = missionId;
|
||||
nameInput.value = name;
|
||||
droneInput.value = drone;
|
||||
statusInput.value = status;
|
||||
dateInput.value = date;
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
187
index.php
187
index.php
@ -1,35 +1,70 @@
|
||||
<?php
|
||||
require_once 'db/config.php';
|
||||
|
||||
// Handle form submission
|
||||
// Handle form submissions for create, update, and delete
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$name = $_POST['name'] ?? '';
|
||||
$drone = $_POST['drone'] ?? '';
|
||||
$status = $_POST['status'] ?? '';
|
||||
$mission_date = $_POST['mission_date'] ?? '';
|
||||
$action = $_POST['action'] ?? 'create';
|
||||
$pdo = db();
|
||||
|
||||
if ($name && $drone && $status && $mission_date) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$sql = "INSERT INTO missions (name, drone, status, mission_date) VALUES (:name, :drone, :status, :mission_date)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
':name' => $name,
|
||||
':drone' => $drone,
|
||||
':status' => $status,
|
||||
':mission_date' => $mission_date,
|
||||
]);
|
||||
// Redirect to avoid form resubmission
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
} catch (PDOException $e) {
|
||||
// For a real app, you'd log this error
|
||||
die("Error saving mission: " . $e->getMessage());
|
||||
try {
|
||||
switch ($action) {
|
||||
case 'create':
|
||||
$name = $_POST['name'] ?? '';
|
||||
$drone = $_POST['drone'] ?? '';
|
||||
$status = $_POST['status'] ?? '';
|
||||
$mission_date = $_POST['mission_date'] ?? '';
|
||||
|
||||
if ($name && $drone && $status && $mission_date) {
|
||||
$sql = "INSERT INTO missions (name, drone, status, mission_date) VALUES (:name, :drone, :status, :mission_date)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
':name' => $name,
|
||||
':drone' => $drone,
|
||||
':status' => $status,
|
||||
':mission_date' => $mission_date,
|
||||
]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$id = $_POST['mission_id'] ?? null;
|
||||
$name = $_POST['name'] ?? '';
|
||||
$drone = $_POST['drone'] ?? '';
|
||||
$status = $_POST['status'] ?? '';
|
||||
$mission_date = $_POST['mission_date'] ?? '';
|
||||
|
||||
if ($id && $name && $drone && $status && $mission_date) {
|
||||
$sql = "UPDATE missions SET name = :name, drone = :drone, status = :status, mission_date = :mission_date WHERE id = :id";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
':id' => $id,
|
||||
':name' => $name,
|
||||
':drone' => $drone,
|
||||
':status' => $status,
|
||||
':mission_date' => $mission_date,
|
||||
]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$id = $_POST['mission_id'] ?? null;
|
||||
if ($id) {
|
||||
$sql = "DELETE FROM missions WHERE id = :id";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([':id' => $id]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Redirect to avoid form resubmission on refresh
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
} catch (PDOException $e) {
|
||||
// For a real app, you'd log this error instead of dying
|
||||
die("Database error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch all missions
|
||||
// Fetch all missions for display
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->query("SELECT id, name, drone, status, DATE_FORMAT(mission_date, '%Y-%m-%d') as mission_date FROM missions ORDER BY mission_date DESC");
|
||||
@ -101,7 +136,7 @@ function getStatusClass($status) {
|
||||
<th scope="col">Drone</th>
|
||||
<th scope="col">Status</th>
|
||||
<th scope="col">Date</th>
|
||||
<th scope="col">Actions</th>
|
||||
<th scope="col" class="text-end">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -116,7 +151,28 @@ function getStatusClass($status) {
|
||||
<td><?php echo htmlspecialchars($mission['drone']); ?></td>
|
||||
<td><span class="status-badge <?php echo getStatusClass($mission['status']); ?>"><?php echo htmlspecialchars($mission['status']); ?></span></td>
|
||||
<td><?php echo htmlspecialchars($mission['mission_date']); ?></td>
|
||||
<td><button class="btn btn-sm btn-outline-secondary"><i data-feather="eye"></i> View</button></td>
|
||||
<td class="text-end">
|
||||
<a href="mission.php?id=<?php echo $mission['id']; ?>" class="btn btn-sm btn-outline-secondary me-1">
|
||||
<i data-feather="eye" class="feather-sm"></i> View
|
||||
</a>
|
||||
<button type="button" class="btn btn-sm btn-outline-primary me-1"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#editMissionModal"
|
||||
data-id="<?php echo $mission['id']; ?>"
|
||||
data-name="<?php echo htmlspecialchars($mission['name']); ?>"
|
||||
data-drone="<?php echo htmlspecialchars($mission['drone']); ?>"
|
||||
data-status="<?php echo htmlspecialchars($mission['status']); ?>"
|
||||
data-date="<?php echo htmlspecialchars($mission['mission_date']); ?>">
|
||||
<i data-feather="edit-2" class="feather-sm"></i> Edit
|
||||
</button>
|
||||
<form method="POST" action="index.php" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this mission?');">
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<input type="hidden" name="mission_id" value="<?php echo $mission['id']; ?>">
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">
|
||||
<i data-feather="trash-2" class="feather-sm"></i> Delete
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
@ -139,13 +195,14 @@ function getStatusClass($status) {
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form id="createMissionForm" method="POST" action="index.php">
|
||||
<input type="hidden" name="action" value="create">
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Mission Name</label>
|
||||
<input type="text" class="form-control" id="name" name="name" required>
|
||||
<label for="create_name" class="form-label">Mission Name</label>
|
||||
<input type="text" class="form-control" id="create_name" name="name" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="drone" class="form-label">Drone</label>
|
||||
<select class="form-select" id="drone" name="drone" required>
|
||||
<label for="create_drone" class="form-label">Drone</label>
|
||||
<select class="form-select" id="create_drone" name="drone" required>
|
||||
<option value="DJI Agras T30">DJI Agras T30</option>
|
||||
<option value="XAG P40">XAG P40</option>
|
||||
<option value="AG-Drone 01">AG-Drone 01</option>
|
||||
@ -154,16 +211,16 @@ function getStatusClass($status) {
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="status" class="form-label">Status</label>
|
||||
<select class="form-select" id="status" name="status" required>
|
||||
<label for="create_status" class="form-label">Status</label>
|
||||
<select class="form-select" id="create_status" name="status" required>
|
||||
<option value="Scheduled">Scheduled</option>
|
||||
<option value="In Progress">In Progress</option>
|
||||
<option value="Completed">Completed</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="mission_date" class="form-label">Date</label>
|
||||
<input type="date" class="form-control" id="mission_date" name="mission_date" required>
|
||||
<label for="create_mission_date" class="form-label">Date</label>
|
||||
<input type="date" class="form-control" id="create_mission_date" name="mission_date" required>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@ -175,14 +232,74 @@ function getStatusClass($status) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Edit Mission Modal -->
|
||||
<div class="modal fade" id="editMissionModal" tabindex="-1" aria-labelledby="editMissionModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="editMissionModalLabel">Edit Mission</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form id="editMissionForm" method="POST" action="index.php">
|
||||
<input type="hidden" name="action" value="update">
|
||||
<input type="hidden" name="mission_id" id="edit_mission_id">
|
||||
<div class="mb-3">
|
||||
<label for="edit_name" class="form-label">Mission Name</label>
|
||||
<input type="text" class="form-control" id="edit_name" name="name" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="edit_drone" class="form-label">Drone</label>
|
||||
<select class="form-select" id="edit_drone" name="drone" required>
|
||||
<option value="DJI Agras T30">DJI Agras T30</option>
|
||||
<option value="XAG P40">XAG P40</option>
|
||||
<option value="AG-Drone 01">AG-Drone 01</option>
|
||||
<option value="AG-Drone 02">AG-Drone 02</option>
|
||||
<option value="AG-Drone 03">AG-Drone 03</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="edit_status" class="form-label">Status</label>
|
||||
<select class="form-select" id="edit_status" name="status" required>
|
||||
<option value="Scheduled">Scheduled</option>
|
||||
<option value="In Progress">In Progress</option>
|
||||
<option value="Completed">Completed</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="edit_mission_date" class="form-label">Date</label>
|
||||
<input type="date" class="form-control" id="edit_mission_date" name="mission_date" required>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
<button type="submit" form="editMissionForm" class="btn btn-primary">Save Changes</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Bootstrap 5 JS -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<!-- Custom JS -->
|
||||
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
||||
|
||||
<script>
|
||||
feather.replace()
|
||||
feather.replace({
|
||||
class: 'feather',
|
||||
'stroke-width': 2,
|
||||
width: 20,
|
||||
height: 20
|
||||
})
|
||||
// Feather icons in buttons are smaller
|
||||
feather.replace({
|
||||
class: 'feather-sm',
|
||||
'stroke-width': 2,
|
||||
width: 16,
|
||||
height: 16
|
||||
})
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
92
mission.php
Normal file
92
mission.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
require_once 'db/config.php';
|
||||
|
||||
$mission_id = $_GET['id'] ?? null;
|
||||
$mission = null;
|
||||
$error = '';
|
||||
|
||||
if ($mission_id) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("SELECT * FROM missions WHERE id = ?");
|
||||
$stmt->execute([$mission_id]);
|
||||
$mission = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$mission) {
|
||||
$error = "Mission not found.";
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$error = "Database error: " . $e->getMessage();
|
||||
}
|
||||
} else {
|
||||
$error = "No mission ID provided.";
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Mission Details - AgroDrone Control</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.css">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<header class="header text-white">
|
||||
<div class="container d-flex justify-content-between align-items-center">
|
||||
<h1 class="logo">AgroDrone</h1>
|
||||
<nav>
|
||||
<a href="index.php" class="nav-link">Dashboard</a>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="container mt-5">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h2>Mission Details</h2>
|
||||
<a href="index.php" class="btn btn-secondary">
|
||||
<i data-feather="arrow-left" class="me-2"></i>Back to Dashboard
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<?php if ($error): ?>
|
||||
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
||||
<?php elseif ($mission): ?>
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h4 class="card-title mb-0">Mission: <?php echo htmlspecialchars($mission['name']); ?></h4>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<p><strong>Drone:</strong> <?php echo htmlspecialchars($mission['drone']); ?></p>
|
||||
<p><strong>Status:</strong> <span class="badge bg-primary"><?php echo htmlspecialchars($mission['status']); ?></span></p>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<p><strong>Date:</strong> <?php echo htmlspecialchars(date('F j, Y', strtotime($mission['mission_date']))); ?></p>
|
||||
<p><strong>Created At:</strong> <?php echo htmlspecialchars(date('F j, Y, g:i a', strtotime($mission['created_at']))); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
</main>
|
||||
|
||||
<footer class="footer mt-auto py-3 bg-light">
|
||||
<div class="container text-center">
|
||||
<span class="text-muted">© <?php echo date("Y"); ?> AgroDrone Control. All rights reserved.</span>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
||||
<script>
|
||||
feather.replace();
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user