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

<?= $this->section('title') ?>Rolegroup - <?= 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">Role Group</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 Role</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_rolegroup()') ?>&nbsp;
                        </li>
                        <?php if ($rekam == 1) { ?>
                        <li class="nav-item">
                            <button type="button" id="rekam-modal" class="btn btn-default waves-effect waves-themed" onclick="rekam_role()"><i class="fal fa-plus"></i></button>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($ubah == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('update', '<i class="fal fa-edit"></i>', 'edit_role()') ?>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($hapus == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('delete', '<i class="fal fa-trash-alt"></i>', 'delete_role()') ?>
                        </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_rolegroup" toolbar="#toolbar_rolegroup" url="<?= site_url('admin/rolegroup/datagrid_rg') ?>" 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:'nama_group',sortable:true,width:130">Nama Role</th>
                                    <th data-options="field:'nama_modul',sortable:true,width:130">Nama Modul</th>
                                    <th data-options="field:'ket',sortable:true,width:400">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 Role</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_rolemenu()') ?>&nbsp;
                        </li>
                        <?php if ($rekam == 1) { ?>
                        <li class="nav-item">
                            <button type="button" id="add-modal" class="btn btn-default waves-effect waves-themed" onclick="rekam_rm()"><i class="fal fa-plus"></i></button>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($ubah == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('update', '<i class="fal fa-edit"></i>', 'edit_rolemenu()') ?>&nbsp;
                        </li>
                        <?php } ?>
                        <?php if ($hapus == 1) { ?>
                        <li class="nav-item">
                            <?= form_button('delete', '<i class="fal fa-trash-alt"></i>', 'delete_rolemenu()') ?>
                        </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_rolemenu" toolbar="#toolbar_rolemenu" url="<?= site_url('admin/rolegroup/datagrid_rm') ?>" 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:'group_id',sortable:true,hidden:true">Group Id</th>
                                    <th data-options="field:'nama_group',sortable:true,width:130">Nama Role</th>
                                    <th data-options="field:'nama_menu',sortable:true,width:200">Nama Menu</th>
                                    <th data-options="field:'id_parent',sortable:true,align:'center',width:60">Parent</th>
                                    <th data-options="field:'rekam',sortable:true,align:'center',width:60">Rekam</th>
                                    <th data-options="field:'ubah',sortable:true,align:'center',width:60">Ubah</th>
                                    <th data-options="field:'hapus',sortable:true,align:'center',width:60">Hapus</th>
                                    <th data-options="field:'exp',sortable:true,align:'center',width:60">Exp</th>
                                </tr>
                            </thead>
                        </table>
                        <!-- grid end -->
                    </div>
                </div>
            </div><!-- end panel main content -->
        </div>
    </div>
</div>

<?php if ($rekam == 1) { ?>
<!-- MODAL REKAM ROLEGROUP -->
<div id="modal-rekam" class="modal fade default-example-modal-left-lg" data-backdrop="static" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-dialog-left modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4">Rekam Data Role</h5>
            </div>
            
            <?= form_open('', 'id="form_rekam"') ?>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-sm-6">
                            <?= form_select2('id_modul', $dd_moduls, 0, 'Nama Modul') ?>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group" id="fgr_nama_group">
                                <label class="control-label" for="nama_group">Nama Role:</label>
                                <input type="text" name="nama_group" id="nama_group" value="" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginput" placeholder="Nama Role" required>
                                <div class="invalid-feedback" id="em_nama_group"></div>
                            </div><br>
                        </div>
                        <div class="col-sm-12">
                            <div class="form-group" id="fgr_ket">
                                <label class="control-label" for="ket">Keterangan:</label>
                                <input type="text" name="ket" id="ket" value="" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginput" placeholder="Keterangan" required>
                                <div class="invalid-feedback" id="em_ket"></div>
                            </div><br>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table id="tb-menu" class="table table-bordered">
                            <thead>
                                <tr>
                                    <th width="70" class="text-center">NO.</th>
                                    <th width="300">MENU</th>
                                    <th class="text-center">PARENT</th>
                                    <th class="text-center">REKAM</th>
                                    <th class="text-center">UBAH</th>
                                    <th class="text-center">HAPUS</th>
                                    <th class="text-center">EXPORT</th>
                                    <th class="text-center" width="100">AKSI</th>
                                </tr>
                            </thead>
                            <tbody>
                                
                            </tbody>
                        </table>
                    </div>
                    <hr/>
                </div><!-- end modal body -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary waves-effect waves-themed" data-dismiss="modal">Close</button>
                    <button type="submit" id="btn_rekam" class="btn btn-primary waves-effect waves-themed">
                      Save Changes
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- END MODAL REKAM ROLEGROUP-->

<!-- MODAL ADD ROLE MENU -->
<div id="modal-add-rm" class="modal fade default-example-modal-right-lg" data-backdrop="static" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-dialog-right modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4">Tambah Menu Role</h5>
            </div>
            
            <div id="form-konten"></div>
        </div>
    </div>
</div><!-- END MODAL ADD ROLE MENU -->

<?php } ?>

<?php if ($ubah == 1) { ?>
<!-- MODAL UBAH ROLE -->
<div id="modal-ubah" 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">Ubah Data Role</h5>
            </div>
            
            <?= form_open('', 'id="form_ubah"') ?>
                <div class="modal-body">

                    <div class="row mb-3">
                        <label class="col-sm-3 col-form-label text-right">Nama Modul:</label>
                        <div class="col-sm-9">
                            <input type="text" name="nama_modul" id="nama_modul" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" value="" disabled>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <label class="col-sm-3 col-form-label text-right">Nama Role:</label>
                        <div class="col-sm-9">
                            <input type="text" name="nama_group" id="ubah_nama_group" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginput" value="" required>
                            <div class="invalid-feedback" id="em_ubah_nama_group"></div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <label class="col-sm-3 col-form-label text-right">Keterangan:</label>
                        <div class="col-sm-9">
                            <input type="text" name="ket" id="ubah_ket" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginput" value="" required>
                            <div class="invalid-feedback" id="em_ubah_ket"></div>
                        </div>
                    </div>
                    <hr/>
                </div><!-- end modal body -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary waves-effect waves-themed" data-dismiss="modal">Close</button>
                    <button type="submit" id="btn_ubah" class="btn btn-primary waves-effect waves-themed">
                      Save Changes
                    </button>
                </div>
            </form>
        </div>
    </div>
</div><!-- END MODAL UBAH ROLE -->

<!-- MODAL UBAH ROLE MENU -->
<div id="modal-ubah-rm" class="modal fade default-example-modal-right" 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">Ubah Data Menu Role</h5>
            </div>
         
            <?= form_open('', 'id="form_ubah_rm"') ?>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="form-group" id="fg_nama_group">
                                <label class="control-label" for="nama_group">Nama Role :</label>
                                <input type="text" id="nama_group_rm" value="" disabled class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent"><br>
                            </div>
                        </div>
                        <div class="col-sm-9">
                            <div class="form-group" id="fg_nama_menu">
                                <label class="control-label" for="nama_menu">Nama Menu :</label>
                                <input type="text" id="nama_menu" value="" disabled class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent">
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group" id="fg_id_parent">
                                <label class="control-label" for="id_parent">Parent :</label>
                                <input type="text" id="id_parent" value="" disabled class="form-control text-center rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent">
                            </div><br>
                        </div>
                        <div class="col-sm-12">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="rekam" value="1" name="rekam">
                                <label class="custom-control-label" for="rekam">REKAM</label>
                            </div><br>
                        </div>
                        <div class="col-sm-12">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="ubah" value="1" name="ubah">
                                <label class="custom-control-label" for="ubah">UBAH</label>
                            </div><br>
                        </div>
                        <div class="col-sm-12">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="hapus" value="1" name="hapus">
                                <label class="custom-control-label" for="hapus">HAPUS</label>
                            </div><br>
                        </div>
                        <div class="col-sm-12">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="exp" value="1" name="exp">
                                <label class="custom-control-label" for="exp">EKSPOR</label>
                            </div>
                        </div>
                    </div>
                    <hr/>
                </div><!-- end modal body -->
                <div class="modal-footer">
                   <button type="button" class="btn btn-secondary waves-effect waves-themed" data-dismiss="modal">Close</button>
                   <button type="submit" id="btn_ubah_rm" class="btn btn-primary waves-effect waves-themed">
                      Save Changes
                    </button>
                </div>
            </form>

        </div>
    </div>
</div><!-- END MODAL UBAH ROLE MENU -->
<?php } ?>

<?= $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
        });
    }

    $('.select2').select2({
        dropdownCssClass:'increasezindex'
    });    
</script>

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

    /*reload datagrid rolegroup*/
    function reload_datagrid_rolegroup()
    {
        $('#datagrid_rolegroup').datagrid('removeFilterRule');
        $('#datagrid_rolegroup').datagrid('load');
    }

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

    /*datagrid rm =====================================*/
    var datagrid_rolemenu = $('#datagrid_rolemenu').datagrid({
        queryParams: {
            "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
        }
    });
    datagrid_rolemenu.datagrid('enableFilter');

    /*reload datagrid rolemenu*/
    function reload_datagrid_rolemenu()
    {
        $('#datagrid_rolemenu').datagrid('removeFilterRule');
        $('#datagrid_rolemenu').datagrid('load');
    }
</script>

<?php if ($rekam == 1) { ?>
<!-- FUNCTION REKAM -->
<script type="text/javascript">
    //REKAM ROLEGROUP ---------
    function rekam_role()
    {
        let tbody = $('#tb-menu tbody');
        tbody.empty();
        $('.id_modul').val(null).trigger('change');
        $('#nama_modul').val('');
        $('#ket').val('');
        $('#modal-rekam').modal('show');
        
        $('.id_modul').on('change', function() { 
            var id_modul = $(this).val();
            $.ajax({
                url: '<?= base_url('admin/rolegroup/get_related_menu') ?>',
                type: "post",
                dataType: 'json',
                data: {
                    id_modul: id_modul,
                    "<?= csrf_token() ?>": "<?= csrf_hash() ?>",
                },
                error: function (xhr, status, message) {
                    var res = JSON.parse(xhr.responseText);
                    $(res.messages).each(function(i,val){
                        var inputname = Object.keys(val)[0];
                        $.each(val,function(k,v){
                           $("#"+inputname).focus();
                           $("#"+k).addClass("is-invalid");
                           $("#em_"+k).html(v).fadeIn();
                        });
                    });
                    BtnReset($("#btn_rekam"));
                },
                success: function(res) {
                    tbody.empty();
                    // Iterasi melalui setiap item dalam respons
                    $.each(res, function(index, item) {
                        // Buat baris baru untuk setiap item
                        let row = `
                            <tr>
                              <td class="text-center">${index + 1}</td>
                              <td>${item.nama_menu}</td>
                              <td class="text-center">${item.id_parent}</td>
                              <td class="text-center"><input type="checkbox" name="rekam_${item.id_menu}" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="ubah_${item.id_menu}" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="hapus_${item.id_menu}" value="1"></td>
                              <td class="text-center"><input type="checkbox" name="exp_${item.id_menu}" value="1"></td>
                              <td class="text-center">
                                    <button type="button" class="btn btn-danger btn-delete btn-xs" data-id="${item.id_menu}"><i class="fal fa-trash"></i></button>
                                </td>
                            </tr>
                        `;

                        // Tambahkan baris ke dalam <tbody>
                        tbody.append(row);
                    });

                    $('.btn-delete').on('click', function() {
                        // Menghapus baris dari tabel
                        $(this).closest('tr').remove();
                    });
                },
            });
        });
    }

    // Event submit form
    $('#form_rekam').on('submit', function(e) {
        e.preventDefault(); // Mencegah submit form secara langsung

        // Ambil semua data form yang sudah ada
        let formData = $(this).serialize(); 

        // Tambahkan data tabel dinamis ke formData
        $('#tb-menu tbody tr').each(function(index) {
            let id_menu = $(this).find('input[name^="rekam_"]').attr('name').split('_')[1];
            let rekam = $(this).find(`input[name="rekam_${id_menu}"]`).is(':checked') ? 1 : 0;
            let ubah = $(this).find(`input[name="ubah_${id_menu}"]`).is(':checked') ? 1 : 0;
            let hapus = $(this).find(`input[name="hapus_${id_menu}"]`).is(':checked') ? 1 : 0;
            let exp = $(this).find(`input[name="exp_${id_menu}"]`).is(':checked') ? 1 : 0;

            // Tambahkan data dinamis secara manual ke formData
            formData += `&data_tabel[${index}][id_menu]=${id_menu}`;
            formData += `&data_tabel[${index}][rekam]=${rekam}`;
            formData += `&data_tabel[${index}][ubah]=${ubah}`;
            formData += `&data_tabel[${index}][hapus]=${hapus}`;
            formData += `&data_tabel[${index}][exp]=${exp}`;
        });

        // Kirim data ke server melalui AJAX
        $.ajax({
            url: '<?= $link_rekam_rg ?>',
            type: 'post',
            dataType: 'json',
            data: formData,
            beforeSend  : function() {
                $(".form-group").removeClass("has-error");
                $(".error-message").html('').hide();
                BtnLoading($("#btn_rekam"));
            },
            success: function(res) {
                toastr.success(res.message, 'Updated!!', {timeOut: 3000});
                $('#nama_group').val('');
                $('#ket').val('');
                $('#modal-rekam').modal('hide');
                $('#datagrid_rolegroup').datagrid('load');
                $('#datagrid_rolemenu').datagrid('load');
            },
            error: function (xhr, status, message) {
                var res = JSON.parse(xhr.responseText); // Parsing respon JSON
                if (res.messages) {
                    $.each(res.messages, function (inputname, errorMessage) {
                        $("#" + inputname).addClass("is-invalid");
                        $("#em_" + inputname).html(errorMessage).fadeIn();
                        $("#" + inputname).focus();
                    });
                }
                // Reset tombol jika ada
                BtnReset($("#btn_rekam"));
            },
            complete: function() {
                BtnReset($("#btn_rekam"));
            }
        });
    });
    //END REKAM ROLEGROUP -----------

    //TAMBAH ROLEMENU----------
    function rekam_rm()
    {
        $('#form-konten').load('<?= $link_add_rm ?>');
        $('#modal-add-rm').modal('show');
    }
</script>
<?php } ?>

<?php if ($ubah == 1) { ?>
<!-- FUNCTION UBAH -->
<script type="text/javascript">
    //UBAH ROLEGROUP
    let sel_id_group = null;

    function edit_role()
    {
        var row = $('#datagrid_rolegroup').datagrid('getSelected');
        if (row) {
            sel_id_group = row.id_group;
            $('#ubah_nama_group').val(row.nama_group);
            $('#nama_modul').val(row.nama_modul);
            $('#ubah_ket').val(row.ket);
            $('#modal-ubah').modal('show');
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    $("#form_ubah").submit(function(e){
        e.preventDefault();
        var url = '<?= $link_ubah_rg ?>'
        var formData = $(this).serialize();
        formData += '&id_group=' + encodeURIComponent(sel_id_group);
        $.ajax({
            url: url,
            type: "post",
            dataType: 'json',
            data: formData,
            beforeSend  : function() {
                $(".form-group").removeClass("has-error");
                $(".error-message").html('').hide();
                BtnLoading($("#btn_ubah"));
            },
            error: function (xhr, status, message) {
                var res = JSON.parse(xhr.responseText); // Parsing respon JSON
                if (res.messages) {
                    $.each(res.messages, function (inputname, errorMessage) {
                        $("#ubah_" + inputname).addClass("is-invalid");
                        $("#em_ubah_" + inputname).html(errorMessage).fadeIn();
                        $("#ubah_" + inputname).focus();
                    });
                }
                // Reset tombol jika ada
                BtnReset($("#btn_rekam"));
            },
            success: function(result) {
                toastr.success(result.message, 'Updated!!', {timeOut: 3000});
                $('#modal-ubah').modal('hide');
                $('#datagrid_rolegroup').datagrid('load');
            },
            complete: function() {
                BtnReset($("#btn_ubah"));
            }
        });
    });
    //END UBAH ROLEGROUP

    //UBAH ROLEMENU
    let sel_id_rm = null;

    function edit_rolemenu()
    {
        var row = $('#datagrid_rolemenu').datagrid('getSelected');
        if (row) {
            sel_id_rm = row.id_rm;
            $('#nama_group_rm').val(row.nama_group);
            $('#nama_menu').val(row.nama_menu);
            $('#id_parent').val(row.id_parent);
            $('#rekam').prop('checked', row.rekam === 'Y');
            $('#ubah').prop('checked', row.ubah === 'Y');
            $('#hapus').prop('checked', row.hapus === 'Y');
            $('#exp').prop('checked', row.exp === 'Y');
            $('#modal-ubah-rm').modal('show');
        } else {
            $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    $("#form_ubah_rm").submit(function(e){
        e.preventDefault();
        var url = '<?= $link_ubah_rm ?>'
        var formData = $(this).serialize();
        formData += '&id_rm=' + encodeURIComponent(sel_id_rm);
        $.ajax({
            url: url,
            type: "post",
            dataType: 'json',
            data: formData,
            beforeSend  : function() {
                $(".form-group").removeClass("has-error");
                $(".error-message").html('').hide();
                BtnLoading($("#btn_ubah_rm"));
            },
            error: function (xhr, status, message) {
                var res = JSON.parse(xhr.responseText);
                $(res.messages).each(function(i,val){
                    var inputname = Object.keys(val)[0];
                    $.each(val,function(k,v){
                        $("#r"+inputname).focus();
                        $("#fgr_"+k).addClass("has-error");
                        $("#rem_"+k).html(v).fadeIn();
                    });
                });
            },
            success: function(result) {
                toastr.success(result.message, 'Updated!!', {timeOut: 3000});
                $('#modal-ubah-rm').modal('hide');
                $('#datagrid_rolemenu').datagrid('load');
            },
            complete: function() {
                BtnReset($("#btn_ubah_rm"));
            }
        });
    });
    //END UBAH ROLEMENU
</script>
<?php } ?>

<?php if ($hapus == 1) { ?>
<!-- FUNCTION HAPUS -->
<script type="text/javascript">
    //Hapus Rolegroup
    function delete_role()
    {
        var rows = $('#datagrid_rolegroup').datagrid('getSelections');
        var ids = [];
        for(var j=0;j<rows.length;j++){
            ids[j] = rows[j].id_group;
        }
        if (ids.length > 0) {
            $.messager.confirm('<?= lang('App.delete') ?>', '<?= lang('App.delete') ?> ' + rows.length + ' data ?', function(r){
                if (r){
                    $.ajax({
                        url: '<?= $link_hapus_rg ?>',
                        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_rolegroup').datagrid('reload');
                            $('#datagrid_rolemenu').datagrid('load');
                            toastr.success(res.message, 'Updated!!', {timeOut: 3000});
                        }
                    });
                }
            });
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }

    //Hapus Rolemenu
    function delete_rolemenu()
    {
        var rows = $('#datagrid_rolemenu').datagrid('getSelections');
        var ids = [];
        for(var j=0;j<rows.length;j++){
            ids[j] = rows[j].id_rm;
        }
        if (ids.length > 0) {
            $.messager.confirm('<?= lang('App.delete') ?>', '<?= lang('App.delete') ?> ' + rows.length + ' data ?', function(r){
                if (r){
                    $.ajax({
                        url: '<?= $link_hapus_rm ?>',
                        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_rolemenu').datagrid('reload');
                            toastr.success(res.message, 'Updated!!', {timeOut: 3000});
                        }
                    });
                }
            });
        } else {
          $.messager.alert('<?= lang('App.warning') ?>','<?= lang('App.select_data') ?>','warning');
        }
    }
</script>
<?php } ?>

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