<?php
/**
 * SMM Paket Satış Scripti - Siparişlerim
 * @version 1.0.0
 */

require_once __DIR__ . '/config.php';

// Giriş yapmamışsa yönlendir
if (!isLoggedIn()) {
    setFlash('error', 'Bu sayfayı görüntülemek için giriş yapmalısınız.');
    redirect(SITE_URL . '/giris.php');
    exit;
}

// Sayfalama
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;

// Filtreler
$statusFilter = isset($_GET['status']) && in_array($_GET['status'], ['pending', 'paid', 'processing', 'completed', 'cancelled', 'refunded']) ? $_GET['status'] : '';
$searchQuery = isset($_GET['q']) ? trim($_GET['q']) : '';

// SQL koşulları
$whereConditions = ["o.user_id = ?"];
$params = [$_SESSION['user_id']];

if ($statusFilter) {
    $whereConditions[] = "o.status = ?";
    $params[] = $statusFilter;
}

if ($searchQuery) {
    $whereConditions[] = "(o.order_number LIKE ? OR p.name LIKE ? OR o.social_media_link LIKE ?)";
    $searchParam = "%{$searchQuery}%";
    $params[] = $searchParam;
    $params[] = $searchParam;
    $params[] = $searchParam;
}

$whereSQL = implode(' AND ', $whereConditions);

// Toplam sipariş sayısı
try {
    $stmt = $db->prepare("
        SELECT COUNT(*) 
        FROM orders o
        JOIN packages p ON o.package_id = p.id
        WHERE {$whereSQL}
    ");
    $stmt->execute($params);
    $totalOrders = $stmt->fetchColumn();
    $totalPages = ceil($totalOrders / $perPage);
} catch (PDOException $e) {
    $totalOrders = 0;
    $totalPages = 1;
}

// Siparişleri çek
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 {$whereSQL}
        ORDER BY o.created_at DESC
        LIMIT {$perPage} OFFSET {$offset}
    ");
    $stmt->execute($params);
    $orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $orders = [];
}

// Durum sayıları
try {
    $stmt = $db->prepare("
        SELECT status, COUNT(*) as count
        FROM orders
        WHERE user_id = ?
        GROUP BY status
    ");
    $stmt->execute([$_SESSION['user_id']]);
    $statusCounts = [];
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $statusCounts[$row['status']] = $row['count'];
    }
} catch (PDOException $e) {
    $statusCounts = [];
}

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

// Durum bilgileri
function getStatusInfo($status) {
    $statuses = [
        'pending' => ['text' => 'Beklemede', 'color' => '#f59e0b', 'icon' => 'fas fa-clock', 'bg' => 'rgba(245, 158, 11, 0.15)'],
        'paid' => ['text' => 'Ödendi', 'color' => '#3b82f6', 'icon' => 'fas fa-credit-card', 'bg' => 'rgba(59, 130, 246, 0.15)'],
        'processing' => ['text' => 'İşleniyor', 'color' => '#8b5cf6', 'icon' => 'fas fa-spinner', 'bg' => 'rgba(139, 92, 246, 0.15)'],
        'completed' => ['text' => 'Tamamlandı', 'color' => '#10b981', 'icon' => 'fas fa-check-circle', 'bg' => 'rgba(16, 185, 129, 0.15)'],
        'cancelled' => ['text' => 'İptal Edildi', 'color' => '#ef4444', 'icon' => 'fas fa-times-circle', 'bg' => 'rgba(239, 68, 68, 0.15)'],
        'refunded' => ['text' => 'İade Edildi', 'color' => '#6b7280', 'icon' => 'fas fa-undo', 'bg' => 'rgba(107, 114, 128, 0.15)']
    ];
    return $statuses[$status] ?? ['text' => $status, 'color' => '#64748b', 'icon' => 'fas fa-question', 'bg' => 'rgba(100, 116, 139, 0.15)'];
}
?>

