<?php
defined('BASEPATH') OR exit('No direct script access allowed');   
    if(!function_exists('put_headers')){
        function put_headers()
        {
            $str = '';
            $ci = &get_instance();
            $ci->config->load("menu");
            $ci->config->load("js_css");
            
            $header_css = $ci->config->item('header_css');
            $header_css_app = $ci->config->item('header_css_app');
            $header_js  = $ci->config->item('header_js');

     
            foreach($header_css AS $item){
                $str .= '<link rel="stylesheet" href="'.base_url().'assets/plugins/'.$item.'" type="text/css" />'."\n";
            }
     
            foreach($header_js AS $item){
                $str .= '<script type="text/javascript" src="'.base_url().'assets/plugins/'.$item.'"></script>'."\n";
            }

            foreach($header_css_app AS $item){
                $str .= '<link rel="stylesheet" href="'.base_url().AZAPP.'assets/plugins/'.$item.'" type="text/css" />'."\n";
            }
     
            return $str;
        }
    }

    if(!function_exists('generate_menu')){
        function generate_menu($active_menu = '', $active_submenu = '', $submenu = '') {
            $ci = &get_instance();
            $arr_menu = $ci->config->item('menu');
            $admin_menu = array(azlang("DASHBOARD"), azlang("SUPPLIER"), azlang('CUSTOMER'), azlang('PRODUCT'), azlang('STOCK'), azlang('TRANSACTION'), azlang('REPORT'), azlang('PRODUCT CATEGORY'), azlang('PRODUCT UNIT'), azlang('DATA PRODUCT'), azlang('STOCK IN'), azlang('STOCK OUT'), azlang('TRANSACTION SALE'), 'PEMASUKAN', 'PENGELUARAN', azlang('SALES REPORT'), azlang('REPORT STOCK IN'), azlang('REPORT STOCK OUT'),azlang('PIUTANG PELANGGAN'));
//, "LAPORAN LABA/RUGI", 'LAPORAN KAS', 'LAPORAN KAS LABA') jika akses menu Laporan Laba/Rugi mau di aktifkan di admin

            $cashier_menu = array(azlang('DASHBOARD'),azlang('PRODUCT'),azlang('DATA PRODUCT'), azlang('TRANSACTION'), azlang('TRANSACTION SALE'), azlang('SALES REPORT'), azlang('REPORT'), azlang('REPORT'), azlang('SALES REPORT'));
            $div_submenu = '';
            if (strlen($submenu) > 0) {
                $arr_menu = $ci->config->item('menu');
                $arr_menu = $arr_menu[$submenu]['submenu'];
                $div_submenu = '-submenu';
            }
            $return = "";
            $user_type = $ci->session->userdata('user_type');

            if ($user_type == 'admin') {
                if (!in_array($active_menu, $admin_menu)) {
                    redirect(app_url());
                }
            }
            else if ($user_type == 'kasir') {
                if (!in_array($active_menu, $cashier_menu)) {
                    redirect(app_url());
                }                
            }

            foreach ($arr_menu as $key => $menu) {
                // if ($ci->session->userdata("user_type") != "administrator") {
                //     if (in_array($menu['name'], $admin_menu)) {
                //         continue;
                //     }
                // }

                if ($user_type == 'admin') {
                    if (!in_array($menu['name'], $admin_menu)) {
                        continue;
                    }
                }
                else if ($user_type == 'kasir') {
                    if (!in_array($menu['name'], $cashier_menu)) {
                        continue;
                    }
                }


                $active = "";
                if ($menu['name'] == $active_menu) {
                    $active = "active ";
                }
                $active2 = "";
                if ($menu['name'] == $active_submenu) {
                    $active2 = " active ";
                }
                $return .= '<li>
                    <a href="'.app_url().$menu['url'].'">
                        <div class="'.$active.$active2.'menu-container'.$div_submenu.'">';

                if (strlen($menu['icon']) > 0) {
                    $return .= '<div>
                                <img src="'.base_url().'application/azkasir/default/assets/images/icon/'.$menu['icon'].'"/>
                            </div>';
                }

                $return .= '<div>
                                '.$menu['name'].'
                            </div>
                        </div>
                    </a>';
                
                if (count($menu['submenu']) > 0) {
                    $return .= '<ul><div class="submenu-caret"></div><div class="submenu-box">';
                    $return .= generate_menu($active_menu, $active_submenu, $key);
                    $return .= '</div></ul>';
                }
                $return .= '</li>';
            }

            return $return;
        }
    }