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

if (!isAdmin()) {
    redirect('user_dashboard.php');
}

$user_id = $_SESSION['user_id'];

// Helper functions for safe DB queries
function getSafeCount($query, $params = []) {
    try {
        $stmt = db()->prepare($query);
        $stmt->execute($params);
        return $stmt->fetchColumn() ?: 0;
    } catch (Exception $e) {
        return 0; 
    }
}

function getSafeValue($query, $params = []) {
    try {
        $stmt = db()->prepare($query);
        $stmt->execute($params);
        return $stmt->fetchColumn() ?: 0;
    } catch (Exception $e) {
        return 0; 
    }
}

// 1. Mail & Tasks
$total_inbound = canView('inbound') ? getSafeCount("SELECT COUNT(*) FROM inbound_mail") : 0;
$total_outbound = canView('outbound') ? getSafeCount("SELECT COUNT(*) FROM outbound_mail") : 0;
$total_internal = canView('internal') ? getSafeCount("SELECT COUNT(*) FROM internal_mail") : 0;

$in_progress_count = 0;
$overdue_count = 0;
try {
    $statuses_data = db()->query("SELECT * FROM mailbox_statuses")->fetchAll(PDO::FETCH_UNIQUE);
    $in_progress_id = null;
    foreach ($statuses_data as $id => $s) {
        if ($s['name'] == 'in_progress') {
            $in_progress_id = $id;
            break;
        }
    }
    
    if ($in_progress_id) {
        if (canView('inbound')) $in_progress_count += getSafeCount("SELECT COUNT(*) FROM inbound_mail WHERE status_id = ?", [$in_progress_id]);
        if (canView('outbound')) $in_progress_count += getSafeCount("SELECT COUNT(*) FROM outbound_mail WHERE status_id = ?", [$in_progress_id]);
        if (canView('internal')) $in_progress_count += getSafeCount("SELECT COUNT(*) FROM internal_mail WHERE status_id = ?", [$in_progress_id]);
    }
    
    if (canView('reports')) {
        $overdue_count += getSafeCount("SELECT COUNT(*) FROM inbound_mail WHERE due_date < CURDATE() AND status_id NOT IN (SELECT id FROM mailbox_statuses WHERE name IN ('closed', 'مكتمل', 'مؤرشف', 'مؤرشفة'))");
        $overdue_count += getSafeCount("SELECT COUNT(*) FROM outbound_mail WHERE due_date < CURDATE() AND status_id NOT IN (SELECT id FROM mailbox_statuses WHERE name IN ('closed', 'مكتمل', 'مؤرشف', 'مؤرشفة'))");
        $overdue_count += getSafeCount("SELECT COUNT(*) FROM internal_mail WHERE due_date < CURDATE() AND status_id NOT IN (SELECT id FROM mailbox_statuses WHERE name IN ('closed', 'مكتمل', 'مؤرشف', 'مؤرشفة'))");
    }
} catch (Exception $e) {}

// 2. HR & Employees
$total_employees = canView('hr_employees') ? getSafeCount("SELECT COUNT(*) FROM hr_employees") : 0;
$pending_leaves = canView('hr_leaves') ? getSafeCount("SELECT COUNT(*) FROM hr_leaves WHERE status = 'pending'") : 0;
$present_today = canView('hr_attendance') ? getSafeCount("SELECT COUNT(DISTINCT employee_id) FROM hr_attendance WHERE date = CURDATE() AND status = 'present'") : 0;

// 3. Accounting & Expenses
$total_expenses_month = canView('expenses') ? getSafeValue("SELECT SUM(amount) FROM expenses WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE())") : 0;
$total_accounts = canView('accounting') ? getSafeCount("SELECT COUNT(*) FROM accounting_accounts") : 0;

// 4. Stock & Inventory
$total_stock_items = canView('stock') ? getSafeCount("SELECT COUNT(*) FROM stock_items") : 0;
$low_stock = canView('stock') ? getSafeCount("SELECT COUNT(*) FROM (SELECT si.id FROM stock_items si LEFT JOIN stock_quantities sq ON si.id = sq.item_id GROUP BY si.id, si.min_quantity HAVING COALESCE(SUM(sq.quantity), 0) <= si.min_quantity) AS t") : 0;

