<?php
include('config.php');
$search=$_POST['search']['value'];
$length=$_POST['length'];


$from_date=$_POST['from_date'];
$to_date=$_POST['to_date'];
if($search==""){
 
        $sql = "SELECT count(*)as count from login_history   order by created_on desc";

    $res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
if($from_date==""){
$sql = "SELECT * from login_history  order by created_on desc limit $start,$length";
 }else{
     $sql = "SELECT * from login_history where created_on>='".$from_date." 00:00:00' and created_on<='".$to_date." 23:59:59'  order by created_on desc limit $start,$length";

 }//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}else{
    
    
$sql = "SELECT count(*) as count from login_history where    (created_on like '%".$search."%' or ip like '%".$search."%' ) order by created_on desc";
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
$sql = "SELECT * from login_history  where    (created_on like '%".$search."%' or ip like '%".$search."%' ) order by created_on desc limit $start, $length";
//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}
?>