<?php
$this->load->helper('rbac');
$current = uri_string();
$is_active = function ($path) use ($current) {
    return (strpos($current, $path) !== false) ? 'active' : '';
};

$nav_reports = array(
    'Circulation' => array(
        'icon' => 'fas fa-exchange-alt',
        'items' => array(
            array('overdue_report_details', 'Overdue Report', 'fas fa-exclamation-triangle'),
            array('issue_report', 'Checkout Report', 'fas fa-book-reader'),
            array('current_checkout', 'Current Checkout Report', 'fas fa-book'),
            array('return_report', 'Return Report', 'fas fa-undo-alt'),
            array('circulation_report', 'Circulation Report', 'fas fa-exchange-alt'),
            array('reservation_report', 'Reservation Report', 'fas fa-bookmark'),
            array('due_clearance', 'Due Clearance', 'fas fa-check-double'),
            array('top_books_report', 'Top Books Report', 'fas fa-chart-line'),
            array('single_book_report', 'Single Catalogue History Report', 'fas fa-book-open'),
            array('top_users_report', 'Top Users Report', 'fas fa-user'),
            array('single_user_report', 'Single User History Report', 'fas fa-user-circle'),
        ),
    ),
    'Catalogue' => array(
        'icon' => 'fas fa-book',
        'items' => array(
            array('accession_register', 'Accession Register Report', 'fas fa-barcode'),
            array('search_catalogue_report', 'Search Catalogue Report', 'fas fa-search'),
            array('new_arrivals', 'New Arrivals Report', 'fas fa-book-medical'),
            array('subject_wise_report', 'Subject Wise Report', 'fas fa-tags'),
            array('book_report', 'Book Report', 'fas fa-book'),
            array('patent_report', 'Patent Report', 'fas fa-stamp'),
            array('institutional_repository', 'Institutional Repository Report', 'fas fa-university'),
            array('standards_data', 'Standards Data Report', 'fas fa-certificate'),
            array('ereport_books', 'EReport Books Report', 'fas fa-file-alt'),
            array('application_notes_report', 'Application Notes Report', 'fas fa-sticky-note'),
            array('reports_book_report', 'Reports Book Report', 'fas fa-book'),
            array('journals_book_report', 'Journals Book Report', 'fas fa-newspaper'),
            array('ebook_report', 'EBook Report', 'fas fa-tablet-alt'),
            array('overall_material_statistics_report', 'Material Statistics Report', 'fas fa-chart-pie'),
            array('publisher_year_wise_report', 'Publication Year Wise Report', 'fas fa-calendar-alt'),
            array('accession_year_wise_report', 'Accession Year Wise Report', 'fas fa-calendar-check'),
            array('accession_date_wise_report', 'Accession Date Wise Report', 'fas fa-calendar-day'),
            array('barcode_range_report', 'Barcode Range Report', 'fas fa-barcode'),
            array('price_range_report', 'Price Range Report', 'fas fa-rupee-sign'),
            array('publisher_person_wise_report', 'Publisher Wise Report', 'fas fa-user-tag'),
        ),
    ),
    'Users' => array(
        'icon' => 'fas fa-users',
        'items' => array(
            array('user_report', 'User Report', 'fas fa-users'),
            array('search_user_report', 'Search User Report', 'fas fa-user-check'),
        ),
    ),
    'Documents' => array(
        'icon' => 'fas fa-folder-open',
        'items' => array(
            array('so_report', 'SO Report', 'fas fa-file-invoice'),
            array('crv_report', 'CRV Report', 'fas fa-file-invoice-dollar'),
            array('lac_new_titles_report', 'LAC New Titles Report', 'fas fa-clipboard-list'),
            array('weed_out_report', 'Weed Out Report', 'fas fa-trash-alt'),
        ),
    ),
    'Acquisitions' => array(
        'icon' => 'fas fa-shopping-cart',
        'items' => array(
            array('budget_expenditure_report', 'Budget Expenditure Report', 'fas fa-coins'),
        ),
    ),
);

