<?php
require_once 'includes/header.php';

if (!canView('hr_dashboard')) {
    echo "<div class='alert alert-danger'>ليس لديك صلاحية للوصول إلى هذه الصفحة.</div>";
    require_once 'includes/footer.php';
    exit;
}

// Fetch Stats
$total_employees = db()->query("SELECT COUNT(*) FROM hr_employees WHERE status = 'active'")->fetchColumn();
$employees_present = db()->query("SELECT COUNT(*) FROM hr_attendance WHERE date = CURDATE() AND status = 'present'")->fetchColumn();
$on_leave = db()->query("SELECT COUNT(*) FROM hr_leaves WHERE CURDATE() BETWEEN start_date AND end_date AND status = 'approved'")->fetchColumn();
$pending_leaves = db()->query("SELECT COUNT(*) FROM hr_leaves WHERE status = 'pending'")->fetchColumn();

// Recent Employees
$recent_employees = db()->query("SELECT * FROM hr_employees ORDER BY join_date DESC LIMIT 5")->fetchAll();
?>

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">لوحة الموارد البشرية</h1>
</div>

<!-- Stats Cards -->
<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="card text-white bg-primary shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="card-title mb-0">إجمالي الموظفين</h6>
                        <h2 class="mt-2 mb-0"><?= number_format($total_employees) ?></h2>
                    </div>
                    <i class="fas fa-users fa-2x opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-white bg-success shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="card-title mb-0">حضور اليوم</h6>
                        <h2 class="mt-2 mb-0"><?= number_format($employees_present) ?></h2>
                    </div>
                    <i class="fas fa-user-check fa-2x opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-white bg-warning shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="card-title mb-0">في إجازة</h6>
                        <h2 class="mt-2 mb-0"><?= number_format($on_leave) ?></h2>
                    </div>
                    <i class="fas fa-plane-departure fa-2x opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-white bg-danger shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="card-title mb-0">طلبات إجازة معلقة</h6>
                        <h2 class="mt-2 mb-0"><?= number_format($pending_leaves) ?></h2>
                    </div>
                    <i class="fas fa-clipboard-list fa-2x opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- Recent Employees -->
    <div class="col-md-6 mb-4">
        <div class="card shadow-sm">
            <div class="card-header bg-white py-3">
                <h5 class="mb-0"><i class="fas fa-user-plus me-2 text-primary"></i> أحدث الموظفين</h5>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0 align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>الاسم</th>
                                <th>الوظيفة</th>
                                <th>تاريخ التعيين</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($recent_employees)): ?>
                                <tr><td colspan="3" class="text-center py-3 text-muted">لا يوجد موظفين مسجلين حالياً</td></tr>
                            <?php else: ?>
                                <?php foreach ($recent_employees as $emp): ?>
                                <tr>
                                    <td><?= htmlspecialchars($emp['first_name'] . ' ' . $emp['last_name']) ?></td>
                                    <td><?= htmlspecialchars($emp['job_title']) ?></td>
                                    <td><?= date('Y-m-d', strtotime($emp['join_date'])) ?></td>
                                </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="card-footer bg-white text-center">
                <a href="hr_employees.php" class="text-decoration-none small fw-bold">عرض كل الموظفين <i class="fas fa-arrow-left ms-1"></i></a>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="col-md-6 mb-4">
        <div class="card shadow-sm h-100">
            <div class="card-header bg-white py-3">
                <h5 class="mb-0"><i class="fas fa-bolt me-2 text-warning"></i> إجراءات سريعة</h5>
            </div>
            <div class="card-body">
                <div class="d-grid gap-3">
                    <?php if (canAdd('hr_employees')): ?>
                    <a href="hr_employees.php?action=add" class="btn btn-outline-primary text-start">
                        <i class="fas fa-plus-circle me-2"></i> إضافة موظف جديد
                    </a>
                    <?php endif; ?>
                    <?php if (canAdd('hr_attendance')): ?>
                    <a href="hr_attendance.php?action=mark" class="btn btn-outline-success text-start">
                        <i class="fas fa-clock me-2"></i> تسجيل حضور وانصراف
                    </a>
                    <?php endif; ?>
                    <?php if (canAdd('hr_leaves')): ?>
                    <a href="hr_leaves.php?action=request" class="btn btn-outline-warning text-start">
                        <i class="fas fa-calendar-plus me-2"></i> تقديم طلب إجازة
                    </a>
                    <?php endif; ?>
                    <?php if (canAdd('hr_payroll')): ?>
                    <a href="hr_payroll.php?action=generate" class="btn btn-outline-info text-start">
                        <i class="fas fa-file-invoice-dollar me-2"></i> إصدار مسير رواتب
                    </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
