false, 'message' => '缺少必要的参数', 'total' => 0, 'pending' => 0, 'completed' => 0, 'bookings' => [] ], JSON_UNESCAPED_UNICODE); exit; } try { // 准备SQL查询 - 查询用户的预约记录(使用新的数据库结构) $sql = "SELECT b.*, p.package_name FROM bookings b LEFT JOIN packages p ON b.package_id = p.id WHERE b.phone = ? ORDER BY b.start_time DESC, b.id DESC"; $stmt = $pdo->prepare($sql); $stmt->execute([$phone]); $results = $stmt->fetchAll(); $bookings = []; $pending = 0; $completed = 0; // 处理查询结果 foreach ($results as $row) { // 格式化日期和时间 $booking_date = $row['start_time'] ? date('Y-m-d', strtotime($row['start_time'])) : ''; $time_slot = ''; if ($row['start_time'] && $row['end_time']) { $start_time = date('H:i', strtotime($row['start_time'])); $end_time = date('H:i', strtotime($row['end_time'])); $time_slot = $start_time . '-' . $end_time; } $booking = [ 'id' => $row['id'], 'booking_date' => $booking_date, 'phone' => $row['phone'], 'car_number' => $row['car_number'] ?? '', 'car_model' => $row['car_model'] ?? '', 'time_slot' => $time_slot, 'status' => $row['status'] ?? '未知', 'package_name' => $row['package_name'] ?? '', 'notes' => $row['notes'] ?? '' ]; // 统计不同状态的预约数量 if ($booking['status'] === '待确认' || $booking['status'] === '已确认' || $booking['status'] === '进行中') { $pending++; } elseif ($booking['status'] === '已完成') { $completed++; } $bookings[] = $booking; } // 计算总预约数 $total = count($bookings); // 构建响应数据 $response = [ 'success' => true, 'total' => $total, 'pending' => $pending, 'completed' => $completed, 'bookings' => $bookings ]; // 返回JSON数据 echo json_encode($response, JSON_UNESCAPED_UNICODE); } catch (Exception $e) { echo json_encode([ 'success' => false, 'message' => '查询失败:' . $e->getMessage(), 'total' => 0, 'pending' => 0, 'completed' => 0, 'bookings' => [] ], JSON_UNESCAPED_UNICODE); } ?>