226 lines
10 KiB
PHP
226 lines
10 KiB
PHP
|
|
<?php
|
|
require_once 'db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Timeframe for stats
|
|
$today_start = date('Y-m-d 00:00:00');
|
|
$this_week_start = date('Y-m-d 00:00:00', strtotime('monday this week'));
|
|
$this_month_start = date('Y-m-01 00:00:00');
|
|
|
|
// Total chats
|
|
$stmt_chats_today = $pdo->prepare("SELECT COUNT(*) FROM chat_logs WHERE created_at >= ?");
|
|
$stmt_chats_today->execute([$today_start]);
|
|
$chats_today = $stmt_chats_today->fetchColumn();
|
|
|
|
$stmt_chats_week = $pdo->prepare("SELECT COUNT(*) FROM chat_logs WHERE created_at >= ?");
|
|
$stmt_chats_week->execute([$this_week_start]);
|
|
$chats_this_week = $stmt_chats_week->fetchColumn();
|
|
|
|
$stmt_chats_month = $pdo->prepare("SELECT COUNT(*) FROM chat_logs WHERE created_at >= ?");
|
|
$stmt_chats_month->execute([$this_month_start]);
|
|
$chats_this_month = $stmt_chats_month->fetchColumn();
|
|
|
|
// Conversion Rate
|
|
$stmt_total_chats = $pdo->query("SELECT COUNT(*) FROM chat_logs");
|
|
$total_chats = $stmt_total_chats->fetchColumn();
|
|
$stmt_converted_chats = $pdo->query("SELECT COUNT(*) FROM chat_logs WHERE was_converted = 1");
|
|
$converted_chats = $stmt_converted_chats->fetchColumn();
|
|
$conversion_rate = ($total_chats > 0) ? ($converted_chats / $total_chats) * 100 : 0;
|
|
|
|
// Average Duration
|
|
$stmt_avg_duration = $pdo->query("SELECT AVG(chat_duration_seconds) FROM chat_logs");
|
|
$avg_duration_seconds = $stmt_avg_duration->fetchColumn();
|
|
$avg_duration = ($avg_duration_seconds) ? gmdate("i:s", $avg_duration_seconds) : 'N/A';
|
|
|
|
// Chat Outcomes (for Pie Chart)
|
|
$stmt_outcomes = $pdo->query("SELECT chat_outcome, COUNT(*) as count FROM chat_logs GROUP BY chat_outcome");
|
|
$chat_outcomes = $stmt_outcomes->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
// Recent Chats
|
|
$stmt_recent_chats = $pdo->query("SELECT * FROM chat_logs ORDER BY created_at DESC LIMIT 10");
|
|
$recent_chats = $stmt_recent_chats->fetchAll();
|
|
|
|
} catch (PDOException $e) {
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
|
|
$project_name = "HVAC Command Center";
|
|
$page_title = "Chat Logs";
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($page_title) ?> | <?= htmlspecialchars($project_name) ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
<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">
|
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="sidebar">
|
|
<div class="sidebar-header">
|
|
<h2 class="h5"><i class="fas fa-tachometer-alt me-2"></i><?= htmlspecialchars($project_name) ?></h2>
|
|
</div>
|
|
<ul class="nav flex-column">
|
|
<li class="nav-item"><a class="nav-link" href="index.php"><i class="fas fa-home"></i>Dashboard</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="customers.php"><i class="fas fa-users"></i>Customers</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="bookings.php"><i class="fas fa-calendar-check"></i>Bookings</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="ai-call-logs.php"><i class="fas fa-robot"></i>AI Call Logs</a></li>
|
|
<li class="nav-item"><a class="nav-link active" href="chat-logs.php"><i class="fas fa-comments"></i>Chat Logs</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="call-tracking.php"><i class="fas fa-phone-alt"></i>Call Tracking</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="reviews.php"><i class="fas fa-star"></i>Reviews</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="calendar.php"><i class="fas fa-calendar-alt"></i>Calendar</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="main-content">
|
|
<header class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1><i class="fas fa-comments me-2"></i><?= htmlspecialchars($page_title) ?></h1>
|
|
</header>
|
|
|
|
<?php if (isset($error)): ?>
|
|
<div class="alert alert-danger">
|
|
<i class="fas fa-exclamation-triangle me-2"></i> <?= htmlspecialchars($error) ?>
|
|
</div>
|
|
<?php else: ?>
|
|
<!-- Stat Cards -->
|
|
<div class="row mb-4">
|
|
<div class="col-md-3">
|
|
<div class="card stat-card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Chats Today</h5>
|
|
<p class="card-text display-4"><?= $chats_today ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card stat-card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">This Week</h5>
|
|
<p class="card-text display-4"><?= $chats_this_week ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card stat-card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Conversion Rate</h5>
|
|
<p class="card-text display-4"><?= number_format($conversion_rate, 1) ?>%</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card stat-card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Avg. Duration</h5>
|
|
<p class="card-text display-4"><?= $avg_duration ?></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<!-- Chat Outcomes Pie Chart -->
|
|
<div class="col-md-4">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5>Chat Outcomes</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<canvas id="chatOutcomesChart"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Recent Chats Table -->
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5>Recent Chats</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Customer</th>
|
|
<th>Duration</th>
|
|
<th>Outcome</th>
|
|
<th>Converted</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($recent_chats)): ?>
|
|
<tr><td colspan="5" class="text-center">No chats found.</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach ($recent_chats as $chat): ?>
|
|
<tr>
|
|
<td><?= date('M d, Y H:i', strtotime($chat['created_at'])) ?></td>
|
|
<td><?= htmlspecialchars($chat['customer_name']) ?></td>
|
|
<td><?= gmdate("i:s", $chat['chat_duration_seconds']) ?></td>
|
|
<td><span class="badge bg-secondary"><?= htmlspecialchars($chat['chat_outcome']) ?></span></td>
|
|
<td>
|
|
<?php if ($chat['was_converted']): ?>
|
|
<span class="badge bg-success">Yes</span>
|
|
<?php else: ?>
|
|
<span class="badge bg-danger">No</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
// Chart.js: Chat Outcomes Pie Chart
|
|
const outcomeLabels = <?= json_encode(array_keys($chat_outcomes)) ?>;
|
|
const outcomeData = <?= json_encode(array_values($chat_outcomes)) ?>;
|
|
|
|
const outcomesChartCtx = document.getElementById('chatOutcomesChart').getContext('2d');
|
|
new Chart(outcomesChartCtx, {
|
|
type: 'pie',
|
|
data: {
|
|
labels: outcomeLabels,
|
|
datasets: [{
|
|
label: 'Chat Outcomes',
|
|
data: outcomeData,
|
|
backgroundColor: ['#4dc9ff', '#f672a7', '#ffc107', '#28a745', '#dc3545', '#6c757d'],
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
plugins: {
|
|
legend: {
|
|
position: 'bottom',
|
|
labels: {
|
|
color: '#fff'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|