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

$userPermissionObj = new userPermission();
$userPermissionObj->setPrimaryKey('id');

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)*$records;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    if(isset($_REQUEST['title']) && $_REQUEST['title'] != ''){
        $where .= " AND title = '".$_REQUEST['title']."' ";
    }
    
    if(isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND (title LIKE '%".$_REQUEST['search']['value']."%' )";
    }    
        
    $orderFields = array('id','title');
    
    // 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(" id desc ");
    }

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

    $permissionDetails = $userPermissionObj->getAllRecords($data);
    $recordsTotal = $userPermissionObj->getAllCount($data);
        
    if($permissionDetails && count($permissionDetails)>0){
        foreach($permissionDetails as $permission){
            $action = '<a href="userPermissionAdd.php?act=edit&id='.$permission->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="'.$permission->id.'" class="check_class" />',
                $permission->id,
                $permission->title,
                date("d-m-Y H:i:s", strtotime($permission->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;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['id'])){
    $userPermissionObj->deleteFromId($_REQUEST['id']);
    $successMsg = base64_encode(RECORD_DELETE);
    header("Location: userPermissionList.php?successMsg=".$successMsg);
    exit;
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="delete_all"){
        $rs = $userPermissionObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
        header("Location: userPermissionList.php?successMsg=".$successMsg);
        exit;
    }
}

if(isset($_REQUEST['successMsg'])){
    $successMsg = $_REQUEST['successMsg'];
}

if(isset($_REQUEST['failedMsg'])){
    $failedMsg = $_REQUEST['failedMsg'];
}

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