<?php

header('Content-Type: application/json');



$q    = trim($_GET['q'] ?? '');

$role = trim($_GET['role'] ?? 'admin');



if (strlen($q) < 2) {

    echo json_encode(['results' => []]);

    exit;

}



$ql = strtolower($q);



// ── Page indexes (sourced directly from each sidebar) ──────────────────────

// Format: [label, subtitle, url, icon, keywords[]]



$pages = [];



// ── sidebar.php → admin ────────────────────────────────────────────────────

$pages['admin'] = [

    ['Dashboard',             'Admin home',                   'dashboard.php',              'fas fa-tachometer-alt',      ['dashboard', 'home']],

    // Student Management

    ['Add Student',           'Register new student',         'add_student.php',            'fas fa-user-plus',           ['add', 'student', 'register', 'new']],

    ['Registration Form',     'Student registration form',    'registration2.php',          'fas fa-file-alt',            ['registration', 'form']],

    ['Manage Students',       'Student list',                 'student_list.php',           'fas fa-user-graduate',       ['manage', 'student', 'list']],

    ['Promote Students',      'Promote to next class',        'promote_students.php',       'fas fa-medal',               ['promote', 'student', 'class']],

    ['Demote Students',       'Demote a student',             'demote_students.php',        'fas fa-user-minus',          ['demote', 'student']],

    ['Registration Token',    'Generate reg tokens',          'registration_token.php',     'fas fa-id-card',             ['token', 'registration']],

    // Teacher Management

    ['Add Teacher',           'Register new teacher',         'add_teacher.php',            'fas fa-plus',                ['add', 'teacher']],

    ["Teacher's List",        'View all teachers',            'teacher_list.php',           'fas fa-list',                ['teacher', 'list']],

    ['Assign Teacher',        'Assign teacher to class',      'assign_teacher.php',         'fas fa-chalkboard-teacher',  ['assign', 'teacher']],

    ['Assignments List',      'Teacher assignments',          'teacher_assignments.php',    'fas fa-book-reader',         ['assignment', 'teacher']],

    // Result Management

    ['Approve Result',        'Approve student results',      'approve_result.php',         'fas fa-check-circle',        ['approve', 'result']],

    ['Edit Result',           'Edit student results',         'edit_result.php',            'fas fa-edit',                ['edit', 'result']],

    ['Release Date',          'Set result release date',      'publish_result.php',         'fas fa-calendar-check',      ['publish', 'release', 'result', 'date']],

    ['Result Broadsheet',     'Print class broadsheet',       'print_broadsheet_admin.php', 'fas fa-print',               ['broadsheet', 'print', 'result']],

    ['Previous Result',       'Past term results',            'previous_result1.php',       'fas fa-history',             ['previous', 'result', 'past']],

    ['Bulk Result (All)',      'Bulk download all results',    'bulk_result.php',            'fas fa-layer-group',         ['bulk', 'result', 'all']],

    ['Bulk Result (Paid)',     'Bulk download paid results',   'bulk_result_fp.php',         'fas fa-filter',              ['bulk', 'result', 'paid']],

    ['Result Analytics',      'Result reports & analytics',   'reports.php',                'fas fa-chart-bar',           ['analytics', 'result', 'report']],

    // Payment Management

    ['Approve Payment',       'Approve fee payments',         'approve.php',                'fas fa-check-double',        ['approve', 'payment']],

    ['View Payments',         'All payment records',          'viewpayment.php',            'fas fa-eye',                 ['view', 'payment', 'fees']],

    ['Payment Analytics',     'Payment overview',             'payment_analytics.php',      'fas fa-chart-line',          ['payment', 'analytics', 'finance']],

    ['Refunds',               'Payment refunds',              'view_refunds.php',           'fas fa-undo',                ['refund', 'payment']],

    // Fees Management

    ['Create Fees',           'Add new fee type',             'addfees.php',                'fas fa-plus-circle',         ['create', 'fees', 'add']],

    ['Manage Fees',           'Edit fee structures',          'feelist.php',                'fas fa-list-alt',            ['manage', 'fees', 'list']],

    // Class Management

    ['Add Class & Arm',       'Create class and section',     'add_class_section.php',      'fas fa-plus-square',         ['add', 'class', 'arm', 'section']],

    ['Manage Class & Arm',    'Edit classes and sections',    'manage_class_section.php',   'fas fa-cogs',                ['manage', 'class', 'arm', 'section']],

    ['Manage Subject',        'Add or edit subjects',         'add_subject.php',            'fas fa-book',                ['subject', 'manage']],

    // Events

    ['Announcement',          'Post announcements',           'announcement.php',           'fas fa-bullhorn',            ['announcement', 'notice']],

    ['Add Event',             'Create calendar event',        'add_event.php',              'fas fa-calendar-plus',       ['add', 'event', 'calendar']],

    ['Academic Calendar',     'View academic calendar',       'calendar.php',               'fas fa-calendar-alt',        ['calendar', 'academic']],

    ['Newsletter (Groups)',   'Send group newsletter',        'newsletterstd.php',          'fas fa-users',               ['newsletter', 'group', 'email']],

    ['Newsletter (Special)',  'Send special newsletter',      'newslettersp.php',           'fas fa-user-tag',            ['newsletter', 'special', 'email']],

    ['Email Logs',            'View sent email logs',         'email_log.php',              'fas fa-envelope-open-text',  ['email', 'log']],

    // Tracking

    ['Track Submissions',     'Track result submissions',     'admin_track_submissions.php','fas fa-route',               ['track', 'submission']],

    ['Attendance Monitor',    'Monitor attendance',           'attendance_monitor.php',     'fas fa-user-check',          ['attendance', 'monitor']],

    ['Ratings',               'Teacher ratings',              'rating_monitor.php',         'fas fa-star',                ['rating', 'monitor']],

    ['Approval Tracking',     'Track approvals',              'approval_tracking.php',      'fas fa-tasks',               ['approval', 'track']],

    // Other

    ['Check Email',           'Admin inbox',                  'admin_emails.php',           'fas fa-envelope',            ['email', 'inbox', 'check']],

    ['Session Management',    'Manage academic sessions',     'manage_session.php',         'fas fa-calendar-alt',        ['session', 'term', 'academic']],

    ['Mid-Term Transcript',   'Mid-term transcript',          'transcript_test.php',        'fas fa-file-medical-alt',    ['transcript', 'midterm']],

    ['Full Transcript',       'Full term transcript',         'transcript.php',             'fas fa-file-alt',            ['transcript', 'full']],

    ['Admission Letter',      'Send admission letters',       'sendadmissionletter.php',    'fas fa-envelope-open',       ['admission', 'letter']],

    ['Generate ID Card',      'Print student ID cards',       'idcard.php',                 'fas fa-id-card',             ['id', 'card', 'generate']],

    ['Upload Documents',      'Upload school documents',      'add_document.php',           'fas fa-upload',              ['upload', 'document']],

    ['Manage Documents',      'Manage school documents',      'document_list.php',          'fas fa-folder-tree',         ['manage', 'document']],

    ['Manage Discount',       'Edit fee discounts',           'manage_discount.php',        'fas fa-cog',                 ['discount', 'manage']],

    ['Add Discount',          'Create new discount',          'add_discount.php',           'fas fa-tag',                 ['discount', 'add']],

    // Security

    ['Register User',         'Create portal user account',   'userregister.php',           'fas fa-user-plus',           ['register', 'user', 'account']],

    ['Audit Logs',            'View system audit logs',       'auditlog.php',               'fas fa-file-alt',            ['audit', 'log']],

    ['Settings',              'Change password / settings',   'changepassword.php',         'fas fa-tools',               ['settings', 'password', 'change']],

];



