<?php
	session_start();
	include_once("../config.php");
	if(isset($_SESSION['objLogin'])){
		if (isset($_GET['token']) && $_GET['token'] === 'getTenantData') {
			$json = [];
			if (!empty($_REQUEST['term'])) {
				$term = mysqli_real_escape_string($link, $_REQUEST['term']);
				$query = "SELECT tid, r_name, r_email, r_contact 
						  FROM tenants 
						  WHERE r_name LIKE '%$term%' 
						  ORDER BY r_name ASC";
		
				$result = mysqli_query($link, $query);
		
				if ($result) {
					while ($row = mysqli_fetch_assoc($result)) {
						$json[] = [
							'id' => $row['tid'],
							'name' => $row['r_name'],
							'email' => $row['r_email'],
							'contact' => $row['r_contact']
						];
					}
				} else {
					error_log("Query Error: " . mysqli_error($link));
				}
			}
			echo json_encode($json);
			die();
		}

	   if(isset($_GET['token']) && $_GET['token'] == 'getOwnerData'){
			$json = array();
			if(!empty($_REQUEST['term'])){
				$result = mysqli_query($link,"SELECT * from tbl_add_owner where o_name LIKE '%".$_REQUEST['term']."%' and branch_id = " . (int)$_SESSION['objLogin']['branch_id'] . " ORDER BY o_name ASC");
				while($rows = mysqli_fetch_array($result)){
					$json[] = array(
						'id'		=> $rows['ownid'],
						'name'		=> $rows['o_name'],
						'mobile'	=> $rows['o_contact'],
						'email'		=> $rows['o_email']
					);
				}
			}
			echo json_encode($json);
			die();
	   }
	   if(isset($_GET['token']) && $_GET['token'] == 'getEmpData'){
			$json = array();
			if(!empty($_REQUEST['term'])){
				$result = mysqli_query($link,"SELECT * from tbl_add_employee where e_name LIKE '%".$_REQUEST['term']."%' and branch_id = " . (int)$_SESSION['objLogin']['branch_id'] . " ORDER BY e_name ASC");
				while($rows = mysqli_fetch_array($result)){
					$json[] = array(
						'id'		=> $rows['eid'],
						'name'		=> $rows['e_name'],
						'mobile'	=> $rows['e_contact'],
						'email'		=> $rows['e_email']
					);
				}
			}
			echo json_encode($json);
			die();
	   }
	} else{
		echo '-99';
		die();
	}
?>