<style>
.orders-section {
    padding: 40px 0 80px;
    min-height: calc(100vh - 80px);
}

.orders-header {
    margin-bottom: 30px;
}

.orders-header h1 {
    font-size: 32px;
    font-weight: 800;
    margin-bottom: 10px;
    color: #fff;
}

.orders-header p {
    color: rgba(255, 255, 255, 0.6);
    font-size: 16px;
    margin: 0;
}

/* Status Tabs */
.status-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 25px;
    flex-wrap: wrap;
}

.status-tab {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    background: rgba(30, 41, 59, 0.6);
    border: 2px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s;
}

.status-tab:hover {
    border-color: rgba(var(--primary-rgb), 0.3);
    color: #fff;
}

.status-tab.active {
    background: rgba(var(--primary-rgb), 0.15);
    border-color: var(--primary);
    color: var(--primary);
}

.status-tab .count {
    background: rgba(255, 255, 255, 0.1);
    padding: 2px 8px;
    border-radius: 20px;
    font-size: 12px;
}

.status-tab.active .count {
    background: var(--primary);
    color: #fff;
}

/* Search & Filter */
.filter-bar {
    display: flex;
    gap: 15px;
    margin-bottom: 25px;
    flex-wrap: wrap;
}

.search-box {
    flex: 1;
    min-width: 250px;
    position: relative;
}

.search-box input {
    width: 100%;
    padding: 14px 20px 14px 50px;
    background: rgba(30, 41, 59, 0.6);
    border: 2px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    color: #fff;
    font-size: 15px;
    transition: all 0.3s;
}

.search-box input:focus {
    outline: none;
    border-color: var(--primary);
    background: rgba(30, 41, 59, 0.8);
}

.search-box input::placeholder {
    color: rgba(255, 255, 255, 0.4);
}

.search-box i {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    color: rgba(255, 255, 255, 0.4);
}

.search-box button {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: var(--gradient);
    border: none;
    color: #fff;
    padding: 10px 20px;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.search-box button:hover {
    opacity: 0.9;
}

/* Orders List */
.orders-list {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.order-card {
    background: rgba(30, 41, 59, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 20px;
    overflow: hidden;
    transition: all 0.3s;
}

.order-card:hover {
    border-color: rgba(var(--primary-rgb), 0.3);
    transform: translateY(-2px);
}

.order-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 25px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    flex-wrap: wrap;
    gap: 15px;
}

.order-info {
    display: flex;
    align-items: center;
    gap: 15px;
}

.order-icon {
    width: 50px;
    height: 50px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    flex-shrink: 0;
}

.order-meta h3 {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 4px;
    color: #fff;
}

.order-meta p {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.5);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.order-meta p span {
    display: flex;
    align-items: center;
    gap: 5px;
}

.order-status {
    display: flex;
    align-items: center;
    gap: 15px;
}

.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 25px;
    font-size: 13px;
    font-weight: 600;
}

.order-amount {
    font-size: 20px;
    font-weight: 800;
    color: #10b981;
}

.order-card-body {
    padding: 20px 25px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
}

.order-detail {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.order-detail label {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.4);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.order-detail span {
    font-size: 14px;
    color: #fff;
    font-weight: 500;
    word-break: break-all;
}

.order-detail span.link {
    color: var(--primary);
}

.order-card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 25px;
    background: rgba(0, 0, 0, 0.2);
    flex-wrap: wrap;
    gap: 10px;
}

.order-date {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.5);
    display: flex;
    align-items: center;
    gap: 6px;
}

.order-actions {
    display: flex;
    gap: 10px;
}

.btn-order {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s;
    border: none;
    cursor: pointer;
}

.btn-order.primary {
    background: var(--gradient);
    color: #fff;
}

.btn-order.primary:hover {
    opacity: 0.9;
    transform: translateY(-2px);
}

.btn-order.secondary {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.btn-order.secondary:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* Pagination */
.pagination-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 40px;
    gap: 8px;
}

