<?= $this->extend('template/layout') ?>

<?= $this->section('title') ?>Modul & Menu <?= env('app.name') ?><?= $this->endSection() ?>

<!-- main section start-->
<?= $this->section('main') ?>
<?php
    $uri      = current_url(true);
    $segment1 = $uri->setSilent()->getSegment(1);
    $segment2 = $uri->setSilent()->getSegment(2);
    $segment3 = $uri->setSilent()->getSegment(3);
?>
<!-- Breadcumb -->
<ol class="breadcrumb page-breadcrumb">
    <li class="breadcrumb-item"><a href="javascript:void(0);"><?= env('app.name') ?></a></li>
    <li class="breadcrumb-item"><?= ucfirst($segment1) ?></li>
    <li class="breadcrumb-item">Modul & Menu</li>
    <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
</ol>

<div class="row">
    <div class="col-xl-6">
        <div id="panel-1" class="panel">
            <div class="panel-hdr border-faded border-top-1 border-right-0 border-left-0 shadow-0">
                <h2>Data Modul</h2>
                <div class="panel-toolbar pr-3">
                    <ul class="nav nav-pills border-bottom-0" role="tablist">
                        <li class="nav-item">
                            <?= form_button('reload', '<i class="fal fa-sync"></i>', 'reload_datagrid_modul()') ?>&nbsp;
                        </li>
                        <li class="nav-item">
                            <?= form_button('read', '<i class="fal fa-eye"></i>', 'show_modul()') ?>&nbsp;
                        </li>
                        <?php if ($rekam == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('create', '<i class="fal fa-plus"></i>', 'new_modul()') ?>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($ubah == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('update', '<i class="fal fa-edit"></i>', 'edit_modul()') ?>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($hapus == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('delete', '<i class="fal fa-trash-alt"></i>', 'delete_modul()') ?>
                        </li>
                        <?php } ?>
                    </ul>
                </div>
            </div><!-- end panel hdr -->

            <div class="panel-container show">
                <div class="panel-content">
                    <div id="panelfit-1" data-options="border:false">
                        <!-- grid start -->
                        <table id="datagrid_modul" toolbar="#toolbar_modul" url="<?= site_url('admin/modul/datagrid_modul') ?>" data-options="nowrap:true,striped:false,ctrlSelect:true,rownumbers:true,pagination:true,multiSort:true,remoteFilter:true,remoteSort:true,pageSize:10,pageList:[10,20,50,100],clientPaging:false,fit:true">
                            <thead data-options="frozen:true">
                                <tr>
                                    <th data-options="field:'ck',checkbox:true"></th>
                                </tr>
                            </thead>
                            <thead>
                                <tr>
                                    <th data-options="field:'fname',sortable:true,width:100">Fname</th>
                                    <th data-options="field:'nama_modul',sortable:true,width:150">Nama Modul</th>
                                    <th data-options="field:'urut',sortable:true,align:'center'">Urut</th>
                                    <th data-options="field:'ikon',sortable:true">Icon</th>
                                    <th data-options="field:'ket',sortable:true">Ket.</th>
                                </tr>
                            </thead>
                        </table>
                        <!-- grid end -->
                    </div>
                </div>
            </div><!-- end panel main content -->
        </div>
    </div>

    <div class="col-xl-6">
        <div id="panel-2" class="panel">
            <div class="panel-hdr border-faded border-top-1 border-right-0 border-left-0 shadow-0">
                <h2>Data Menu</h2>
                <div class="panel-toolbar pr-3">
                    <ul class="nav nav-pills border-bottom-0" role="tablist">
                        <li class="nav-item">
                            <?= form_button('reload', '<i class="fal fa-sync"></i>', 'reload_datagrid_menu()') ?>&nbsp;
                        </li>
                        <li class="nav-item">
                            <?= form_button('read', '<i class="fal fa-eye"></i>', 'show_menu()') ?>&nbsp;
                        </li>
                        <?php if ($rekam == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('create', '<i class="fal fa-plus"></i>', 'new_menu()') ?>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($ubah == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('update', '<i class="fal fa-edit"></i>', 'edit_menu()') ?>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($hapus == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('delete', '<i class="fal fa-trash-alt"></i>', 'delete_menu()') ?>
                        </li>
                        <?php } ?>
                    </ul>
                </div>
            </div><!-- end panel hdr -->

            <div class="panel-container show">
                <div class="panel-content">
                    <div id="panelfit-2" data-options="border:false">
                        <!-- grid start -->
                        <table id="datagrid_menu" toolbar="#toolbar_menu" url="<?= site_url('admin/modul/datagrid_menu') ?>" data-options="nowrap:true,striped:false,ctrlSelect:true,rownumbers:true,pagination:true,multiSort:true,remoteFilter:true,remoteSort:true,pageSize:10,pageList:[10,20,50,100],clientPaging:false,fit:true" style="min-height: 300px;">
                            <thead data-options="frozen:true">
                                <tr>
                                    <th data-options="field:'ck',checkbox:true"></th>
                                </tr>
                            </thead>
                            <thead>
                                <tr>
                                    <th data-options="field:'nama_modul',sortable:true,width:110">Nama Modul</th>
                                    <th data-options="field:'nama_menu',sortable:true,width:130">Nama Menu</th>
                                    <th data-options="field:'nama_parent',sortable:true,width:130">Nama Parent</th>
                                    <th data-options="field:'urut',sortable:true,align:'center'">Urut</th>
                                    <th data-options="field:'method',sortable:true,width:150">Method</th>
                                </tr>
                            </thead>
                        </table>
                        <!-- grid end -->
                    </div>
                </div>
            </div><!-- end panel main content -->
        </div>
    </div>
</div>

<!-- MODAL MODUL -->
<div id="modal-modul" class="modal fade default-example-modal-left" data-backdrop="static" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-dialog-left">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4" id="mod-title"></h5>
            </div>
            
            <div id="form-konten"></div>            
        </div>
    </div>
</div><!-- END MODAL MODUL -->

<!-- MODAL MENU -->
<div id="modal-menu" class="modal fade default-example-modal-right" data-backdrop="static" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-dialog-right">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4" id="men-title"></h5>
            </div>
            
            <div id="form-konten-menu"></div>            
        </div>
    </div>
</div><!-- END MODAL MENU -->

<?= $this->endSection() ?>
<!-- main section end-->

<!-- pageScripts section start-->
<?= $this->section('pageScripts') ?>

<script type="text/javascript" src="<?= base_url('smart/dist/adlte/jquery-easyui-1.10.17/jquery.easyui.min.js') ?>"></script>
<script type="text/javascript" src="<?= base_url('smart/dist/adlte/jquery-easyui-1.10.17/datagrid-filter-1.0.6/datagrid-filter.js') ?>"></script>

<script type="text/javascript">
   $(document).ready(function () {
        adjustpanelfit();
    })

    $(window).resize(function() {
        adjustpanelfit();
    });

    function adjustpanelfit(){
        var height = $(window).height();
        $('#panelfit-1, #panelfit-2').panel({
            height:height - 400
        });
    }
</script>

<script type="text/javascript">
    /*datagrid modul =====================================*/
    var datagrid_modul = $('#datagrid_modul').datagrid({
        queryParams: {
            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
        },
        onDblClickRow : function(index,row){
            show_child();
        }
    });
    datagrid_modul.datagrid('enableFilter');

    /*reload datagrid modul*/
    function reload_datagrid_modul()
    {
        $('#datagrid_modul').datagrid('removeFilterRule');
        $('#datagrid_modul').datagrid('load');
    }

    function show_modul()
    {
        var row = $('#datagrid_modul').datagrid('getSelected');
        if (row) {
            var id_modul = row.id_modul;
            $('#mod-title').html('Detail Modul');
            $('#form-konten').load('<?= base_url('admin/modul/show_modul/') ?>' + id_modul);
            $('#modal-modul').modal('show');
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    function show_menu()
    {
        var row = $('#datagrid_menu').datagrid('getSelected');
        if (row) {
            var id_menu = row.id_menu;
            $('#men-title').html('Detail Menu');
            $('#form-konten-menu').load('<?= base_url('admin/modul/show_menu/') ?>' + id_menu);
            $('#modal-menu').modal('show');
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    //double klik row modul tarik data child
    function show_child()
    {
        var row = $('#datagrid_modul').datagrid('getSelected');
        if (row) {
            var id_modul = row.id_modul;
            $.ajax({
                url: '<?= base_url('admin/modul/datagrid_menu') ?>',
                type: "post",
                dataType: 'json',
                data: {
                    id_modul: id_modul,
                    "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
                },
                error: function (xhr, status, message) {
                    $.messager.alert(status,message,'warning');
                },
                success: function(res) {
                    $('#datagrid_menu').datagrid('loadData', res);
                    //$('#datagrid_modul').datagrid('reload');
                    //$.messager.alert('<?= lang('App.success') ?>', res.message, 'info');
                }
            });
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    /*datagrid menu =====================================*/
    var datagrid_menu = $('#datagrid_menu').datagrid({
        queryParams: {
            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
        },
        onDblClickRow : function(index,row){
            show_menu();
        }
    });
    datagrid_menu.datagrid('enableFilter');

    /*reload datagrid menu*/
    function reload_datagrid_menu()
    {
        $('#datagrid_menu').datagrid('removeFilterRule');
        $('#datagrid_menu').datagrid('load');
    }   
</script>

<?php if ($rekam == 1) { ?>
<!-- FUNCTION REKAM -->
<script type="text/javascript">
    //tampilkan form rekam modul
    function new_modul()
    {
        $('#mod-title').html('Rekam Modul');
        $('#form-konten').load('<?= $link_rekam_modul ?>');
        $('#modal-modul').modal('show');
    }

    //tampilkan form rekam menu
    function new_menu()
    {
        $('#men-title').html('Rekam Menu');
        $('#form-konten-menu').load('<?= $link_rekam_menu ?>');
        $('#modal-menu').modal('show');
    }
</script>
<?php } ?>

<?php if ($ubah == 1) { ?>
<!-- FUNCTION UBAH -->
<script type="text/javascript">
    //tampilkan form edit modul
    function edit_modul()
    {
        var row = $('#datagrid_modul').datagrid('getSelected');
        if (row) {
          var id_modul = row.id_modul;
          $('#mod-title').html('Edit Modul');
          $('#form-konten').load('<?= $link_ubah_modul ?>' + id_modul);
          $('#modal-modul').modal('show');
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    //tampilkan form edit menu
    function edit_menu()
    {
        var row = $('#datagrid_menu').datagrid('getSelected');
        if (row) {
          var id_menu = row.id_menu;
          $('#men-title').html('Edit Menu');
          $('#form-konten-menu').load('<?= $link_ubah_menu ?>' + id_menu);
          $('#modal-menu').modal('show');
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }
</script>
<?php } ?>

<?php if ($hapus == 1) { ?>
<!-- FUNCTION HAPUS -->
<script type="text/javascript">
    //Hapus Modul
    function delete_modul()
    {
        var rows = $('#datagrid_modul').datagrid('getSelections');
        var ids = [];
        for(var j=0;j<rows.length;j++){
            ids[j] = rows[j].id_modul;
        }
        if (ids.length > 0) {
            $.messager.confirm('<?= lang('App.delete') ?>', '<?= lang('App.delete') ?> ' + rows.length + ' data ?', function(r){
                if (r){
                    $.ajax({
                        url: '<?= $link_hapus_modul ?>',
                        type: "post",
                        dataType: 'json',
                        data: {
                            ids: ids,
                            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
                        },
                        error: function (xhr, status, message) {
                            $.messager.alert(status,message,'warning');
                        },
                        success: function(res) {
                            $('#datagrid_modul').datagrid('reload');
                            $('#datagrid_menu').datagrid('reload');
                            toastr.success(res.message, 'Updated!!', {timeOut: 3000});
                        }
                    });
                }
            });
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    } //End Hapus Modul

    //Hapus Menu
    function delete_menu()
    {
        var rows = $('#datagrid_menu').datagrid('getSelections');
        var ids = [];
        for(var j=0;j<rows.length;j++){
            ids[j] = rows[j].id_menu;
        }
        if (ids.length > 0) {
            $.messager.confirm('<?= lang('App.delete') ?>', '<?= lang('App.delete') ?> ' + rows.length + ' data ?', function(r){
                if (r){
                    $.ajax({
                        url: '<?= $link_hapus_menu ?>',
                        type: "post",
                        dataType: 'json',
                        data: {
                            ids: ids,
                            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
                        },
                        error: function (xhr, status, message) {
                            $.messager.alert(status,message,'warning');
                        },
                        success: function(res) {
                            $('#datagrid_menu').datagrid('reload');
                            toastr.success(res.message, 'Updated!!', {timeOut: 3000});
                        }
                    });
                }
            });
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    } //End Hapus Menu
</script>
<?php } ?>

<?= $this->endSection() ?>
<!-- pageScripts section end-->