<?php
/*
  parent_dashboard.php
  نسخة إصلاح كاملة لخطأ HTTP ERROR 500
  متوافقة مع PHP قديم:
  - لا تستخدم ??
  - لا تستخدم []
  - لا تستخدم mysqli_stmt_get_result()
  - تفحص الجداول والأعمدة قبل أي استعلام
*/

session_start();

if (!isset($_SESSION['parent_id']) || !isset($_SESSION['parent_student_id'])) {
    header('Location: parent_login.php');
    exit();
}

require_once __DIR__ . '/parent_portal_common.php';

$conn = pp_connect();
parent_portal_install($conn);

$student_id = intval($_SESSION['parent_student_id']);
$student = pp_fetch_student($conn, $student_id);

if (!$student) {
    die("<div style='font-family:Cairo,Arial;direction:rtl;padding:25px;color:#991b1b'>لم يتم العثور على بيانات الطالب.</div>");
}

$student_name = pp_value($student, 'name');
$stage = pp_value($student, 'stage');
$class = pp_value($student, 'class');

$stage_s = stage_short($stage);
$stage_safe = mysqli_real_escape_string($conn, $stage);
$stage_short_safe = mysqli_real_escape_string($conn, $stage_s);
$class_safe = mysqli_real_escape_string($conn, $class);

/* الدرجات */
$grades = false;
$has_final_exam = false;

if (table_exists($conn, 'grades')) {
    $order = "subject ASC";
    if (column_exists($conn, 'grades', 'saved_at')) {
        $order = "subject ASC, saved_at DESC";
    }

    $grades = mysqli_query($conn, "SELECT * FROM grades WHERE student_id='$student_id' ORDER BY $order");
    $has_final_exam = column_exists($conn, 'grades', 'final_exam');
}

/* الحضور */
$att = array(
    'total' => 0,
    'present' => 0,
    'late' => 0,
    'absent' => 0,
    'excused' => 0
);

$attendance_rows = false;

