<?php
require_once __DIR__ . '/../includes/coordinator_auth.php';
require_once __DIR__ . '/../includes/coordinator_layout_top.php';

$stats = [
    'students' => 0,
    'batches' => 0,
    'assessments' => 0,
    'submissions' => 0,
];

function table_count(mysqli $conn, string $table): int {
    $safe = preg_replace('/[^a-zA-Z0-9_]/', '', $table);
    $res = $conn->query("SHOW TABLES LIKE '{$safe}'");
    if (!$res || $res->num_rows === 0) return 0;

    $q = $conn->query("SELECT COUNT(*) AS c FROM `$safe`");
    if (!$q) return 0;

    $row = $q->fetch_assoc();
    return (int)($row['c'] ?? 0);
}

$stats['students']    = table_count($conn, 'studentss');
$stats['batches']     = table_count($conn, 'batches');
$stats['assessments'] = table_count($conn, 'lms_assessments');
$stats['submissions'] = table_count($conn, 'lms_submissions');
?>

<div class="d-flex justify-content-between align-items-start mb-4">
    <div>
        <h1 class="page-title">Coordinator Dashboard</h1>
        <div class="page-subtitle">ELMS assigned operations overview</div>
    </div>

    <a href="/elms/coordinator/announcements.php" class="btn btn-eden">
        <i class="bi bi-plus-circle me-1"></i> New Announcement
    </a>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="eden-card p-3">
            <div class="text-muted small">Students</div>
            <div class="fs-3 fw-bold"><?= number_format($stats['students']) ?></div>
            <i class="bi bi-people fs-4 text-danger"></i>
        </div>
    </div>

    <div class="col-md-3">
        <div class="eden-card p-3">
            <div class="text-muted small">Batches</div>
            <div class="fs-3 fw-bold"><?= number_format($stats['batches']) ?></div>
            <i class="bi bi-layers fs-4 text-danger"></i>
        </div>
    </div>

    <div class="col-md-3">
        <div class="eden-card p-3">
            <div class="text-muted small">Assessments</div>
            <div class="fs-3 fw-bold"><?= number_format($stats['assessments']) ?></div>
            <i class="bi bi-clipboard-check fs-4 text-danger"></i>
        </div>
    </div>

    <div class="col-md-3">
        <div class="eden-card p-3">
            <div class="text-muted small">Submissions</div>
            <div class="fs-3 fw-bold"><?= number_format($stats['submissions']) ?></div>
            <i class="bi bi-inboxes fs-4 text-danger"></i>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-8">
        <div class="eden-card p-4">
            <h5 class="fw-bold mb-3">Quick Actions</h5>

            <div class="row g-2">
                <div class="col-md-6">
                    <a href="/elms/coordinator/students.php" class="btn btn-light border w-100 text-start p-3 rounded-4">
                        <i class="bi bi-people me-2 text-danger"></i> View Students
                    </a>
                </div>

                <div class="col-md-6">
                    <a href="/elms/coordinator/batches.php" class="btn btn-light border w-100 text-start p-3 rounded-4">
                        <i class="bi bi-layers me-2 text-danger"></i> View Batches
                    </a>
                </div>

                <div class="col-md-6">
                    <a href="/elms/coordinator/submissions.php" class="btn btn-light border w-100 text-start p-3 rounded-4">
                        <i class="bi bi-inboxes me-2 text-danger"></i> Check Submissions
                    </a>
                </div>

                <div class="col-md-6">
                    <a href="/elms/coordinator/reports.php" class="btn btn-light border w-100 text-start p-3 rounded-4">
                        <i class="bi bi-bar-chart me-2 text-danger"></i> View Reports
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="eden-card p-4">
            <h5 class="fw-bold mb-3">Coordinator Access</h5>
            <p class="text-muted mb-3">
                Coordinator account can manage assigned LMS operation pages only.
            </p>

            <ul class="list-unstyled small mb-0">
                <li class="mb-2"><i class="bi bi-check-circle text-success me-1"></i> View students</li>
                <li class="mb-2"><i class="bi bi-check-circle text-success me-1"></i> View batches</li>
                <li class="mb-2"><i class="bi bi-check-circle text-success me-1"></i> Manage announcements</li>
                <li class="mb-2"><i class="bi bi-check-circle text-success me-1"></i> Check submissions</li>
                <li><i class="bi bi-x-circle text-danger me-1"></i> No system settings access</li>
            </ul>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/coordinator_layout_bottom.php'; ?>