<?php
// Check if user is logged in and is admin using the new function
require_once '../includes/require_admin.php';

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

// Get statistics
$total_products = count(getAllProducts($pdo));
$total_users_stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
$total_users = $total_users_stmt->fetch(PDO::FETCH_ASSOC)['count'];

$total_orders_stmt = $pdo->query("SELECT COUNT(*) as count FROM orders");
$total_orders = $total_orders_stmt->fetch(PDO::FETCH_ASSOC)['count'];

$total_revenue_stmt = $pdo->query("SELECT SUM(total_amount) as total FROM orders");
$total_revenue = $total_revenue_stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

// Get recent tickets
$recent_tickets = [];
try {
    // Include ticket functions
    require_once '../includes/ticket_functions.php';
    $recent_tickets = getRecentTickets($pdo, 5); // Get 5 most recent tickets
} catch (Exception $e) {
    error_log("Error fetching recent tickets: " . $e->getMessage());
    $recent_tickets = [];
}
?>

<div class="container">
    <div class="admin-header">
        <h2>لوحة تحكم الإدارة</h2>
        <a href="<?php echo SITE_URL; ?>/admin/add_product.php" class="btn btn-primary">إضافة منتج جديد</a>
    </div>
    
    <div class="dashboard-stats">
        <div class="stat-card">
            <h3>إجمالي المنتجات</h3>
            <div class="stat-value"><?php echo $total_products; ?></div>
        </div>
        
        <div class="stat-card">
            <h3>إجمالي المستخدمين</h3>
            <div class="stat-value"><?php echo $total_users; ?></div>
        </div>
        
        <div class="stat-card">
            <h3>إجمالي الطلبات</h3>
            <div class="stat-value"><?php echo $total_orders; ?></div>
        </div>
        
        <div class="stat-card">
            <h3>إجمالي الإيرادات</h3>
            <div class="stat-value">$<?php echo number_format($total_revenue, 2); ?></div>
        </div>
    </div>
    
    <div class="admin-nav">
        <ul>
            <li><a href="<?php echo SITE_URL; ?>/admin/index.php" class="active">لوحة التحكم</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/products.php">المنتجات</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/manage_users.php">المستخدمون</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/orders.php">الطلبات</a></li>
            <li><a href="<?php echo SITE_URL; ?>/tickets/admin_dashboard.php">تحليل التذاكر</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/marketing.php">التسويق</a></li>
        </ul>
    </div>
    
    <!-- Recent Tickets Section -->
    <div class="section-title">
        <h2>التذاكر الحديثة</h2>
    </div>
    
    <?php if (empty($recent_tickets)): ?>
        <div class="message info">
            <p>لا توجد تذاكر.</p>
        </div>
    <?php else: ?>
        <div class="tickets-table-container">
            <table class="tickets-table">
                <thead>
                    <tr>
                        <th>رقم التذكرة</th>
                        <th>الموضوع</th>
                        <th>العميل</th>
                        <th>النوع</th>
                        <th>الحالة</th>
                        <th>الأولوية</th>
                        <th>تاريخ الإنشاء</th>
                        <th>الإجراءات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent_tickets as $ticket): ?>
                        <tr>
                            <td>#<?php echo htmlspecialchars($ticket['id']); ?></td>
                            <td><?php echo htmlspecialchars($ticket['subject']); ?></td>
                            <td><?php echo htmlspecialchars($ticket['customer_name'] ?? 'غير متوفر'); ?></td>
                            <td><?php echo htmlspecialchars($ticket['type_name'] ?? 'غير متوفر'); ?></td>
                            <td>
                                <span class="status-badge status-<?php echo strtolower(htmlspecialchars($ticket['status'])); ?>">
                                    <?php echo htmlspecialchars($ticket['status']); ?>
                                </span>
                            </td>
                            <td>
                                <span class="priority-badge priority-<?php echo strtolower(htmlspecialchars($ticket['priority'])); ?>">
                                    <?php echo htmlspecialchars($ticket['priority']); ?>
                                </span>
                            </td>
                            <td><?php echo date('j M Y', strtotime($ticket['created_at'])); ?></td>
                            <td>
                                <a href="<?php echo SITE_URL; ?>/tickets/admin_view.php?id=<?php echo $ticket['id']; ?>" class="btn btn-outline btn-small">عرض</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
    
    <div class="section-title">
        <h2>المنتجات الحديثة</h2>
    </div>
    
    <div class="products-grid">
        <?php 
        $recent_products = array_slice(getAllProducts($pdo), 0, 4);
        foreach ($recent_products as $product): ?>
            <div class="product-card" data-product-id="<?php echo $product['id']; ?>">
                <div class="product-image">
                    <?php if (!empty($product['image'])): ?>
                        <img src="<?php echo SITE_URL . '/' . htmlspecialchars($product['image']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                    <?php else: ?>
                        <i class="fas fa-image" style="font-size: 3rem; color: #ccc;"></i>
                    <?php endif; ?>
                </div>
                <div class="product-info">
                    <h3><?php echo htmlspecialchars($product['name']); ?></h3>
                    <div class="product-price">$<?php echo number_format($product['price'], 2); ?></div>
                    <a href="<?php echo SITE_URL; ?>/admin/edit_product.php?id=<?php echo $product['id']; ?>" class="btn btn-outline btn-block">تعديل</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<style>
.tickets-table-container {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    margin-bottom: 30px;
}

.tickets-table {
    width: 100%;
    border-collapse: collapse;
}

.tickets-table th,
.tickets-table td {
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid #eee;
}

.tickets-table th {
    background-color: #f8f9fa;
    font-weight: 600;
    color: #2c3e50;
}

.tickets-table tbody tr:hover {
    background-color: #f8f9fa;
}

.status-badge {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 500;
    text-transform: uppercase;
}

.status-new {
    background-color: #fff3cd;
    color: #856404;
}

.status-in-review {
    background-color: #cce5ff;
    color: #004085;
}

.status-resolved {
    background-color: #d4edda;
    color: #155724;
}

.status-closed {
    background-color: #e2e3e5;
    color: #383d41;
}

.priority-badge {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 500;
    text-transform: uppercase;
}

.priority-low {
    background-color: #d1ecf1;
    color: #0c5460;
}

.priority-medium {
    background-color: #fff3cd;
    color: #856404;
}

.priority-high {
    background-color: #f8d7da;
    color: #721c24;
}

.priority-urgent {
    background-color: #c2185b;
    color: #fff;
}

.btn-small {
    padding: 4px 8px;
    font-size: 0.85rem;
}
</style>

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