<?php 
include_once('../libs/main.php');

$userPermissionObj = new userPermission();

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $page = (isset($_GET['start']) && $_GET['start']!='0') ? $_GET['start'] : 0;
    $limit = array('start'=>$page,'record'=>$records);

    $where = " 1=1 ";

    if(isset($_REQUEST['title']) && $_REQUEST['title'] != ''){
        $where .= " AND title LIKE '%".$_REQUEST['title']."%' ";
    }
    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $s = $_REQUEST['search']['value'];
        $where = " title LIKE '%$s%' ";
    }

    $orderFields = array('id','title');    
    $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(" id desc ");
    }

    $data = array('where' => $where,'limit' => $limit,'orderBy'=>$orderby);
    
    //$data['debug']=true;

    $details = $userPermissionObj->getAllRecords($data);
    $recordsTotal = $userPermissionObj->getAllCount($data);

    if ($details && count($details)>0){
        foreach ($details as $perm){
            
            $action = '<a href="userPermissionadd.php?act=edit&id='.$perm->id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2"></i></a>';

            $response[] = array(
                '<input type="checkbox" name="check[]" value="'.$perm->id.'" class="check_class" />',
                $perm->id,
                $perm->title,
                $perm->created_at,
                $action
            );
        }
        $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');
?>
