<?php
	@session_start();
	//======================================
	include('lib/config.php');
	require_once 'scripts/PHPMailer/class.phpmailer.php';
	//======================================
	
	if(isset($_POST['action']) && $_POST['action'] == 'arequest')
	{
		$organization_name = isset($_POST['organization_name'])?$_POST['organization_name']:'';
		$full_name = isset($_POST['full_name'])?$_POST['full_name']:'';
		$contactno = isset($_POST['contactno'])?$_POST['contactno']:'';
		$email = isset($_POST['email'])?$_POST['email']:'';
		
		//--------------------- Send Email ---------------------
		$messageBody = '<html>
		<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">			
		</head>	
		<body>
		<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Dear Admin (RI),</font></p>
		<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">A new Request for account creation on Stakeholders Mapping has been placed. Here are the request details.</font></p>
		<table width="100%" border="0" cellspacing="2" cellpadding="2">
		  <tr>
			<td width="14%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Organization Name:</font></td>
			<td width="86%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>'. $organization_name .'</strong></font></td>
		  </tr>
		  <tr>
			<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Name:</font></td>
			<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>'.$full_name.'</strong></font></td>
		  </tr>
		  <tr>
			<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Contact#:</font></td>
			<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>'.$contactno.'</strong></font></td>
		  </tr>
		  <tr>
			<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email:</font></td>
			<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>'.$email.'</strong></font></td>
		  </tr>
		</table>
		<hr size="1">
		<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Best Regards,<br />
		  <a href="http://rehabinitiative.org" style="color:#000000;">Rehab Intiative</a><br /></font>
		  </p>
		</body>
		</html>';
		
		$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

		try {
			  //$mail->AddReplyTo('name@yourdomain.com', 'First Last'); 
			  $mail->AddAddress("amjad@rehabinitiative.org", "Muhammad Amjad");
			  $mail->AddAddress("amjad187@gmail.com", "Muhammad Amjad");
			  $mail->SetFrom('rehabinitiativercm@rcmsonline.org', 'Rehab initiative');
			  $mail->AddReplyTo('noreply@rcmsonline.org', 'Rehab initiative');
			  $mail->Subject = 'Request for account - Stakeholders Mapping';
			  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
			  //$mail->MsgHTML(file_get_contents('contents.html'));
			  $mail->MsgHTML($messageBody);
			  //$mail->AddAttachment('images/phpmailer.gif');      // attachment
			  //$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
			  $mail->Send();
		} catch (phpmailerException $e) {
		  echo $e->errorMessage(); //Pretty error messages from PHPMailer
		} catch (Exception $e) {
		  echo $e->getMessage(); //Boring error messages from anything else!
		}
		//----------------------------------------------------------------
		
		$msg = 'Account request sent successfully';
		redirectPage('account-request.php',$msg);
		exit();
	}
	elseif(isset($_POST['action']) && $_POST['action'] == 'login')
	{
		if( (isset($_POST['user_name']) && $_POST['user_name']!='') && (isset($_POST['user_password']) && $_POST['user_password']!='') )
		{
			$encPW = md5($_POST['user_password']);
			
			$selQuery = "SELECT * FROM ".TBL_USERS." WHERE user_name = '$_POST[user_name]' AND user_password = '$encPW'";
			$userInfo = $con->getSingleRow($selQuery);		
	
			if(isset($userInfo['user_id']) && $userInfo['user_id'] !='')
			{
				$_SESSION['userID'] = $userInfo['user_id'];
				$_SESSION['userGroupID'] = $userInfo['group_id'];
				$_SESSION['userFullName'] = $userInfo['first_name'].''.$userInfo['last_name'];
				$_SESSION['userName'] = $userInfo['user_name'];
				$_SESSION['user_pic'] = $userInfo['user_pic'];
				
				//--------------- Users Info ----------------							  								  
				  $selQry_users = "SELECT * FROM ".TBL_USERS." WHERE group_id !='1' ORDER BY first_name";
				  $usersInfo =  $con->getMultiRow($selQry_users, __FILE__,__LINE__);
				  $totUsersListings = count($usersInfo);
				  //--------------- Departments Info ----------
				  $selQry_depts = "SELECT * FROM ".TBL_DEPARTMENTS." ORDER BY name";
				  $deptsInfo =  $con->getMultiRow($selQry_depts, __FILE__,__LINE__);
				  $totDeptsListings = count($deptsInfo);
				  //--------------- Files Info ----------------
				  $selQry_files = "SELECT * FROM ".TBL_FILES." ORDER BY name";
				  $filesInfo =  $con->getMultiRow($selQry_files, __FILE__,__LINE__);
				  $totFilesListings = count($filesInfo);
				  //--------------- Stakeholders Info ---------
				  $selQry_sh = "SELECT * FROM ".TBL_STAKEHOLDERS." ORDER BY name";
				  $shInfo =  $con->getMultiRow($selQry_sh, __FILE__,__LINE__);
				  $totSHListings = count($shInfo);
				//---------------------------------------------		
				if( ($totUsersListings>0) && ($totDeptsListings>0) && ($totFilesListings>0) && ($totSHListings>0) )
				{
					redirectPage('dashboard.php','');
					exit();
				}
				else
				{
					redirectPage('configurations.php','');
					exit();
				}			
			}
			else
			{	
				$msg = 'Invalid user email or password.';
				
				redirectPage('index.php',$msg);
				exit();
			}
	}
		else
		{
			$msg = 'Missing info: please provide both Email and Password.';			
			redirectPage('index.php',$msg);
			exit();
		}	
	}
	elseif(isset($_GET['TP']) && $_GET['TP'] == 'LO')
	{
		unset($_SESSION["userID"]);	
		unset($_SESSION["userGroupID"]);	
		unset($_SESSION["userFullName"]);
		unset($_SESSION["userName"]);
		unset($_SESSION["user_pic"]);
		
		$msg = 'Logout successfully';		
		redirectPage('index.php',$msg);
		exit();
	}
	else
	{
		$msg = 'No activity selected.';
		redirectPage('index.php',$msg);
		exit();
	}
?>