<?php
/**
 * SMM Paket Satış Scripti - Sipariş Başarılı Sayfası
 * @version 1.1.0
 */

require_once __DIR__ . '/config.php';

// Session'dan sipariş bilgisi al - header'dan ÖNCE kontrol et
if (!isset($_SESSION['order_success'])) {
    redirect(SITE_URL);
    exit;
}

$orderInfo = $_SESSION['order_success'];
unset($_SESSION['order_success']);

// QR Kod URL'i oluştur (QRServer API - ücretsiz ve güvenilir)
$qrLink = SITE_URL . '/siparis-sorgula.php?no=' . urlencode($orderInfo['order_number']);
$qrCodeUrl = 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=' . urlencode($qrLink);

// Şimdi header'ı dahil et
$pageTitle = 'Sipariş Başarılı';
require_once __DIR__ . '/header.php';
?>

<style>
.success-page {
    min-height: calc(100vh - 200px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
}
.success-card {
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 24px;
    padding: 50px;
    text-align: center;
    max-width: 550px;
    width: 100%;
}
.success-icon {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #10b981, #059669);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 30px;
    font-size: 50px;
    color: #fff;
    animation: successPulse 2s ease-in-out infinite;
}
@keyframes successPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4); }
    50% { box-shadow: 0 0 0 20px rgba(16, 185, 129, 0); }
}
.success-title {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 15px;
    color: #10b981;
}
.success-desc {
    font-size: 16px;
    opacity: 0.8;
    margin-bottom: 35px;
    line-height: 1.7;
}
.order-box {
    background: rgba(var(--primary-rgb), 0.1);
    border: 1px solid rgba(var(--primary-rgb), 0.2);
    border-radius: 16px;
    padding: 25px;
    margin-bottom: 30px;
}
.order-number {
    font-size: 14px;
    opacity: 0.7;
    margin-bottom: 5px;
}
.order-number-value {
    font-size: 28px;
    font-weight: 800;
    font-family: monospace;
    color: var(--primary);
    letter-spacing: 2px;
}
.order-details {
    display: flex;
    justify-content: space-around;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid rgba(255,255,255,0.1);
}
.order-detail {
    text-align: center;
}
.order-detail-label {
    font-size: 13px;
    opacity: 0.6;
    margin-bottom: 5px;
}
.order-detail-value {
    font-size: 16px;
    font-weight: 600;
}

/* QR Kod Bölümü */
.qr-section {
    background: rgba(255,255,255,0.05);
    border: 1px dashed rgba(255,255,255,0.15);
    border-radius: 16px;
    padding: 25px;
    margin-bottom: 30px;
}
.qr-title {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--text-secondary);
}
.qr-title i {
    color: var(--primary);
}
.qr-code-wrapper {
    background: #fff;
    border-radius: 12px;
    padding: 15px;
    display: inline-block;
    margin-bottom: 15px;
}
.qr-code-wrapper img {
    display: block;
    width: 150px;
    height: 150px;
}
.qr-desc {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 15px;
}
.qr-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}
.btn-qr {
    background: rgba(var(--primary-rgb), 0.15);
    border: 1px solid rgba(var(--primary-rgb), 0.3);
    color: var(--primary);
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.btn-qr:hover {
    background: var(--primary);
    color: #fff;
}

.info-box {
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.2);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 30px;
    text-align: left;
}
.info-box-title {
    font-weight: 600;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
    color: #3b82f6;
}
.info-box ul {
    margin: 0;
    padding-left: 20px;
    font-size: 14px;
    opacity: 0.8;
}
.info-box li {
    margin-bottom: 8px;
}

.success-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
}
.btn-success-primary {
    background: var(--gradient);
    color: #fff;
    padding: 14px 30px;
    border-radius: 12px;
    font-weight: 600;
    transition: all 0.3s;
}
.btn-success-primary:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
    color: #fff;
}
.btn-success-outline {
    background: transparent;
    border: 2px solid rgba(255,255,255,0.2);
    color: #fff;
    padding: 14px 30px;
    border-radius: 12px;
    font-weight: 600;
    transition: all 0.3s;
}
.btn-success-outline:hover {
    background: rgba(255,255,255,0.1);
    border-color: rgba(255,255,255,0.3);
    color: #fff;
}

@media (max-width: 576px) {
    .success-card {
        padding: 30px 20px;
    }
    .order-number-value {
        font-size: 20px;
    }
    .order-details {
        flex-direction: column;
        gap: 15px;
    }
}
</style>

