<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));

if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call_1"){
    require "../inc/ajax_header.php";
    $role_id = $_POST["role_id"];
    $module_id = $_POST["module_id"];
    $display = $_POST["flag"];

    $modulePermissionData = $commonFunctions->getRowData('sys_erp_adm_user_access_permission','WHERE module_id = "'.$module_id.'" and role_id = "'.$role_id.'" ');
    if(!empty($modulePermissionData["id"])){
        $commonFunctions->updateData('sys_erp_adm_user_access_permission'," WHERE id = '".$modulePermissionData["id"]."' ", ' display = "'.$display.'" ','');
    }else{
        $commonFunctions->addData('sys_erp_adm_user_access_permission'," (display,role_id,module_id) VALUES ('".$display."','".$role_id."','".$module_id."' ) ",'');
    }
    exit();
}
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call"){
    require "../inc/ajax_header.php";
    $role_id = $_POST["role_id"];
    $moduleData = $commonFunctions->getData('sys_erp_module','');
    $html = "";
    $i = 0;
    foreach ($moduleData as $rw) {
        $modulePermissionData = $commonFunctions->getRowData('sys_erp_adm_user_access_permission','WHERE module_id = "'.$rw['id'].'" and role_id = "'.$role_id.'" ');
        $flag = '';$flag_text=1;
        if(@$modulePermissionData['display'] == 1){
            $flag = 'checked=""';
            $flag_text=0;
        }

        $html .= "<tr>
            <td>".$rw['name']."</td>
            <td>
                <div class='custom-control custom-switch switch-primary'>
                    <input type='checkbox' class='custom-control-input' id='customSwitchPrimary".$i."' ".$flag." onchange=change_flag('".$rw['id']."','".$role_id."','".$flag_text."')>
                    <label class='custom-control-label' for='customSwitchPrimary".$i."'></label>
                </div>
            </td>
        </tr>";
        $i++;
    }
    echo json_encode(array("table_body"=>$html));
    exit();
}
require "../inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL); ?>
    
    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col">
                        <h4 class="page-title">Admin Settings</h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Admin Settings</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">User Permission</a></li>
                        </ol>
                    </div><!--end col-->
                    <!-- <div class="col-auto align-self-center">
                        <a href="#" class="btn btn-sm btn-outline-primary" id="Dash_Date">
                            <span class="day-name" id="Day_Name">Today:</span>&nbsp;
                            <span class="" id="Select_date">Jan 11</span>
                            <i data-feather="calendar" class="align-self-center icon-xs ml-1"></i>
                        </a>
                        <a href="#" class="btn btn-sm btn-outline-primary">
                            <i data-feather="download" class="align-self-center icon-xs"></i>
                        </a>
                    </div> --><!--end col-->  
                </div><!--end row-->                                                              
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <!-- end page title end breadcrumb -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title"><span class="fas fa-tools valignmiddle widthpictotitle pictotitle" style=""></span> User Permission</h4>
                    <?php echo !empty($msg)?$msg:''; ?>        
                    <?php echo !empty($msg1)?$msg1:''; ?>
                </div><!--end card-header-->
                <div class="card-body">
                    <div class="row">    
                        <div class="col-4">
                            <label>Select Role</label>
                            <select name="role_id" id="role_id" class="form-control">
                                <option value="">Select</option>
                                <?php $roleData = $commonFunctions->getData('sys_erp_adm_user_roles',' WHERE id != 1 '); ?>
                                <?php foreach ($roleData as $rw) { ?>
                                    <option value="<?php echo $rw['id'];?>" ><?php echo $rw['role']; ?></option>     
                                <?php } ?>
                            </select>
                            <br>
                        </div>
                    </div>

                    <table id="datatable" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                        <thead>
                            <tr>
                                <th class="text-center">Modules</th>
                                <th class="text-center">Permission</th>
                            </tr>
                        </thead>
                        <tbody id="tbodyData" style="text-align: center;">
                            
                        </tbody>
                    </table>

                </div>
            </div>
        </div> <!-- end col -->
    </div> <!-- end row -->


<?php require "../inc/footer.php"; ?>
<script type="text/javascript">
    $("#role_id").change(function(){
        var role_id = $("#role_id option:selected").val();
        if(role_id != ""){
            $.ajax({
                type: 'POST',
                url: "",
                data: {"role_id":role_id,"action":"ajax_call"},
                success: function(response) { 
                    obj = JSON.parse(response);
                    $("#tbodyData").html(obj.table_body);
                }
            });
        }else{
            $("#tbodyData").html("");
        }
    });
    function change_flag(module_id,role_id,flag){
        $.ajax({
            type: 'POST',
            url: "",
            data: {"module_id":module_id, "flag":flag, "role_id":role_id, "action":"ajax_call_1"},
            success: function(response) { 
                obj = JSON.parse(response);
                $("#tbodyData").html(obj.table_body);
            }
        });
    }
</script>