<?php
session_start();
require_once('../config/db.php');

// ✅ استدعاء الهيدر في البداية لتعريف نظام الترجمة والاتجاهات
include('../includes/header.php');

if (!isset($_SESSION['user'])) {
    header('Location: ../auth/login.php');
    exit;
}

// جلب جميع الطلاب
try {
    $stmt = $conn->query("SELECT * FROM students ORDER BY created_at DESC");
    $students = $stmt->fetchAll();
} catch (PDOException $e) {
    die(__('error_db_general') . $e->getMessage());
}
?>

<?php include('../includes/navbar.php'); ?>

<div class="max-w-7xl mx-auto p-6" dir="<?= $dir ?>">
    <div class="bg-white dark:bg-gray-800 rounded-2xl shadow-md p-6">
        
        <div class="flex flex-col sm:flex-row justify-between items-center mb-6 gap-4">
            <h2 class="text-2xl font-bold text-gray-800 dark:text-gray-100">
                <i class="fas fa-users <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?> text-blue-600"></i>
                <?= __('students_report_title') ?>
            </h2>
            <a href="add.php" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-full shadow font-medium transition flex items-center">
                <i class="fas fa-plus-circle <?= $dir == 'rtl' ? 'ml-2' : 'mr-2' ?>"></i>
                <?= __('add_new_student') ?>
            </a>
        </div>

        <div class="overflow-x-auto rounded-xl border border-gray-200 dark:border-gray-700">
            <table class="min-w-full <?= $dir == 'rtl' ? 'text-right' : 'text-left' ?> border-collapse">
                <thead class="bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-200 text-sm font-semibold">
                    <tr>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3"><?= __('full_name') ?></th>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3"><?= __('phone') ?></th>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3"><?= __('school_name_or_email') ?></th>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3 text-center"><?= __('gender') ?></th>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3"><?= __('birth_date') ?></th>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3"><?= __('branch_or_address') ?></th>
                        <th class="border-b border-gray-300 dark:border-gray-600 px-4 py-3"><?= __('registration_date') ?></th>
                    </tr>
                </thead>
                <tbody class="text-sm text-gray-800 dark:text-gray-300 divide-y divide-gray-200 dark:divide-gray-700">
                    <?php if (count($students) > 0): ?>
                        <?php foreach ($students as $student): ?>
                            <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 transition">
                                <td class="px-4 py-3 font-semibold text-blue-700 dark:text-blue-400">
                                    <?= htmlspecialchars($student['full_name']) ?>
                                </td>
                                <td class="px-4 py-3"><?= htmlspecialchars($student['phone'] ?? '-') ?></td>
                                <td class="px-4 py-3"><?= htmlspecialchars($student['email'] ?? '-') ?></td>
                                <td class="px-4 py-3 text-center">
                                    <span class="px-2 py-1 rounded-md text-xs <?= ($student['gender'] == 'ذكر' || $student['gender'] == 'Male') ? 'bg-blue-100 text-blue-700' : 'bg-pink-100 text-pink-700' ?>">
                                        <?= __($student['gender'] ?? 'unknown') ?>
                                    </span>
                                </td>
                                <td class="px-4 py-3">
                                    <?= $student['birth_date'] ? date('Y-m-d', strtotime($student['birth_date'])) : '-' ?>
                                </td>
                                <td class="px-4 py-3">
                                    <?= nl2br(htmlspecialchars($student['address'] ?? '-')) ?>
                                </td>
                                <td class="px-4 py-3 text-xs text-gray-500">
                                    <?= date('Y-m-d H:i', strtotime($student['created_at'])) ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" class="px-4 py-10 text-center text-gray-500 italic bg-gray-50 dark:bg-gray-800/50">
                                <i class="fas fa-folder-open block text-3xl mb-2 opacity-20"></i>
                                <?= __('no_students_registered') ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include('../includes/footer.php'); ?>