// ── sidebar2.php → admin2 ──────────────────────────────────────────────────

$pages['admin2'] = [

    ['Dashboard',             'Mini admin home',              'dashboard1.php',             'fas fa-tachometer-alt',      ['dashboard', 'home']],

    // Student Management

    ['Add Student',           'Register new student',         'add_student1.php',           'fas fa-user-plus',           ['add', 'student']],

    ['Registration Form',     'Student registration form',    'registration21.php',         'fas fa-file-alt',            ['registration', 'form']],

    ['Manage Students',       'Student list',                 'student_list1.php',          'fas fa-user-graduate',       ['manage', 'student', 'list']],

    // Teacher Management

    ['Add Teacher',           'Register new teacher',         'add_teacher1.php',           'fas fa-plus',                ['add', 'teacher']],

    ["Teacher's List",        'View all teachers',            'teacher_list1.php',          'fas fa-list',                ['teacher', 'list']],

    ['Assign Teacher',        'Assign teacher to class',      'assign_teacher1.php',        'fas fa-chalkboard-teacher',  ['assign', 'teacher']],

    // Payment Management

    ['Payment Request (RS)',  'Registered student payment',   'pay_tuition1.php',           'fas fa-file-invoice-dollar', ['payment', 'request', 'registered']],

    ['Payment Request (NRS)', 'Non-registered student pay',   'pay_tuition11.php',          'fas fa-receipt',             ['payment', 'request', 'non-registered']],

    ['View Payment',          'All payment records',          'viewpayment1.php',           'fas fa-eye',                 ['view', 'payment']],

    // Events

    ['Add Event',             'Create calendar event',        'add_event1.php',             'fas fa-calendar-plus',       ['add', 'event']],

    ['Academic Calendar',     'View academic calendar',       'calendar2.php',              'fas fa-calendar-alt',        ['calendar', 'academic']],

    // Tracking

    ['Track Submissions',     'Track result submissions',     'admin_track_submissions1.php','fas fa-route',              ['track', 'submission']],

    ['Attendance',            'Monitor attendance',           'attendance_monitor1.php',    'fas fa-user-check',          ['attendance', 'monitor']],

    ['Ratings',               'Teacher ratings',              'rating_monitor1.php',        'fas fa-star-half-alt',       ['rating', 'monitor']],

    // Documents

    ['Upload Documents',      'Upload school documents',      'add_document1.php',          'fas fa-upload',              ['upload', 'document']],

    ['Manage Documents',      'Manage school documents',      'document_list1.php',         'fas fa-folder-tree',         ['manage', 'document']],

    // Security

    ['Settings',              'Change password / settings',   'changepassword1.php',        'fas fa-tools',               ['settings', 'password']],

];



