<?php
/**
 * SMM Paket Satış Scripti - Sipariş Sorgulama
 * @version 1.1.0
 */

$pageTitle = 'Sipariş Sorgula';
require_once __DIR__ . '/header.php';

$order = null;
$searchQuery = '';
$error = '';

// Sipariş sorgula
if (isset($_GET['no']) || isset($_POST['order_number'])) {
    $searchQuery = trim($_GET['no'] ?? $_POST['order_number'] ?? '');
    
    if (!empty($searchQuery)) {
        try {
            $stmt = $db->prepare("
                SELECT o.*, p.name as package_name, p.quantity as package_quantity,
                       c.name as category_name, c.icon as category_icon, c.color as category_color
                FROM orders o
                JOIN packages p ON o.package_id = p.id
                JOIN categories c ON p.category_id = c.id
                WHERE o.order_number = ? OR o.customer_email = ?
                ORDER BY o.created_at DESC
                LIMIT 1
            ");
            $stmt->execute([$searchQuery, $searchQuery]);
            $order = $stmt->fetch();
            
            if (!$order) {
                $error = 'Sipariş bulunamadı. Lütfen sipariş numaranızı veya e-posta adresinizi kontrol edin.';
            }
        } catch (PDOException $e) {
            $error = 'Bir hata oluştu. Lütfen tekrar deneyin.';
        }
    }
}

// Durum renkleri ve metinleri
$statusConfig = [
    'pending' => ['color' => '#f59e0b', 'bg' => 'rgba(245, 158, 11, 0.1)', 'text' => 'Ödeme Bekleniyor', 'icon' => 'fas fa-clock'],
    'paid' => ['color' => '#3b82f6', 'bg' => 'rgba(59, 130, 246, 0.1)', 'text' => 'Ödeme Onaylandı', 'icon' => 'fas fa-check-circle'],
    'processing' => ['color' => '#8b5cf6', 'bg' => 'rgba(139, 92, 246, 0.1)', 'text' => 'İşleniyor', 'icon' => 'fas fa-spinner'],
    'completed' => ['color' => '#10b981', 'bg' => 'rgba(16, 185, 129, 0.1)', 'text' => 'Tamamlandı', 'icon' => 'fas fa-check-double'],
    'cancelled' => ['color' => '#ef4444', 'bg' => 'rgba(239, 68, 68, 0.1)', 'text' => 'İptal Edildi', 'icon' => 'fas fa-times-circle'],
    'refunded' => ['color' => '#6b7280', 'bg' => 'rgba(107, 114, 128, 0.1)', 'text' => 'İade Edildi', 'icon' => 'fas fa-undo'],
    'partial' => ['color' => '#f97316', 'bg' => 'rgba(249, 115, 22, 0.1)', 'text' => 'Kısmi Tamamlandı', 'icon' => 'fas fa-adjust']
];

// QR Kod URL
$qrCodeUrl = '';
if ($order) {
    $qrLink = SITE_URL . '/siparis-sorgula.php?no=' . urlencode($order['order_number']);
    $qrCodeUrl = 'https://api.qrserver.com/v1/create-qr-code/?size=120x120&data=' . urlencode($qrLink);
}
?>

<style>
.query-page {
    padding: 60px 0 100px;
}
.query-container {
    max-width: 700px;
    margin: 0 auto;
}

/* Search Box */
.search-box {
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 20px;
    padding: 40px;
    margin-bottom: 40px;
}
.search-title {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 10px;
    text-align: center;
}
.search-desc {
    text-align: center;
    opacity: 0.7;
    margin-bottom: 30px;
}
.search-form {
    display: flex;
    gap: 15px;
}
.search-input {
    flex: 1;
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    padding: 16px 20px;
    color: #fff;
    font-size: 16px;
}
.search-input:focus {
    outline: none;
    border-color: var(--primary);
}
.search-input::placeholder {
    color: rgba(255,255,255,0.4);
}
.search-btn {
    background: var(--gradient);
    border: none;
    color: #fff;
    padding: 16px 30px;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    white-space: nowrap;
}
.search-btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

@media (max-width: 576px) {
    .search-form {
        flex-direction: column;
    }
    .search-btn {
        width: 100%;
    }
}

/* Error */
.error-message {
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    color: #ef4444;
}
.error-message i {
    font-size: 24px;
    margin-bottom: 10px;
    display: block;
}

/* Order Result */
.order-result {
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 20px;
    overflow: hidden;
}
.order-header {
    padding: 30px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}
.order-header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 15px;
}
.order-number-label {
    font-size: 14px;
    opacity: 0.6;
}
.order-number-text {
    font-size: 22px;
    font-weight: 800;
    font-family: monospace;
    letter-spacing: 1px;
}
.order-status {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 50px;
    font-weight: 600;
    font-size: 14px;
}

