'error', 'message' => '只允许POST请求']); exit; } // 检查必要的参数 if (!isset($_POST['booking_id']) || !isset($_POST['action'])) { echo json_encode(['status' => 'error', 'message' => '缺少必要参数']); exit; } $booking_id = $_POST['booking_id']; $action = $_POST['action']; $response = ['status' => 'error', 'message' => '未知操作']; try { // 验证预约ID是否存在 $stmt = $pdo->prepare("SELECT id FROM bookings WHERE id = ?"); $stmt->execute([$booking_id]); if ($stmt->rowCount() === 0) { $response['message'] = '预约不存在'; echo json_encode($response); exit; } if (in_array($action, ['已确认', '已完成', '已取消'])) { // 更新预约状态 $stmt = $pdo->prepare("UPDATE bookings SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt->execute([$action, $booking_id]); $response = ['status' => 'success', 'message' => '预约状态更新成功!']; } elseif (in_array($action, ['已付款', '未付款'])) { // 更新付款状态 $stmt = $pdo->prepare("UPDATE bookings SET payment_status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt->execute([$action, $booking_id]); $response = ['status' => 'success', 'message' => '付款状态更新成功!']; } elseif ($action == 'update_time' && isset($_POST['new_start_time']) && isset($_POST['new_end_time'])) { // 更新预约时间 $new_start_time = $_POST['new_start_time']; $new_end_time = $_POST['new_end_time']; // 验证时间有效性 $start_dt = new DateTime($new_start_time); $end_dt = new DateTime($new_end_time); $now = new DateTime(); if ($start_dt < $now) { $response['message'] = '开始时间不能早于当前时间'; echo json_encode($response); exit; } if ($end_dt <= $start_dt) { $response['message'] = '结束时间必须晚于开始时间'; echo json_encode($response); exit; } // 计算新的持续时间(分钟) $duration = $start_dt->diff($end_dt)->i + ($start_dt->diff($end_dt)->h * 60); // 更新预约时间和持续时间 $stmt = $pdo->prepare("UPDATE bookings SET start_time = ?, end_time = ?, duration = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); $stmt->execute([$new_start_time, $new_end_time, $duration, $booking_id]); $response = ['status' => 'success', 'message' => '预约时间更新成功!']; } } catch (Exception $e) { $response['message'] = '操作失败:' . $e->getMessage(); } echo json_encode($response); ?>