<?php

class Hakakses extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('Model_petugas');
    }

    function index()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/hakakses');
        $data['title'] = 'arifahpulsa';
        $data['halaman'] = 'Hak Akses';

        $data['petugas'] = $this->Model_petugas->get_all_petugas();
        $this->template->load('includes/header', 'petugas/view_hakakses', $data);
    }

    function modul()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/hakakses');
        $mst_petugas_id = $_GET['mst_petugas_id'];
        echo "<table id='example1' class='table table-bordered table-striped'>";

        $no = 1;
        echo " <thead>
                    <tr>
                        <th>No.</th>
                        <th>Nama Grup</th>
                        <th>Nama Menu</th>
                        <th>Link</th>
                        <th>Akses</th>
                    </tr>
                </thead
                <tbody>";
        $menu = $this->db->get('tbl_mst_menu');

        $no = 1;
        foreach ($menu->result() as $row) {
            echo '<tr>';
            echo '<td style="text-align: center;">' . $no++ . '</td>';
            echo '<td style="text-align: left;">' . strtoupper($row->mst_menu_grup) . '</td>';
            echo '<td style="text-align: left;">' . strtoupper($row->mst_menu_nama) . '</td>';
            echo '<td style="text-align: left;">' . $row->mst_menu_link . '</td>';
            echo "<td align='center'><input type='checkbox' ";
            $this->chek_akses($mst_petugas_id, $row->mst_menu_id);
            echo " onclick='addRule($row->mst_menu_id)'></td>";
            echo '</tr>';
        }

        echo "</tbody>
            </table>";

        echo "<script>
              
                $(document).ready(function () {
                    var groupColumn = 1;
                    var table = $('#example1').DataTable({
                        columnDefs: [{ visible: false, targets: groupColumn }],
                        responsive: true, 
                        lengthChange : true, 
                        autoWidth: true,
                        paging: true,
                        buttons: ['copy' ,'csv', 'excel', 'pdf', 'print'],
                        //order: [[groupColumn, 'asc']],
                        //displayLength: 25,
                        drawCallback: function (settings) {
                            var api = this.api();
                            var rows = api.rows({ page: 'current' }).nodes();
                            var last = null;
                
                            api
                                .column(groupColumn, { page: 'current' })
                                .data()
                                .each(function (group, i) {
                                    if (last !== group) {
                                        $(rows)
                                            .eq(i)
                                            .before('<tr ";
        echo 'class="group"><th colspan="4">';
        echo "' + group + ";
        echo "'</th></tr>');
                
                                      last = group;
                                    }
                                });
                        },
                    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
                
                  
                });
                
            </script>";
    }

    function chek_akses($mst_petugas_id, $mst_menu_id)
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/hakakses');
        $data = array('mst_akses_petugas_id' => $mst_petugas_id, 'mst_akses_menu_id' => $mst_menu_id);
        $chek = $this->db->get_where('tbl_mst_akses', $data);
        if ($chek->num_rows() > 0) {
            echo "checked";
        }
    }




    function addrule()
    {
        $this->Model_squrity->getsqurity();
        $this->Model_squrity->cekmenu('petugas/hakakses');
        $mst_petugas_id = $_GET['mst_petugas_id'];
        $mst_menu_id    = $_GET['mst_menu_id'];
        $data       = array('mst_akses_petugas_id' => $mst_petugas_id, 'mst_akses_menu_id' => $mst_menu_id);
        $menu       = array('mst_petugas_id' => $mst_petugas_id);
        $chek       = $this->db->get_where('tbl_mst_akses', $data);
        if ($chek->num_rows() < 1) {
            $this->db->insert('tbl_mst_akses', $data);
            echo "berhasil memberikan akses modul";
        } else {
            $this->db->where('mst_akses_menu_id', $mst_menu_id);
            $this->db->where('mst_akses_petugas_id', $mst_petugas_id);
            $this->db->delete('tbl_mst_akses');
            echo " berhasil delete akses modul";
        }
    }
}
