<?php

require_once("function.php");

//Auth checking
$agent_id = token_auth($_POST['token']);

//change_db($con);

$master_response = array();

/***** Get user profile data *****/
$response = array();

// check for required fields
$result = mysql_query("SELECT * FROM agent WHERE id = $agent_id");

if (mysql_num_rows($result) > 0) {
    
	// looping through all results
    $response = array();
    
    while ($row = mysql_fetch_assoc($result)) {
        // temp user array
        $profile = array();
	    //$profile["id"] = $row["id"];
		$profile["firstname"] = $row["person"];
		$profile["lastname"] = $row["lastname"];
		$profile["phone"] = $row["tel"];
		$profile["email"] = $row["email"];
		$profile["initial"] = $row["initial"];
		array_push($response, $profile);
    }
    // success
	$master_response["profile"] = ($response);
	
	
} else {
    // no call purpose found
    $response["message"] = "No profile data found";

    // echo no users JSON
    $master_response['profile'] = ($response);
}


/**** All transits *****/
$response = array();
$transit_active = array();
$result = mysql_query("SELECT transit_view.*, transit.*, ba.name as boarding_agent_name, purpose_text as call_purpose_text FROM transit_view JOIN transit ON transit.transit_id = transit_view.trans_id
LEFT JOIN boarding_agent AS ba ON ba.boarding_agent_id = transit.boarding_agent_id WHERE transit.status='Awaited' order by transit.eta");

// check for empty result
if (mysql_num_rows($result) > 0) {
    
	// looping through all results
    $response = array();
    
    while ($row = mysql_fetch_assoc($result)) {
        // temp user array
       
		$transit_active[$row['trans_id']] = $row;
		
		if(!$row['picture'])
			$row['picture'] = 'images/none.png';
		
		// push single product into final response array
        array_push($response, $row);
    }

    // echoing JSON response
    $master_response["transit"] = ($response);
	
} else {
    // no call purpose found
    $response["message"] = "No transit found";

    // echo no users JSON
    $master_response['transit'] = ($response);
}


/****** Get all emails of transit******/
$response = array();

//$result = mysql_query("SELECT * FROM transit_email");
$result = mysql_query("SELECT * FROM transit_email_rtr JOIN transit ON transit.transit_id = transit_email_rtr.transit_id WHERE transit.status = 'Awaited' GROUP BY transit.transit_id");

// check for empty result
if (mysql_num_rows($result) > 0) {
    
	// looping through all results
    $response = array();
    
    while ($row = mysql_fetch_assoc($result)) {
        // temp user array
        /*$product = array();
        $product["transit_id"] = $row["transit_id"];
		$product["email1"] = $row["email1"];
		$product["email2"] = $row["email2"];
		$product["email3"] = $row["email3"];
		$product["email4"] = $row["email4"];
		$product["email5"] = $row["email5"];*/
		
		// push single product into final response array
        array_push($response, $row);
    }

    // echoing JSON response
    $master_response["transit_email"] = ($response);
	
	
} else {
    
	// no call purpose found
    //$response["message"] = "No data";
	
	$master_response['transit_email'] = [];
}


/****** Get different tables data  ******/

$tables = array('pmsc','boarding_agent', 'call_purpose');

foreach ($tables as $key => $table) {
	# code...
	$response = array();
	$result = mysql_query('SELECT * FROM '.$table);

	if (mysql_num_rows($result) > 0) {
    
		// looping through all results
	    while ($row = mysql_fetch_assoc($result)) {
	        // assign row values to an array
			array_push($response, $row);
	    }
	    // success
	  	
	  	$master_response['get_data'][$table] = $response;  
		
	} 
}


/******** Get all template and template items *****/
change_db($con);

// array for JSON response
$response = array();

$result = mysql_query("SELECT * FROM template");

$items = array();
// check for empty result
if (mysql_num_rows($result) > 0) {
    
	// looping through all results
    $response = array();
    
    while ($row = mysql_fetch_assoc($result)) {
        // temp user array
        $product = array();
        $product["template_id"] = $row["template_id"];
        $product["name"] = $row["name"];
		//$product["items"] = array();
        $template_id = $row['template_id'];
	    $r = mysql_query("SELECT * FROM template_item WHERE template_id = $template_id");

		if (mysql_num_rows($r) > 0) {
	    
			// looping through all results
		    while ($row_item = mysql_fetch_assoc($r)) {
		        // temp user array
		        $product_item = array();
		        $product_item["template_id"] = $row_item["template_id"];
		        $product_item["template_item_id"] = $row_item["template_item_id"];
				$product_item["item"] = $row_item["item"];
				$product_item["description"] = $row_item["description"];
		       	
				array_push($items, $product_item);
		    }

		}

		array_push($response, $product);
    }

    // echoing JSON response
    $master_response["template"] = ($response);
    $master_response["items"] = ($items);
	
	
} else {
    // no template found
    $response["message"] = "No Template found";

    // echo no users JSON
    $master_response['template'] = ($response);
}


/************ Get carrier tasks ************/
$response = array();
$tasks = array();
foreach ($transit_active as $key => $value) {
	# code...
	$transit_p = array();
	$transit_id = $key;
	$transit_p['carrier'] = $value['carrier'];
	$transit_p['eta'] = $value['eta'];
	$transit_p['purpose_text'] = $value['purpose_text'];
	//$transit_p['tasks'] = array();

	$result = mysql_query("SELECT * FROM event WHERE transit_id = $transit_id AND agent_id = $agent_id ORDER BY event_id");

	// check for empty result
	if (mysql_num_rows($result) > 0) {
	    
		// looping through all results
	    while ($row = mysql_fetch_assoc($result)) {
	        // temp user array

	        $transit = array();
			$transit["event_id"] = $row["event_id"];
			$transit["title"] = $row["title"];
			$transit["description"] = $row["description"];
	        $transit["transit_id"] = $row["transit_id"];
	        $transit["carrier"] = $row["carrier"];
			$transit["status"] = $row["status"];
			$transit["sent_time"] = $row["sent_time"];
			$transit["urgent_attention"] = $row["urgent_attention"];
			$transit["picture"] = $row["picture"];
			$transit["agent_id"] = $row["agent_id"];
			$transit["reported_time"] = $row["reported_time"];
			
			// push single product into final response array
	        array_push($tasks, $transit);
	    }

	} 
	array_push($response, $transit_p);
}

$master_response['transit_tasks'] = $response;
$master_response['tasks'] = $tasks;

$final_response = array(
	'data' => $master_response,
	'message' => 'All Services Data',
	'status' => 1
);


echo json_encode($final_response);

exit();
?>
