<?php
require_once __DIR__ . '/includes/auth.php';
requireLogin();

if (!isset($_GET['booking_id'])) {
    header('Location: dashboard.php');
    exit();
}

$booking_id = (int) $_GET['booking_id'];
$conn = getDBConnection();
$stmt = $conn->prepare("
    SELECT b.*, s.name as service_name, p.transaction_id, p.amount as paid_amount
    FROM bookings b
    JOIN services s ON b.service_id = s.id
    LEFT JOIN payments p ON b.id = p.booking_id
    WHERE b.id = ? AND b.user_id = ?
");
$stmt->bind_param('ii', $booking_id, $_SESSION['user_id']);
$stmt->execute();
$result = $stmt->get_result();
$booking = $result->fetch_assoc();
$stmt->close();
$conn->close();

if (!$booking) {
    header('Location: dashboard.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Payment Successful - Oldage Home</title>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'>
    <link href='https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap' rel='stylesheet'>
    <link rel='stylesheet' href='css/style.css'>
</head>
<body>
    <?php include 'includes/header.php'; ?>

    <section class='py-5'>
        <div class='container'>
            <div class='row justify-content-center'>
                <div class='col-lg-8'>
                    <div class='card shadow border-success'>
                        <div class='card-body p-5 text-center'>
                            <div class='mb-4'><i class='fas fa-check-circle text-success' style='font-size: 4rem;'></i></div>
                            <h2 class='text-success mb-3'>Payment Successful!</h2>
                            <p class='lead mb-4'>Your booking has been confirmed and payment processed successfully.</p>

                            <div class='card mb-4'>
                                <div class='card-header bg-success text-white'><h5 class='mb-0'>Booking Confirmation</h5></div>
                                <div class='card-body text-start'>
                                    <div class='row'>
                                        <div class='col-md-6'>
                                            <p><strong>Booking ID:</strong> #<?php echo str_pad($booking['id'], 6, '0', STR_PAD_LEFT); ?></p>
                                            <p><strong>Service:</strong> <?php echo htmlspecialchars($booking['service_name']); ?></p>
                                            <p><strong>Date:</strong> <?php echo date('F j, Y', strtotime($booking['booking_date'])); ?></p>
                                            <p><strong>Time:</strong> <?php echo date('g:i A', strtotime($booking['booking_time'])); ?></p>
                                        </div>
                                        <div class='col-md-6'>
                                            <p><strong>Status:</strong> <span class='badge bg-success'>Confirmed</span></p>
                                            <p><strong>Amount Paid:</strong> $<?php echo number_format((float) ($booking['paid_amount'] ?? 0), 2); ?></p>
                                            <?php if ($booking['transaction_id']): ?>
                                            <p><strong>Transaction ID:</strong> <?php echo htmlspecialchars($booking['transaction_id']); ?></p>
                                            <?php endif; ?>
                                            <?php if ($booking['notes']): ?>
                                            <p><strong>Notes:</strong> <?php echo htmlspecialchars($booking['notes']); ?></p>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class='alert alert-info'>
                                <h6><i class='fas fa-envelope me-2'></i>What's Next?</h6>
                                <p class='mb-0'>You will receive a confirmation email shortly with all booking details. Our team will contact you if we need any additional information.</p>
                            </div>

                            <div class='d-flex justify-content-center gap-3'>
                                <a href='dashboard.php' class='btn btn-primary'><i class='fas fa-tachometer-alt me-2'></i>View My Bookings</a>
                                <a href='book-services.php' class='btn btn-outline-primary'><i class='fas fa-plus me-2'></i>Book Another Service</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <?php include 'includes/footer.php'; ?>
    <script src='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js'></script>
    <script src='js/main.js'></script>
</body>
</html>

