<?php
require_once 'config.php';

$edaraId = isset($_GET['edara_id']) ? intval($_GET['edara_id']) : 0;
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all'; // all, completed, pending

// Build query based on filter
$whereCondition = "";
if ($edaraId > 0) {
    $whereCondition = "WHERE s.EdaraID = $edaraId";
}

if ($filter == 'completed') {
    $whereCondition .= ($whereCondition ? " AND " : "WHERE ") . "s.data_entered = 1";
} elseif ($filter == 'pending') {
    $whereCondition .= ($whereCondition ? " AND " : "WHERE ") . "s.data_entered = 0";
}

$query = "SELECT 
    s.SchoolID,
    s.SchoolName,
    s.data_entered,
    g.GovName,
    e.EdaraName,
    bd.*,
    CASE WHEN s.data_entered = 1 THEN 'مكتملة' ELSE 'قيد الإدخال' END as status
FROM school s
LEFT JOIN school_basic_data bd ON s.SchoolID = bd.SchoolID
LEFT JOIN edara e ON s.EdaraID = e.EdaraID
LEFT JOIN government g ON e.GovID = g.GovID
$whereCondition
ORDER BY g.GovName, e.EdaraName, s.data_entered, s.SchoolName";

$result = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>عرض بيانات المدارس</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: #f0f2f5;
            padding: 20px;
        }
        .table-container {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .table th {
            background: #4a00e0;
            color: white;
            text-align: center;
            white-space: nowrap;
        }
        .table td {
            text-align: center;
            vertical-align: middle;
        }
        .btn-back {
            margin-bottom: 20px;
        }
        .table-responsive {
            max-height: 600px;
            overflow-y: auto;
        }
        .badge-data {
            background: #6c5ce7;
            color: white;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 12px;
        }
        .status-completed {
            color: #00b894;
            font-weight: bold;
        }
        .status-pending {
            color: #fdcb6e;
            font-weight: bold;
        }
        .filter-buttons {
            margin-bottom: 20px;
        }
        .filter-buttons .btn {
            margin-left: 10px;
        }
        .btn-active {
            background: #4a00e0 !important;
            color: white !important;
        }
        .status-badge {
            display: inline-block;
            padding: 3px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }
        .status-badge.completed {
            background: #d4edda;
            color: #155724;
        }
        .status-badge.pending {
            background: #fff3cd;
            color: #856404;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <a href="schooldesc.php" class="btn btn-primary btn-back">
                    <i class="fas fa-arrow-right me-2"></i>العودة للإدخال
                </a>
            </div>
        </div>
        
        <div class="table-container">
            <h3 class="text-center mb-4">
                <i class="fas fa-table me-2"></i>
                عرض جميع بيانات المدارس
            </h3>
            
            <!-- Filter Buttons -->
            <div class="filter-buttons text-center">
                <a href="?filter=all<?= $edaraId ? '&edara_id='.$edaraId : '' ?>" 
                   class="btn <?= $filter == 'all' ? 'btn-active' : 'btn-outline-primary' ?>">
                    <i class="fas fa-list me-1"></i> الكل
                </a>
                <a href="?filter=completed<?= $edaraId ? '&edara_id='.$edaraId : '' ?>" 
                   class="btn <?= $filter == 'completed' ? 'btn-active' : 'btn-outline-success' ?>">
                    <i class="fas fa-check-circle me-1"></i> المكتملة
                </a>
                <a href="?filter=pending<?= $edaraId ? '&edara_id='.$edaraId : '' ?>" 
                   class="btn <?= $filter == 'pending' ? 'btn-active' : 'btn-outline-warning' ?>">
                    <i class="fas fa-clock me-1"></i> قيد الإدخال
                </a>
            </div>
            
            <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>
                            <th>انترنت</th>
                            <th>المعلمين</th>
                            <th>الطلاب</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $counter = 1;
                        $hasData = false;
                        while ($row = mysqli_fetch_assoc($result)): 
                            $hasData = true;
                            $statusClass = $row['data_entered'] == 1 ? 'completed' : 'pending';
                            $statusText = $row['data_entered'] == 1 ? 'مكتملة ✅' : 'قيد الإدخال ⏳';
                        ?>
                        <tr>
                            <td><?= $counter++ ?></td>
                            <td><?= htmlspecialchars($row['GovName'] ?? 'غير محدد') ?></td>
                            <td><?= htmlspecialchars($row['EdaraName'] ?? 'غير محدد') ?></td>
                            <td><strong><?= htmlspecialchars($row['SchoolName']) ?></strong></td>
                            <td>
                                <span class="status-badge <?= $statusClass ?>">
                                    <?= $statusText ?>
                                </span>
                            </td>
                            <td><?= $row['school_type'] ?? '-' ?></td>
                            <td><?= $row['study_period'] ?? '-' ?></td>
                            <td><?= $row['environment'] ?? '-' ?></td>
                            <td><?= isset($row['has_electricity']) ? ($row['has_electricity'] ? '✅' : '❌') : '-' ?></td>
                            <td><?= isset($row['has_internet']) ? ($row['has_internet'] ? '✅' : '❌') : '-' ?></td>
                            <td>
                                <span class="badge-data">
                                    <?= ($row['female_teachers'] ?? 0) + ($row['male_teachers'] ?? 0) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge-data">
                                    <?= $row['total_students'] ?? 0 ?>
                                </span>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                        <?php if (!$hasData): ?>
                        <tr>
                            <td colspan="12" class="text-center py-4">
                                <i class="fas fa-info-circle me-2"></i>
                                لا توجد بيانات تطابق الفلتر المحدد
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>