// ── sidebar3.php → admin3 ──────────────────────────────────────────────────

$pages['admin3'] = [

    ['Dashboard',             'Search admin home',            'dashboard2.php',             'fas fa-tachometer-alt',      ['dashboard', 'home']],

    ['Manage Students',       'Student list',                 'student_list11.php',         'fas fa-user-graduate',       ['manage', 'student', 'list']],

    ['Manage Teachers',       'Teacher list',                 'teacher_list11.php',         'fas fa-user-tie',            ['manage', 'teacher', 'list']],

    ['Settings',              'Account settings',             '#',                          'fas fa-tools',               ['settings']],

];



// ── sidebaroldstd.php → oldstd (admitted day student) ─────────────────────

$pages['oldstd'] = [

    ['Dashboard',             'Student home',                 'oldstdday.php',              'fas fa-gauge',               ['dashboard', 'home']],

    ['Pay School Fees',       'Pay tuition fees',             'redirectday.php',            'fas fa-money-check-dollar',  ['pay', 'school', 'fees', 'tuition']],

    ['Pay Other Fees',        'Pay miscellaneous fees',       'oldstddaypayother.php',      'fas fa-coins',               ['pay', 'other', 'fees']],

    ['Payment Log',           'View payment history',         'recentoldday2.php',          'fas fa-list',                ['payment', 'log', 'history']],

    ['Payment Receipt',       'Download payment receipt',     'recentoldday.php',           'fas fa-receipt',             ['payment', 'receipt']],

    ['My Subjects',           'View my subjects',             'view_subjects.php',          'fas fa-book-open',           ['subjects', 'my', 'courses']],

    ['Mid-Term Result',       'View mid-term result',         'test_result.php',            'fas fa-clipboard-check',     ['midterm', 'result', 'test']],

    ['End-of-Term Result',    'View end-of-term result',      'verify_email_otp.php',       'fas fa-award',               ['result', 'end', 'term', 'exam']],

    ['Prev. Mid-Term Results','Previous mid-term results',    'test_result1.php',           'fas fa-clipboard-check',     ['previous', 'midterm', 'result']],

    ['Prev. Exam Results',    'Previous exam results',        'verify_email_otp1.php',      'fas fa-award',               ['previous', 'exam', 'result']],

    ['View Attendance',       'My attendance record',         'view_attendance.php',        'fas fa-calendar-days',       ['attendance', 'view']],

    ['Download Document',     'Download school documents',    'downloaddocumentoldday.php', 'fas fa-download',            ['download', 'document']],

    ['Profile',               'My student profile',          'stdprofileoldday.php',       'fas fa-user',                ['profile', 'account']],

    ['Settings',              'Change password',              'changepasswordoldstd.php',   'fas fa-gear',                ['settings', 'password', 'change']],

];