if (
    table_exists($conn, 'attendance_daily') &&
    column_exists($conn, 'attendance_daily', 'student_id') &&
    column_exists($conn, 'attendance_daily', 'status')
) {
    $att_q = mysqli_query($conn, "
        SELECT
            COUNT(*) AS total,
            SUM(CASE WHEN status='present' THEN 1 ELSE 0 END) AS present_count,
            SUM(CASE WHEN status='late' THEN 1 ELSE 0 END) AS late_count,
            SUM(CASE WHEN status='absent' THEN 1 ELSE 0 END) AS absent_count,
            SUM(CASE WHEN status='excused' THEN 1 ELSE 0 END) AS excused_count
        FROM attendance_daily
        WHERE student_id='$student_id'
    ");

    if ($att_q && mysqli_num_rows($att_q) > 0) {
        $a = mysqli_fetch_assoc($att_q);
        $att = array(
            'total' => intval($a['total']),
            'present' => intval($a['present_count']),
            'late' => intval($a['late_count']),
            'absent' => intval($a['absent_count']),
            'excused' => intval($a['excused_count'])
        );
    }

    $order_att = "id DESC";
    if (column_exists($conn, 'attendance_daily', 'att_date')) {
        $order_att = "att_date DESC, id DESC";
    }

    $attendance_rows = mysqli_query($conn, "
        SELECT *
        FROM attendance_daily
        WHERE student_id='$student_id'
        ORDER BY $order_att
        LIMIT 10
    ");
}

/* الواجبات */
$homeworks = false;

if (
    table_exists($conn, 'homeworks') &&
    column_exists($conn, 'homeworks', 'stage')
) {
    $class_col = '';
    if (column_exists($conn, 'homeworks', 'class')) {
        $class_col = '`class`';
    } elseif (column_exists($conn, 'homeworks', 'class_name')) {
        $class_col = 'class_name';
    }

    if ($class_col != '') {
        $homework_order = "id DESC";
        if (column_exists($conn, 'homeworks', 'created_at')) {
            $homework_order = "created_at DESC, id DESC";
        }

        $homeworks = mysqli_query($conn, "
            SELECT *
            FROM homeworks
            WHERE (stage='$stage_safe' OR stage='$stage_short_safe')
            AND ($class_col='$class_safe' OR $class_col='جميع الشعب' OR $class_col='all' OR $class_col='')
            ORDER BY $homework_order
            LIMIT 10
        ");
    }
}

/* المادة الداخلة */
$exam_topics = false;

if (
    table_exists($conn, 'exam_topics') &&
    column_exists($conn, 'exam_topics', 'stage')
) {
    $topic_class_col = '';
    if (column_exists($conn, 'exam_topics', 'class')) {
        $topic_class_col = '`class`';
    } elseif (column_exists($conn, 'exam_topics', 'class_name')) {
        $topic_class_col = 'class_name';
    }

    if ($topic_class_col != '') {
        $topic_order = "id DESC";
        if (column_exists($conn, 'exam_topics', 'created_at')) {
            $topic_order = "created_at DESC, id DESC";
        }

        $exam_topics = mysqli_query($conn, "
            SELECT *
            FROM exam_topics
            WHERE (stage='$stage_safe' OR stage='$stage_short_safe')
            AND ($topic_class_col='$class_safe' OR $topic_class_col='جميع الشعب' OR $topic_class_col='all' OR $topic_class_col='')
            ORDER BY $topic_order
            LIMIT 10
        ");
    }
}

/* إشعارات الإدارة */
$notifications = false;

if (table_exists($conn, 'parent_notifications')) {
    $notifications = mysqli_query($conn, "
        SELECT *
        FROM parent_notifications
        WHERE is_active=1
        AND (
            target_type='all'
            OR (target_type='student' AND student_id='$student_id')
            OR (target_type='stage' AND (stage='$stage_safe' OR stage='$stage_short_safe'))
            OR (target_type='class' AND (stage='$stage_safe' OR stage='$stage_short_safe') AND class_name='$class_safe')
        )
        ORDER BY id DESC
        LIMIT 10
    ");
}

/* جداول الامتحانات */
$exam_schedules = false;

if (table_exists($conn, 'parent_exam_schedules')) {
    $exam_order = "id DESC";
    if (column_exists($conn, 'parent_exam_schedules', 'exam_date')) {
        $exam_order = "exam_date ASC, id DESC";
    }

    $exam_schedules = mysqli_query($conn, "
        SELECT *
        FROM parent_exam_schedules
        WHERE (stage='$stage_safe' OR stage='$stage_short_safe')
        AND (
            class_name='$class_safe'
            OR class_name=''
            OR class_name IS NULL
            OR class_name='all'
            OR class_name='جميع الشعب'
        )
        ORDER BY $exam_order
        LIMIT 10
    ");
}

function row_get($row, $key) {
    if (is_array($row) && isset($row[$key])) {
        return $row[$key];
    }
    return '';
}

function attendance_status_ar($status) {
    if ($status == 'present') return 'حاضر';
    if ($status == 'absent') return 'غائب';
    if ($status == 'late') return 'متأخر';
    if ($status == 'excused') return 'غياب بعذر';
    return $status;
}
?>
<!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://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet">
<style>
:root{
    --navy:#0b2d4d;
    --navy2:#143f68;
    --dark:#071828;
    --gold:#d7a328;
    --text:#132238;
    --muted:#64748b;
    --line:#e2e8f0;
    --white:#fff;
    --glass:rgba(255,255,255,.84);
}
*{box-sizing:border-box}
html{scroll-behavior:smooth}
body{
    margin:0;
    font-family:Cairo,Arial;
    background:
        radial-gradient(circle at 12% 8%,rgba(215,163,40,.18),transparent 24%),
        radial-gradient(circle at 88% 15%,rgba(11,45,77,.18),transparent 24%),
        linear-gradient(135deg,#f8fafc,#edf4fb,#f7f2e8);
    color:var(--text);
}
.container{width:95%;max-width:1320px;margin:22px auto 40px}
.hero{
    position:relative;
    overflow:hidden;
    background:linear-gradient(135deg,rgba(7,24,40,.98),rgba(11,45,77,.94));
    border-radius:34px;
    padding:24px;
    color:#fff;
    box-shadow:0 28px 80px rgba(12,25,46,.22);
}
.hero:before{
    content:'';
    position:absolute;
    width:300px;
    height:300px;
    border-radius:50%;
    background:rgba(215,163,40,.16);
    top:-120px;
    left:-100px;
}
.hero:after{
    content:'';
    position:absolute;
    width:230px;
    height:230px;
    border-radius:50%;
    background:rgba(255,255,255,.06);
    bottom:-80px;
    right:-70px;
}
.hero-inner{
    position:relative;
    z-index:2;
    display:grid;
    grid-template-columns:110px 1fr auto;
    gap:20px;
    align-items:center;
}
.logo{
    width:100px;
    height:100px;
    background:#fff;
    border-radius:26px;
    padding:11px;
    border:3px solid rgba(215,163,40,.85);
    box-shadow:0 18px 40px rgba(0,0,0,.18);
}
.logo img{width:100%;height:100%;object-fit:contain}
.tag{
    display:inline-block;
    padding:8px 15px;
    border-radius:999px;
    background:rgba(215,163,40,.18);
    border:1px solid rgba(215,163,40,.4);
    color:#ffe4a1;
    font-weight:800;
    font-size:13px;
    margin-bottom:10px;
}
.hero h1{margin:0;font-size:32px;line-height:1.4}
.hero p{margin:8px 0 0;color:#dbeafe;line-height:1.9}
.hero-actions{display:flex;gap:10px;flex-wrap:wrap}
.logout,.ghost-btn{
    padding:12px 16px;
    border-radius:16px;
    text-decoration:none;
    font-weight:900;
}
.logout{background:#fff7df;color:#6e5207}
.ghost-btn{background:rgba(255,255,255,.12);border:1px solid rgba(255,255,255,.15);color:#fff}
.summary{
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:15px;
    margin:18px 0;
}
.sum-card{
    background:var(--glass);
    backdrop-filter:blur(15px);
    border:1px solid rgba(255,255,255,.7);
    border-radius:24px;
    padding:18px;
    box-shadow:0 18px 40px rgba(15,23,42,.08);
}
.sum-title{font-size:14px;color:var(--muted);font-weight:800}
.sum-value{font-size:32px;color:var(--navy);font-weight:900;margin-top:4px}
.student-card,.section-box{
    background:var(--white);
    border-radius:28px;
    padding:20px;
    box-shadow:0 18px 42px rgba(15,23,42,.07);
    margin-bottom:18px;
}
.section-box{display:none}
.section-box.active{display:block;animation:fadeIn .35s ease}
.section-head{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:12px;
    margin-bottom:14px;
}
.section-head h2{margin:0;color:var(--navy);font-size:24px}
.student-grid{
    display:grid;
    grid-template-columns:repeat(3,1fr);
    gap:12px;
}
.info{
    background:#f8fafc;
    border:1px solid var(--line);
    border-radius:18px;
    padding:14px;
}
.info span{display:block;color:var(--muted);font-size:13px;font-weight:800}
.info b{display:block;margin-top:4px;color:#132238}
.icon-menu{
    display:grid;
    grid-template-columns:repeat(4,1fr);
    gap:16px;
    margin-bottom:18px;
}
.menu-btn{
    position:relative;
    background:linear-gradient(180deg,#ffffff,#f7fbff);
    border:1px solid #ebf1f7;
    border-radius:26px;
    padding:18px 16px;
    text-align:center;
    cursor:pointer;
    transition:.25s;
    box-shadow:0 14px 32px rgba(15,23,42,.06);
}
.menu-btn:hover{transform:translateY(-4px);box-shadow:0 22px 38px rgba(15,23,42,.1)}
.menu-btn.active{background:linear-gradient(135deg,var(--navy),var(--navy2));border-color:transparent}
.menu-btn.active .mi-title,.menu-btn.active .mi-sub{color:#fff}
.mi-icon{
    width:76px;
    height:76px;
    border-radius:24px;
    margin:0 auto 12px;
    background:linear-gradient(135deg,#0b2d4d,#194f81);
    display:flex;
    align-items:center;
    justify-content:center;
    box-shadow:0 16px 28px rgba(11,45,77,.18);
}
.menu-btn.active .mi-icon{background:linear-gradient(135deg,#d7a328,#f2cc6d)}
.mi-icon svg{width:38px;height:38px}
.mi-title{font-size:18px;font-weight:900;color:var(--navy)}
.mi-sub{font-size:12px;color:var(--muted);margin-top:4px;line-height:1.7}
.notice{
    background:#fff7df;
    border:1px solid #f5d77b;
    color:#5f4206;
    border-radius:18px;
    padding:14px;
    margin-bottom:11px;
    line-height:1.9;
}
.empty{padding:20px;text-align:center;color:var(--muted);font-weight:800}
.table-wrap{overflow-x:auto;border-radius:20px;border:1px solid #edf2f7}
.portal-table{width:100%;border-collapse:collapse;min-width:880px;background:#fff}
.portal-table th,.portal-table td{
    padding:11px;
    text-align:center;
    border-bottom:1px solid #edf2f7;
    white-space:nowrap;
}
.portal-table th{background:var(--navy);color:#fff;font-size:14px}
.panel-chip{
    display:inline-block;
    background:#eef5ff;
    border:1px solid #d9e7f5;
    color:#25507c;
    padding:7px 12px;
    border-radius:999px;
    font-weight:800;
    font-size:12px;
}
.footer-badge{
    position:fixed;
    left:12px;
    bottom:12px;
    background:#071828;
    color:#f4d06f;
    padding:8px 12px;
    border-radius:999px;
    font-size:12px;
    font-weight:900;
    box-shadow:0 10px 25px rgba(0,0,0,.24);
}
@keyframes fadeIn{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:translateY(0)}}
@media(max-width:1100px){
    .icon-menu{grid-template-columns:repeat(2,1fr)}
    .summary{grid-template-columns:repeat(2,1fr)}
}
@media(max-width:900px){
    .hero-inner{grid-template-columns:1fr;text-align:center}
    .logo{margin:auto}
    .hero-actions{justify-content:center}
    .student-grid{grid-template-columns:1fr}
}
@media(max-width:650px){
    .icon-menu,.summary{grid-template-columns:1fr}
    .menu-btn{padding:16px}
    .mi-icon{width:66px;height:66px}
}
</style>
</head>
<body>

<div class="container">

    <div class="hero">
        <div class="hero-inner">
            <div class="logo">
                <img src="/sy/school_system/logo.png" alt="شعار المدرسة">
            </div>

            <div>
                <div class="tag">
                    مرحباً <?php echo h(isset($_SESSION['parent_name']) ? $_SESSION['parent_name'] : 'ولي الأمر'); ?>
                </div>
                <h1>لوحة ولي الأمر الحديثة</h1>
                <p>تابع بيانات الطالب، درجاته، جداول الامتحانات، الواجبات، وإشعارات الإدارة من واجهة حديثة ومرتبة وسهلة الاستخدام.</p>
            </div>

            <div class="hero-actions">
                <a class="ghost-btn" href="#student-info">بيانات الطالب</a>
                <a class="logout" href="parent_logout.php">تسجيل الخروج</a>
            </div>
        </div>
    </div>

    <div class="summary">
        <div class="sum-card">
            <div class="sum-title">أيام الحضور</div>
            <div class="sum-value"><?php echo $att['present']; ?></div>
        </div>

        <div class="sum-card">
            <div class="sum-title">عدد الغيابات</div>
            <div class="sum-value"><?php echo $att['absent']; ?></div>
        </div>

        <div class="sum-card">
            <div class="sum-title">مرات التأخير</div>
            <div class="sum-value"><?php echo $att['late']; ?></div>
        </div>

        <div class="sum-card">
            <div class="sum-title">غياب بعذر</div>
            <div class="sum-value"><?php echo $att['excused']; ?></div>
        </div>
    </div>

    <div id="student-info" class="student-card">
        <div class="section-head">
            <h2>معلومات الطالب</h2>
            <span class="panel-chip">حساب ولي الأمر</span>
        </div>

        <div class="student-grid">
            <div class="info">
                <span>اسم الطالب</span>
                <b><?php echo h($student_name); ?></b>
            </div>

            <div class="info">
                <span>المرحلة</span>
                <b><?php echo h($stage); ?></b>
            </div>

            <div class="info">
                <span>الشعبة</span>
                <b><?php echo h($class); ?></b>
            </div>
        </div>
    </div>

    <div class="icon-menu">

        <div class="menu-btn active" data-target="panel-exams">
            <div class="mi-icon">
                <svg viewBox="0 0 24 24" fill="none">
                    <path d="M7 3v4M17 3v4M4 9h16" stroke="white" stroke-width="1.8" stroke-linecap="round"/>
                    <rect x="4" y="5" width="16" height="16" rx="3" stroke="white" stroke-width="1.8"/>
                    <path d="M8 13h3v3H8z" fill="white"/>
                </svg>
            </div>
            <div class="mi-title">جداول الامتحانات</div>
            <div class="mi-sub">مواعيد وتفاصيل الامتحانات القادمة</div>
        </div>

        <div class="menu-btn" data-target="panel-topics-homework">
            <div class="mi-icon">
                <svg viewBox="0 0 24 24" fill="none">
                    <path d="M6 4h9l3 3v13H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z" stroke="white" stroke-width="1.7"/>
                    <path d="M8 10h8M8 14h8M8 18h5" stroke="white" stroke-width="1.7" stroke-linecap="round"/>
                </svg>
            </div>
            <div class="mi-title">المادة الداخلة والواجبات</div>
            <div class="mi-sub">كل ما يخص الواجبات والمواد المطلوبة</div>
        </div>

        <div class="menu-btn" data-target="panel-grades">
            <div class="mi-icon">
                <svg viewBox="0 0 24 24" fill="none">
                    <path d="M5 19V9" stroke="white" stroke-width="1.9" stroke-linecap="round"/>
                    <path d="M12 19V5" stroke="white" stroke-width="1.9" stroke-linecap="round"/>
                    <path d="M19 19v-7" stroke="white" stroke-width="1.9" stroke-linecap="round"/>
                    <path d="M3 19h18" stroke="white" stroke-width="1.9" stroke-linecap="round"/>
                </svg>
            </div>
            <div class="mi-title">درجات الطالب</div>
            <div class="mi-sub">عرض تفصيلي لكل الدرجات والنتائج</div>
        </div>

        <div class="menu-btn" data-target="panel-attendance">
            <div class="mi-icon">
                <svg viewBox="0 0 24 24" fill="none">
                    <path d="M12 8v4l3 2" stroke="white" stroke-width="1.8" stroke-linecap="round"/>
                    <circle cx="12" cy="12" r="9" stroke="white" stroke-width="1.7"/>
                    <path d="M7 3v3M17 3v3" stroke="white" stroke-width="1.7" stroke-linecap="round"/>
                </svg>
            </div>
            <div class="mi-title">آخر سجلات الحضور والغياب</div>
            <div class="mi-sub">آخر تحديثات الحضور اليومية للطالب</div>
        </div>

    </div>

    <div class="section-box active" id="panel-exams">
        <div class="section-head">
            <h2>جداول الامتحانات</h2>
            <span class="panel-chip">عرض من خلال الأيقونة</span>
        </div>

        <?php if($exam_schedules && mysqli_num_rows($exam_schedules) > 0) { ?>
            <div class="table-wrap">
                <table class="portal-table">
                    <tr>
                        <th>العنوان</th>
                        <th>المادة</th>
                        <th>التاريخ</th>
                        <th>الوقت</th>
                        <th>الملاحظات</th>
                    </tr>

                    <?php while($e = mysqli_fetch_assoc($exam_schedules)) { ?>
                        <tr>
                            <td><?php echo h(row_get($e, 'title')); ?></td>
                            <td><?php echo h(row_get($e, 'subject')); ?></td>
                            <td><?php echo h(row_get($e, 'exam_date')); ?></td>
                            <td><?php echo h(row_get($e, 'exam_time')); ?></td>
                            <td><?php echo h(row_get($e, 'notes')); ?></td>
                        </tr>
                    <?php } ?>
                </table>
            </div>
        <?php } else { ?>
            <div class="empty">لا توجد جداول امتحانات حالياً.</div>
        <?php } ?>
    </div>

    <div class="section-box" id="panel-topics-homework">
        <div class="section-head">
            <h2>المادة الداخلة والواجبات</h2>
            <span class="panel-chip">واجبات + مادة امتحان</span>
        </div>

        <?php if($exam_topics && mysqli_num_rows($exam_topics) > 0) { ?>
            <?php while($et = mysqli_fetch_assoc($exam_topics)) { ?>
                <div class="notice">
                    <b><?php echo h(row_get($et, 'teacher_subject')); ?> - <?php echo h(row_get($et, 'exam_type')); ?></b><br>
                    <?php echo nl2br(h(row_get($et, 'topics'))); ?><br>
                    <small><?php echo h(row_get($et, 'created_at')); ?></small>
                </div>
            <?php } ?>
        <?php } ?>

        <?php if($homeworks && mysqli_num_rows($homeworks) > 0) { ?>
            <?php while($hw = mysqli_fetch_assoc($homeworks)) { ?>
                <div class="notice">
                    <b><?php echo h(row_get($hw, 'subject')); ?> - واجب</b><br>
                    <?php echo nl2br(h(row_get($hw, 'homework'))); ?><br>
                    <small><?php echo h(row_get($hw, 'created_at')); ?></small>
                </div>
            <?php } ?>
        <?php } ?>

        <?php if((!$exam_topics || mysqli_num_rows($exam_topics) == 0) && (!$homeworks || mysqli_num_rows($homeworks) == 0)) { ?>
            <div class="empty">لا توجد واجبات أو مواد امتحان حالياً.</div>
        <?php } ?>
    </div>

    <div class="section-box" id="panel-grades">
        <div class="section-head">
            <h2>درجات الطالب</h2>
            <span class="panel-chip">عرض شامل للنتائج</span>
        </div>

        <div class="table-wrap">
            <table class="portal-table">
                <tr>
                    <th>المادة</th>
                    <th>ش1</th>
                    <th>ش2</th>
                    <th>ش3</th>
                    <th>يومي ف1</th>
                    <th>ف1</th>
                    <th>نصف السنة</th>
                    <th>ش1 ف2</th>
                    <th>ش2 ف2</th>
                    <th>ش3 ف2</th>
                    <th>يومي ف2</th>
                    <th>ف2</th>
                    <th>المعدل السنوي</th>
                    <th>امتحان آخر السنة</th>
                    <th>النتيجة النهائية</th>
                </tr>

                <?php if($grades && mysqli_num_rows($grades) > 0) { ?>
                    <?php while($g = mysqli_fetch_assoc($grades)) { ?>
                        <tr>
                            <td><?php echo h(row_get($g, 'subject')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'month1')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'month2')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'month3')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'daily1')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'semester1')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'midterm')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'month4')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'month5')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'month6')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'daily2')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'semester2')); ?></td>
                            <td><?php echo grade_val(row_get($g, 'annual_average')); ?></td>
                            <td><?php echo $has_final_exam ? grade_val(row_get($g, 'final_exam')) : '-'; ?></td>
                            <td><?php echo grade_val(row_get($g, 'final_result')); ?></td>
                        </tr>
                    <?php } ?>
                <?php } else { ?>
                    <tr>
                        <td colspan="15">لا توجد درجات مدخلة حالياً.</td>
                    </tr>
                <?php } ?>
            </table>
        </div>
    </div>

    <div class="section-box" id="panel-attendance">
        <div class="section-head">
            <h2>آخر سجلات الحضور والغياب</h2>
            <span class="panel-chip">آخر 10 سجلات</span>
        </div>

        <div class="table-wrap">
            <table class="portal-table">
                <tr>
                    <th>التاريخ</th>
                    <th>الحالة</th>
                    <th>وقت الدخول</th>
                    <th>وقت الخروج</th>
                    <th>ملاحظات</th>
                </tr>

                <?php if($attendance_rows && mysqli_num_rows($attendance_rows) > 0) { ?>
                    <?php while($ar = mysqli_fetch_assoc($attendance_rows)) { ?>
                        <tr>
                            <td><?php echo h(row_get($ar, 'att_date')); ?></td>
                            <td><?php echo h(attendance_status_ar(row_get($ar, 'status'))); ?></td>
                            <td><?php echo h(row_get($ar, 'checkin_at')); ?></td>
                            <td><?php echo h(row_get($ar, 'checkout_at')); ?></td>
                            <td><?php echo h(row_get($ar, 'notes')); ?></td>
                        </tr>
                    <?php } ?>
                <?php } else { ?>
                    <tr>
                        <td colspan="5">لا توجد سجلات حضور بعد.</td>
                    </tr>
                <?php } ?>
            </table>
        </div>
    </div>

    <div class="student-card">
        <div class="section-head">
            <h2>إشعارات الإدارة</h2>
            <span class="panel-chip">آخر الإشعارات</span>
        </div>

        <?php if($notifications && mysqli_num_rows($notifications) > 0) { ?>
            <?php while($n = mysqli_fetch_assoc($notifications)) { ?>
                <div class="notice">
                    <b><?php echo h(row_get($n, 'title')); ?></b><br>
                    <?php echo nl2br(h(row_get($n, 'body'))); ?><br>
                    <small><?php echo h(row_get($n, 'created_at')); ?></small>
                </div>
            <?php } ?>
        <?php } else { ?>
            <div class="empty">لا توجد إشعارات حالياً.</div>
        <?php } ?>
    </div>

</div>

<div class="footer-badge">PHP Legacy Safe 500 Fix</div>

<script>
(function(){
    var buttons = document.querySelectorAll('.menu-btn');
    var panels = document.querySelectorAll('.section-box');

    function activate(targetId){
        var i;

        for(i = 0; i < buttons.length; i++){
            if(buttons[i].getAttribute('data-target') == targetId){
                buttons[i].className = 'menu-btn active';
            } else {
                buttons[i].className = 'menu-btn';
            }
        }

        for(i = 0; i < panels.length; i++){
            if(panels[i].id == targetId){
                panels[i].className = 'section-box active';
            } else {
                panels[i].className = 'section-box';
            }
        }
    }

    var i;
    for(i = 0; i < buttons.length; i++){
        buttons[i].onclick = function(){
            activate(this.getAttribute('data-target'));
        };
    }
})();
</script>

</body>
</html>
