<?php
// success.php
require 'config/db.php';


// 1. Safety Check: If no transaction ID, redirect to home
if (!isset($_GET['tid'])) {
    header("Location: index.php");
    exit;
}

$tid = htmlspecialchars($_GET['tid']);
$item_name = isset($_GET['item']) ? htmlspecialchars($_GET['item']) : "Digital Asset";
$total_price = isset($_GET['total']) ? htmlspecialchars($_GET['total']) : "0.00";
// Optional: If you pass the seller ID in the URL, the "Contact Seller" button becomes dynamic
$seller_id = isset($_GET['seller']) ? (int)$_GET['seller'] : 0;

include 'includes/header.php';
?>

<style>
    .success-container { max-width: 1200px; margin: 0 auto; padding: 60px 20px; font-family: 'Inter', sans-serif; }
    .success-card {
        max-width: 550px;
        margin: 0 auto;
        background: var(--nav-bg);
        border: 1px solid var(--border);
        border-radius: 24px;
        padding: 50px 40px;
        text-align: center;
        box-shadow: 0 20px 50px rgba(0,0,0,0.6);
        position: relative;
        overflow: hidden;
    }
    /* Subtle glow effect in the background */
    .success-card::before {
        content: '';
        position: absolute;
        top: -50%; left: -50%;
        width: 200%; height: 200%;
        background: radial-gradient(circle, rgba(0, 255, 136, 0.03) 0%, transparent 70%);
        pointer-events: none;
    }
    .check-icon {
        width: 90px;
        height: 90px;
        background: var(--primary);
        color: black;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 45px;
        margin: 0 auto 30px;
        box-shadow: 0 0 30px rgba(0, 255, 136, 0.4);
    }
    .tid-badge {
        background: #0d1117;
        border: 1px solid var(--border);
        padding: 20px;
        border-radius: 16px;
        margin: 30px 0;
        cursor: pointer;
        transition: 0.2s;
    }
    .tid-badge:hover { border-color: var(--primary); background: rgba(0, 255, 136, 0.02); }
    .tid-number {
        display: block;
        font-size: 22px;
        color: var(--primary);
        font-family: 'Syncopate', sans-serif;
        font-weight: 900;
        letter-spacing: 1px;
        margin-top: 8px;
    }
    .warning-box {
        font-size: 12px;
        color: #ff4d4d;
        background: rgba(255, 77, 77, 0.05);
        border: 1px solid rgba(255, 77, 77, 0.2);
        padding: 15px;
        border-radius: 12px;
        text-align: left;
        line-height: 1.5;
    }
    .btn-action {
        display: block;
        width: 100%;
        background: var(--primary);
        color: black;
        text-decoration: none;
        padding: 16px;
        border-radius: 12px;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 1px;
        transition: 0.3s;
        border: none;
        cursor: pointer;
    }
    .btn-action:hover {
        transform: translateY(-3px);
        box-shadow: 0 10px 20px rgba(0, 255, 136, 0.2);
    }
</style>

<div class="success-container">
    <div class="success-card">
        <div class="check-icon">
            <i class="fa-solid fa-shield-check"></i>
        </div>
        
        <h2 style="margin: 0; color: white; font-family: 'Syncopate', sans-serif; font-size: 22px;">TRANSACTION SECURED</h2>
        <p style="color: var(--text-gray); margin-top: 15px; font-size: 14px;">
            Vault clearance granted. Your purchase of <br>
            <strong style="color: white;"><?php echo $item_name; ?></strong> is complete.
        </p>

        <div class="tid-badge" onclick="copyTid()" title="Click to copy">
            <span style="font-size: 10px; color: var(--text-gray); text-transform: uppercase; font-weight: 800; letter-spacing: 2px;">Receipt Signature</span>
            <span class="tid-number" id="tid-text"><?php echo $tid; ?></span>
            <span id="copy-status" style="font-size: 9px; color: var(--primary); display: none; margin-top: 5px;">COPIED TO CLIPBOARD</span>
        </div>

        <div class="warning-box">
            <i class="fa-solid fa-triangle-exclamation" style="margin-right: 8px;"></i> 
            <strong>SECURITY NOTICE:</strong> Encrypt and store this ID. Our automated dispute protocol requires this signature to verify item non-delivery or defects.
        </div>

        <div style="margin-top: 40px; display: flex; flex-direction: column; gap: 15px;">
            <a href="messages.php?receiver_id=<?php echo $seller_id; ?>&subject=Order <?php echo $tid; ?>" class="btn-action">
                <i class="fa-solid fa-comment-dots"></i> INITIALIZE DELIVERY CHAT
            </a>
            <a href="index.php" style="color: var(--text-gray); text-decoration: none; font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: 1px;">
                Return to Mainframe
            </a>
        </div>
    </div>
</div>

<script>
function copyTid() {
    const tid = document.getElementById('tid-text').innerText;
    navigator.clipboard.writeText(tid).then(() => {
        const status = document.getElementById('copy-status');
        status.style.display = 'block';
        setTimeout(() => { status.style.display = 'none'; }, 2000);
    });
}
</script>

<?php include 'includes/footer.php'; ?>