<section class="success-page">
    <div class="success-card" data-aos="zoom-in">
        <div class="success-icon">
            <i class="fas fa-check"></i>
        </div>
        
        <h1 class="success-title">Siparişiniz Alındı!</h1>
        <p class="success-desc">
            Siparişiniz başarıyla oluşturuldu. Ödemeniz onaylandıktan sonra 
            siparişiniz işleme alınacaktır.
        </p>
        
        <div class="order-box">
            <div class="order-number">Sipariş Numaranız</div>
            <div class="order-number-value"><?php echo clean($orderInfo['order_number']); ?></div>
            
            <div class="order-details">
                <div class="order-detail">
                    <div class="order-detail-label">Paket</div>
                    <div class="order-detail-value"><?php echo clean($orderInfo['package_name']); ?></div>
                </div>
                <div class="order-detail">
                    <div class="order-detail-label">Tutar</div>
                    <div class="order-detail-value"><?php echo formatMoney($orderInfo['amount']); ?></div>
                </div>
            </div>
        </div>
        
        <!-- QR Kod Bölümü -->
        <div class="qr-section">
            <div class="qr-title">
                <i class="fas fa-qrcode"></i> Sipariş Takip QR Kodu
            </div>
            <div class="qr-code-wrapper">
                <img src="<?php echo $qrCodeUrl; ?>" alt="Sipariş QR Kodu" id="qrCodeImg">
            </div>
            <p class="qr-desc">
                Bu QR kodu taratarak sipariş durumunuzu anında sorgulayabilirsiniz.
            </p>
            <div class="qr-buttons">
                <a href="<?php echo $qrCodeUrl; ?>&format=png" download="siparis-<?php echo $orderInfo['order_number']; ?>-qr.png" class="btn-qr">
                    <i class="fas fa-download"></i> QR İndir
                </a>
                <button type="button" class="btn-qr" onclick="copyOrderLink()">
                    <i class="fas fa-copy"></i> Link Kopyala
                </button>
                <button type="button" class="btn-qr" onclick="shareOrder()">
                    <i class="fas fa-share-alt"></i> Paylaş
                </button>
            </div>
        </div>
        
        <div class="info-box">
            <div class="info-box-title">
                <i class="fas fa-info-circle"></i> Sonraki Adımlar
            </div>
            <ul>
                <li>Havale/EFT yaptıysanız ödemeniz kontrol edilecektir.</li>
                <li>Ödeme onaylandığında e-posta ile bilgilendirileceksiniz.</li>
                <li>Sipariş durumunuzu QR kod ile veya aşağıdaki butonla sorgulayabilirsiniz.</li>
            </ul>
        </div>
        
        <div class="success-buttons">
            <a href="<?php echo SITE_URL; ?>/siparis-sorgula.php?no=<?php echo urlencode($orderInfo['order_number']); ?>" class="btn-success-primary">
                <i class="fas fa-search me-2"></i>Siparişi Sorgula
            </a>
            <a href="<?php echo SITE_URL; ?>" class="btn-success-outline">
                <i class="fas fa-home me-2"></i>Ana Sayfa
            </a>
        </div>
    </div>
</section>

<script>
var qrLink = '<?php echo $qrLink; ?>';
var orderNumber = '<?php echo $orderInfo['order_number']; ?>';

// Sipariş Linkini Kopyala
function copyOrderLink() {
    if (navigator.clipboard) {
        navigator.clipboard.writeText(qrLink).then(function() {
            showToast('Link kopyalandı!', 'success');
        });
    } else {
        var input = document.createElement('input');
        input.value = qrLink;
        document.body.appendChild(input);
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        showToast('Link kopyalandı!', 'success');
    }
}

// Paylaş
function shareOrder() {
    var text = 'Sipariş Takip: ' + orderNumber;
    
    if (navigator.share) {
        navigator.share({
            title: 'Sipariş Takip',
            text: text,
            url: qrLink
        });
    } else {
        var whatsappUrl = 'https://wa.me/?text=' + encodeURIComponent(text + '\n' + qrLink);
        window.open(whatsappUrl, '_blank');
    }
}

// Toast bildirimi
function showToast(message, type) {
    var toast = document.createElement('div');
    toast.style.cssText = 'position:fixed;bottom:20px;left:50%;transform:translateX(-50%);background:' + (type === 'success' ? '#10b981' : '#ef4444') + ';color:#fff;padding:12px 24px;border-radius:8px;font-size:14px;font-weight:600;z-index:9999;';
    toast.textContent = message;
    document.body.appendChild(toast);
    
    setTimeout(function() {
        toast.style.opacity = '0';
        toast.style.transition = 'opacity 0.3s';
        setTimeout(function() {
            document.body.removeChild(toast);
        }, 300);
    }, 2000);
}
</script>

<?php require_once __DIR__ . '/footer.php'; ?>