.order-package {
    display: flex;
    align-items: center;
    gap: 15px;
}
.order-package-icon {
    width: 55px;
    height: 55px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: #fff;
}
.order-package-info h4 {
    font-size: 18px;
    font-weight: 600;
    margin: 0 0 4px;
}
.order-package-info span {
    font-size: 14px;
    opacity: 0.7;
}

.order-body {
    padding: 30px;
}
.order-details-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 25px;
}
@media (max-width: 576px) {
    .order-details-grid {
        grid-template-columns: 1fr;
    }
}
.order-detail-item {
    background: rgba(255,255,255,0.02);
    border-radius: 12px;
    padding: 18px;
}
.order-detail-label {
    font-size: 13px;
    opacity: 0.6;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.order-detail-value {
    font-size: 16px;
    font-weight: 500;
    word-break: break-all;
}

.order-timeline {
    margin-top: 30px;
    padding-top: 30px;
    border-top: 1px solid rgba(255,255,255,0.08);
}
.timeline-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 20px;
}
.timeline-list {
    position: relative;
    padding-left: 30px;
}
.timeline-list::before {
    content: '';
    position: absolute;
    left: 8px;
    top: 5px;
    bottom: 5px;
    width: 2px;
    background: rgba(255,255,255,0.1);
}
.timeline-item {
    position: relative;
    padding-bottom: 20px;
}
.timeline-item:last-child {
    padding-bottom: 0;
}
.timeline-dot {
    position: absolute;
    left: -26px;
    top: 4px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    border: 2px solid var(--bg);
}
.timeline-item.active .timeline-dot {
    background: #10b981;
}
.timeline-item.current .timeline-dot {
    background: var(--primary);
    box-shadow: 0 0 0 4px rgba(var(--primary-rgb), 0.2);
}
.timeline-content {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}
.timeline-text {
    font-size: 14px;
}
.timeline-date {
    font-size: 12px;
    opacity: 0.5;
}

/* QR Section */
.order-qr-section {
    margin-top: 30px;
    padding-top: 30px;
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    flex-wrap: wrap;
}
.qr-info {
    flex: 1;
}
.qr-info h5 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.qr-info h5 i {
    color: var(--primary);
}
.qr-info p {
    font-size: 13px;
    opacity: 0.6;
    margin: 0;
}
.qr-code-box {
    background: #fff;
    padding: 10px;
    border-radius: 12px;
}
.qr-code-box img {
    display: block;
    width: 80px;
    height: 80px;
}

.order-footer {
    padding: 25px 30px;
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}
.order-total {
    font-size: 14px;
    opacity: 0.7;
}
.order-total strong {
    font-size: 24px;
    font-weight: 800;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.order-help {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    opacity: 0.7;
}
.order-help a {
    color: var(--primary);
}

/* Action Buttons */
.order-actions {
    padding: 20px 30px;
    background: rgba(var(--primary-rgb), 0.05);
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}
.btn-order-action {
    flex: 1;
    min-width: 150px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 20px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 14px;
    text-decoration: none;
    transition: all 0.3s;
    border: none;
    cursor: pointer;
}
.btn-invoice {
    background: var(--gradient);
    color: #fff;
}
.btn-invoice:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
    color: #fff;
}
.btn-print {
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.1);
    color: #fff;
}
.btn-print:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
}
.btn-copy {
    background: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.2);
    color: #10b981;
}
.btn-copy:hover {
    background: #10b981;
    color: #fff;
}

@media (max-width: 576px) {
    .order-actions {
        flex-direction: column;
    }
    .btn-order-action {
        width: 100%;
    }
}
</style>