// ── sidebaroldstdboard.php → oldstdboard (admitted boarding student) ───────

$pages['oldstdboard'] = [

    ['Dashboard',             'Student home',                 'oldstdboard.php',            'fas fa-gauge',               ['dashboard', 'home']],

    ['Pay School Fees',       'Pay tuition fees',             'redirectboard.php',          'fas fa-money-check-dollar',  ['pay', 'school', 'fees', 'tuition']],

    ['Pay Other Fees',        'Pay miscellaneous fees',       'oldstdboardpayother.php',    'fas fa-coins',               ['pay', 'other', 'fees']],

    ['Payment Log',           'View payment history',         'recentoldboard2.php',        'fas fa-list',                ['payment', 'log', 'history']],

    ['Payment Receipt',       'Download payment receipt',     'recentoldboard.php',         'fas fa-receipt',             ['payment', 'receipt']],

    ['My Subjects',           'View my subjects',             'view_subjects_b.php',        'fas fa-book-open',           ['subjects', 'my', 'courses']],

    ['Mid-Term Result',       'View mid-term result',         'test_result_b.php',          'fas fa-clipboard-check',     ['midterm', 'result', 'test']],

    ['End-of-Term Result',    'View end-of-term result',      'verify_email_otp_b.php',     'fas fa-award',               ['result', 'end', 'term', 'exam']],

    ['Prev. Mid-Term Results','Previous mid-term results',    'test_result_b1.php',         'fas fa-clipboard-check',     ['previous', 'midterm', 'result']],

    ['Prev. Exam Results',    'Previous exam results',        'verify_email_otp_b1.php',    'fas fa-award',               ['previous', 'exam', 'result']],

    ['View Attendance',       'My attendance record',         'view_attendance_b.php',      'fas fa-calendar-days',       ['attendance', 'view']],

    ['Download Document',     'Download school documents',    'downloaddocumentoldboard.php','fas fa-download',           ['download', 'document']],

    ['Profile',               'My student profile',          'stdprofileoldboard.php',     'fas fa-user',                ['profile', 'account']],

    ['Settings',              'Change password',              'changepasswordoldstdb.php',  'fas fa-gear',                ['settings', 'password', 'change']],

];



// ── sidebarstd.php → newstd (new day student with reg number) ─────────────

