237 lines
12 KiB
PHP
237 lines
12 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Fetch total customers
|
|
$stmt_customers = $pdo->query('SELECT COUNT(*) as total FROM customers');
|
|
$total_customers = $stmt_customers->fetch()['total'];
|
|
|
|
// Fetch total bookings
|
|
$stmt_bookings = $pdo->query('SELECT COUNT(*) as total FROM bookings');
|
|
$total_bookings = $stmt_bookings->fetch()['total'];
|
|
|
|
// Fetch total revenue
|
|
$stmt_revenue = $pdo->query("SELECT SUM(actual_revenue) as total FROM bookings WHERE status = 'completed'");
|
|
$total_revenue = $stmt_revenue->fetch()['total'] ?? 0;
|
|
|
|
// Fetch recent bookings
|
|
$stmt_recent_bookings = $pdo->query('SELECT * FROM bookings ORDER BY created_at DESC LIMIT 5');
|
|
$recent_bookings = $stmt_recent_bookings->fetchAll();
|
|
|
|
// --- API Key Management ---
|
|
|
|
// Handle API key generation
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['generate_api_key'])) {
|
|
$new_key = 'hvac_' . bin2hex(random_bytes(16));
|
|
$stmt_insert_key = $pdo->prepare("INSERT INTO api_keys (api_key) VALUES (?)");
|
|
$stmt_insert_key->execute([$new_key]);
|
|
// Redirect to avoid form resubmission
|
|
header("Location: " . $_SERVER['PHP_SELF']);
|
|
exit;
|
|
}
|
|
|
|
// Fetch all API keys
|
|
$stmt_api_keys = $pdo->query('SELECT * FROM api_keys ORDER BY created_at DESC');
|
|
$api_keys = $stmt_api_keys->fetchAll();
|
|
|
|
} catch (PDOException $e) {
|
|
// For production, you would log this error and show a user-friendly message.
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
|
|
$project_name = "HVAC Command Center";
|
|
$project_description = "Central dashboard for managing your HVAC business operations.";
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title><?= htmlspecialchars($project_name) ?></title>
|
|
<meta name="description" content="<?= htmlspecialchars($project_description) ?>">
|
|
|
|
<!-- Open Graph / Twitter -->
|
|
<meta property="og:title" content="<?= htmlspecialchars($project_name) ?>">
|
|
<meta property="og:description" content="<?= htmlspecialchars($project_description) ?>">
|
|
<meta property="og:image" content="<?= htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? '') ?>">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
|
|
<!-- Styles -->
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
|
|
<!-- Fonts -->
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@700&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
|
|
</head>
|
|
<body>
|
|
|
|
<header class="header text-white">
|
|
<div class="container-fluid">
|
|
<h1 class="display-6 m-0"><?= htmlspecialchars($project_name) ?></h1>
|
|
</div>
|
|
</header>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
|
<div class="container-fluid">
|
|
<a class="navbar-brand" href="index.php">Dashboard</a>
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav">
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="customers.php">Customers</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="bookings.php">Bookings</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="ai-call-logs.php">AI Call Logs</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container-fluid mt-4">
|
|
|
|
<?php if (isset($error)): ?>
|
|
<div class="alert alert-danger">
|
|
<i class="bi bi-exclamation-triangle-fill"></i> <?= htmlspecialchars($error) ?>
|
|
</div>
|
|
<?php else: ?>
|
|
<!-- Stat Cards -->
|
|
<div class="row g-4">
|
|
<div class="col-md-4">
|
|
<div class="card h-100 shadow-sm">
|
|
<div class="card-body d-flex align-items-center">
|
|
<i class="bi bi-people-fill display-4 text-primary me-3"></i>
|
|
<div>
|
|
<h5 class="card-title">Total Customers</h5>
|
|
<p class="card-text fs-2 fw-bold"><?= htmlspecialchars($total_customers) ?></p>
|
|
<a href="customers.php" class="btn btn-outline-primary btn-sm mt-2">View All</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card h-100 shadow-sm">
|
|
<div class="card-body d-flex align-items-center">
|
|
<i class="bi bi-calendar-check-fill display-4 text-success me-3"></i>
|
|
<div>
|
|
<h5 class="card-title">Total Bookings</h5>
|
|
<p class="card-text fs-2 fw-bold"><?= htmlspecialchars($total_bookings) ?></p>
|
|
<a href="bookings.php" class="btn btn-outline-success btn-sm mt-2">View All</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card h-100 shadow-sm">
|
|
<div class="card-body d-flex align-items-center">
|
|
<i class="bi bi-cash-stack display-4 text-info me-3"></i>
|
|
<div>
|
|
<h5 class="card-title">Completed Revenue</h5>
|
|
<p class="card-text fs-2 fw-bold">$<?= number_format($total_revenue, 2) ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- API Key Management -->
|
|
<div class="card mt-4 shadow-sm">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<h5 class="m-0"><i class="bi bi-key-fill me-2"></i>API Keys</h5>
|
|
<form method="POST" action="">
|
|
<button type="submit" name="generate_api_key" class="btn btn-primary btn-sm">Generate New Key</button>
|
|
</form>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>API Key</th>
|
|
<th>Status</th>
|
|
<th>Created On</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($api_keys as $key): ?>
|
|
<tr>
|
|
<td><input type="text" readonly class="form-control-plaintext" value="<?= htmlspecialchars($key['api_key']) ?>"></td>
|
|
<td><span class="badge bg-<?= $key['is_active'] ? 'success' : 'danger' ?>"><?= $key['is_active'] ? 'Active' : 'Inactive' ?></span></td>
|
|
<td><?= htmlspecialchars(date("M d, Y", strtotime($key['created_at']))) ?></td>
|
|
<td><!-- Action buttons here --></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($api_keys)): ?>
|
|
<tr>
|
|
<td colspan="4" class="text-center text-muted">No API keys found.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Recent Bookings Table -->
|
|
<div class="card mt-4 shadow-sm">
|
|
<div class="card-header">
|
|
<h5 class="m-0"><i class="bi bi-clock-history me-2"></i>Recent Bookings</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Customer</th>
|
|
<th>Service</th>
|
|
<th>Urgency</th>
|
|
<th>Status</th>
|
|
<th class="text-end">Est. Revenue</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($recent_bookings as $booking): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars(date("M d, Y", strtotime($booking['appointment_date']))) ?></td>
|
|
<td><?= htmlspecialchars($booking['customer_name']) ?></td>
|
|
<td><?= htmlspecialchars($booking['service_type']) ?></td>
|
|
<td><span class="badge bg-<?= strtolower(htmlspecialchars($booking['urgency_level'])) == 'emergency' ? 'danger' : (strtolower(htmlspecialchars($booking['urgency_level'])) == 'urgent' ? 'warning' : 'secondary') ?>"><?= htmlspecialchars(ucfirst($booking['urgency_level'])) ?></span></td>
|
|
<td><span class="badge bg-light text-dark border"><?= htmlspecialchars(ucfirst($booking['status'])) ?></span></td>
|
|
<td class="text-end fw-bold">$<?= number_format($booking['estimated_revenue'], 2) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($recent_bookings)): ?>
|
|
<tr>
|
|
<td colspan="6" class="text-center text-muted">No recent bookings found.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
|
|
<footer class="container-fluid text-center text-muted py-3 mt-4">
|
|
<small>Powered by Flatlogic</small>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|