query("SELECT COUNT(*) FROM incidents WHERE DATE(created_at) = CURDATE() AND channel = 'First Call'"); if ($stmt) $currentShiftCalls = $stmt->fetchColumn(); // Active cases (Not 'ปิดเคส' and not 'ค้างปรับปรุงข้อมูล') $stmt = $pdo->query("SELECT COUNT(*) FROM incidents WHERE status != 'ปิดเคส' AND status != 'ค้างปรับปรุงข้อมูล'"); if ($stmt) $activeCasesCount = $stmt->fetchColumn(); // Pending cases ('ค้างปรับปรุงข้อมูล') $stmt = $pdo->query("SELECT COUNT(*) FROM incidents WHERE status = 'ค้างปรับปรุงข้อมูล'"); if ($stmt) $pendingCasesCount = $stmt->fetchColumn(); // Units not ready $stmt = $pdo->query("SELECT COUNT(*) FROM units WHERE status = 'not_ready'"); if ($stmt) $notReadyUnitsCount = $stmt->fetchColumn(); } catch (Exception $e) {} // 2. Fetch Active Cases list (Left column) $activeCases = []; try { // Since we don't have unit names directly in incidents in our mock, we will mock the unit name if empty, or join if unit table has it. // Our mock data didn't have unit names in `incidents`, so we'll just show it using assigned_unit_id or a fallback. $query = " SELECT i.id, i.case_number, i.call_type, i.status, i.diagnosis, i.created_at, u.name as unit_name FROM incidents i LEFT JOIN units u ON i.assigned_unit_id = u.id WHERE i.status != 'ปิดเคส' ORDER BY i.created_at DESC "; $stmt = $pdo->query($query); if ($stmt) $activeCases = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) {} // 3. Fetch Pending Cases Details (Right column) $pendingCases = []; try { // Join with users to get the name of the dispatcher who created it, since we don't have a 'responsible_dispatcher' column explicitly in our mock. We'll use created_by or completed_by. $query = " SELECT i.case_number, i.created_at, i.status, u.name as responsible_name FROM incidents i LEFT JOIN users u ON i.created_by = u.id WHERE i.status = 'ค้างปรับปรุงข้อมูล' ORDER BY i.created_at ASC "; $stmt = $pdo->query($query); if ($stmt) $pendingCases = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) {} // Function to calculate time diff function getPendingTime($datetime) { $now = new DateTime(); $past = new DateTime($datetime); $diff = $now->diff($past); $parts = []; if ($diff->d > 0) $parts[] = $diff->d . ' วัน'; if ($diff->h > 0) $parts[] = $diff->h . ' ชม.'; if ($diff->i > 0) $parts[] = $diff->i . ' นาที'; return empty($parts) ? 'เพิ่งเกิดขึ้น' : implode(' ', $parts); } ?>