<?php
// منع الدخول المباشر بدون مرجع (Referer) كما في كودك الأصلي
if(!isset($_SERVER['HTTP_REFERER'])){
    header('location:../index.php');
    exit;
}

include_once('head.php'); 
include_once('../view/header_dpt.php'); 
include_once('sidebar_dpt.php'); 
include_once('alert.php'); 
include_once('../controller/config.php');

// --- منطق البحث ---
$show_results = false;
$search_error = "";

if(isset($_GET['search'])) {
    $search_val = mysqli_real_escape_string($conn, $_GET['search_val']);
    
    // البحث برقم التسجيل أو اللقب
    $sql = "SELECT * FROM etudiants WHERE nins='$search_val' OR noma LIKE '%$search_val%' LIMIT 1";
    $result = mysqli_query($conn, $sql);
    
    if(mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $nins = $row['nins'];
        $nom = $row['nom'];
        $prenom = $row['prenom'];
        $noma = $row['noma'];
        $prenoma = $row['prenoma'];
        $speId = $row['speId'];
        $libelle_niveau = $row['libelle_niveau'];
        $image = isset($row['image']) ? $row['image'] : 'default.png'; // افتراضي إذا لم توجد صورة
        $show_results = true;
    } else {
        $search_error = "لم يتم العثور على طالب بهذا الرقم أو الاسم.";
    }
}
?>

