<?php
require_once __DIR__ . '/config.php';
requireAdmin();
$pdo = getDB();

$stats = [
    'products' => 0, 'categories' => 0, 'gallery' => 0, 'videos' => 0,
    'enquiries' => 0, 'clients' => 0, 'stats_counters' => 0, 'footer_cols' => 0,
];
try { $stats['products'] = (int)$pdo->query("SELECT COUNT(*) FROM products")->fetchColumn(); } catch (Exception $e) {}
try { $stats['categories'] = (int)$pdo->query("SELECT COUNT(*) FROM product_categories")->fetchColumn(); } catch (Exception $e) {}
try { $stats['gallery'] = (int)$pdo->query("SELECT COUNT(*) FROM gallery")->fetchColumn(); } catch (Exception $e) {}
try { $stats['videos'] = (int)$pdo->query("SELECT COUNT(*) FROM videos")->fetchColumn(); } catch (Exception $e) {}
try { $stats['enquiries'] = (int)$pdo->query("SELECT COUNT(*) FROM enquiries WHERE is_read = 0")->fetchColumn(); } catch (Exception $e) {}
try { $stats['clients'] = (int)$pdo->query("SELECT COUNT(*) FROM our_clients WHERE is_active = 1")->fetchColumn(); } catch (Exception $e) {}
try { $stats['stats_counters'] = (int)$pdo->query("SELECT COUNT(*) FROM stats_counters WHERE is_active = 1")->fetchColumn(); } catch (Exception $e) {}
try { $stats['footer_cols'] = (int)$pdo->query("SELECT COUNT(*) FROM footer_columns WHERE is_active = 1")->fetchColumn(); } catch (Exception $e) {}

$pageTitle = 'Dashboard';
require_once __DIR__ . '/includes/header.php';
?>
<div class="admin-dashboard">
    <div class="admin-page-header">
        <h1>Dashboard</h1>
        <p class="admin-page-lead">Manage products, home sections, footer, and enquiries from here.</p>
        <p><a href="help.php" class="btn" style="display:inline-block; margin-top:8px;">Open Help &amp; guide</a> – explains every admin option.</p>
    </div>

    <h2 class="admin-section-title">Quick counts</h2>
    <div class="stats-grid">
        <a href="products.php" class="stat-card stat-card--teal">
            <span class="stat-num"><?php echo $stats['products']; ?></span>
            <span class="stat-label">Products</span>
        </a>
        <a href="categories.php" class="stat-card stat-card--slate">
            <span class="stat-num"><?php echo $stats['categories']; ?></span>
            <span class="stat-label">Categories</span>
        </a>
        <a href="gallery.php" class="stat-card stat-card--amber">
            <span class="stat-num"><?php echo $stats['gallery']; ?></span>
            <span class="stat-label">Gallery</span>
        </a>
        <a href="videos.php" class="stat-card stat-card--violet">
            <span class="stat-num"><?php echo $stats['videos']; ?></span>
            <span class="stat-label">Videos</span>
        </a>
        <a href="enquiries.php" class="stat-card stat-card--alert<?php echo $stats['enquiries'] ? ' has-badge' : ''; ?>">
            <?php if ($stats['enquiries']): ?><span class="badge"><?php echo $stats['enquiries']; ?></span><?php endif; ?>
            <span class="stat-num"><?php echo $stats['enquiries'] ?: '0'; ?></span>
            <span class="stat-label">Unread enquiries</span>
        </a>
        <a href="clients.php" class="stat-card stat-card--teal">
            <span class="stat-num"><?php echo $stats['clients']; ?></span>
            <span class="stat-label">Our clients</span>
        </a>
        <a href="stats.php" class="stat-card stat-card--slate">
            <span class="stat-num"><?php echo $stats['stats_counters']; ?></span>
            <span class="stat-label">Stats on home</span>
        </a>
        <a href="footer-content.php" class="stat-card stat-card--amber">
            <span class="stat-num"><?php echo $stats['footer_cols']; ?></span>
            <span class="stat-label">Footer columns</span>
        </a>
    </div>

    <h2 class="admin-section-title">Home page &amp; layout</h2>
    <div class="admin-quick-grid">
        <a href="home-sections.php" class="admin-quick-card">
            <strong>Client section</strong>
            <span>Headline, subtext &amp; image above stats</span>
        </a>
        <a href="stats.php" class="admin-quick-card">
            <strong>Stats counters</strong>
            <span>6+, 50+ style numbers on home</span>
        </a>
        <a href="clients.php" class="admin-quick-card">
            <strong>Our clients</strong>
            <span>Logo strip — add/replace client logos</span>
        </a>
        <a href="footer-content.php" class="admin-quick-card">
            <strong>Footer</strong>
            <span>Columns, links &amp; bottom HTML</span>
        </a>
    </div>

    <h2 class="admin-section-title">Catalogue &amp; content</h2>
    <div class="admin-quick-grid">
        <a href="products.php" class="admin-quick-card">
            <strong>Products</strong>
            <span>Add, edit, images</span>
        </a>
        <a href="categories.php" class="admin-quick-card">
            <strong>Categories</strong>
            <span>Product grouping</span>
        </a>
        <a href="pages.php" class="admin-quick-card">
            <strong>Pages</strong>
            <span>About, Home text, etc.</span>
        </a>
        <a href="about-edit.php" class="admin-quick-card">
            <strong>About page</strong>
            <span>Company, growth, owner, team</span>
        </a>
        <a href="tech-quality-edit.php" class="admin-quick-card">
            <strong>Technology &amp; Quality</strong>
            <span>Quality policy, Plant &amp; Machinery</span>
        </a>
        <a href="gallery.php" class="admin-quick-card">
            <strong>Photo gallery</strong>
            <span>Upload images</span>
        </a>
        <a href="videos.php" class="admin-quick-card">
            <strong>Video gallery</strong>
            <span>YouTube / embed URLs</span>
        </a>
    </div>

    <h2 class="admin-section-title">Site settings</h2>
    <div class="admin-quick-grid admin-quick-grid--narrow">
        <a href="settings.php" class="admin-quick-card">
            <strong>Settings</strong>
            <span>Site name, contact, social</span>
        </a>
        <a href="enquiries.php" class="admin-quick-card">
            <strong>Enquiries</strong>
            <span>Form submissions</span>
        </a>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
