<?php

// Map your method
Flight::map('verifyUser', function($login){
	$db = Flight::get('db');
	
	$username = $login[0];
	$password = $login[1];
	
	$stmt = $db->prepare('select * from tbl_users where username = :username and password = :password');
	$stmt ->execute(array(':username' => $username, ':password' => $password));
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	return $row;
});

//Select User Profile
Flight::map('selectAll', function($username){
	$db = Flight::get('db');
	
	//$username = $test[0];
	//$password = $test[1];
	
	$stmt = $db->prepare('select * from tbl_users where username = :username');
	$stmt ->execute(array(':username' => $username));
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	return $row;
});

// Update User Password
Flight::map('updatePass', function($credential){
	$db = Flight::get('db');
	
	$username = $credential[0];
	$password = $credential[1];
	
	$stmt = $db->prepare('update tbl_users set employee_pass = :new_pass where employee_no = :username ');
	$stmt-> execute(array(':new_pass' => $password, ':username' => $username));

});

// Used to update LM encrypted password
Flight::map('lm_save_pass', function($lm_data){
	$db = Flight::get('db');
	
	$employeeid = $lm_data[0];
	$password = $lm_data[1];
	
	$stmt = $db->prepare("update employee set password = AES_ENCRYPT(:pass, 'portal123') where employeeid = :employeeid ");
	$stmt-> execute(array(':pass' => $password, ':employeeid' => $employeeid));

});

// Used to insert uploaded schedule
Flight::map('uploadSchedule', function($uploadStatus){
	$db = Flight::get('db');
	
	if(isset($_SESSION['UPLOAD'])){
		$rows = $_SESSION['UPLOAD'];
	}
	
	try {
		foreach ($rows as $row) {
			/* echo '<tr>';
			echo '	<td>'.$row['EmployeeNo'].'</td>';
			echo '	<td>'.$row['PrincipalCode'].'</td>';
			echo '	<td>'.$row['StoreLocationCode'].'</td>';
			echo '	<td>'.$row['WorkingDays'].'</td>';
			echo '	<td>'.$row['StartTime'].'</td>';
			echo '	<td>'.$row['EndTime'].'</td>';		
			echo '	<td>'.$row['isActive'].'</td>';
			echo '</tr>'; */	

			$stmt = $db->prepare("INSERT INTO deploymentassignment ( employeeNo,  customerCode,  storeLocCode,  workingdays,  starttime,  endtime,  isActive)
											                VALUES (:employeeNo, :customerCode, :storeLocCode, :workingdays, :starttime, :endtime, :isActive)");
			$stmt-> execute(array(':employeeNo' => $row['EmployeeNo'], ':customerCode' => $row['PrincipalCode'], ':storeLocCode' => $row['StoreLocationCode'], ':workingdays' => $row['WorkingDays'], ':starttime' => $row['StartTime'], ':endtime' => $row['EndTime'], ':isActive' => $row['isActive']));
		}
		
		$uploadStatus = '<div id="uploadStatus"><h1 class="title is-5">Record has been successfully uploaded.</h1><a href="./3" class="button is-link is-small"><b>Upload another record</b></a></div>';
	} catch(Exception $e) {
		$uploadStatus = '<div id="uploadStatus"><h1 class="title is-5" style="color:red;">There is a problem uploading your record. Please try again.</h1><a href="./3" class="button is-warning is-small"><b>Go back</b></a></div>';
	}
	
	if(isset($_SESSION['UPLOAD'])){
		unset($_SESSION['UPLOAD']);
	}
	
	return $uploadStatus;
});

// Verify User
Flight::map('verifyUserxxx', function($username, $passwd){
	$db = Flight::get('db');
	
	$stmt = $db->prepare('select * from tbl_users where username=:username and password=:password');
	$stmt ->execute(array(':username' => $username,':password' => $passwd));
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	$row = 'ok';
	return $row;
});

?>