<?php
/**
 * Payment Success Page (Deep Blue User App UI - Fixed Status)
 */
require_once 'includes/db.php';

// Get payment data from request or session (Fixed parameter names for various gateways)
$invoice_no = $_REQUEST['invoice_no'] ?? $_REQUEST['invoice'] ?? $_REQUEST['order_id'] ?? ($_SESSION['checkout_invoice'] ?? null);
$transaction_id = $_REQUEST['trx_id'] ?? $_REQUEST['transaction_id'] ?? $_REQUEST['tran_id'] ?? strtoupper(uniqid('TRX-'));
$payment_method = $_REQUEST['payment_method'] ?? 'Online Payment';

$total_amount = $_REQUEST['amount'] ?? 0;
$order_status = 'SUCCESS';

if ($invoice_no) {
    // Update order status to approved (Standardized status)
    $stmt = $pdo->prepare("UPDATE orders SET status = 'approved', payment_method = ? WHERE invoice_no = ?");
    $stmt->execute([$payment_method, $invoice_no]);
    
    // Fetch order details for display
    $stmt = $pdo->prepare("SELECT SUM(amount) as total_amount FROM orders WHERE invoice_no = ?");
    $stmt->execute([$invoice_no]);
    $order_info = $stmt->fetch();
    
    if ($order_info && !empty($order_info['total_amount'])) {
        $total_amount = $order_info['total_amount'];
    }
    
    // Clear session variables
    unset($_SESSION['checkout_invoice']);
    $_SESSION['cart'] = [];
} else {
    // If session is lost (e.g., page refresh) or parameter missing, still show success on this endpoint
    $invoice_no = "N/A (Verified)";
    $order_status = "SUCCESS";
}

$page_title = 'Payment Successful';
require_once 'includes/header.php';
?>

<style>
    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&family=Noto+Serif+Bengali:wght@300;400;500;600;700&display=swap');

    :root {
        --primary: #00059E; /* Deep Blue */
        --primary-hover: #00037a;
        --bg-light: #f8fafc;
        --card-bg: #ffffff;
        --border: #e2e8f0;
        --text-main: #0f172a;
        --text-muted: #64748b;
        --success: #10b981;
        --success-light: #ecfdf5;
        --success-border: #a7f3d0;
    }

    body {
        background-color: var(--bg-light);
        font-family: 'Roboto', 'Noto Serif Bengali', sans-serif !important;
    }

    .success-wrapper { 
        margin: 60px auto 100px;
        max-width: 500px;
        padding: 0 15px;
    }
    
    .success-card { 
        background: var(--card-bg); 
        border: 1px solid var(--border); 
        border-radius: 12px; 
        padding: 40px 30px; 
        text-align: center;
        box-shadow: 0 4px 6px rgba(0,0,0,0.02); 
    }

    .success-icon-wrapper {
        width: 80px;
        height: 80px;
        background: var(--success-light);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 20px;
        border: 4px solid var(--success-border);
    }

    .success-icon-wrapper svg {
        width: 40px;
        height: 40px;
        color: var(--success);
    }

    .success-title { 
        font-size: 1.8rem; 
        font-weight: 800; 
        color: var(--text-main); 
        margin-bottom: 10px; 
    }
    
    .success-subtitle { 
        color: var(--text-muted); 
        font-size: 1rem; 
        margin-bottom: 30px; 
        line-height: 1.5;
    }

    .transaction-details {
        background: var(--bg-light);
        border: 1px solid var(--border);
        border-radius: 8px;
        padding: 20px;
        text-align: left;
        margin-bottom: 30px;
    }

    .transaction-details h3 {
        font-size: 1.1rem;
        font-weight: 700;
        color: var(--primary);
        margin-bottom: 15px;
        border-bottom: 1px dashed var(--border);
        padding-bottom: 10px;
        text-align: center;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .detail-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0;
        border-bottom: 1px solid var(--border);
        font-size: 0.95rem;
    }

    .detail-row:last-child {
        border-bottom: none;
    }

    .detail-label {
        color: var(--text-muted);
        font-weight: 600;
    }

    .detail-value {
        color: var(--text-main);
        font-weight: 700;
    }

    .detail-value.success-text {
        color: var(--success);
        text-transform: uppercase;
    }

    .btn-dashboard { 
        display: flex; 
        align-items: center; 
        justify-content: center; 
        gap: 8px; 
        width: 100%; 
        background: var(--primary); 
        color: white; 
        padding: 15px; 
        border-radius: 8px; 
        font-size: 1.05rem; 
        font-weight: 700; 
        border: none; 
        cursor: pointer; 
        transition: background 0.3s ease; 
        text-transform: uppercase; 
        letter-spacing: 0.5px; 
        text-decoration: none;
        box-sizing: border-box;
    }
    .btn-dashboard:hover { 
        background: var(--primary-hover); 
    }
</style>

<div class="container success-wrapper">
    <div class="success-card">
        
        <div class="success-icon-wrapper">
            <svg fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
                <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
            </svg>
        </div>

        <h1 class="success-title">Payment Successful!</h1>
        <p class="success-subtitle">Thank you for your purchase. Your payment has been securely processed.</p>

        <div class="transaction-details">
            <h3>Transaction Details</h3>
            
            <div class="detail-row">
                <span class="detail-label">Payment Type:</span>
                <span class="detail-value"><?php echo htmlspecialchars($payment_method); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Invoice No:</span>
                <span class="detail-value"><?php echo htmlspecialchars($invoice_no); ?></span>
            </div>

            <div class="detail-row">
                <span class="detail-label">Transaction ID:</span>
                <span class="detail-value"><?php echo htmlspecialchars($transaction_id); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Amount:</span>
                <span class="detail-value"><?php echo defined('CURRENCY_SYMBOL') ? CURRENCY_SYMBOL : '৳'; ?><?php echo number_format((float)$total_amount, 2); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Status:</span>
                <span class="detail-value success-text"><?php echo htmlspecialchars($order_status); ?></span>
            </div>
        </div>

        <a href="account" class="btn-dashboard">
            <svg width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                <path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
            </svg>
            Go to My Downloads
        </a>

    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
<?php require_once 'includes/mobile-nav.php'; ?>
