<?php
// Payment Success Callback Page
// This page is called when PhonePe payment is successful

header('Content-Type: text/html; charset=UTF-8');

// Include database configuration
require_once '../config/database.php';

// Initialize database
define('INITIALIZE_DB', true);
initializeDatabase();

// Get database connection
$pdo = getDBConnection();

// Get payment details from URL parameters
$transactionId = $_GET['transactionId'] ?? '';
$orderId = $_GET['orderId'] ?? '';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Successful - AstroSidhi</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container {
            max-width: 500px;
            background: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            text-align: center;
        }
        .success-icon {
            font-size: 80px;
            color: #27ae60;
            margin-bottom: 20px;
        }
        h1 {
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 28px;
        }
        .message {
            color: #7f8c8d;
            font-size: 16px;
            line-height: 1.6;
            margin-bottom: 30px;
        }
        .details {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin: 20px 0;
            text-align: left;
        }
        .detail-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            padding: 5px 0;
            border-bottom: 1px solid #e9ecef;
        }
        .detail-row:last-child {
            border-bottom: none;
        }
        .detail-label {
            font-weight: 600;
            color: #495057;
        }
        .detail-value {
            color: #6c757d;
        }
        .button {
            background: linear-gradient(45deg, #27ae60, #2ecc71);
            color: white;
            padding: 15px 30px;
            border: none;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            margin: 10px;
            transition: all 0.3s ease;
        }
        .button:hover {
            background: linear-gradient(45deg, #2ecc71, #27ae60);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(46, 204, 113, 0.3);
        }
        .countdown {
            font-size: 14px;
            color: #6c757d;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="success-icon">✅</div>
        <h1>Payment Successful!</h1>
        <div class="message">
            Your payment has been processed successfully. Your appointment has been confirmed.
        </div>
        
        <?php if ($transactionId || $orderId): ?>
        <div class="details">
            <h3 style="margin-top: 0; color: #2c3e50;">Payment Details</h3>
            <?php if ($transactionId): ?>
            <div class="detail-row">
                <span class="detail-label">Transaction ID:</span>
                <span class="detail-value"><?php echo htmlspecialchars($transactionId); ?></span>
            </div>
            <?php endif; ?>
            <?php if ($orderId): ?>
            <div class="detail-row">
                <span class="detail-label">Order ID:</span>
                <span class="detail-value"><?php echo htmlspecialchars($orderId); ?></span>
            </div>
            <?php endif; ?>
            <div class="detail-row">
                <span class="detail-label">Status:</span>
                <span class="detail-value" style="color: #27ae60; font-weight: 600;">SUCCESS</span>
            </div>
            <div class="detail-row">
                <span class="detail-label">Date:</span>
                <span class="detail-value"><?php echo date('d M Y, h:i A'); ?></span>
            </div>
        </div>
        <?php endif; ?>
        
        <div class="message">
            You can now close this window and return to the app. Your appointment details will be updated automatically.
        </div>
        
        <div class="countdown" id="countdown">
            This window will close automatically in <span id="timer">10</span> seconds.
        </div>
    </div>

    <script>
        // Auto-close window after 10 seconds
        let timeLeft = 10;
        const timerElement = document.getElementById('timer');
        
        const countdown = setInterval(() => {
            timeLeft--;
            timerElement.textContent = timeLeft;
            
            if (timeLeft <= 0) {
                clearInterval(countdown);
                // Try to close the window
                window.close();
                
                // If window.close() doesn't work, show message
                setTimeout(() => {
                    document.getElementById('countdown').innerHTML = 
                        'You can now close this window manually.';
                }, 1000);
            }
        }, 1000);
        
        // Also try to notify the parent app (if opened from app)
        if (window.opener) {
            window.opener.postMessage({
                type: 'PAYMENT_SUCCESS',
                transactionId: '<?php echo $transactionId; ?>',
                orderId: '<?php echo $orderId; ?>'
            }, '*');
        }
    </script>
</body>
</html>