<?php
session_start();
header('Content-Type: text/html; charset=utf-8');

  $role = $_SESSION['role'];
  $id_user = $_SESSION['id'];

	$dbset = json_decode($_SESSION['settingsApp'],true);
	$db = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);

		
	$db->query("SET CHARSET SET utf8");
	$db->query("SET NAMES 'utf8'");
	if (mysqli_connect_errno()) {
		echo "Error connecting to DB";
		exit;
	}

if(isset($_REQUEST['load'])){
	
	$total = 0;
	
	$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
	$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 30;
	
	$query="SELECT COUNT(id) FROM ms_photo_spy;";
	if ($stmt = $db->prepare($query)) 
	{
        $stmt->execute();
        $stmt->bind_result($total);
        $stmt->fetch();
        $stmt->close();
	}
	
	$query = "SELECT * FROM ms_photo_spy LIMIT ".abs($start).",".($limit).";";

	if(!$stmt = $db->query($query)){
			echo '{"success":false,"message":"'.$db->errno.' '.$db->error.'"}';
	}else{
			for ($result = array(); $tmp = $stmt->fetch_array(MYSQLI_ASSOC);)
			{
				$tmp['fileName'] = '<a href="app/SFA/photo/'.$tmp['fileName'].'" target="_blank" >'.$tmp['fileName'].'</a>';
				$result[] = $tmp;
			}
			
			$stmt->close();
			echo '{"total":"'.$total.'","rows":'.json_encode($result).'}';
	}
	$db->close();
	exit;
}

?>