<?php
// Payment Failed Callback Page
// This page is called when PhonePe payment fails or is cancelled

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'] ?? '';
$error = $_GET['error'] ?? 'Payment was cancelled or failed';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Failed - AstroSidhi</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 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;
        }
        .error-icon {
            font-size: 80px;
            color: #e74c3c;
            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, #3498db, #2980b9);
            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, #2980b9, #3498db);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
        }
        .retry-button {
            background: linear-gradient(45deg, #e74c3c, #c0392b);
        }
        .retry-button:hover {
            background: linear-gradient(45deg, #c0392b, #e74c3c);
            box-shadow: 0 5px 15px rgba(231, 76, 60, 0.3);
        }
        .countdown {
            font-size: 14px;
            color: #6c757d;
            margin-top: 20px;
        }
        .error-message {
            background: #fdf2f2;
            border: 1px solid #fecaca;
            color: #991b1b;
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="error-icon">❌</div>
        <h1>Payment Failed</h1>
        <div class="message">
            Your payment could not be processed. Please try again or contact support if the problem persists.
        </div>
        
        <div class="error-message">
            <strong>Error:</strong> <?php echo htmlspecialchars($error); ?>
        </div>
        
        <?php if ($transactionId || $orderId): ?>
        <div class="details">
            <h3 style="margin-top: 0; color: #2c3e50;">Transaction 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: #e74c3c; font-weight: 600;">FAILED</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 close this window and return to the app to try the payment again.
        </div>
        
        <div class="countdown" id="countdown">
            This window will close automatically in <span id="timer">15</span> seconds.
        </div>
    </div>

    <script>
        // Auto-close window after 15 seconds
        let timeLeft = 15;
        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_FAILED',
                transactionId: '<?php echo $transactionId; ?>',
                orderId: '<?php echo $orderId; ?>',
                error: '<?php echo addslashes($error); ?>'
            }, '*');
        }
    </script>
</body>
</html>