<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}
include '../includes/db.php';

// Get counts
$service_count = $conn->query("SELECT COUNT(*) as count FROM services")->fetch_assoc()['count'] ?? 0;
$message_count = $conn->query("SELECT COUNT(*) as count FROM contact_messages")->fetch_assoc()['count'] ?? 0;
$app_res = $conn->query("SELECT COUNT(*) as count FROM job_applications");
$app_count = ($app_res) ? ($app_res->fetch_assoc()['count'] ?? 0) : 0; 
$blog_count = $conn->query("SELECT COUNT(*) as count FROM blogs")->fetch_assoc()['count'] ?? 0;

// Company, Founder, Director counts
$company_count = $conn->query("SELECT COUNT(*) as count FROM companies")->fetch_assoc()['count'] ?? 0;
$founder_count = $conn->query("SELECT COUNT(*) as count FROM founders")->fetch_assoc()['count'] ?? 0;
$director_count = $conn->query("SELECT COUNT(*) as count FROM directors")->fetch_assoc()['count'] ?? 0;

// Visitor Count
try {
    $visit_res = $conn->query("SELECT total_visits FROM site_analytics WHERE id=1");
    if (!$visit_res) {
        $conn->query("CREATE TABLE IF NOT EXISTS site_analytics (id INT PRIMARY KEY, total_visits INT DEFAULT 0)");
        $conn->query("INSERT INTO site_analytics (id, total_visits) VALUES (1, 0) ON DUPLICATE KEY UPDATE id=1");
        $visit_count = 0;
    } else {
        $visit_count = ($visit_res->num_rows > 0) ? $visit_res->fetch_assoc()['total_visits'] : 0;
    }
} catch (Exception $e) {
    $visit_count = 0;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Balimeen</title>
    <link rel="shortcut icon" href="/bm/BM LOGO.jpg" type="image/jpeg">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="dashboard.css">
    <style>
        .quick-action-card {
            background: #fff;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.06);
            transition: all 0.2s ease;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .quick-action-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 16px rgba(0,0,0,0.1);
        }
        .stat-card {
            border-radius: 12px;
            padding: 22px;
            color: #fff;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
            position: relative;
            overflow: hidden;
        }
        .stat-card i.bg-icon {
            position: absolute;
            right: 15px;
            bottom: 15px;
            font-size: 48px;
            opacity: 0.18;
        }
        .stat-card h3 {
            font-size: 32px;
            font-weight: 700;
            margin-bottom: 4px;
        }
        .stat-card p {
            font-size: 14px;
            margin-bottom: 0;
            opacity: 0.9;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
    </style>
</head>
<body>
    <div class="sidebar d-flex flex-column flex-shrink-0 p-3 text-white">
        <a href="dashboard.php" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
            <span class="fs-4 fw-bold">Admin Panel</span>
        </a>
        <hr>
        <ul class="nav nav-pills flex-column mb-auto">
            <li class="nav-item">
                <a href="dashboard.php" class="active"><i class="fas fa-tachometer-alt me-2"></i> Dashboard</a>
            </li>
            <li>
                <a href="manage_services.php"><i class="fas fa-tools me-2"></i> Manage Services</a>
            </li>
            <li>
                <a href="manage_customers.php"><i class="fas fa-building me-2"></i> Manage Companies</a>
            </li>
            <li>
                <a href="manage_founders.php"><i class="fas fa-user-tie me-2"></i> Manage Founders</a>
            </li>
            <li>
                <a href="manage_directors.php"><i class="fas fa-users-cog me-2"></i> Manage Directors</a>
            </li>
            <li>
                <a href="manage_pages.php"><i class="fas fa-file-alt me-2"></i> Manage Content</a>
            </li>
            <li>
                <a href="manage_blogs.php"><i class="fas fa-blog me-2"></i> Manage Blogs</a>
            </li>
            <li>
                <a href="job_applications.php"><i class="fas fa-briefcase me-2"></i> Job Applications</a>
            </li>
            <li>
                <a href="contact_messages.php"><i class="fas fa-envelope me-2"></i> Messages</a>
            </li>
            <li>
                <a href="manage_credentials.php"><i class="fas fa-user-cog me-2"></i> Manage Credentials</a>
            </li>
            <li>
                <a href="logout.php" class="text-danger"><i class="fas fa-sign-out-alt me-2"></i> Logout</a>
            </li>
        </ul>
    </div>

    <div class="content">
        <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
            <div>
                <h2 class="fw-bold mb-1">Executive Dashboard</h2>
                <p class="text-muted mb-0">Overview of Balimeen Private Limited management platform</p>
            </div>
            <div class="d-flex gap-2">
                <a href="/bm/" target="_blank" class="btn btn-outline-primary"><i class="fas fa-globe me-1"></i> View Website</a>
                <a href="company_add.php" class="btn btn-primary"><i class="fas fa-plus me-1"></i> Add Company</a>
            </div>
        </div>
        
        <!-- Stats Grid -->
        <div class="row g-3 mb-4">
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #2563eb, #1d4ed8);">
                    <h3><?php echo $company_count; ?></h3>
                    <p>Companies / Clients</p>
                    <i class="fas fa-building bg-icon"></i>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #0d9488, #0f766e);">
                    <h3><?php echo $founder_count; ?></h3>
                    <p>Founders & Proprietors</p>
                    <i class="fas fa-user-tie bg-icon"></i>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #6366f1, #4338ca);">
                    <h3><?php echo $director_count; ?></h3>
                    <p>Board of Directors</p>
                    <i class="fas fa-users-cog bg-icon"></i>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #ea580c, #c2410c);">
                    <h3><?php echo $service_count; ?></h3>
                    <p>Total Services</p>
                    <i class="fas fa-tools bg-icon"></i>
                </div>
            </div>

            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #0284c7, #0369a1);">
                    <h3><?php echo $app_count; ?></h3>
                    <p>Job Applications</p>
                    <i class="fas fa-briefcase bg-icon"></i>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #059669, #047857);">
                    <h3><?php echo $message_count; ?></h3>
                    <p>Contact Messages</p>
                    <i class="fas fa-envelope bg-icon"></i>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #8b5cf6, #7c3aed);">
                    <h3><?php echo $blog_count; ?></h3>
                    <p>Published Blogs</p>
                    <i class="fas fa-blog bg-icon"></i>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card" style="background: linear-gradient(135deg, #d97706, #b45309);">
                    <h3><?php echo $visit_count; ?></h3>
                    <p>Total Site Visits</p>
                    <i class="fas fa-chart-line bg-icon"></i>
                </div>
            </div>
        </div>

        <!-- Quick Company & Profile Management Hub -->
        <h4 class="fw-bold mb-3"><i class="fas fa-th-large text-primary me-2"></i> Company & Leadership Management Hub</h4>
        <div class="row g-3 mb-4">
            <div class="col-md-4">
                <div class="quick-action-card">
                    <div>
                        <div class="d-flex align-items-center mb-3">
                            <div class="p-3 bg-primary-subtle text-primary rounded-3 me-3">
                                <i class="fas fa-building fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="fw-bold mb-0">Companies</h5>
                                <small class="text-muted">Corporate profiles & info</small>
                            </div>
                        </div>
                        <p class="text-secondary small">Add companies, update CIN/GST/PAN details, upload banners & logos, set display order.</p>
                    </div>
                    <div class="d-flex gap-2">
                        <a href="manage_customers.php" class="btn btn-primary btn-sm flex-grow-1"><i class="fas fa-list me-1"></i> Manage</a>
                        <a href="company_add.php" class="btn btn-outline-primary btn-sm"><i class="fas fa-plus"></i> Add</a>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="quick-action-card">
                    <div>
                        <div class="d-flex align-items-center mb-3">
                            <div class="p-3 bg-success-subtle text-success rounded-3 me-3">
                                <i class="fas fa-user-tie fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="fw-bold mb-0">Founders</h5>
                                <small class="text-muted">Proprietors & Key Founders</small>
                            </div>
                        </div>
                        <p class="text-secondary small">Manage founder biographies, education, credentials, social profiles and public executive spotlights.</p>
                    </div>
                    <div class="d-flex gap-2">
                        <a href="manage_founders.php" class="btn btn-success btn-sm flex-grow-1"><i class="fas fa-list me-1"></i> Manage</a>
                        <a href="founder_add.php" class="btn btn-outline-success btn-sm"><i class="fas fa-plus"></i> Add</a>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="quick-action-card">
                    <div>
                        <div class="d-flex align-items-center mb-3">
                            <div class="p-3 bg-info-subtle text-info rounded-3 me-3">
                                <i class="fas fa-users-cog fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="fw-bold mb-0">Directors</h5>
                                <small class="text-muted">Board & Executive Leaders</small>
                            </div>
                        </div>
                        <p class="text-secondary small">Manage multiple directors per company (Managing Director, Executive, Independent Directors).</p>
                    </div>
                    <div class="d-flex gap-2">
                        <a href="manage_directors.php" class="btn btn-info text-white btn-sm flex-grow-1"><i class="fas fa-list me-1"></i> Manage</a>
                        <a href="director_add.php" class="btn btn-outline-info btn-sm"><i class="fas fa-plus"></i> Add</a>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
