<?php
include('../extension/connect.php');
include('../extension/check-login.php');
//include('../extension/rank_check.php');
$userid = $_SESSION['userid'];

$cred = mysqli_query($con,"select user_rank from users where user_id='$userid'");
$myinfo = mysqli_fetch_array($cred);
$mrank = $myinfo['user_rank'];

$thm = mysqli_query($con,"select theme, theme_text from admin");
$thminfo = mysqli_fetch_array($thm);
$thm_color = $thminfo['theme'];
$thm_text = $thminfo['theme_text'];

$requestData= $_REQUEST;
if(empty($requestData)){
	header("location:dashboard");
	exit;	
}

$columns = array( 
	0	=> 'description', 
	1	=> 'date'
);

$query = mysqli_query($con,"select id, user_id, description, date from credit_log");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;

$sql = "select id, user_id, description, date from credit_log where 1=1 and user_id='$userid'";
$query = mysqli_query($con,$sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;

if( !empty($requestData['search']['value']) ) { 
	$sql.=" AND ( description LIKE '%".$requestData['search']['value']."%' "; 
	$sql.=" OR date LIKE '%".$requestData['search']['value']."%' ) ";
}

$query = mysqli_query($con,$sql);
$totalFiltered = mysqli_num_rows($query);
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

$query = mysqli_query($con,$sql);

$data = array();
while($row = mysqli_fetch_array($query)) {
	$nestedData=array();
    
    $id = $row['id'];
    $user_id = $row['user_id'];
    $description = $row['description'];
    $date = $row['date'];
                           
	$aydi = "'$id'";
	$delete_ = "'delete'";
	
	$nestedData[] = '<i class="fa fa-caret-right"></i>';
	$nestedData[] = $description;
	$nestedData[] = $date;

	$data[] = $nestedData;	
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] )? intval( $_REQUEST['draw'] ) : 0,
			"recordsTotal"    => intval( $totalData ),
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => ($data )
			);

echo json_encode($json_data);
?>