// Service : packageRequest

$mainframe = JFactory::getApplication();
$mailfrom  = $mainframe->getCfg( 'mailfrom' );
$sitename  = $mainframe->getCfg( 'sitename' );
$option = JRequest::getVar('option','com_webservice','','string');
global $HTTP_RAW_POST_DATA;
$post = json_decode($HTTP_RAW_POST_DATA, true);

$table_prefix = '#__jetour_';
$uri = JURI::getInstance();
$url= $uri->root();


/*$post['package_id'] = 474;
$post['email'] = 'vishal@harmistechnology.com';
$post['comment'] = 'Testing TEST';
$post['fullname'] = 'Vishal Gothi';
$post['phone_no'] = '0123465789';*/


if(@$post['package_id']){
			$first_lastname = explode(' ',$post['fullname']);
			$queryquotation = 'INSERT INTO '.$table_prefix.'request_form (`package_id`,`first_name`,`last_name`,`email`,`phone_no`,`comment`) VALUES ("'.$post['package_id'].'","'.$first_lastname[0].'","'.$first_lastname[1].'","'.$post['email'].'","'.$post['phone_no'].'","'.$post['comment'].'")';
			$db->setQuery( $queryquotation );
			$db->Query();
			
			$query = 'SELECT * FROM '.$table_prefix.'setting WHERE id = 1 ';
			$db->setQuery( $query );
			$settinginfo = $db->loadObject();
			
			$subject 	= $post['fullname'].' : '.JText::_( 'YOU_REQUEST_FOR_PACKAGE' );
			
			$messagebody = '';
			$messagebody .= '<table>';
		
			 
			$messagebody .= '<tr><td>'.JText::_( 'WE_ARE_CONTACT_SOON' ).'</td>';
			$messagebody .= '</tr>';
			$messagebody .= '<tr><td>'.JText::_( 'WE_WILL_CONTACT_YOU_SOON' ).'</td>';
			$messagebody .= '</tr>';
			$messagebody .= '<tr><td>'.JText::_( 'THANKS_REGARDS' ).'</td>';
			$messagebody .= '</tr>';
			$messagebody .= '<tr><td>'.$sitename.'</td>';
			$messagebody .= '</tr>';
		
			$messagebody .='</table>';
			
			JFactory::getMailer()->sendMail($settinginfo->package_req_email, 'WilD Discovery', $post['email'], $subject,$messagebody,1);
			
			$querypack = 'SELECT pname,packagetype_id FROM '.$table_prefix.'package WHERE id ='.$post['package_id'];
			$db->setQuery( $querypack );
			$querypackage = $db->loadObject();
			
			$subject2 	= 'Package Request';
			
			$messagebodyadmin = '';
			$messagebodyadmin .= '<table>';
			$messagebodyadmin .= '<tr><td>Dears</td></tr>';
			$messagebodyadmin .= '<tr><td>'.$post['fullname'].' Was Requesting for "'.$querypackage->pname.'" package</td></tr>';
			$messagebodyadmin .= '<tr><td><u>User Information</u></td></tr>';
			$messagebodyadmin .= '<tr><td>Name : '.$post['fullname'].'</td>';
			$messagebodyadmin .= '</tr>';
			$messagebodyadmin .= '<tr><td>Email : '.$post['email'].'</td>';
			$messagebodyadmin .= '</tr>';
			if(@$post['phone_no']){
			$messagebodyadmin .= '<tr><td>Phone No : '.$post['phone_no'].'</td>';
			$messagebodyadmin .= '</tr>';
			}
			if(@$post['comment']){
			$messagebodyadmin .= '<tr><td>Comments : '.$post['comment'].'</td>';
			$messagebodyadmin .= '</tr>';
			}
			$messagebodyadmin .='</table>';
			$messagebodyadmin .='<table>';
			$messagebodyadmin .= '<tr><td><u>Requested Package Information</u></td></tr>';
			
			$messagebodyadmin .= '<tr><td><u>Package : '.$querypackage->pname.'</u></td></tr>';
			
			$messagebodyadmin .='</table>';
			
			$messagebodyadmin .='<table>';	
			$messagebodyadmin .= '<tr><td>'.JText::_( 'THANKS_REGARDS' ).'</td>';
			$messagebodyadmin .= '</tr>';
			$messagebodyadmin .= '<tr><td>'.$sitename.'</td>';
			$messagebodyadmin .= '</tr>';
		
			$messagebodyadmin .='</table>';
			
			JFactory::getMailer()->sendMail($mailfrom, 'WilD Discovery', $settinginfo->package_req_email, $subject2, $messagebodyadmin,1);
			//JFactory::getMailer()->sendMail($mailfrom, 'WilD Discovery', 'vishal@harmistechnology.com', $subject2, $messagebodyadmin,1);
			$arr = array('success' => 1, 'message' => "Your Request has been sent !" ); 
 			echo json_encode($arr);exit;
			
}else{
	$arr = array('success' => 0, 'message' => "No Records Found" ); 
 	echo json_encode($arr);exit;
}