<?php
// Ambil segment URI saat ini (Dirapikan agar tidak duplikat)
$uri = service('uri');
$segment = $uri->getSegment(1); // Mengambil kata pertama setelah base_url
$sub_segment = ($uri->getTotalSegments() >= 2) ? $uri->getSegment(2) : '';
?>

<div class="sidebar" style="width: 210px; height: 100vh; position: fixed; background: #111827; color: white; padding: 20px 10px; z-index: 1000;">
    <div class="text-center mb-4 border-bottom border-secondary pb-3">
        <img src="<?= base_url('assets/img/ikon.png') ?>" width="35">
        <div class="fw-bold mt-2 small text-white">PRESENSI AI</div>
        <div class="badge <?= (session()->get('level') == 'admin') ? 'bg-primary' : 'bg-success' ?> mt-1" style="font-size: 0.6rem;">
            <?= (session()->get('level') == 'admin') ? 'ADMIN EDP' : 'ADMIN OPD' ?>
        </div>
    </div>

    <nav class="nav flex-column" id="nav-accordion">
        <a class="nav-link text-white mb-2 <?= ($segment == 'dashboard') ? 'active-link' : 'opacity-75' ?>" href="<?= base_url('dashboard') ?>">
            <i class="fas fa-th-large me-2"></i> Dashboard
        </a>

        <div class="nav-item">
            <?php $master_active = in_array($segment, ['opd', 'users', 'pegawai']); ?>
            <a class="nav-link text-white mb-2 d-flex justify-content-between align-items-center <?= $master_active ? '' : 'opacity-75' ?>"
                data-bs-toggle="collapse" href="#menuMaster" role="button" aria-expanded="<?= $master_active ? 'true' : 'false' ?>">
                <span><i class="fas fa-server me-2"></i> Master Data</span>
                <i class="fas fa-chevron-right small" style="font-size: 0.7rem;"></i>
            </a>
            <div class="collapse ps-3 <?= $master_active ? 'show' : '' ?>" id="menuMaster" data-bs-parent="#nav-accordion">
                <?php if (session()->get('level') == 'admin'): ?>
                    <a class="nav-link text-white py-1 small <?= ($segment == 'opd') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('opd') ?>">
                        <i class="fas fa-university me-2"></i> Data Instansi
                    </a>
                    <a class="nav-link text-white py-1 small <?= ($segment == 'users') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('users') ?>">
                        <i class="fas fa-user-tie me-2"></i> Kelola User
                    </a>
                <?php endif; ?>

                <a class="nav-link text-white py-1 small <?= ($segment == 'pegawai') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('pegawai') ?>">
                    <i class="fas fa-user-tie me-2"></i> Data Pegawai
                </a>
            </div>
        </div>

        <div class="nav-item">
            <?php $config_active = in_array($segment, ['jamkerja', 'harilibur', 'plotting', 'cuti', 'pesan']); ?>
            <a class="nav-link text-white mb-2 d-flex justify-content-between align-items-center <?= $config_active ? '' : 'opacity-75' ?>"
                data-bs-toggle="collapse" href="#menuConfig" role="button" aria-expanded="<?= $config_active ? 'true' : 'false' ?>">
                <span><i class="fas fa-tools me-2"></i> Konfigurasi</span>
                <i class="fas fa-chevron-right small" style="font-size: 0.7rem;"></i>
            </a>
            <div class="collapse ps-3 <?= $config_active ? 'show' : '' ?>" id="menuConfig" data-bs-parent="#nav-accordion">
                <?php if (session()->get('level') == 'admin'): ?>
                    <a class="nav-link text-white py-1 small <?= ($segment == 'jamkerja') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('jamkerja') ?>">
                        <i class="fas fa-business-time me-2"></i> Jam Kerja
                    </a>
                    <a class="nav-link text-white py-1 small <?= ($segment == 'harilibur') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('harilibur') ?>">
                        <i class="fas fa-calendar-day me-2"></i> Hari Libur
                    </a>
                    <a class="nav-link text-white py-1 small <?= ($segment == 'pesan') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('pesan') ?>">
                        <i class="fas fa-comment-dots me-2"></i> Pesan Sapaan
                    </a>
                <?php endif; ?>

                <a class="nav-link text-white py-1 small <?= ($segment == 'plotting') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('plotting') ?>">
                    <i class="fas fa-map-marked-alt me-2"></i> Plotting
                </a>

                <a class="nav-link text-white py-1 small <?= ($segment == 'cuti') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('cuti') ?>">
                    <i class="fas fa-user-clock me-2"></i> Cuti Pegawai
                </a>
            </div>
        </div>

        <a class="nav-link text-white mb-2 <?= ($segment == 'apel') ? 'active-link' : 'opacity-75' ?>" href="<?= base_url('apel') ?>">
            <i class="fas fa-bullhorn me-2 text-warning"></i> Manajemen Apel
        </a>

        <a class="nav-link text-white mb-2 <?= ($segment == 'approval') ? 'active-link' : 'opacity-75' ?>" href="<?= base_url('approval') ?>">
            <i class="fas fa-check-circle me-2"></i> Approval
        </a>

        <div class="nav-item">
            <?php $rekap_active = in_array($segment, ['rekap', 'keterlambatan', 'cetaktpp']); ?>
            <a class="nav-link text-white mb-2 d-flex justify-content-between align-items-center <?= $rekap_active ? '' : 'opacity-75' ?>"
                data-bs-toggle="collapse" href="#menuRekap" role="button" aria-expanded="<?= $rekap_active ? 'true' : 'false' ?>">
                <span><i class="fas fa-clipboard-list me-2"></i> Rekapitulasi</span>
                <i class="fas fa-chevron-right small" style="font-size: 0.7rem;"></i>
            </a>
            <div class="collapse ps-3 <?= $rekap_active ? 'show' : '' ?>" id="menuRekap" data-bs-parent="#nav-accordion">

                <a class="nav-link text-white py-1 small <?= ($uri->getSegment(2) == 'manual') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('rekap/manual') ?>">
                    <i class="fas fa-user-edit me-2"></i> Absen Manual
                </a>
                <a class="nav-link text-white py-1 small <?= ($uri->getSegment(2) == 'harian') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('rekap/harian') ?>">
                    <i class="fas fa-calendar-day me-2 text-warning"></i> <b>Rekap Harian</b>
                </a>

                <a class="nav-link text-white py-1 small <?= ($segment == 'rekap' && $uri->getSegment(2) != 'manual' && $uri->getSegment(2) != 'harian') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('rekap') ?>">
                    <i class="fas fa-calendar-check me-2"></i> Rekap Kehadiran
                </a>

                <a class="nav-link text-white py-1 small <?= ($segment == 'keterlambatan') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('keterlambatan') ?>">
                    <i class="fas fa-user-clock me-2"></i> Rekap Telat
                </a>

                <a class="nav-link text-white py-1 small <?= ($segment == 'cetaktpp') ? 'active-sub' : 'opacity-50' ?>" href="<?= base_url('cetaktpp') ?>">
                    <i class="fas fa-file-invoice-dollar me-2"></i> Rekap TPP
                </a>
            </div>
        </div>
        <div class="mt-auto p-3">
            <button class="btn btn-outline-warning btn-sm w-100 rounded-pill shadow-sm" data-bs-toggle="modal" data-bs-target="#modalGantiPassword">
                <i class="fas fa-key me-2"></i> Ganti Password
            </button>
        </div>

        <a class="nav-link text-danger mt-4" href="<?= base_url('logout') ?>">
            <i class="fas fa-sign-out-alt me-2"></i> Keluar
        </a>
    </nav>
</div>

<style>
    /* Indikator link utama yang aktif (Dashboard) */
    .active-link {
        background: rgba(13, 110, 253, 0.2);
        border-left: 4px solid #0d6efd;
        border-radius: 4px;
        opacity: 1 !important;
        font-weight: bold;
    }

    /* Indikator sub-menu yang aktif */
    .active-sub {
        color: #3b82f6 !important;
        opacity: 1 !important;
        font-weight: bold;
    }

    /* Animasi panah */
    [data-bs-toggle="collapse"][aria-expanded="true"] .fa-chevron-right {
        transform: rotate(90deg);
        transition: 0.3s;
    }

    .nav-link:hover {
        background: rgba(255, 255, 255, 0.05);
        border-radius: 8px;
        transition: 0.2s;
    }

    .nav-link i {
        width: 20px;
        text-align: center;
    }
</style>