<?php /* var_dump($permission); */ ?>
<?php (empty($permission) ? redirect(base_url("error404")) : ""); ?>
<?php if (!empty($this->app->is_can($permission, "view"))) { /* Can View */ ?>
    <!-- Google Font: Source Sans Pro -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>plugins/fontawesome-free/css/all.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/adminlte.min.css">
    <!-- overlayScrollbars -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">
    <!-- Toastr -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>plugins/toastr/toastr.min.css">

    <style>
        .IsNo {
            width: 30px;
        }

        .IsNotSort {
            width: 30px;
        }

        .table td,
        .table th {

            font-size: 12px;
        }
    </style>

    <!-- ============================================================== -->
    <!-- Start Page Content -->
    <!-- ============================================================== -->
    <?php
    $sql = "SELECT * FROM sys_module WHERE parent_module ='' AND record_status='N' AND is_active ='1' ORDER BY sort ASC";
    ?>
    <div class="col-12 pt-3">
        <div class="text-left">
            <a class="btn btn-sm mb-2" href="<?php echo base_url(); ?>sys/group">
                <h5><i class="fa fa-backward"></i> <?php echo $group->group_name; ?></h5>
            </a>
        </div>
        <div class="table-responsive m-t-40">
            <form method="post" action="<?php echo base_url('sys/permission/set'); ?>">
                <input type="hidden" name="group_id" value="<?php echo $group->id; ?>">
                <table id="mTable" class="table table-bordered table-striped table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th class="IsNo text-center">NO.</th>
                            <th>MODULE</th>
                            <th>NAME</th>
                            <th>DESCRIPTION</th>
                            <?php if ($this->app->is_can($permission, "edit")) { ?>
                                <th class="IsNotSort text-center">ดู</th>
                                <th class="IsNotSort text-center">เพิ่ม</th>
                                <th class="IsNotSort text-center">แก้ไข</th>
                                <th class="IsNotSort text-center">ลบ</th>
                                <th class="IsNotSort text-center">อนุมัติ</th>
                            <?php } ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($this->db->query($sql)->result() as $i1 => $item) {
                        ?>
                            <tr style="background-color: beige;">
                                <td><?php echo $i1 + 1; ?></td>
                                <td><?php echo $item->module; ?></td>
                                <td><i class="<?php echo $item->module_icon; ?>"></i> <?php echo $item->module_name; ?></td>
                                <td colspan="6"><?php echo $item->module_description; ?></td>
                            </tr>
                            <?php
                            $sql = "SELECT sys_module.*
                            , sys_permission.view AS vw
                            , sys_permission.add AS ad
                            , sys_permission.edit AS ed 
                            , sys_permission.delete AS de  
                            , sys_permission.approve AS ap  
                            FROM sys_module 
                            LEFT JOIN sys_permission ON sys_module.module = sys_permission.module AND sys_permission.group_id='" . $group->id . "' 
                             WHERE sys_module.parent_module ='" . $item->module . "' 
                             AND sys_module.record_status='N' AND sys_module.is_active ='1'      
                             ORDER BY sys_module.sort ASC";
                            // echo $sql;
                            foreach ($this->db->query($sql)->result() as $i2 => $item2) {
                                $permission = $this->app->encrypt_segment($item2->module);
                            ?>
                                <tr title="<?php echo $item2->module_name; ?>">
                                    <td style="text-indent: 2px;"><i class="fa fa-caret-right"></i> <?php echo $i2 + 1; ?></td>
                                    <td><?php echo $item2->module; ?></td>
                                    <td><i class="<?php echo $item2->module_icon; ?>"></i> <?php echo $item2->module_name; ?></td>
                                    <td><?php echo $item2->module_description; ?></td>
                                    <td class="text-center">
                                        <input type="checkbox" name="<?php echo $permission; ?>[]" value="view" <?php echo ($item2->view == 0 ? 'disabled' : '') ?> <?php echo ($item2->vw == 1 ? 'checked' : '') ?>>
                                    </td>
                                    <td class="text-center">
                                        <input type="checkbox" name="<?php echo $permission; ?>[]" value="add" <?php echo ($item2->add == 0 ? 'disabled' : '') ?> <?php echo ($item2->ad == 1 ? 'checked' : '') ?>>
                                    </td>
                                    <td class="text-center">
                                        <input type="checkbox" name="<?php echo $permission; ?>[]" value="edit" <?php echo ($item2->edit == 0 ? 'disabled' : '') ?> <?php echo ($item2->ed == 1 ? 'checked' : '') ?>>
                                    </td>
                                    <td class="text-center">
                                        <input type="checkbox" name="<?php echo $permission; ?>[]" value="delete" <?php echo ($item2->delete == 0 ? 'disabled' : '') ?> <?php echo ($item2->de == 1 ? 'checked' : '') ?>>
                                    </td>
                                    <td class="text-center">
                                        <input type="checkbox" name="<?php echo $permission; ?>[]" value="approve" <?php echo ($item2->approve == 0 ? 'disabled' : '') ?> <?php echo ($item2->ap == 1 ? 'checked' : '') ?>>
                                    </td>
                                </tr>
                            <?php
                            }
                            ?>
                        <?php
                        }
                        ?>

                    </tbody>
                </table>
                <div class="text-right">
                    <button type="submit" class="btn btn-block btn-primary">บันทึก</button>
                </div>
            </form>
        </div>
    </div>

    <!-- jQuery -->
    <script src="<?php echo base_url(); ?>plugins/jquery/jquery.min.js"></script>
    <!-- jQuery UI 1.11.4 -->
    <script src="<?php echo base_url(); ?>plugins/jquery-ui/jquery-ui.min.js"></script>
    <!-- Bootstrap 4 -->
    <script src="<?php echo base_url(); ?>plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <!-- SweetAlert2 -->
    <script src="<?php echo base_url(); ?>plugins/sweetalert2/sweetalert2.min.js"></script>
    <!-- Toastr -->
    <script src="<?php echo base_url(); ?>plugins/toastr/toastr.min.js"></script>
    <!-- overlayScrollbars -->
    <script src="<?php echo base_url(); ?>plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
    <!-- AdminLTE App -->
    <script src="<?php echo base_url(); ?>dist/js/adminlte.js"></script>

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

        $(function() {
            <?php if ($this->session->flashdata('type')) { ?>
                toastr.<?php echo $this->session->flashdata('type') ?>('<?php echo $this->session->flashdata('type') ?>', '<?php echo $this->session->flashdata('msg') ?>', "top-right", "#ff6849", "1000");
            <?php } ?>

        });
    </script>
<?php
} /* Can View */ ?>