.pagination-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 45px;
    height: 45px;
    background: rgba(30, 41, 59, 0.6);
    border: 2px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    color: rgba(255, 255, 255, 0.7);
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s;
}

.pagination-btn:hover {
    border-color: rgba(var(--primary-rgb), 0.3);
    color: #fff;
}

.pagination-btn.active {
    background: var(--gradient);
    border-color: transparent;
    color: #fff;
}

.pagination-btn.disabled {
    opacity: 0.5;
    pointer-events: none;
}

.pagination-info {
    padding: 0 15px;
    font-size: 14px;
    color: rgba(255, 255, 255, 0.5);
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 80px 20px;
    background: rgba(30, 41, 59, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 24px;
}

.empty-state i {
    font-size: 70px;
    color: rgba(255, 255, 255, 0.1);
    margin-bottom: 25px;
}

.empty-state h3 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 10px;
    color: #fff;
}

.empty-state p {
    font-size: 15px;
    color: rgba(255, 255, 255, 0.5);
    margin-bottom: 25px;
}

.empty-state a {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: var(--gradient);
    color: #fff;
    padding: 14px 30px;
    border-radius: 12px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s;
}

.empty-state a:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 30px rgba(var(--primary-rgb), 0.3);
}

/* Order Detail Modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    z-index: 9999;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-backdrop.show {
    display: flex;
}

.modal-content {
    background: #1e293b;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 24px;
    width: 100%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 25px 30px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.modal-header h3 {
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 12px;
}

.modal-header h3 i {
    color: var(--primary);
}

.modal-close {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.3s;
}

.modal-close:hover {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
}

.modal-body {
    padding: 30px;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 15px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

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

.detail-row label {
    color: rgba(255, 255, 255, 0.5);
    font-size: 14px;
}

.detail-row span {
    color: #fff;
    font-weight: 600;
    font-size: 14px;
    text-align: right;
    word-break: break-all;
    max-width: 60%;
}

/* Responsive */
@media (max-width: 991px) {
    .order-card-body {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 768px) {
    .orders-header h1 {
        font-size: 26px;
    }
    
    .status-tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
        padding-bottom: 10px;
        -webkit-overflow-scrolling: touch;
    }
    
    .status-tab {
        flex-shrink: 0;
    }
    
    .order-card-header {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .order-status {
        width: 100%;
        justify-content: space-between;
    }
    
    .order-card-body {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 576px) {
    .filter-bar {
        flex-direction: column;
    }
    
    .search-box {
        min-width: 100%;
    }
    
    .order-card-footer {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .order-actions {
        width: 100%;
    }
    
    .btn-order {
        flex: 1;
        justify-content: center;
    }
}
</style>

<section class="orders-section">
    <div class="container">
        <!-- Header -->
        <div class="orders-header" data-aos="fade-up">
            <h1><i class="fas fa-shopping-bag me-3" style="color: var(--primary);"></i>Siparişlerim</h1>
            <p>Tüm siparişlerinizi buradan takip edebilirsiniz</p>
        </div>
        
        <!-- Status Tabs -->
        <div class="status-tabs" data-aos="fade-up" data-aos-delay="100">
            <a href="<?php echo SITE_URL; ?>/siparislerim.php" class="status-tab <?php echo !$statusFilter ? 'active' : ''; ?>">
                <i class="fas fa-list"></i> Tümü
                <span class="count"><?php echo $totalOrders; ?></span>
            </a>
            <a href="<?php echo SITE_URL; ?>/siparislerim.php?status=pending" class="status-tab <?php echo $statusFilter === 'pending' ? 'active' : ''; ?>">
                <i class="fas fa-clock"></i> Beklemede
                <span class="count"><?php echo $statusCounts['pending'] ?? 0; ?></span>
            </a>
            <a href="<?php echo SITE_URL; ?>/siparislerim.php?status=processing" class="status-tab <?php echo $statusFilter === 'processing' ? 'active' : ''; ?>">
                <i class="fas fa-spinner"></i> İşleniyor
                <span class="count"><?php echo $statusCounts['processing'] ?? 0; ?></span>
            </a>
            <a href="<?php echo SITE_URL; ?>/siparislerim.php?status=completed" class="status-tab <?php echo $statusFilter === 'completed' ? 'active' : ''; ?>">
                <i class="fas fa-check-circle"></i> Tamamlandı
                <span class="count"><?php echo $statusCounts['completed'] ?? 0; ?></span>
            </a>
            <a href="<?php echo SITE_URL; ?>/siparislerim.php?status=cancelled" class="status-tab <?php echo $statusFilter === 'cancelled' ? 'active' : ''; ?>">
                <i class="fas fa-times-circle"></i> İptal
                <span class="count"><?php echo $statusCounts['cancelled'] ?? 0; ?></span>
            </a>
        </div>
        
        <!-- Search -->
        <form method="GET" action="" class="filter-bar" data-aos="fade-up" data-aos-delay="150">
            <?php if ($statusFilter): ?>
            <input type="hidden" name="status" value="<?php echo $statusFilter; ?>">
            <?php endif; ?>
            <div class="search-box">
                <i class="fas fa-search"></i>
                <input type="text" name="q" placeholder="Sipariş no, paket adı veya link ara..." 
                       value="<?php echo clean($searchQuery); ?>">
                <button type="submit"><i class="fas fa-search"></i></button>
            </div>
        </form>
        
        <!-- Orders List -->
        <?php if (empty($orders)): ?>
        <div class="empty-state" data-aos="fade-up">
            <i class="fas fa-shopping-cart"></i>
            <h3>Sipariş bulunamadı</h3>
            <p>
                <?php if ($searchQuery || $statusFilter): ?>
                    Arama kriterlerinize uygun sipariş bulunamadı.
                <?php else: ?>
                    Henüz hiç sipariş vermediniz. Hemen ilk siparişinizi verin!
                <?php endif; ?>
            </p>
            <?php if ($searchQuery || $statusFilter): ?>
            <a href="<?php echo SITE_URL; ?>/siparislerim.php">
                <i class="fas fa-arrow-left"></i> Tüm Siparişleri Göster
            </a>
            <?php else: ?>
            <a href="<?php echo SITE_URL; ?>/#packages">
                <i class="fas fa-box"></i> Paketleri İncele
            </a>
            <?php endif; ?>
        </div>
        <?php else: ?>
        <div class="orders-list">
            <?php foreach ($orders as $index => $order): 
                $status = getStatusInfo($order['status']);
            ?>
            <div class="order-card" data-aos="fade-up" data-aos-delay="<?php echo 100 + ($index * 50); ?>">
                <div class="order-card-header">
                    <div class="order-info">
                        <div class="order-icon" style="background: <?php echo $order['category_color']; ?>20; color: <?php echo $order['category_color']; ?>">
                            <i class="<?php echo $order['category_icon']; ?>"></i>
                        </div>
                        <div class="order-meta">
                            <h3><?php echo clean($order['package_name']); ?></h3>
                            <p>
                                <span><i class="fas fa-hashtag"></i> <?php echo clean($order['order_number']); ?></span>
                                <span><i class="fas fa-cubes"></i> <?php echo number_format($order['package_quantity']); ?> Adet</span>
                            </p>
                        </div>
                    </div>
                    <div class="order-status">
                        <span class="status-badge" style="background: <?php echo $status['bg']; ?>; color: <?php echo $status['color']; ?>">
                            <i class="<?php echo $status['icon']; ?>"></i> <?php echo $status['text']; ?>
                        </span>
                        <span class="order-amount"><?php echo number_format($order['amount'], 2, ',', '.'); ?>₺</span>
                    </div>
                </div>
                
                <div class="order-card-body">
                    <div class="order-detail">
                        <label>Hedef Link</label>
                        <span class="link"><?php echo clean(mb_strlen($order['social_media_link'], 'UTF-8') > 40 ? mb_substr($order['social_media_link'], 0, 40, 'UTF-8') . '...' : $order['social_media_link']); ?></span>
                    </div>
                    <div class="order-detail">
                        <label>Ödeme Yöntemi</label>
                        <span><?php echo $order['payment_method'] === 'bank_transfer' ? 'Havale/EFT' : clean($order['payment_method']); ?></span>
                    </div>
                    <div class="order-detail">
                        <label>API Durumu</label>
                        <span><?php echo $order['api_status'] ?: '-'; ?></span>
                    </div>
                    <div class="order-detail">
                        <label>Kalan</label>
                        <span><?php echo $order['api_remains'] !== null ? number_format($order['api_remains']) : '-'; ?></span>
                    </div>
                </div>
                
                <div class="order-card-footer">
                    <span class="order-date">
                        <i class="fas fa-calendar-alt"></i>
                        <?php echo date('d.m.Y H:i', strtotime($order['created_at'])); ?>
                    </span>
                    <div class="order-actions">
                        <button class="btn-order secondary" onclick="showOrderDetail(<?php echo htmlspecialchars(json_encode($order), ENT_QUOTES, 'UTF-8'); ?>)">
                            <i class="fas fa-eye"></i> Detay
                        </button>
                        <?php if ($order['status'] === 'pending'): ?>
                        <a href="<?php echo SITE_URL; ?>/odeme.php?order=<?php echo $order['order_number']; ?>" class="btn-order primary">
                            <i class="fas fa-credit-card"></i> Ödeme Yap
                        </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
        <div class="pagination-wrapper" data-aos="fade-up">
            <a href="?page=<?php echo max(1, $page - 1); ?><?php echo $statusFilter ? '&status=' . $statusFilter : ''; ?><?php echo $searchQuery ? '&q=' . urlencode($searchQuery) : ''; ?>" 
               class="pagination-btn <?php echo $page <= 1 ? 'disabled' : ''; ?>">
                <i class="fas fa-chevron-left"></i>
            </a>
            
            <?php
            $startPage = max(1, $page - 2);
            $endPage = min($totalPages, $page + 2);
            
            if ($startPage > 1): ?>
            <a href="?page=1<?php echo $statusFilter ? '&status=' . $statusFilter : ''; ?><?php echo $searchQuery ? '&q=' . urlencode($searchQuery) : ''; ?>" class="pagination-btn">1</a>
            <?php if ($startPage > 2): ?>
            <span class="pagination-info">...</span>
            <?php endif; ?>
            <?php endif; ?>
            
            <?php for ($i = $startPage; $i <= $endPage; $i++): ?>
            <a href="?page=<?php echo $i; ?><?php echo $statusFilter ? '&status=' . $statusFilter : ''; ?><?php echo $searchQuery ? '&q=' . urlencode($searchQuery) : ''; ?>" 
               class="pagination-btn <?php echo $i === $page ? 'active' : ''; ?>">
                <?php echo $i; ?>
            </a>
            <?php endfor; ?>
            
            <?php if ($endPage < $totalPages): ?>
            <?php if ($endPage < $totalPages - 1): ?>
            <span class="pagination-info">...</span>
            <?php endif; ?>
            <a href="?page=<?php echo $totalPages; ?><?php echo $statusFilter ? '&status=' . $statusFilter : ''; ?><?php echo $searchQuery ? '&q=' . urlencode($searchQuery) : ''; ?>" class="pagination-btn"><?php echo $totalPages; ?></a>
            <?php endif; ?>
            
            <a href="?page=<?php echo min($totalPages, $page + 1); ?><?php echo $statusFilter ? '&status=' . $statusFilter : ''; ?><?php echo $searchQuery ? '&q=' . urlencode($searchQuery) : ''; ?>" 
               class="pagination-btn <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
                <i class="fas fa-chevron-right"></i>
            </a>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</section>

<!-- Order Detail Modal -->
<div class="modal-backdrop" id="orderModal">
    <div class="modal-content">
        <div class="modal-header">
            <h3><i class="fas fa-file-invoice"></i> Sipariş Detayı</h3>
            <button class="modal-close" onclick="closeModal()">
                <i class="fas fa-times"></i>
            </button>
        </div>
        <div class="modal-body" id="orderModalBody">
            <!-- Dinamik içerik -->
        </div>
    </div>
</div>

<script>
function showOrderDetail(order) {
    var statusTexts = {
        'pending': 'Beklemede',
        'paid': 'Ödendi',
        'processing': 'İşleniyor',
        'completed': 'Tamamlandı',
        'cancelled': 'İptal Edildi',
        'refunded': 'İade Edildi'
    };
    
    var paymentMethods = {
        'bank_transfer': 'Havale/EFT',
        'credit_card': 'Kredi Kartı'
    };
    
    var html = `
        <div class="detail-row">
            <label>Sipariş No</label>
            <span>#${order.order_number}</span>
        </div>
        <div class="detail-row">
            <label>Paket</label>
            <span>${order.package_name}</span>
        </div>
        <div class="detail-row">
            <label>Durum</label>
            <span>${statusTexts[order.status] || order.status}</span>
        </div>
        <div class="detail-row">
            <label>Tutar</label>
            <span style="color: #10b981;">${parseFloat(order.amount).toLocaleString('tr-TR', {minimumFractionDigits: 2})}₺</span>
        </div>
        <div class="detail-row">
            <label>Hedef Link</label>
            <span style="color: var(--primary);">${order.social_media_link}</span>
        </div>
        <div class="detail-row">
            <label>Ödeme Yöntemi</label>
            <span>${paymentMethods[order.payment_method] || order.payment_method}</span>
        </div>
        <div class="detail-row">
            <label>Sipariş Tarihi</label>
            <span>${new Date(order.created_at).toLocaleString('tr-TR')}</span>
        </div>
    `;
    
    if (order.api_order_id) {
        html += `
            <div class="detail-row">
                <label>API Sipariş ID</label>
                <span>${order.api_order_id}</span>
            </div>
        `;
    }
    
    if (order.api_status) {
        html += `
            <div class="detail-row">
                <label>API Durumu</label>
                <span>${order.api_status}</span>
            </div>
        `;
    }
    
    if (order.api_start_count !== null) {
        html += `
            <div class="detail-row">
                <label>Başlangıç Sayısı</label>
                <span>${parseInt(order.api_start_count).toLocaleString('tr-TR')}</span>
            </div>
        `;
    }
    
    if (order.api_remains !== null) {
        html += `
            <div class="detail-row">
                <label>Kalan</label>
                <span>${parseInt(order.api_remains).toLocaleString('tr-TR')}</span>
            </div>
        `;
    }
    
    if (order.customer_note) {
        html += `
            <div class="detail-row">
                <label>Notunuz</label>
                <span>${order.customer_note}</span>
            </div>
        `;
    }
    
    if (order.completed_at) {
        html += `
            <div class="detail-row">
                <label>Tamamlanma Tarihi</label>
                <span>${new Date(order.completed_at).toLocaleString('tr-TR')}</span>
            </div>
        `;
    }
    
    document.getElementById('orderModalBody').innerHTML = html;
    document.getElementById('orderModal').classList.add('show');
    document.body.style.overflow = 'hidden';
}

function closeModal() {
    document.getElementById('orderModal').classList.remove('show');
    document.body.style.overflow = '';
}

// Modal dışına tıklayınca kapat
document.getElementById('orderModal').addEventListener('click', function(e) {
    if (e.target === this) {
        closeModal();
    }
});

// ESC tuşu ile kapat
document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') {
        closeModal();
    }
});
</script>

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