<?php

namespace App\Models\Panel\Utils;

use CodeIgniter\Model;

class SidebarModel extends Model
{
    public function menu()
    {
        $menu = [];
        $menu[] = [
            "name" => "Dashboard",
            "url" => "panel/dashboard",
            "active_url" => [],
            "roles" => ['admin', 'kepsek'],
            "icon" => "lni lni-dashboard",
            "child" => []
        ];
        $menu[] = [
            "name" => "Data Master",
            "url" => "",
            "active_url" => [],
            "roles" => ['admin', 'guru'],
            "icon" => "lni lni-layers",
            "child" => [
                [
                    "name" => "Tahun Pelajaran",
                    "url" => "panel/e-learning/tahun-pelajaran",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Mata Pelajaran",
                    "url" => "panel/md/pelajaran",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Jurusan",
                    "url" => "panel/md/jurusan",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Tingkat",
                    "url" => "panel/md/level",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Kelas",
                    "url" => "panel/md/kelas",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Jenis Ujian",
                    "url" => "panel/md/jenis-ujian",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Ruang Ujian",
                    "url" => "panel/md/ruang-ujian",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Siswa",
                    "url" => "panel/md/siswa",
                    "roles" => ['admin', 'guru'],
                ],
                [
                    "name" => "Alumni",
                    "url" => "panel/md/alumni",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Import Data",
                    "url" => "panel/md/import",
                    "roles" => ['admin'],
                ],
            ]
        ];
        if (options('el_mode')) {

            $menu[] = [
                "name" => "E-Learning",
                "url" => "",
                "active_url" => [],
                "roles" => ['admin', 'guru',  'karyawan'],
                "icon" => "lni lni-ruler-pencil",
                "child" => [
                    [
                        "name" => "Jadwal Libur",
                        "url" => "panel/e-learning/jadwal-libur",
                        "roles" => ['admin'],
                    ],
                    [
                        "name" => "Presensi",
                        "url" => "panel/daily-presence/map",
                        "roles" => ['guru',  'karyawan'],
                    ],
                    [
                        "name" => "Rekap Mengajar",
                        "url" => "panel/e-learning/rekap/mengajar",
                        "roles" => ['admin', 'piket'],
                    ],
                    [
                        "name" => "Rekap Agenda Mengajar",
                        "url" => "panel/e-learning/rekap/agenda/mengajar",
                        "roles" => ['admin', 'piket', 'guru', 'kepsek'],
                    ],
                    [
                        "name" => "Approval Kehadiran",
                        "url" => "panel/approval/presence",
                        "roles" => ['guru'],
                    ],
                    [
                        "name" => "Kompetensi Dasar",
                        "url" => "panel/e-learning/kompetensi-dasar",
                        "roles" => ['admin', 'guru'],
                    ],
                    [
                        "name" => "RPP",
                        "url" => "panel/e-learning/rpp",
                        "roles" => ['admin', 'guru'],
                    ],
                    [
                        "name" => "Jadwal Pelajaran",
                        "url" => "panel/e-learning/jadwal-pelajaran",
                        "roles" => ['admin', 'guru'],
                    ],
                    [
                        "name" => "Jadwal Mengajar",
                        "url" => "panel/e-learning/jadwal-mengajar",
                        "roles" => ['guru'],
                    ],
                    [
                        "name" => "Rekap Mengajar Guru",
                        "url" => "panel/e-learning/rekap/guru/mengajar",
                        "roles" => ['admin'],
                    ],
                    [
                        "name" => "Rekap Belajar Siswa",
                        "url" => "panel/e-learning/rekap/siswa/belajar",
                        "roles" => ['admin', 'guru'],
                    ],
                ]
            ];
            $menu[] = [
                "name" => "Presensi",
                "url" => "",
                "active_url" => [],
                "roles" => ["admin", "piket", 'kepsek'],
                "icon" => "lni lni-calendar",
                "child" => [
                    [
                        "name" => "Rekap Presensi Guru",
                        "url" => "panel/e-learning/rekap/guru/absensi",
                        "roles" => ["admin", "piket", 'kepsek'],
                    ],
                    [
                        "name" => "Rekap Presensi Karyawan",
                        "url" => "panel/e-learning/rekap/karyawan/absensi",
                        "roles" => ["admin", "piket", 'kepsek'],
                    ],
                    [
                        "name" => "Rekap Presensi Siswa",
                        "url" => "panel/e-learning/rekap/siswa/absensi",
                        "roles" => ["admin", "piket", 'kepsek'],
                    ],
                    [
                        "name" => "Approval Kehadiran Siswa",
                        "url" => "panel/approvals/presence/siswa",
                        "roles" => ["admin", "piket", 'kepsek'],
                    ],
                    [
                        "name" => "Approval Kehadiran Guru",
                        "url" => "panel/approvals/presence/guru",
                        "roles" => ["admin", "piket", 'kepsek'],
                    ],
                ]
            ];
        }
        $menu[] = [
            "name" => "Ujian",
            "url" => "",
            "active_url" => [],
            "roles" => ["admin", "guru", "pengawas", 'kepsek'],
            "icon" => "lni lni-ruler-pencil",
            "child" => [
                [
                    "name" => "Bank Soal",
                    "url" => "panel/bank-soal",
                    "roles" => ["admin", "guru"],
                ],
                [
                    "name" => "Overview",
                    "url" => "panel/ujian/overview",
                    "roles" => ["admin", "guru", "pengawas", 'kepsek'],
                ],
                [
                    "name" => "Draft",
                    "url" => "panel/ujian/draft",
                    "roles" => ['admin', 'guru', 'kepsek'],
                ],
                [
                    "name" => "Jadwal",
                    "url" => "panel/ujian/jadwal",
                    "roles" => ["admin", "guru", "pengawas", 'kepsek'],
                ],
                [
                    "name" => "Legger Nilai",
                    "url" => "panel/ujian/legger-nilai",
                    "roles" => ["admin", "guru", "pengawas", 'kepsek'],
                ],
            ]
        ];
        $menu[] = [
            "name" => "Pengumuman",
            "url" => "panel/announcement",
            "active_url" => [],
            "roles" => ["guru", "pengawas", "piket"],
            "icon" => "fas fa-newspaper",
            "child" => []
        ];
        $menu[] = [
            "name" => "Manajemen User",
            "url" => "",
            "active_url" => [],
            "roles" => ['admin'],
            "icon" => "lni lni-users",
            "child" => [
                [
                    "name" => "Administrator",
                    "url" => "panel/users/administrator",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Staff",
                    "url" => "panel/users/staff",
                    "roles" => ['admin'],
                ],
            ]
        ];
        $menu[] = [
            "name" => "Lain Lain",
            "url" => "",
            "active_url" => [],
            "roles" => ['admin', 'guru', 'kepsek'],
            "icon" => "lni lni-files",
            "child" => [
                [
                    "name" => "Berita Acara",
                    "url" => "panel/berita-acara",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Pengumuman",
                    "url" => "panel/pengumuman",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Riwayat Aktivitas",
                    "url" => "panel/logs",
                    "roles" => ['admin'],
                ],

                [
                    "name" => "Whatsapp Perangkat",
                    "url" => "panel/whatsapp/devices",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Whatsapp Pengaturan",
                    "url" => "panel/whatsapp/settings",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Whatsapp Kirim Pesan",
                    "url" => "panel/whatsapp/send",
                    "roles" => ['admin', 'guru', 'kepsek'],
                ],
                [
                    "name" => "Whatsapp Riwayat Pesan",
                    "url" => "panel/whatsapp/history",
                    "roles" => ['admin'],
                ],

                [
                    "name" => "Attendance Device",
                    "url" => "panel/attendance-solution/device",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Attendance Assign",
                    "url" => "panel/attendance-solution/assign",
                    "roles" => ['admin'],
                ],
            ]
        ];

        $menu[] = [
            "name" => "Candy Exambro",
            "url" => "",
            "active_url" => [],
            "roles" => ['admin'],
            "icon" => "lni lni-files",
            "child" => [
                [
                    "name" => "Scan QR",
                    "url" => "panel/display/scan-app-qr",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Blokir Aplikasi",
                    "url" => "panel/exambro/blocked-apps",
                    "roles" => ['admin'],
                ],
            ]
        ];
        $menu[] = [
            "name" => "Pengaturan",
            "url" => "",
            "active_url" => [],
            "icon" => "lni lni-cogs",
            "roles" => ['admin'],
            "child" => [
                [
                    "name" => "Umum",
                    "url" => "panel/app/settings/general",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Cadangkan & Pulihkan",
                    "url" => "panel/app/settings/backup-restore",
                    "roles" => ['admin'],
                ],
                // [
                //     "name" => "Servers",
                //     "url" => "panel/app/settings/servers",
                //     "roles" => ['admin'],
                // ],
                [
                    "name" => "Secret Key",
                    "url" => "panel/app/settings/secret-key",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Redis",
                    "url" => "panel/app/settings/redis",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Presensi",
                    "url" => "panel/app/settings/attendance",
                    "roles" => ['admin'],
                ],
                [
                    "name" => "Pembaruan",
                    "url" => "panel/app/settings/updates",
                    "roles" => ['admin'],
                ],
            ]
        ];
        $menu[] = [
            "name" => "Auth",
            "url" => "",
            "active_url" => [],
            "icon" => "lni lni-exit",
            "roles" => ['all'],
            "child" => [
                [
                    "name" => "Login",
                    "url" => "panel/auth/login",
                    "roles" => ['unauth'],
                ],
                [
                    "name" => "Logout",
                    "url" => "panel/auth/logout",
                    "roles" => ["admin", "guru", "pengawas", 'piket', 'kepsek', 'karyawan'],
                ]
            ]
        ];
        $dm = [];
        $logged_in = logged_in();
        foreach ($menu as $m) {
            if ($m['roles'] == 'all' || !$logged_in || in_array('unauth', $m['roles'])) {
                $dmc = [];
                foreach ($m['child'] as $mc) {
                    if ($logged_in) {
                        if (!user()->hasRole($mc['roles'])) continue;
                        unset($mc['roles']);
                        $dmc[] = $mc;
                    } else {
                        if (!in_array('unauth', $mc['roles']) || !in_array('all', $mc['roles'])) continue;
                        unset($mc['roles']);
                        $dmc[] = $mc;
                    }
                }
            } else {
                if (!user()->hasRole($m['roles']) && !in_array('all', $m['roles'])) continue;
                $dmc = [];
                foreach ($m['child'] as $mc) {
                    if (!user()->hasRole($mc['roles']) && !in_array('all', $mc['roles'])) continue;
                    unset($mc['roles']);
                    $dmc[] = $mc;
                }
            }
            $m['child'] = $dmc;
            unset($m['roles']);
            $dm[] = $m;
        }
        return $dm;
    }
}
