<?php
session_start();
require_once 'db.php';
include_once "ewcfg12.php";
include_once "phpfn12.php";
include_once "class.user.php";

$user     = new USER($DB_con);

if(!$user->is_loggedin())
{
   $user->redirect('login.php');
}

// storing  request (ie, get/post) global array to a variable  
$req  = $_REQUEST;
$page = $_POST["allPage"];



// datatable column index  => database column name
$columns = array( 
	0 => 'USER_ID', 
	1 => 'STATUS_DESC', 
	2 => 'ACTIVITY_MESSAGE',
	3 => 'CREATED_AT',
);

// getting total number records without any search
$sql = "SELECT USER_ID
          FROM VIEW_WEBACTIVITYLOGS
       ";
$stmt = $DB_con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$totalData     = $stmt->rowCount();
$totalFiltered = $totalData;

//if there is a search parameter, $req['search']['value'] contains search parameter
if (isset($req['userID']) && !empty($req['userID'])){
    $where = "WHERE USER_ID like '%".$req['userID']."%'";
} else {
    $where = "";
}

if (isset($req['activity']) && !empty($req['activity'])){
    if ($where==""){
        $where = "WHERE STATUS_ID=".$req['activity']." ";
    } else {
        $where .= "  AND STATUS_ID=".$req['activity']." ";
    }
}

if (isset($req['dtFrom']) && !empty($req['dtFrom'])){
    if ($where==""){
        $where = "WHERE convert(varchar(10),CREATED_AT,120) between '".$req['dtFrom']."' AND '".$req['dtTo']."'";
    } else {
        $where .= "  AND convert(varchar(10),CREATED_AT,120) between '".$req['dtFrom']."' AND '".$req['dtTo']."'";
    }
}



// retrieve VIEW_WEBACTIVITYLOGS table
$sql  =  "SELECT  USER_ID
                 ,STATUS_ID
                 ,STATUS_DESC
                 ,ACTIVITY_MESSAGE
                 ,CREATED_AT
            FROM VIEW_WEBACTIVITYLOGS
         " . $where;
$stmt = $DB_con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$totalFiltered = $stmt->rowCount();
                      if ($page!=="ALL"){
$sql .= "  ORDER BY " . $columns[$req['order'][0]['column']] // column to sort
                      . " "
                      . $req['order'][0]['dir']              // sort order desc/asc
                      . " OFFSET "
                      . $req['start']                        // start from
                      . " ROWS FETCH NEXT "
                      . $req['length']                       // total to retrieve
                      . " ROWS ONLY ";
                      }
$stmt = $DB_con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$data = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
	$nestedData=array(); 

	$nestedData[] = $row["USER_ID"];
	$nestedData[] = $row["STATUS_DESC"];
	$nestedData[] = $row["ACTIVITY_MESSAGE"];
	$nestedData[] = $row["CREATED_AT"];

	$data[]       = $nestedData;
  
}

$json_data = array(
			"draw"            => intval( $req['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),     // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data                     // total data array
			);

echo json_encode($json_data);  // send data as json format
//echo $sql;

?>
