34429-vm/index.php
Flatlogic Bot 933c384e49 feat: Implement Create Mission functionality
- Add a database setup script to create the `missions` table.
- Implement a modal form for creating new missions.
- Add backend PHP logic to handle form submission and save missions to the database.
- Display the list of missions dynamically from the database.
2025-09-27 08:49:28 +00:00

189 lines
8.0 KiB
PHP

<?php
require_once 'db/config.php';
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$drone = $_POST['drone'] ?? '';
$status = $_POST['status'] ?? '';
$mission_date = $_POST['mission_date'] ?? '';
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());
}
}
}
// Fetch all missions
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");
$missions = $stmt->fetchAll();
} catch (PDOException $e) {
die("Error fetching missions: " . $e->getMessage());
}
function getStatusClass($status) {
switch (strtolower($status)) {
case 'completed':
return 'status-completed';
case 'in progress':
return 'status-in-progress';
case 'scheduled':
return 'status-pending';
default:
return 'status-pending';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AgroDrone Control - Mission Dashboard</title>
<meta name="description" content="Dashboard for managing agricultural drone missions.">
<!-- Open Graph -->
<meta property="og:title" content="AgroDrone Control">
<meta property="og:description" content="Dashboard for managing agricultural drone missions.">
<meta property="og:image" content="https://picsum.photos/seed/drone1/400/300">
<meta property="og:url" content="">
<meta property="og:type" content="website">
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<!-- Feather Icons -->
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
</head>
<body>
<header class="header text-center">
<div class="container">
<h1 class="display-4">AgroDrone Control</h1>
<p class="lead">Welcome to your mission control center</p>
</div>
</header>
<main class="container my-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="h4">Mission Dashboard</h2>
<button id="createMissionBtn" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createMissionModal">
<i data-feather="plus" class="me-1"></i> Create New Mission
</button>
</div>
<div class="mission-table table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th scope="col">Mission Name</th>
<th scope="col">Drone</th>
<th scope="col">Status</th>
<th scope="col">Date</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($missions)): ?>
<tr>
<td colspan="5" class="text-center">No missions found. Create one to get started!</td>
</tr>
<?php else: ?>
<?php foreach ($missions as $mission): ?>
<tr>
<td><?php echo htmlspecialchars($mission['name']); ?></td>
<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>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</main>
<footer class="footer text-center">
<p>&copy; <?php echo date("Y"); ?> AgroDrone Control. All Rights Reserved.</p>
</footer>
<!-- Create Mission Modal -->
<div class="modal fade" id="createMissionModal" tabindex="-1" aria-labelledby="createMissionModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="createMissionModalLabel">New Mission</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="createMissionForm" method="POST" action="index.php">
<div class="mb-3">
<label for="name" class="form-label">Mission Name</label>
<input type="text" class="form-control" id="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>
<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="status" class="form-label">Status</label>
<select class="form-select" id="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>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" form="createMissionForm" class="btn btn-primary">Save Mission</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()
</script>
</body>
</html>