$home = rbac_first_allowed_report();
$home_url = $home ? base_url('index.php/' . $home) : base_url('index.php/login');
?>
<aside class="main-sidebar sidebar-dark-primary elevation-0 krc-sidebar">
  <a href="<?php echo $home_url; ?>" class="brand-link">
    <img src="<?php echo base_url('uploads/univlogo/DLRL_logo.png'); ?>" alt="DLRL" class="brand-image img-circle" style="opacity:1;max-height:33px;">
    <span class="brand-text">KRC Reports</span>
  </a>
  <div class="sidebar">
    <nav class="mt-2 krc-side-nav">
      <p class="krc-nav-label">Reports</p>
      <ul class="nav nav-pills nav-sidebar flex-column krc-nav-accordion" data-widget="treeview" role="menu" data-accordion="true">

        <?php foreach ($nav_reports as $group => $group_data) {
            $items = isset($group_data['items']) ? $group_data['items'] : $group_data;
            $group_icon = isset($group_data['icon']) ? $group_data['icon'] : 'fas fa-folder';
            $visible = array();
            $group_open = false;
            foreach ($items as $item) {
                if (rbac_can_view($item[0])) {
                    $visible[] = $item;
                    if ($is_active($item[0]) === 'active') {
                        $group_open = true;
                    }
                }
            }
            if (empty($visible)) {
                continue;
            }
        ?>
        <li class="nav-item has-treeview krc-nav-group <?php echo $group_open ? 'menu-open' : ''; ?>">
          <a href="#" class="nav-link krc-nav-parent <?php echo $group_open ? 'active' : ''; ?>">
            <span class="krc-nav-parent-icon"><i class="<?php echo $group_icon; ?>"></i></span>
            <p>
              <span class="krc-nav-parent-text"><?php echo htmlspecialchars($group); ?></span>
              <span class="krc-nav-meta">
                <span class="krc-nav-count"><?php echo count($visible); ?></span>
                <i class="krc-nav-chevron fas fa-chevron-down"></i>
              </span>
            </p>
          </a>
          <ul class="nav nav-treeview krc-nav-children">
            <?php foreach ($visible as $item) {
                $active = $is_active($item[0]);
            ?>
            <li class="nav-item">
              <a href="<?php echo base_url('index.php/report/' . $item[0]); ?>" class="nav-link krc-nav-child <?php echo $active; ?>">
                <span class="krc-nav-child-dot" aria-hidden="true"></span>
                <p><?php echo htmlspecialchars($item[1]); ?></p>
              </a>
            </li>
            <?php } ?>
          </ul>
        </li>
        <?php } ?>

        <?php
        $admin_items = array();
        if (rbac_can_view('manage_users')) {
            $admin_items[] = array('users', 'Users', 'fas fa-user-cog', 'users');
        }
        if (rbac_can_view('manage_roles')) {
            $admin_items[] = array('roles', 'Roles', 'fas fa-user-shield', 'roles');
        }
        if (rbac_can_view('manage_permissions')) {
            $admin_items[] = array('permissions', 'Role Permissions', 'fas fa-key', 'permissions');
        }
        if (rbac_can_view('manage_font_settings')) {
            $admin_items[] = array('settings', 'Settings', 'fas fa-cog', 'settings');
        }
        $admin_open = false;
        foreach ($admin_items as $ai) {
            if ($is_active($ai[3]) === 'active') {
                $admin_open = true;
                break;
            }
        }
        if (!empty($admin_items)) {
        ?>
        <li class="nav-item has-treeview krc-nav-group <?php echo $admin_open ? 'menu-open' : ''; ?>">
          <a href="#" class="nav-link krc-nav-parent <?php echo $admin_open ? 'active' : ''; ?>">
            <span class="krc-nav-parent-icon"><i class="fas fa-cogs"></i></span>
            <p>
              <span class="krc-nav-parent-text">Administration</span>
              <span class="krc-nav-meta">
                <span class="krc-nav-count"><?php echo count($admin_items); ?></span>
                <i class="krc-nav-chevron fas fa-chevron-down"></i>
              </span>
            </p>
          </a>
          <ul class="nav nav-treeview krc-nav-children">
            <?php foreach ($admin_items as $item) { ?>
            <li class="nav-item">
              <a href="<?php echo base_url('index.php/' . $item[0]); ?>" class="nav-link krc-nav-child <?php echo $is_active($item[3]); ?>">
                <span class="krc-nav-child-dot" aria-hidden="true"></span>
                <p><?php echo htmlspecialchars($item[1]); ?></p>
              </a>
            </li>
            <?php } ?>
          </ul>
        </li>
        <?php } ?>
      </ul>

      <div class="krc-nav-account">
        <a href="<?php echo base_url('index.php/login/logout'); ?>" class="krc-nav-logout">
          <i class="fas fa-sign-out-alt"></i>
          <span>Logout</span>
        </a>
      </div>
    </nav>
  </div>
</aside>