$pages['newstd'] = [

    ['Dashboard',             'Student home',                 'newstdday.php',              'fas fa-gauge',               ['dashboard', 'home']],

    ['Pay School Fees',       'Pay tuition fees',             'payschoolfeesnew.php',       'fas fa-money-check-dollar',  ['pay', 'school', 'fees', 'tuition']],

    ['Payment Log',           'View payment history',         'recentnewday2.php',          'fas fa-list',                ['payment', 'log', 'history']],

    ['Payment Receipt',       'Download payment receipt',     'recentnewday.php',           'fas fa-receipt',             ['payment', 'receipt']],

    ['Download Document',     'Download school documents',    'downloaddocument.php',       'fas fa-download',            ['download', 'document']],

    ['Profile',               'My student profile',          'stdprofile.php',             'fas fa-user',                ['profile', 'account']],

];



// ── sidebarstdboard.php → newstdboard (new boarding student) ──────────────

$pages['newstdboard'] = [

    ['Dashboard',             'Student home',                 'newstdboard.php',            'fas fa-gauge',               ['dashboard', 'home']],

    ['Pay School Fees',       'Pay tuition fees',             'payschoolfeesnewb.php',      'fas fa-money-check-dollar',  ['pay', 'school', 'fees', 'tuition']],

    ['Payment Log',           'View payment history',         'recentnewboard2.php',        'fas fa-list',                ['payment', 'log', 'history']],

    ['Payment Receipt',       'Download payment receipt',     'recentnewboard.php',         'fas fa-receipt',             ['payment', 'receipt']],

    ['Download Document',     'Download school documents',    'downloaddocumentnewboard.php','fas fa-download',           ['download', 'document']],

    ['Profile',               'My student profile',          'stdprofilenewboard.php',     'fas fa-user',                ['profile', 'account']],

];



// ── sidebart.php → teacher ─────────────────────────────────────────────────

$pages['teacher'] = [

    ['Dashboard',             'Teacher home',                 'teacher_dashboard.php',      'fas fa-tachometer-alt',      ['dashboard', 'home']],

    ['Previous Result',       'Past term results',            'previous_result.php',        'fas fa-history',             ['previous', 'result', 'past']],

    ['Upload Material',       'Upload subject material',      'add_document_teacher.php',   'fas fa-upload',              ['upload', 'material', 'document']],

    ['Manage Material',       'Manage subject material',      'document_list_teacher.php',  'fas fa-folder-tree',         ['manage', 'material', 'document']],

    ['Settings',              'Change password',              'changepassword_teacher.php', 'fas fa-tools',               ['settings', 'password', 'change']],

    ['My Profile',            'Teacher profile',              'profile_teacher.php',        'fas fa-user-circle',         ['profile', 'account']],

];



// ── entrancesidebar.php → entrance ────────────────────────────────────────

$pages['entrance'] = [

    ['Dashboard',             'Entrance portal home',         'entrancedashboard.php',      'fas fa-tachometer-alt',      ['dashboard', 'home']],

    ['Exit',                  'Return to main site',          'index.php',                  'fas fa-sign-out-alt',        ['exit', 'logout', 'leave']],

];



// ── Helpers ────────────────────────────────────────────────────────────────

function matchPages($pageList, $ql) {

    $matched = [];

    foreach ($pageList as $p) {

        $label    = $p[0];

        $sub      = $p[1];

        $url      = $p[2];

        $icon     = $p[3];

        $keywords = $p[4];

        $haystack = strtolower($label . ' ' . $sub . ' ' . implode(' ', $keywords));

        if (strpos($haystack, $ql) !== false) {

            $matched[] = [

                'label'  => $label,

                'sub'    => $sub,

                'detail' => 'Page',

                'icon'   => $icon,

                'url'    => $url,

                'type'   => 'page',

            ];

        }

    }

    return array_slice($matched, 0, 10);

}



// ── Build results ──────────────────────────────────────────────────────────

$rolePages = isset($pages[$role]) ? $pages[$role] : [];

$results   = matchPages($rolePages, $ql);



echo json_encode(['results' => $results]);

