<?php
/**
 * Admin Dashboard
 * Video Portal Management System
 */

require_once '../config/database.php';
require_once '../includes/functions.php';
require_once '../includes/auth.php';

requireAdmin();

$settings = getAllSettings();
$stats = getDashboardStats();
$recentDownloads = getRecentDownloads(5);
$recentVideos = getVideos(null, null, 5);

$siteName = $settings['site_name'] ?? 'Video Portal';
$portalLogo = $settings['portal_logo'] ?? '';
$rtl = $settings['rtl_direction'] ?? '0';

$flash = getFlashMessage();
?>
<!DOCTYPE html>
<html lang="<?php echo $rtl == '1' ? 'he' : 'en'; ?>" dir="<?php echo $rtl == '1' ? 'rtl' : 'ltr'; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - <?php echo htmlspecialchars($siteName); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="../assets/css/style.css">
    <?php if (!empty($settings['primary_color'])): ?>
    <style>
        :root {
            --primary-color: <?php echo $settings['primary_color']; ?>;
            --secondary-color: <?php echo $settings['secondary_color'] ?? '#2c3e50'; ?>;
            --sidebar-color: <?php echo $settings['sidebar_color'] ?? '#1e272e'; ?>;
        }
    </style>
    <?php endif; ?>
</head>
<body class="<?php echo $rtl == '1' ? 'rtl' : ''; ?>">
    <div class="portal-wrapper">
        <!-- Sidebar -->
        <aside class="sidebar">
            <div class="sidebar-header">
                <?php if (!empty($portalLogo)): ?>
                    <img src="../assets/uploads/logos/<?php echo $portalLogo; ?>" alt="<?php echo htmlspecialchars($siteName); ?>">
                <?php else: ?>
                    <i class="fas fa-play-circle" style="font-size: 40px;"></i>
                <?php endif; ?>
                <h2>Admin Panel</h2>
            </div>
            
            <ul class="sidebar-menu">
                <li>
                    <a href="dashboard.php" class="active">
                        <i class="fas fa-tachometer-alt"></i>
                        <span>Dashboard</span>
                    </a>
                </li>
                
                <li>
                    <a href="videos.php">
                        <i class="fas fa-video"></i>
                        <span>Videos</span>
                    </a>
                </li>
                
                <li>
                    <a href="categories.php">
                        <i class="fas fa-folder"></i>
                        <span>Categories</span>
                    </a>
                </li>
                
                <li>
                    <a href="users.php">
                        <i class="fas fa-users"></i>
                        <span>Users</span>
                    </a>
                </li>
                
                <li>
                    <a href="settings.php">
                        <i class="fas fa-cog"></i>
                        <span>Settings</span>
                    </a>
                </li>
                
                <li>
                    <a href="logs.php">
                        <i class="fas fa-file-alt"></i>
                        <span>Upload Logs</span>
                    </a>
                </li>
                
                <div class="menu-divider"></div>
                
                <li>
                    <a href="../index.php">
                        <i class="fas fa-home"></i>
                        <span>Back to Portal</span>
                    </a>
                </li>
                
                <li>
                    <a href="../logout.php">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>Logout</span>
                    </a>
                </li>
            </ul>
        </aside>
        
        <!-- Main Content -->
        <main class="main-content">
            <!-- Top Bar -->
            <div class="top-bar">
                <h1><i class="fas fa-tachometer-alt"></i> Dashboard</h1>
                
                <div class="user-info">
                    <span>Admin: <?php echo htmlspecialchars($_SESSION['username']); ?></span>
                    <div class="avatar">
                        <?php echo strtoupper(substr($_SESSION['username'], 0, 1)); ?>
                    </div>
                </div>
            </div>
            
            <?php if ($flash): ?>
                <div class="alert alert-<?php echo $flash['type']; ?>">
                    <i class="fas fa-<?php echo $flash['type'] === 'success' ? 'check-circle' : 'exclamation-circle'; ?>"></i>
                    <?php echo $flash['message']; ?>
                </div>
            <?php endif; ?>
            
            <!-- Stats Grid -->
            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-icon blue">
                        <i class="fas fa-video"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($stats['total_videos']); ?></h3>
                        <p>Total Videos</p>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-icon green">
                        <i class="fas fa-users"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($stats['total_users']); ?></h3>
                        <p>Registered Users</p>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-icon orange">
                        <i class="fas fa-download"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($stats['total_downloads']); ?></h3>
                        <p>Total Downloads</p>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-icon red">
                        <i class="fas fa-folder"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($stats['total_categories']); ?></h3>
                        <p>Categories</p>
                    </div>
                </div>
            </div>
            
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(400px, 1fr)); gap: 25px;">
                <!-- Recent Videos -->
                <div class="card">
                    <div class="card-header">
                        <h2><i class="fas fa-video"></i> Recent Videos</h2>
                        <a href="videos.php" class="btn btn-primary btn-sm">View All</a>
                    </div>
                    <div class="card-body">
                        <?php if (empty($recentVideos)): ?>
                            <p style="text-align: center; color: #666;">No videos yet</p>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>Title</th>
                                            <th>Downloads</th>
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recentVideos as $video): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($video['title']); ?></td>
                                            <td><?php echo number_format($video['download_count']); ?></td>
                                            <td><?php echo formatDate($video['created_at'], 'd/m/Y'); ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- Recent Downloads -->
                <div class="card">
                    <div class="card-header">
                        <h2><i class="fas fa-download"></i> Recent Downloads</h2>
                    </div>
                    <div class="card-body">
                        <?php if (empty($recentDownloads)): ?>
                            <p style="text-align: center; color: #666;">No downloads yet</p>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th>User</th>
                                            <th>Video</th>
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recentDownloads as $download): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($download['username']); ?></td>
                                            <td><?php echo htmlspecialchars($download['video_title']); ?></td>
                                            <td><?php echo formatDate($download['downloaded_at'], 'd/m/Y H:i'); ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </main>
    </div>
    
    <!-- Mobile Toggle -->
    <button class="mobile-toggle">
        <i class="fas fa-bars"></i>
    </button>
    
    <script src="../assets/js/script.js"></script>
</body>
</html>
