0ffb4f610b
添加付款状态字段到预约表,实现状态更新API和前端交互 新增update_booking.php处理预约状态、付款状态和时间更新 改进bookings.php前端界面,添加付款状态显示和AJAX操作 添加测试页面test_update_booking.php验证功能
77 lines
2.9 KiB
PHP
77 lines
2.9 KiB
PHP
<?php
|
|
// update_booking.php - 处理预约更新功能
|
|
require_once 'db_connect.php';
|
|
|
|
// 只接受POST请求
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('HTTP/1.1 405 Method Not Allowed');
|
|
echo json_encode(['status' => '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);
|
|
?>
|