// 5. Charity & Committees
$total_charity_members = canView('charity_members') ? getSafeCount("SELECT COUNT(*) FROM charity_members") : 0;
$total_committees = canView('committees') ? getSafeCount("SELECT COUNT(*) FROM committees") : 0;

// 6. Events & Meetings
$upcoming_meetings = canView('meetings') ? getSafeCount("SELECT COUNT(*) FROM meetings WHERE DATE(start_time) >= CURRENT_DATE() AND status != 'completed'") : 0;
$upcoming_events = canView('events') ? getSafeCount("SELECT COUNT(*) FROM events WHERE event_date >= CURRENT_DATE()") : 0;

// 7. Users
$total_users = canView('users') ? getSafeCount("SELECT COUNT(*) FROM users") : 0;
?>

<style>
.hover-lift {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    text-decoration: none !important;
}
.hover-lift:hover {
    transform: translateY(-5px);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important;
}
.card-title-muted {
    font-size: 0.9rem;
    color: #6c757d;
}
</style>

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-4 border-bottom">
    <h1 class="h2"><i class="fas fa-chart-pie me-2 text-primary"></i> النظرة الشاملة للنظام</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <?php if (canView('settings')): ?>
            <a href="charity-settings.php" class="btn btn-sm btn-outline-dark"><i class="fas fa-cog me-1"></i> الإعدادات</a>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Overdue Alert -->
<?php if ($overdue_count > 0): ?>
<div class="row mb-4">
    <div class="col-12">
        <div class="alert alert-danger shadow-sm border-0 d-flex align-items-center justify-content-between mb-0 rounded-3">
            <div>
                <i class="fas fa-exclamation-triangle fs-4 me-3 text-danger"></i>
                <span class="fw-bold">يوجد (<?= $overdue_count ?>) مهام أو مراسلات متأخرة تتطلب انتباهك!</span>
            </div>
            <a href="overdue_report.php" class="btn btn-danger btn-sm rounded-pill px-3 shadow-sm hover-lift">عرض التقرير</a>
        </div>
    </div>
</div>
<?php endif; ?>


<?php 
// Section 1: Mail
$show_mail = canView('inbound') || canView('outbound') || canView('internal');
if ($show_mail): 
?>
<div class="row g-4 mb-4">
    <div class="col-12 mb-1">
        <h5 class="fw-bold text-secondary border-bottom pb-2 m-0"><i class="fas fa-envelope-open-text me-2 text-primary"></i> المراسلات والمهام</h5>
    </div>
    
    <?php if (canView('inbound')): ?>
    <div class="col-md-3">
        <a href="inbound.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-primary border-4 rounded-3 hover-lift">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">البريد الوارد</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_inbound ?></h3>
                    </div>
                    <div class="bg-primary bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-inbox text-primary fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('outbound')): ?>
    <div class="col-md-3">
        <a href="outbound.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-success border-4 rounded-3 hover-lift">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">البريد الصادر</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_outbound ?></h3>
                    </div>
                    <div class="bg-success bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-paper-plane text-success fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('internal')): ?>
    <div class="col-md-3">
        <a href="internal_inbox.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-info border-4 rounded-3 hover-lift">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">المراسلات الداخلية</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_internal ?></h3>
                    </div>
                    <div class="bg-info bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-building text-info fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <div class="col-md-3">
        <a href="#" class="text-decoration-none cursor-default" style="cursor: default;">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-warning border-4 rounded-3 hover-lift">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">قيد المعالجة (نشط)</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $in_progress_count ?></h3>
                    </div>
                    <div class="bg-warning bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-spinner fa-spin text-warning fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
</div>
<?php endif; ?>


<?php 
// Section 2: HR
$show_hr = canView('hr_employees') || canView('hr_leaves') || canView('hr_attendance');
if ($show_hr): 
?>
<div class="row g-4 mb-4">
    <div class="col-12 mb-1">
        <h5 class="fw-bold text-secondary border-bottom pb-2 m-0"><i class="fas fa-users-cog me-2 text-primary"></i> الموارد البشرية (HR)</h5>
    </div>
    
    <?php if (canView('hr_employees')): ?>
    <div class="col-md-4">
        <a href="hr_employees.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-primary border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">إجمالي الموظفين</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_employees ?></h3>
                    </div>
                    <div class="bg-primary bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-id-card text-primary fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('hr_attendance')): ?>
    <div class="col-md-4">
        <a href="hr_attendance.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-success border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">حضور اليوم</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $present_today ?></h3>
                    </div>
                    <div class="bg-success bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-fingerprint text-success fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('hr_leaves')): ?>
    <div class="col-md-4">
        <a href="hr_leaves.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-warning border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">طلبات إجازة معلقة</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $pending_leaves ?></h3>
                    </div>
                    <div class="bg-warning bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-user-clock text-warning fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>


