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

// Logical section of page
$adminlogObj =  new  admin_log();

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);
    
    if($_REQUEST['module_id'] != '')
    {
        $where = " 1=1 and admin_log_module_id=".$_REQUEST['module_id']." ";    
    }else{
        $where = " 1=1 ";
    }
    

    /*if(isset($_REQUEST['login_fullname']) && $_REQUEST['login_fullname'] != '')
    {

        $loginname = preg_replace('/\s\s+/', ' ', $_REQUEST['login_fullname']);
        $fullname = explode(" ", $loginname);
        if($fullname && count($fullname)>0)
        {
            $where ="";
            foreach ($fullname as $key => $f) 
            {
                $where .=" admin_fname LIKE '%".$f."%' OR admin_fname LIKE '%".ucfirst($f)."%'  OR admin_lname LIKE '%".$f."%' OR admin_lname LIKE '%".ucfirst($f)."%'";
                if( ($key+1) != count($fullname) )
                {
                        $where .=" OR ";
                }
            }
        }
    }*/

    if(isset($_REQUEST['login_user_id']) && $_REQUEST['login_user_id'] != '')
    {
        $where .=" AND  ad.admin_id = '".$_REQUEST['login_user_id']."'";   
    }

    if(isset($_REQUEST['module_name']) && $_REQUEST['module_name'] != '')
    {
        $where .=" AND  m.id = '".$_REQUEST['module_name']."'";           
    }

    /*if(isset($_REQUEST['page_name']) && $_REQUEST['page_name'] != '')
    {
        $where .=" AND  page LIKE '%".$_REQUEST['page_name']."%'";           
    }*/

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " a.admin_log_id LIKE '%".$_REQUEST['search']['value']."%' OR m.name LIKE '%".$_REQUEST['search']['value']."%' OR mp.page LIKE '%".$_REQUEST['search']['value']."%'  OR ad.admin_fname LIKE '%".ucfirst($_REQUEST['search']['value'])."%' OR ad.admin_lname LIKE '%".ucfirst($_REQUEST['search']['value'])."%' OR ad.admin_fname LIKE '%".ucwords($_REQUEST['search']['value'])."%' OR ad.admin_lname LIKE '%".ucwords($_REQUEST['search']['value'])."%' OR a.admin_log_detail LIKE '%".$_REQUEST['search']['value']."%'";
    }    
        
    $orderFields = array('admin_log_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("admin_log_id desc ");
    }
    

    $tables = array('tbl_admin ad','tbl_modules m','tbl_module_pages mp');
    $jfields = array('a.admin_log_admin_id = ad.admin_id','a.admin_log_module_id = m.id','a.admin_log_page_id = mp.id');
    $jtype   = array("INNER","INNER","LEFT");
    $data    = array('where' => $where, 'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);
    $adminlogDetails = $adminlogObj->getAllRecords($data);
    $recordsTotal   =   $adminlogObj->getAllCount($data);
        
    if ($adminlogDetails && count($adminlogDetails)>0){
        foreach ($adminlogDetails as $key => $adminlogdata){

        
            $response[] = array( 
                    
                    "<b>Name</b>: ".$adminlogdata->admin_fname." ".$adminlogdata->admin_lname."<br/><b>Email</b> : ".$adminlogdata->admin_email_address,
                    $adminlogdata->name,
                    $adminlogdata->admin_log_create_at,
                    $adminlogdata->admin_log_detail
            );
        }
        $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');
?>