<?php
if (session_status() == PHP_SESSION_NONE) { session_start(); }
include_once('../controller/config.php');

// 1. جلب بيانات المستخدم المسجل (من جدول teacher بناءً على الصور السابقة)
$my_index = $_SESSION["index_number"];
$sql_user = "SELECT * FROM teacher WHERE index_number='$my_index'";
$res_user = mysqli_query($conn, $sql_user);
$u_row = mysqli_fetch_assoc($res_user);

// حماية في حال فشل الاستعلام أو كان المستخدم غير موجود
$user_name = isset($u_row['i_name']) ? $u_row['i_name'] : "Utilisateur";
$user_img  = (isset($u_row['image_name']) && !empty($u_row['image_name'])) ? $u_row['image_name'] : "dist/img/avatar5.png";

// 2. جلب عدد مواعيد اليوم كإشعارات (تصحيح اسم الحقل ليكون app_date)
$sql_notif = "SELECT COUNT(*) as total FROM clinic_appointments WHERE app_date = CURDATE()";
$res_notif = mysqli_query($conn, $sql_notif);
$row_notif = mysqli_fetch_assoc($res_notif);
$notif_count = $row_notif['total'];
?>

<header class="main-header">
    <a href="dashboard_med.php" class="logo">
        <span class="logo-mini"><strong>ENS</strong></span>
        <span class="logo-lg"><strong>ENS-Sétif</strong> | Santé</span>
    </a>

    <nav class="navbar navbar-static-top">
        <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
            <span class="sr-only">Toggle navigation</span>
        </a>

        <div class="navbar-custom-menu">
            <ul class="nav navbar-nav">
                
                <li class="dropdown notifications-menu">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                        <i class="fa fa-bell-o"></i>
                        <?php if($notif_count > 0): ?>
                            <span class="label label-warning"><?php echo $notif_count; ?></span>
                        <?php endif; ?>
                    </a>
                    <ul class="dropdown-menu">
                        <li class="header">Vous avez <?php echo $notif_count; ?> rendez-vous aujourd'hui</li>
                        <li>
                            <ul class="menu">
                                <li>
                                    <a href="clinic_appointments.php">
                                        <i class="fa fa-calendar text-aqua"></i> Consulter le planning du jour
                                    </a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>

                <li class="dropdown user user-menu">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                        <img src="../<?php echo $user_img; ?>" class="user-image" alt="User Image">
                        <span class="hidden-xs"><?php echo $user_name; ?></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li class="user-header">
                            <img src="../<?php echo $user_img; ?>" class="img-circle" alt="User Image">
                            <p>
                                <?php echo $user_name; ?>
                                <small>Service Médical ENS</small>
                            </p>
                        </li>
                        <li class="user-footer">
                            <div class="pull-left">
                                <a href="profile.php" class="btn btn-default btn-flat">Profil</a>
                            </div>
                            <div class="pull-right">
                                <a href="../logout.php" class="btn btn-default btn-flat">Déconnexion</a>
                            </div>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </nav>
</header>