<style>
/* التنسيقات الخاصة بك */
.form-control-feedback { pointer-events: auto; }
.set-width-tooltip + .tooltip > .tooltip-inner { min-width:180px; }
.msk-fade { animation: animatetop 0.5s; }
@keyframes animatetop { from {top:-300px; opacity:0} to {top:0; opacity:1} }
.tHead { height:40px; background-color:#8e1c82; color:#FFF; text-align:center; border:1px solid #FFF; }
.cal-number-td { border:1px solid white; width:70px; background-color:#677be2; color:white; }
.search-box { margin: 20px 0; padding: 20px; background: #f4f4f4; border-radius: 8px; border: 1px solid #ddd; }
</style>

<div class="content-wrapper">
    <section class="content-header">
        <h1>لوحة التحكم <small>البحث عن غيابات الطلاب</small></h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="search-box">
                    <form method="GET" action="">
                        <div class="input-group input-group-lg">
                            <input type="text" name="search_val" class="form-control" placeholder="أدخل رقم التسجيل أو لقب الطالب..." required value="<?php echo isset($_GET['search_val']) ? $_GET['search_val'] : ''; ?>">
                            <span class="input-group-btn">
                                <button type="submit" name="search" class="btn btn-primary btn-flat">بحث!</button>
                            </span>
                        </div>
                    </form>
                    <?php if($search_error): ?>
                        <div class="text-danger" style="margin-top:10px;"><b>⚠️ <?php echo $search_error; ?></b></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <?php if($show_results): ?>
        <div class="row">
            <div class="col-md-12">
                <h5><?php echo $nom.' '.$prenom; ?>, <strong><span style="color:#cf4ed4;"> معلومات الطالب المستعلم عنه </span></strong></h5>
                
                <div class="panel panel-primary">
                    <div class="panel-heading bg-aqua-active">
                        <h5 class="panel-title" align="center">تفاصيل الملف الشخصي</h5>
                    </div>
                    <div class="panel-body">
                        <div class="col-md-6 col-md-offset-3">
                            <table class="table table-bordered table-striped">
                                <tbody>
                                    <tr style="background-color: #D1FAFF; border-style:dotted;">
                                        <th>
                                            <h3 align="right">السنة الجامعية 2025-2026</h3>
                                            <h3 align="right">المعلومات الشخصية</h3>
                                        </th>
                                        <th style="text-align:right">
                                            <img id="photo2" src="../<?php echo $image; ?>" class="img-circle img-responsive" style="width:100px; height:100px; float:left;">
                                        </th>
                                    </tr>
                                    <tr style="background-color: #D1FAFF; border-style:dotted;">
                                        <td align="center"><span style="font-size: large;"><?php echo $nins; ?></span></td>
                                        <td align="center"><b>رقم التسجيل</b></td>
                                    </tr>
                                    <tr style="background-color: #D1FAFF; border-style:dotted;">
                                        <td align="center"><span style="font-size: large;"><?php echo $prenoma; ?></span></td>
                                        <td align="center"><b>الإسم</b></td>
                                    </tr>
                                    <tr style="background-color: #D1FAFF; border-style:dotted;">
                                        <td align="center"><span style="font-size: large;"><?php echo $noma; ?></span></td>
                                        <td align="center"><b>اللقب</b></td>
                                    </tr>
                                    <tr style="background-color: #D1FAFF; border-style:dotted;">
                                        <td align="center"><span style="font-size: large;"><?php echo $speId; ?></span></td>
                                        <td align="center"><b>التخصص</b></td>
                                    </tr>
                                    <tr style="background-color: #D1FAFF; border-style:dotted;">
                                        <td align="center"><span style="font-size: large;"><?php echo $libelle_niveau; ?></span></td>
                                        <td align="center"><b>المستوى</b></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="panel panel-primary">
                    <div class="panel-body text-center">
                        <h3 style="color:#2c3e50;"><b>إجمالي الغيابات حسب المقياس</b></h3>
                        <div class="row">
                            <div class="col-md-8 col-md-offset-2">
                                <ul class="list-group">
                                    <?php
                                    $summary_query = mysqli_query($conn, "SELECT subject, COUNT(*) as total FROM attendance WHERE registration_number = '$nins' GROUP BY subject");
                                    if (mysqli_num_rows($summary_query) > 0) {
                                        while ($sum = mysqli_fetch_array($summary_query)) {
                                            $label_class = ($sum['total'] >= 3) ? 'label-danger' : 'label-warning';
                                            $word = ($sum['total'] == 1) ? 'غياب' : 'غيابات';
                                            echo '<li class="list-group-item text-right" style="font-size: 16px;">';
                                            echo 'مقياس <b>' . $sum['subject'] . '</b>: ';
                                            echo '<span class="label ' . $label_class . '" style="float: left; font-size: 14px;">' . $sum['total'] . ' ' . $word . '</span>';
                                            echo '</li>';
                                        }
                                    } else {
                                        echo '<div class="alert alert-success">لا توجد غيابات مسجلة لهذا الطالب</div>';
                                    }
                                    ?>
                                </ul>
                            </div>
                        </div>
                        
                        <hr>
                        <h3><b>سجل الغيابات المفصل</b></h3>
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped table-hover">
                                <thead style="background-color: #337ab7; color: white;">
                                    <tr>
                                        <th>الملاحظات</th>
                                        <th>الحالة</th>
                                        <th>التاريخ</th>
                                        <th>الحصة</th>
                                        <th>الأستاذ</th>
                                        <th>المقياس</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $query = mysqli_query($conn, "SELECT * FROM attendance WHERE registration_number = '$nins' ORDER BY session_date DESC");
                                    if (mysqli_num_rows($query) > 0) {
                                        while ($row_att = mysqli_fetch_array($query)) {
                                            echo "<tr>";
                                            echo "<td>" . $row_att['notes'] . "</td>";
                                            echo "<td style='color:red;'>غائب</td>";
                                            echo "<td>" . $row_att['session_date'] . "</td>";
                                            echo "<td>" . (isset($row_att['session_type']) ? $row_att['session_type'] : 'TD') . "</td>";
                                            echo "<td>" . $row_att['professor_name'] . "</td>";
                                            echo "<td>" . $row_att['subject'] . "</td>";
                                            echo "</tr>";
                                        }
                                    } else {
                                        echo "<tr><td colspan='6'>لا توجد بيانات مفصلة</td></tr>";
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </section>
</div>

<?php include_once('footer.php'); ?>