<?php
require_once '../config.php';

// التحقق من تسجيل الدخول
requireLogin();

// معالجة البحث
$searchTerm = isset($_GET['search']) ? sanitizeInput($_GET['search']) : '';
$specializationFilter = isset($_GET['specialization']) ? sanitizeInput($_GET['specialization']) : '';
$statusFilter = isset($_GET['status']) ? sanitizeInput($_GET['status']) : 'all';

// استعلام قائمة المؤطرين
$query = "SELECT * FROM coaches WHERE 1=1";
$params = [];

// إضافة شروط البحث
if (!empty($searchTerm)) {
    $query .= " AND (first_name LIKE :search OR last_name LIKE :search OR phone LIKE :search OR email LIKE :search)";
    $params[':search'] = "%$searchTerm%";
}

if (!empty($specializationFilter)) {
    $query .= " AND specialization = :specialization";
    $params[':specialization'] = $specializationFilter;
}

if ($statusFilter !== 'all') {
    $query .= " AND status = :status";
    $params[':status'] = $statusFilter;
}

// ترتيب النتائج
$query .= " ORDER BY first_name, last_name";

// تنفيذ الاستعلام
$coaches = fetchAll($query, $params);

// الحصول على قائمة التخصصات للفلتر
$specializationsQuery = "SELECT DISTINCT specialization FROM coaches WHERE specialization IS NOT NULL AND specialization != '' ORDER BY specialization";
$specializations = fetchAll($specializationsQuery);

// عنوان الصفحة
$pageTitle = 'إدارة المؤطرين';

// تضمين ملف الهيدر
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3 mb-0"><i class="fas fa-user-tie me-2"></i> إدارة المؤطرين</h1>
    <a href="<?php echo SITE_URL; ?>/coaches/add.php" class="btn btn-primary">
        <i class="fas fa-plus-circle"></i> إضافة مؤطر جديد
    </a>
</div>

<!-- فلاتر البحث -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" action="" class="row g-3">
            <div class="col-md-4">
                <label for="search" class="form-label">بحث</label>
                <input type="text" class="form-control" id="search" name="search" value="<?php echo $searchTerm; ?>" placeholder="الاسم أو رقم الهاتف أو البريد الإلكتروني...">
            </div>
            <div class="col-md-3">
                <label for="specialization" class="form-label">التخصص</label>
                <select class="form-select" id="specialization" name="specialization">
                    <option value="">جميع التخصصات</option>
                    <?php foreach ($specializations as $spec): ?>
                        <option value="<?php echo $spec['specialization']; ?>" <?php echo ($specializationFilter == $spec['specialization']) ? 'selected' : ''; ?>>
                            <?php echo $spec['specialization']; ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label for="status" class="form-label">الحالة</label>
                <select class="form-select" id="status" name="status">
                    <option value="all" <?php echo ($statusFilter === 'all') ? 'selected' : ''; ?>>الكل</option>
                    <option value="active" <?php echo ($statusFilter === 'active') ? 'selected' : ''; ?>>نشط</option>
                    <option value="inactive" <?php echo ($statusFilter === 'inactive') ? 'selected' : ''; ?>>غير نشط</option>
                </select>
            </div>
            <div class="col-md-2 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="fas fa-search"></i> بحث
                </button>
            </div>
        </form>
    </div>
</div>

<!-- جدول المؤطرين -->
<div class="card">
    <div class="card-body">
        <?php if (count($coaches) > 0): ?>
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>الصورة</th>
                            <th>الاسم الكامل</th>
                            <th>التخصص</th>
                            <th>رقم الهاتف</th>
                            <th>البريد الإلكتروني</th>
                            <th>تاريخ التعيين</th>
                            <th>الحالة</th>
                            <th>الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($coaches as $index => $coach): ?>
                            <tr>
                                <td><?php echo $index + 1; ?></td>
                                <td>
                                    <?php
                                    $photoPath = !empty($coach['photo']) 
                                        ? SITE_URL . '/uploads/coaches/' . $coach['photo'] 
                                        : SITE_URL . '/assets/img/default-user.png';
                                    ?>
                                    <img src="<?php echo $photoPath; ?>" alt="صورة" class="rounded-circle" width="40" height="40">
                                </td>
                                <td><?php echo $coach['first_name'] . ' ' . $coach['last_name']; ?></td>
                                <td><?php echo $coach['specialization']; ?></td>
                                <td><?php echo $coach['phone']; ?></td>
                                <td><?php echo $coach['email']; ?></td>
                                <td><?php echo formatDate($coach['hire_date']); ?></td>
                                <td>
                                    <?php if ($coach['status'] === 'active'): ?>
                                        <span class="badge bg-success">نشط</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">غير نشط</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?php echo SITE_URL; ?>/coaches/view.php?id=<?php echo $coach['id']; ?>" class="btn btn-info" title="عرض">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="<?php echo SITE_URL; ?>/coaches/edit.php?id=<?php echo $coach['id']; ?>" class="btn btn-primary" title="تعديل">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="<?php echo SITE_URL; ?>/coaches/delete.php?id=<?php echo $coach['id']; ?>" class="btn btn-danger btn-delete" title="حذف" onclick="return confirm('هل أنت متأكد من رغبتك في حذف هذا المؤطر؟')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i> لا يوجد مؤطرين مطابقين لمعايير البحث.
            </div>
        <?php endif; ?>
    </div>
</div>

<?php
// تضمين ملف الفوتر
include '../includes/footer.php';
?>
