<?php
session_start();
include_once("../class/class.MySQL.php");
$db	=new MySQL();

$action = $_GET['act'];

if($action == "list"){

	//$where['is_blocked'] = 0;
	//$leaves  = $db->fetchAll("designation"); 
	$leaves = $db->fetchQuery("SELECT * FROM device join branches on branches.branch_id =device.branch_id order by dev_id DESC");
	
	$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='Device' and m.menu_id=s.menu_id "));
	
	$userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['s_submenu_id']	= $sub_rights['s_id'];
	if($userid==1)
	{
	$leaves[count($leaves)-1]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$leaves[count($leaves)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	}
	
	//print_r($leaves);
	//$leaves[0]['m']='';
	echo '{"leaves":'.json_encode($leaves).'}';
	
}elseif($action == "update"){
	$userid		= $_GET['userid'];

	$data['branch_id']	   = $_POST['branch'];
	$data['device_name']   = $_POST['device_name'];
	$data['sl_number']     = $_POST['serial_no'];
	$where['dev_id']	   = $_POST['userid'];
	$db->Update('device',$data,$where);
	echo json_encode($data);


}elseif($action == "remove"){

	$where['dev_id']	= $_GET['did'];
	$deductions = $db->Delete('device',$where);
	

}
elseif($action == "insert"){
	
	$vars['branch_id'] 	 = $_POST['branch'];
	$vars['device_name'] = $_POST['device_name'];
	$vars['sl_number'] 	 = $_POST['serial_no'];
	$vars['status'] 	 = '1';
	
	$db->Insert($vars,'device');
	
	$vars['id']				=$db->LastInsertID();
	
	echo '{"leaves":'.json_encode($vars).'}';
	
}
elseif($action == "changeStatus"){

	$set['status'] = $_GET['set'];
	$where['dev_id'] = $_GET['userid'];
	$db->Update('device',$set,$where);
	
}


?>