<?php  
// Include main file
include_once('../libs/main.php');
// Check dependant Login
include_once('checkLogin.php');

// Logical section of page
$roleObj =  new  role();
$moduleObj = new modules();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'rolelist.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['role_id']))
{
    if(isset($_REQUEST['status']))
    {
        $roleDetaildata = $roleObj->fromId($_REQUEST['role_id']);
        if($roleDetaildata->role_status == '1')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

        if($_REQUEST['status'] == '1')
        {
            $new_status = 'Inctive';
        }else{
            $new_status = 'Active';
        }
        $roleObj->changeStatus($_REQUEST['role_id']);
        $role_detail = " <b>Role status</b>: "." ".$old_status." To ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$role_detail);
        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['role_id'])){
    $modulesObj->deleteFromId($_REQUEST['id']);
    echo RECORD_DELETE;

}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("role_status" => '1');

        $chk_data = $_POST['check'];

        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $roleObj->fromId($val_actchk);
            if($old_data->role_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->role_status != '1')
            {
                $new_status = 'Active';
                $roledata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$roledata);
            }
        }
        $rs = $roleObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("role_status" => '0');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $roleObj->fromId($val_actchk);
            if($old_data->role_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->role_status != '0')
            {
                $new_status = 'Inactive';
                $roledata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$roledata);
            }
        }
        $rs = $roleObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        
        $roledata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$roledata);
            
        $rs = $roleObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    if(isset($_REQUEST['rolename']) && $_REQUEST['rolename'] != '')
    {
        $where .= " AND role_name LIKE  '%".$_REQUEST['rolename']."%'";
    }
    if(isset($_REQUEST['role_status']) && $_REQUEST['role_status'] != '')
    {
        $where .= "AND role_status LIKE '%".$_REQUEST['role_status']."%'";
    }
    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " role_id LIKE '%".$_REQUEST['search']['value']."%' OR  role_name LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
        
    $orderFields = array('role_id');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" role_id desc ");
    }

    $data   = array('where' => $where,'limit' => $limit);

    $roleDetails = $roleObj->getAllRecords($data);
    //print_r($roleDetails);exit;
    $recordsTotal   =   $roleObj->getAllCount($data);
     
    if ($roleDetails && count($roleDetails)>0){
        foreach ($roleDetails as $key => $roledata)
        {
            
            if(isset($roledata->role_access) && $roledata->role_access != '' )
            {
                if($roledata->role_access != 'Full')
                {
                    $where = " id IN (".$roledata->role_access.")";
                    $moduledataArg = array('where' => $where);
                    $moduledata = $moduleObj->getAllRecords($moduledataArg);
                    //print_r($modulepage);exit;
                    $moduleName = "";
                    foreach ($moduledata as $mkey => $mdvalue) 
                    {
                        $moduleName .= $mdvalue->name.",";
                    }
                }else
                {
                    $moduleName = 'Full';
                }
                
            }else{
                $moduleName = "";
            }
            $status = "";
            $role_edit = "";
            if($roledata->role_id != '1')
            {
                if($roledata->role_status =='1')
                {
                  $status = '<a href="javascript:void(0)" onclick="change_status('.$roledata->role_id.',1)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
                }else{
                  $status = '<a href="javascript:void(0)" onclick="change_status('.$roledata->role_id.',0)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
                }

                $role_edit = '<a href="roleadd.php?act=edit&id='.$roledata->role_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';
            }
            
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$roledata->role_id.'" class="check_class" />',
                    $roledata->role_id,
                    $roledata->role_name,
                    rtrim($moduleName,","),
                    "<div id='td_status_".$roledata->role_id."'>".$status."</div>",
                    $role_edit,
                   // date('Y-m-d',strtotime($dependant->dependant_created_at))
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>