<?php
require_once '../includes/header.php';

// Check if user is logged in
if (!isLoggedIn()) {
    $_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
    redirect(SITE_URL . '/auth/login.php');
}

require_once '../includes/ticket_functions.php';

// Get user tickets with pagination
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

// Get ticket count for pagination
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM tickets WHERE user_id = ?");
$stmt->execute([$_SESSION['user_id']]);
$total_tickets = $stmt->fetch(PDO::FETCH_ASSOC)['count'];
$total_pages = ceil($total_tickets / $limit);

// Get user tickets with limit
$sql = "SELECT t.*, tt.name as type_name FROM tickets t LEFT JOIN ticket_types tt ON t.type_id = tt.id WHERE t.user_id = ? ORDER BY t.created_at DESC LIMIT " . (int)$offset . ", " . (int)$limit;
$stmt = $pdo->prepare($sql);
$stmt->execute([$_SESSION['user_id']]);
$tickets = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container">
    <div class="section-title">
        <h2>تذاكري الدعم</h2>
    </div>
    
    <?php if (empty($tickets)): ?>
        <div class="empty-products">
            <i class="fas fa-ticket-alt"></i>
            <h3>لم يتم العثور على تذاكر</h3>
            <p>لم تقم بإنشاء أي تذاكر دعم حتى الآن.</p>
            <a href="<?php echo SITE_URL; ?>/tickets/create.php" class="btn btn-primary">إنشاء تذكرة جديدة</a>
        </div>
    <?php else: ?>
        <div class="admin-nav">
            <ul>
                <li><a href="<?php echo SITE_URL; ?>/tickets/create.php" class="btn btn-primary">إنشاء تذكرة جديدة</a></li>
            </ul>
        </div>
        
        <div class="products-grid">
            <?php foreach ($tickets as $ticket): ?>
                <div class="product-card">
                    <div class="product-info">
                        <h3>التذكرة #<?php echo $ticket['id']; ?></h3>
                        <div class="product-category"><?php echo htmlspecialchars($ticket['type_name']); ?></div>
                        <p><strong>الموضوع:</strong> <?php echo htmlspecialchars($ticket['subject']); ?></p>
                        <p><strong>الحالة:</strong> 
                            <span class="status-<?php echo strtolower(str_replace(' ', '-', $ticket['status'])); ?>">
                                <?php echo $ticket['status']; ?>
                            </span>
                        </p>
                        <p><strong>تاريخ الإنشاء:</strong> <?php echo date('j M Y g:i A', strtotime($ticket['created_at'])); ?></p>
                        <a href="<?php echo SITE_URL; ?>/tickets/view.php?id=<?php echo $ticket['id']; ?>" class="btn btn-outline btn-block">عرض التفاصيل</a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <ul>
                <?php if ($page > 1): ?>
                    <li><a href="<?php echo SITE_URL; ?>/tickets/my_tickets.php?page=<?php echo $page - 1; ?>" class="btn btn-outline">السابق</a></li>
                <?php endif; ?>
                
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <li>
                        <?php if ($i == $page): ?>
                            <span class="btn btn-primary"><?php echo $i; ?></span>
                        <?php else: ?>
                            <a href="<?php echo SITE_URL; ?>/tickets/my_tickets.php?page=<?php echo $i; ?>" class="btn btn-outline"><?php echo $i; ?></a>
                        <?php endif; ?>
                    </li>
                <?php endfor; ?>
                
                <?php if ($page < $total_pages): ?>
                    <li><a href="<?php echo SITE_URL; ?>/tickets/my_tickets.php?page=<?php echo $page + 1; ?>" class="btn btn-outline">التالي</a></li>
                <?php endif; ?>
            </ul>
        </div>
        <?php endif; ?>
    <?php endif; ?>
</div>

<style>
.status-new { color: #3498db; font-weight: bold; }
.status-in-review { color: #f39c12; font-weight: bold; }
.status-resolved { color: #2ecc71; font-weight: bold; }
.status-closed { color: #95a5a6; font-weight: bold; }

.pagination {
    display: flex;
    justify-content: center;
    margin-top: 30px;
}

.pagination ul {
    display: flex;
    list-style: none;
    gap: 10px;
}

.pagination li {
    margin: 0;
}

.pagination a,
.pagination span {
    padding: 8px 12px;
    text-decoration: none;
    border-radius: 4px;
}
</style>

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