<?php
require_once 'config.php';
require_once 'auth_functions.php';
requireAdmin();

$total_employees = $conn->query("SELECT COUNT(*) as count FROM users WHERE role = 'employee'")->fetch_assoc()['count'];
$pending_leaves = $conn->query("SELECT COUNT(*) as count FROM leave_applications WHERE status = 'pending'")->fetch_assoc()['count'];
$approved_today = $conn->query("SELECT COUNT(*) as count FROM leave_applications WHERE status = 'approved' AND DATE(approved_date) = CURDATE()")->fetch_assoc()['count'];
$casual_leaves_today = $conn->query("SELECT COUNT(DISTINCT user_id) as count FROM leave_applications WHERE leave_type = 'casual' AND status = 'approved' AND CURDATE() BETWEEN start_date AND end_date")->fetch_assoc()['count'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Leave Management System-PCC</title>
    <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="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
    <style>
        body { background-color: #f8f9fa; }
        .navbar { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
        .stats-card {
            border-radius: 10px;
            border: none;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        .stats-card:hover { transform: translateY(-5px); }
        .card-employees { border-left: 4px solid #007bff; }
        .card-pending { border-left: 4px solid #ffc107; }
        .card-approved { border-left: 4px solid #28a745; }
        .card-casual { border-left: 4px solid #17a2b8; }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#"><i class="fas fa-calendar-check"></i> Leave Management System-PCC</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link active" href="admin_dashboard.php"><i class="fas fa-home"></i> Dashboard</a></li>
                    <li class="nav-item"><a class="nav-link" href="manage_employees.php"><i class="fas fa-users"></i> Employees</a></li>
                    <li class="nav-item"><a class="nav-link" href="approve_leaves.php"><i class="fas fa-check-circle"></i> Approve Leaves</a></li>
                    <li class="nav-item"><a class="nav-link" href="leave_reports.php"><i class="fas fa-file-alt"></i> Reports</a></li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                            <i class="fas fa-user-shield"></i> <?php echo $_SESSION['full_name']; ?>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <h2 class="mb-4">Admin Dashboard</h2>
        
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card stats-card card-employees">
                    <div class="card-body">
                        <h6 class="card-title text-muted">Total Employees</h6>
                        <h3 class="mb-0"><?php echo $total_employees; ?></h3>
                        <small class="text-muted">Registered</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card card-pending">
                    <div class="card-body">
                        <h6 class="card-title text-muted">Pending Leaves</h6>
                        <h3 class="mb-0"><?php echo $pending_leaves; ?></h3>
                        <small class="text-muted">Awaiting approval</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card card-approved">
                    <div class="card-body">
                        <h6 class="card-title text-muted">Approved Today</h6>
                        <h3 class="mb-0"><?php echo $approved_today; ?></h3>
                        <small class="text-muted">Applications</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card card-casual">
                    <div class="card-body">
                        <h6 class="card-title text-muted">Casual Leaves Today</h6>
                        <h3 class="mb-0"><?php echo $casual_leaves_today; ?> / 5</h3>
                        <small class="text-muted">Active employees</small>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-clock"></i> Recent Leave Applications</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Employee</th>
                                        <th>Type</th>
                                        <th>Date</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sql_recent = "SELECT la.*, u.full_name FROM leave_applications la 
                                                  JOIN users u ON la.user_id = u.id 
                                                  ORDER BY la.apply_date DESC LIMIT 5";
                                    $recent_result = $conn->query($sql_recent);
                                    while ($row = $recent_result->fetch_assoc()):
                                    ?>
                                    <tr>
                                        <td><?php echo $row['full_name']; ?></td>
                                        <td><?php echo ucfirst($row['leave_type']); ?></td>
                                        <td><?php echo date('M d', strtotime($row['start_date'])); ?></td>
                                        <td>
                                            <?php
                                            $badge = $row['status'] === 'approved' ? 'success' : ($row['status'] === 'rejected' ? 'danger' : 'warning');
                                            echo "<span class='badge bg-{$badge}'>" . ucfirst($row['status']) . "</span>";
                                            ?>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0"><i class="fas fa-chart-bar"></i> Quick Actions</h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <a href="approve_leaves.php" class="btn btn-outline-primary">
                                <i class="fas fa-check-circle"></i> Approve Pending Leaves
                            </a>
                            <a href="manage_employees.php" class="btn btn-outline-success">
                                <i class="fas fa-user-plus"></i> Add New Employee
                            </a>
                            <a href="leave_reports.php" class="btn btn-outline-info">
                                <i class="fas fa-download"></i> Download Reports
                            </a>
                            <a href="all_leaves_table.php" class="btn btn-outline-warning">
                                <i class="fas fa-table"></i> View All Leaves
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <center><label>Developed by <br>ICT SECTION <br>Passara Central College</label></center>
</body>
</html>