<?php 
// Section 3: Accounting & Expenses
$show_accounting = canView('accounting') || canView('expenses');
if ($show_accounting): 
?>
<div class="row g-4 mb-4">
    <div class="col-12 mb-1">
        <h5 class="fw-bold text-secondary border-bottom pb-2 m-0"><i class="fas fa-file-invoice-dollar me-2 text-primary"></i> المالية والمصروفات</h5>
    </div>
    
    <?php if (canView('accounting')): ?>
    <div class="col-md-6">
        <a href="accounts.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-info border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">الحسابات المالية (شجرة الحسابات)</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_accounts ?></h3>
                    </div>
                    <div class="bg-info bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-book-open text-info fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('expenses')): ?>
    <div class="col-md-6">
        <a href="expenses.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-danger border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">إجمالي مصروفات الشهر الحالي</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= number_format((float)$total_expenses_month, 2) ?></h3>
                    </div>
                    <div class="bg-danger bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-money-bill-wave text-danger fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>


<?php 
// Section 4: Stock
$show_stock = canView('stock');
if ($show_stock): 
?>
<div class="row g-4 mb-4">
    <div class="col-12 mb-1">
        <h5 class="fw-bold text-secondary border-bottom pb-2 m-0"><i class="fas fa-boxes me-2 text-primary"></i> المخزون والعهد</h5>
    </div>
    
    <div class="col-md-6">
        <a href="stock_items.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-primary border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">إجمالي الأصناف المسجلة</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_stock_items ?></h3>
                    </div>
                    <div class="bg-primary bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-cubes text-primary fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>

    <div class="col-md-6">
        <a href="stock_reports.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-danger border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">نواقص المخزون (أقل من الحد الأدنى)</h6>
                        <h3 class="fw-bold mb-0 text-danger"><?= $low_stock ?></h3>
                    </div>
                    <div class="bg-danger bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-exclamation-circle text-danger fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
</div>
<?php endif; ?>


<?php 
// Section 5: Charity, Events & Meetings
$show_events = canView('events') || canView('meetings') || canView('charity_members') || canView('committees') || canView('users');
if ($show_events): 
?>
<div class="row g-4 mb-4">
    <div class="col-12 mb-1">
        <h5 class="fw-bold text-secondary border-bottom pb-2 m-0"><i class="fas fa-hands-helping me-2 text-primary"></i> الإدارة والأنشطة</h5>
    </div>
    
    <?php if (canView('charity_members')): ?>
    <div class="col-md-3">
        <a href="charity_members.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-success border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">أعضاء الجمعية</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_charity_members ?></h3>
                    </div>
                    <div class="bg-success bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-handshake text-success fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('committees')): ?>
    <div class="col-md-3">
        <a href="committees.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-info border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">اللجان العاملة</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_committees ?></h3>
                    </div>
                    <div class="bg-info bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-users-cog text-info fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <?php if (canView('meetings')): ?>
    <div class="col-md-3">
        <a href="meetings.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-primary border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">الاجتماعات القادمة</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $upcoming_meetings ?></h3>
                    </div>
                    <div class="bg-primary bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-comments text-primary fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>
    
    <?php if (canView('events')): ?>
    <div class="col-md-3">
        <a href="events.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-warning border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">الفعاليات القادمة</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $upcoming_events ?></h3>
                    </div>
                    <div class="bg-warning bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-calendar-alt text-warning fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>
    
    <?php if (canView('users')): ?>
    <div class="col-md-3">
        <a href="users.php" class="text-decoration-none">
            <div class="card h-100 p-3 shadow-sm border-0 border-start border-dark border-4 rounded-3 hover-lift bg-light">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="card-title-muted fw-bold mb-1">مستخدمي النظام</h6>
                        <h3 class="fw-bold mb-0 text-dark"><?= $total_users ?></h3>
                    </div>
                    <div class="bg-dark bg-opacity-10 p-3 rounded-circle">
                        <i class="fas fa-user-shield text-dark fs-4"></i>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

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