<script src="<?= BASE_ASSET; ?>/js/jquery.hotkeys.js"></script>
<link rel="stylesheet" type="text/css" href="<?= BASE_ASSET; ?>nestable/nesteable.css">
<link rel="stylesheet" href="<?= BASE_ASSET; ?>m-switch/css/style.css">
<script src="<?= BASE_ASSET; ?>m-switch/js/jquery.mswitch.js" type="text/javascript"></script>
<script type="text/javascript">
   //This page is a result of an autogenerated content made by running test.html with firefox.
   function domo(){
    
      $('*').bind('keydown', 'Ctrl+a', function assets() {
          window.location.href = BASE_URL + '/administrator/menu/add/' + '<?= $this->uri->segment(4); ?>';
          return false;
      });

      $('*').bind('keydown', 'Ctrl+r', function assets() {
          window.location.href = BASE_URL + '/administrator/menu_type/add/';
          return false;
      });
   
      $('*').bind('keydown', 'Ctrl+s', function assets() {
          $('#btn_save').trigger('click');
          return false;
      });
   
   }
   
   jQuery(document).ready(domo);
</script>
<!-- Content Header (Page header) -->
<section class="content-header">
   <h1>
      <?= cclang('menu') ?>
   </h1>
   <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> <?= cclang('home') ?></a></li>
      <li class="active"><?= cclang('menu') ?></li>
   </ol>
</section>
<!-- Main content -->
<section class="content">
   <div class="row" >
      <div class="col-md-4">
         <div class="box box-warning">
           <div class="box-header with-border">
                 <h3 class="box-title "><?= cclang('menu_type'); ?></h3>
           </div>
            <div class="box-body ">
               <!-- Widget: user widget style 1 -->
               <div class="menu-type-wrapper <?= $this->uri->segment(4) == 'side-menu' ? 'active' :''; ?>">
               <div data-href="<?= site_url('administrator/menu/index/'.url_title('side menu')); ?>" class="clickable btn-block menu-type btn-group "> <?= cclang('side_menu'); ?>
               </div>
                <a class="menu-type-action">
                &nbsp;
                 </a>
               </div>
               <?php foreach (db_get_all_data('menu_type', 'name!= "side menu"') as $row): ?>
               <div class="menu-type-wrapper  <?= $this->uri->segment(4) == url_title($row->name) ? 'active' :''; ?>">
                 <span data-href="<?= site_url('administrator/menu/index/'.url_title($row->name)); ?>" class="clickable btn-block menu-type btn-group">
                    <?= _ent(ucwords($row->name)); ?>
                  
                 </span>
                 <a class="menu-type-action remove-data" data-href="<?= base_url('administrator/menu_type/delete/'.$row->id); ?>" href="javascript:void()">
                     <i class="fa fa-trash"></i>
                 </a>
               </div>
               <?php endforeach; ?> 
               <br>
               <a href="<?= site_url('administrator/menu_type/add'); ?>" class="btn btn-block btn-add btn-add-menu btn-flat" title="add menu type (Ctrl+r)"><i class="fa fa-plus-square-o"></i> <?= cclang('add_menu_type'); ?></a>
            </div>
            <!--/box body -->
         </div>
         <!--/box -->
      </div>
      <div class="col-md-6">
         <div class="box box-warning">
              <!-- Widget: user widget style 1 -->
             <div class="box-header with-border">
                  
                   <h3 class="box-title pull-left"><?= cclang('menu') ?> <?= ucwords(str_replace('-', ' ', $this->uri->segment(4))); ?></h3>
             </div>
            <div class="box-body ">
               <div class="message">
                <div class="callout callout-info btn-flat">
                  # double click menu to active or inactive
                </div>
               </div>
               <!-- Widget: user widget style 1 -->
               <div style="margin: 15px 0px 15px 0px !important;">
                <?php is_allowed('menu_add', function(){?>
                <a class="btn btn-flat btn-default btn_add_new" id="btn_add_new" title="add new menu (Ctrl+a)" href="<?= site_url('administrator/menu/add/'. $this->uri->segment(4)); ?>"><i class="fa fa-plus-square-o" ></i>  <?= cclang('add_new_button', cclang('menu')); ?></a>
                <?php }) ?>
                <span class="loading loading-hide"><img src="<?= BASE_ASSET; ?>/img/loading-spin-primary.svg"> <i><?= cclang('loading_saving_data'); ?></i></span>
             </div>
              <div class="dd" id="nestable">
                 <?php
                  $menu = display_menu_module(0, 1, $this->uri->segment(4), true); 
                  if (empty($menu)): ?>
                  <div class="box-no-data">No data menu</div>
                 <?php else: 
                 echo $menu;
                  endif; ?>
              </div>
              <div class="nestable-output"></div>
               </div>
              
            </div>
            <!--/box body -->
         </div>
      </div>
   </div>
