<?php
/**
 * Enterprise HRM ERP System - Dashboard / Index
 */

require_once __DIR__ . '/includes/init.php';
requireLogin();

$pageTitle = 'Dashboard';
$currentModule = 'dashboard';

// Get statistics
try {
    $db = getDB();
    $siteId = getCurrentSiteId();
    
    // Total Employees
    $stmt = $db->prepare("SELECT COUNT(*) FROM employees WHERE site_id = ? AND employment_status = 'active'");
    $stmt->execute([$siteId]);
    $totalEmployees = $stmt->fetchColumn();
    
    // Today's Attendance
    $stmt = $db->prepare("
        SELECT COUNT(*) FROM attendance 
        WHERE site_id = ? AND attendance_date = CURDATE() AND attendance_status = 'present'
    ");
    $stmt->execute([$siteId]);
    $todayPresent = $stmt->fetchColumn();
    
    // Pending Leave Applications
    $stmt = $db->prepare("
        SELECT COUNT(*) FROM leave_applications 
        WHERE site_id = ? AND application_status = 'pending'
    ");
    $stmt->execute([$siteId]);
    $pendingLeaves = $stmt->fetchColumn();
    
    // Pending Payroll Periods
    $stmt = $db->prepare("
        SELECT COUNT(*) FROM payroll_periods 
        WHERE site_id = ? AND period_status IN ('draft', 'processing')
    ");
    $stmt->execute([$siteId]);
    $pendingPayrolls = $stmt->fetchColumn();
    
} catch (Exception $e) {
    error_log("Dashboard error: " . $e->getMessage());
    $totalEmployees = 0;
    $todayPresent = 0;
    $pendingLeaves = 0;
    $pendingPayrolls = 0;
}

include __DIR__ . '/includes/header.php';
?>

<div class="dashboard-stats">
    <div class="stat-card">
        <div class="stat-icon">👥</div>
        <div class="stat-content">
            <div class="stat-value"><?php echo number_format($totalEmployees); ?></div>
            <div class="stat-label">Total Employees</div>
        </div>
    </div>
    
    <div class="stat-card">
        <div class="stat-icon">✓</div>
        <div class="stat-content">
            <div class="stat-value"><?php echo number_format($todayPresent); ?></div>
            <div class="stat-label">Present Today</div>
        </div>
    </div>
    
    <div class="stat-card">
        <div class="stat-icon">📅</div>
        <div class="stat-content">
            <div class="stat-value"><?php echo number_format($pendingLeaves); ?></div>
            <div class="stat-label">Pending Leaves</div>
        </div>
    </div>
    
    <div class="stat-card">
        <div class="stat-icon">💰</div>
        <div class="stat-content">
            <div class="stat-value"><?php echo number_format($pendingPayrolls); ?></div>
            <div class="stat-label">Pending Payrolls</div>
        </div>
    </div>
</div>

<style>
.dashboard-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: white;
    padding: 25px;
    border-radius: 8px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 20px;
}

.stat-icon {
    font-size: 48px;
    opacity: 0.8;
}

.stat-content {
    flex: 1;
}

.stat-value {
    font-size: 32px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 5px;
}

.stat-label {
    font-size: 14px;
    color: var(--text-secondary);
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>