<section class="query-page">
    <div class="container">
        <div class="query-container">
            <!-- Search Box -->
            <div class="search-box" data-aos="fade-up">
                <h1 class="search-title">Sipariş Sorgula</h1>
                <p class="search-desc">Sipariş numaranız veya e-posta adresiniz ile siparişinizi sorgulayın</p>
                
                <form method="GET" class="search-form">
                    <input type="text" name="no" class="search-input" placeholder="Sipariş numarası veya e-posta" value="<?php echo clean($searchQuery); ?>" required>
                    <button type="submit" class="search-btn">
                        <i class="fas fa-search me-2"></i>Sorgula
                    </button>
                </form>
            </div>
            
            <?php if ($error): ?>
            <div class="error-message" data-aos="fade-up">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo clean($error); ?>
            </div>
            <?php endif; ?>
            
            <?php if ($order): ?>
            <?php $status = $statusConfig[$order['status']] ?? $statusConfig['pending']; ?>
            <div class="order-result" data-aos="fade-up">
                <div class="order-header">
                    <div class="order-header-top">
                        <div>
                            <div class="order-number-label">Sipariş Numarası</div>
                            <div class="order-number-text"><?php echo clean($order['order_number']); ?></div>
                        </div>
                        <div class="order-status" style="background: <?php echo $status['bg']; ?>; color: <?php echo $status['color']; ?>">
                            <i class="<?php echo $status['icon']; ?>"></i>
                            <?php echo $status['text']; ?>
                        </div>
                    </div>
                    
                    <div class="order-package">
                        <div class="order-package-icon" style="background: <?php echo $order['category_color']; ?>">
                            <i class="<?php echo $order['category_icon']; ?>"></i>
                        </div>
                        <div class="order-package-info">
                            <h4><?php echo clean($order['package_name']); ?></h4>
                            <span><?php echo clean($order['category_name']); ?></span>
                        </div>
                    </div>
                </div>
                
                <div class="order-body">
                    <div class="order-details-grid">
                        <div class="order-detail-item">
                            <div class="order-detail-label"><i class="fas fa-user"></i> Müşteri</div>
                            <div class="order-detail-value"><?php echo clean($order['customer_name']); ?></div>
                        </div>
                        <div class="order-detail-item">
                            <div class="order-detail-label"><i class="fas fa-envelope"></i> E-posta</div>
                            <div class="order-detail-value"><?php echo clean($order['customer_email']); ?></div>
                        </div>
                        <div class="order-detail-item">
                            <div class="order-detail-label"><i class="fas fa-link"></i> Sosyal Medya</div>
                            <div class="order-detail-value"><?php echo clean($order['social_media_link']); ?></div>
                        </div>
                        <div class="order-detail-item">
                            <div class="order-detail-label"><i class="fas fa-calendar"></i> Sipariş Tarihi</div>
                            <div class="order-detail-value"><?php echo formatDate($order['created_at']); ?></div>
                        </div>
                        <?php if ($order['discount_amount'] > 0): ?>
                        <div class="order-detail-item">
                            <div class="order-detail-label"><i class="fas fa-tag"></i> İndirim</div>
                            <div class="order-detail-value" style="color: #10b981;">
                                -<?php echo formatMoney($order['discount_amount']); ?>
                                <?php if ($order['coupon_code']): ?>
                                <small style="opacity: 0.7;">(<?php echo clean($order['coupon_code']); ?>)</small>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endif; ?>
                        <?php if ($order['payment_method']): ?>
                        <div class="order-detail-item">
                            <div class="order-detail-label"><i class="fas fa-credit-card"></i> Ödeme Yöntemi</div>
                            <div class="order-detail-value">
                                <?php 
                                $paymentLabels = [
                                    'bank_transfer' => 'Havale/EFT',
                                    'credit_card' => 'Kredi Kartı',
                                    'papara' => 'Papara',
                                    'crypto' => 'Kripto Para'
                                ];
                                echo $paymentLabels[$order['payment_method']] ?? $order['payment_method'];
                                ?>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                    
                    <?php if ($order['admin_note']): ?>
                    <div class="order-detail-item" style="margin-top: 25px;">
                        <div class="order-detail-label"><i class="fas fa-comment"></i> Admin Notu</div>
                        <div class="order-detail-value"><?php echo clean($order['admin_note']); ?></div>
                    </div>
                    <?php endif; ?>
                    
                    <!-- Timeline -->
                    <div class="order-timeline">
                        <h4 class="timeline-title">Sipariş Durumu</h4>
                        <div class="timeline-list">
                            <div class="timeline-item active">
                                <div class="timeline-dot"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text">Sipariş Oluşturuldu</span>
                                    <span class="timeline-date"><?php echo formatDate($order['created_at']); ?></span>
                                </div>
                            </div>
                            
                            <?php if (in_array($order['status'], ['paid', 'processing', 'completed', 'partial'])): ?>
                            <div class="timeline-item active">
                                <div class="timeline-dot"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text">Ödeme Onaylandı</span>
                                    <span class="timeline-date"><?php echo $order['paid_at'] ? formatDate($order['paid_at']) : '-'; ?></span>
                                </div>
                            </div>
                            <?php endif; ?>
                            
                            <?php if (in_array($order['status'], ['processing', 'completed', 'partial'])): ?>
                            <div class="timeline-item <?php echo $order['status'] === 'processing' ? 'current' : 'active'; ?>">
                                <div class="timeline-dot"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text">İşleniyor</span>
                                    <span class="timeline-date"><?php echo $order['api_sent_at'] ? formatDate($order['api_sent_at']) : '-'; ?></span>
                                </div>
                            </div>
                            <?php endif; ?>
                            
                            <?php if ($order['status'] === 'completed'): ?>
                            <div class="timeline-item active">
                                <div class="timeline-dot"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text">Tamamlandı</span>
                                    <span class="timeline-date"><?php echo $order['completed_at'] ? formatDate($order['completed_at']) : '-'; ?></span>
                                </div>
                            </div>
                            <?php endif; ?>
                            
                            <?php if ($order['status'] === 'partial'): ?>
                            <div class="timeline-item current">
                                <div class="timeline-dot" style="background: #f97316;"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text" style="color: #f97316;">Kısmi Tamamlandı</span>
                                    <span class="timeline-date"><?php echo $order['completed_at'] ? formatDate($order['completed_at']) : '-'; ?></span>
                                </div>
                            </div>
                            <?php endif; ?>
                            
                            <?php if ($order['status'] === 'pending'): ?>
                            <div class="timeline-item current">
                                <div class="timeline-dot"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text">Ödeme Bekleniyor</span>
                                    <span class="timeline-date">-</span>
                                </div>
                            </div>
                            <?php endif; ?>
                            
                            <?php if ($order['status'] === 'cancelled'): ?>
                            <div class="timeline-item current">
                                <div class="timeline-dot" style="background: #ef4444;"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text" style="color: #ef4444;">İptal Edildi</span>
                                    <span class="timeline-date">-</span>
                                </div>
                            </div>
                            <?php endif; ?>
                            
                            <?php if ($order['status'] === 'refunded'): ?>
                            <div class="timeline-item current">
                                <div class="timeline-dot" style="background: #6b7280;"></div>
                                <div class="timeline-content">
                                    <span class="timeline-text" style="color: #6b7280;">İade Edildi</span>
                                    <span class="timeline-date">-</span>
                                </div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- QR Code Section -->
                    <div class="order-qr-section">
                        <div class="qr-info">
                            <h5><i class="fas fa-qrcode"></i> Sipariş Takip QR Kodu</h5>
                            <p>Bu QR kodu taratarak sipariş durumunuzu her zaman kontrol edebilirsiniz.</p>
                        </div>
                        <div class="qr-code-box">
                            <img src="<?php echo $qrCodeUrl; ?>" alt="Sipariş QR Kodu">
                        </div>
                    </div>
                </div>
                
                <!-- Action Buttons -->
                <div class="order-actions">
                    <a href="<?php echo SITE_URL; ?>/fatura.php?no=<?php echo urlencode($order['order_number']); ?>" class="btn-order-action btn-invoice" target="_blank">
                        <i class="fas fa-file-invoice"></i>
                        Fatura / Makbuz
                    </a>
                    <button type="button" class="btn-order-action btn-copy" onclick="copyOrderNumber()">
                        <i class="fas fa-copy"></i>
                        Sipariş No Kopyala
                    </button>
                    <a href="<?php echo SITE_URL; ?>/destek.php" class="btn-order-action btn-print">
                        <i class="fas fa-headset"></i>
                        Destek Al
                    </a>
                </div>
                
                <div class="order-footer">
                    <div class="order-total">
                        Toplam Tutar: <strong><?php echo formatMoney($order['amount']); ?></strong>
                    </div>
                    <div class="order-help">
                        <i class="fas fa-question-circle"></i>
                        Yardım mı lazım? <a href="<?php echo SITE_URL; ?>/iletisim.php">Bize ulaşın</a>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<script>
function copyOrderNumber() {
    var orderNumber = '<?php echo $order ? $order['order_number'] : ''; ?>';
    
    if (navigator.clipboard) {
        navigator.clipboard.writeText(orderNumber).then(function() {
            showToast('Sipariş numarası kopyalandı!', 'success');
        });
    } else {
        var input = document.createElement('input');
        input.value = orderNumber;
        document.body.appendChild(input);
        input.select();
        document.execCommand('copy');
        document.body.removeChild(input);
        showToast('Sipariş numarası kopyalandı!', 'success');
    }
}

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:10px;font-size:14px;font-weight:600;z-index:9999;box-shadow:0 4px 15px rgba(0,0,0,0.2);';
    toast.innerHTML = '<i class="fas fa-check-circle" style="margin-right:8px;"></i>' + message;
    document.body.appendChild(toast);
    
    setTimeout(function() {
        toast.style.opacity = '0';
        toast.style.transition = 'opacity 0.3s';
        setTimeout(function() {
            document.body.removeChild(toast);
        }, 300);
    }, 2500);
}
</script>

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