</section>

<script src="<?= BASE_ASSET; ?>nestable/jquery.nestable.js"></script>
<script>
$(document).ready(function() {
    $('.remove-data').click(function() {
        var url = $(this).attr('data-href');
        swal({
                title: "Are you sure?",
                text: "data to be deleted can not be restored!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                cancelButtonText: "No, cancel plx!",
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function(isConfirm) {
                if (isConfirm) {
                    document.location.href = url;
                }
            });

        return false;
    }); /*end remove data click*/

    var timeout;
    $('.dd').on('change', function() {
        clearTimeout(timeout);
        timeout = setTimeout(updateOrderMenu, 2000);
    });

    function updateOrderMenu(ignoreMessage) {
            $('.loading').removeClass('loading-hide');
            var shownotif = true;
            var menu = $('.dd').nestable('serialize');

            if (typeof shownotif == 'undefined') {
                var shownotif = true;
            }


            if (typeof ignoreMessage == 'undefined') {
                var ignoreMessage = false;
            }

            $.ajax({
                    url: BASE_URL + 'administrator/menu/save_ordering',
                    type: 'POST',
                    dataType: 'JSON',
                    data: {
                        'menu': menu,
                        '<?= $this->security->get_csrf_token_name(); ?>': '<?= $this->security->get_csrf_hash(); ?>'
                    },
                })
                .done(function(res) {
                    if (res.success) {
                        $('.sidebar-menu').html(res.menu);
                        if (shownotif) {
                            if (!ignoreMessage) {
                              toastr['success'](res.message);
                            }
                        }
                    } else {
                        if (shownotif) {
                            if (!ignoreMessage) {
                              toastr['warning'](res.message);
                            }
                        }
                    }
                })
                .fail(function() {
                    if (!ignoreMessage) {
                      toastr['warning']('Error save data please try again later');
                    }
                })
                .always(function() {
                    $('.loading').addClass('loading-hide');
                });
        }
        // activate Nestable for list 1
    $('#nestable').nestable({
        group: 1
    });


    $('.clickable').on('click', function() {
        var href = $(this).attr('data-href');

        window.location.href = href;

        return false;
    }); /*end clickable click*/

     $(".m_switch_check:checkbox").mSwitch({
          onRender:function(elem){
              changeSharingDashboard(elem.val(), 'dont_update');
              if (elem.val() == 0){
                  $.mSwitch.turnOff(elem);
              }else{
                  $.mSwitch.turnOn(elem);
              }
          },
          onTurnOn:function(elem){
             changeSharingDashboard(1, 'update');
          },
          onTurnOff:function(elem){
             changeSharingDashboard(0, 'update');
          }
      });



      function setMenuActive(id, status) {
        var data = [];

         data.push({
            name: csrf,
            value: token
        });
        data.push({
            name: 'status',
            value: status
        });
        data.push({
            name: 'id',
            value: id
        });

        $.ajax({
                url: BASE_URL + '/administrator/menu/set_status',
                type: 'POST',
                dataType: 'JSON',
                data: data,
            })
            .done(function(data) {
                if (data.success) {
                    toastr['success'](data.message);
                    updateOrderMenu(true)
                } else {
                    toastr['warning'](data.message);
                }

            })
            .fail(function() {
                toastr['error']('Error update status');
            });
      }


      $('.menu-toggle-activate').dblclick(function(event) {
        event.stopPropagation();
        var status = $(this).data('status');
        var id = $(this).data('id');

        switch (status) {
          case undefined : case 0 :
          $(this).removeClass('menu-toggle-activate_inactive');
          $(this).data('status', 1)
          setMenuActive(id,  1);
          break;
          case 1 :
          $(this).addClass('menu-toggle-activate_inactive');
          $(this).data('status', 0)
          setMenuActive(id,  0);
          break;
        }
      });

      $('.dd3-item').hover(function() {
        $('.dropdown').removeClass('open')

        $(this).find('.dropdown:first').addClass('open')
      });

}); /*end doc ready*/
</script>