<?php
class inet {
	
	public function __construct(){
		global $err;
		
		$act=preg_replace('/[^a-z_]/','',$_GET['act']);
		if($act){
			call_user_func(array(&$this,$act));
		} 
	}
	
	public function get(){
		global $DB;
		$limit=intval($_POST['limit']);
		$start=intval($_POST['start']);
		
		$dir=preg_replace("/[^(ASC|DESC)]/",'',$_POST['dir']);
		if($dir!='ASC'&&$dir!='DESC') $dir='ASC';
		$sort=$_POST['sort'];
		
		if(!$sort){
			$sort='id';
		}

		$where_str=array();
		
		function parse_sql_query($item){
			return $item;
		}
		
		$search=$_POST;
		if (strlen($search["login"]) && $search["login"] != "Логин"){
			$query = split(" ", $search["login"]);
			foreach ($query as $item)
			$where_str[] = " cli.login LIKE '%".parse_sql_query($item)."%'";
		}		
		if (strlen($search["ip"]) && $search["ip"]!="IP-адрес") $where_str[]= "inter.ip LIKE '%".$search["ip"]."%'";
		if (strlen($search["mac"]) && $search["mac"]!="МАС-адрес") $where_str[]= "inter.mac LIKE '%".$search["mac"]."%'";
		if (count($where_str)) {
			$where = join(" AND ", $where_str);			
		} else {
			$where = "1=1";
		}
		
		$data_t = $DB->selectPage($totalCount,"
								SELECT cli.login,cli.balance,inter.*,pack.name AS pname,idate.name AS pay_day,vl.name AS vname
								FROM internet AS inter 
								LEFT JOIN packets AS pack ON (inter.packet_id=pack.id)
								LEFT JOIN idate ON (inter.time_id=idate.id)
								LEFT JOIN vlans AS vl ON (inter.vlan_id=vl.id)
								LEFT JOIN clients AS cli ON (inter.client_id=cli.id)
								WHERE $where 
								ORDER BY $sort $dir 
								LIMIT ?d,?d",$start,$limit);
		foreach($data_t as $data_k=>$data_v)
		{
			$data[$data_k]["id"] = $data_v["id"];
			$data[$data_k]["login"] = $data_v["login"];
			$data[$data_k]["pname"] = $data_v["pname"];
			$data[$data_k]["vname"] = $data_v["vname"];
			$data[$data_k]["pay_day"] = $data_v["pay_day"];
			$data[$data_k]["ip"] = $data_v["ip"];
			$data[$data_k]["mac"] = $data_v["mac"];
			if($data_v["active"]==1) $data[$data_k]["status"] = "Включена";
			elseif($data_v["active"]==2) $data[$data_k]["status"] = "Отключена принудительно"; 
			elseif($data_v["balance"]<0)$data[$data_k]["status"] = "Минусовой балланс"; 
			else $data[$data_k]["status"] = "Отключена";
		}
		$json=array("totalCount"=>$totalCount,"data"=>$data);
		echo json_encode($json);
	}
	
}
?>