<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions


$debug = true;

if(isset($_REQUEST['p'])){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!=''){
	$userId = trim($_REQUEST['user_id']);
	$curDate = date('Y-m-d H:i:s');	

	if(isset($_POST['send'])) {
		
		if(isset($_POST['message']) && strlen($_POST['message'])>0) {
			$message = trim($_POST['message']);
		} else {
			$error = 'Please enter message';
		}	
		
		if(isset($_POST['type']) && strlen($_POST['type'])>0) {
			$type = trim($_POST['type']);
		} else {
			$error = 'Please select SMS type';
		}
		
		//get mobile details
		$userRec = mysqli_fetch_array(mysqli_query($con, "SELECT phone FROM tbl_user WHERE id = '".$userId."'"));
		if($userRec['phone']!=''){
			$phone = trim($userRec['phone']);
		} else {
			$error = 'Sorry, The user does not have a phone number';
		}
		
				
		if(empty($error)) {			
			$smsMessage = urlencode($message);
			//echo $type;
			if($type == 'trans'){
				$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phone.'&message='.$smsMessage.'&sender=odpT&type=1&route=2');
			} else if($type == 'promo'){
				$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phone.'&message='.$smsMessage.'&sender=odp&type=1&route=3');
			}
			$smsRes = json_decode($smsRes); 
			//print_r($smsRes);
			if($smsRes->error!= 'null' && $smsRes->error!=''){
				$error = $smsRes->error;
				if($error!=''){
					$error = 'Unable to send SMS';
				}
			} else {
				/*$insert = mysqli_query($con, "INSERT INTO tbl_otp(otp_rand, otp) VALUES ('".$otpRand."', '".$otp."')");
				$smsMessageId = $smsRes->msg_id;
				if($insert){*/
					$smsSuccess = true;
					$success = 'Message sent Successfully';					
					header('Location:index.php?page=send_sms_user&user_id='.$userId.'&success='.$success);
					exit();	
				//}
			}
					
		} 
	}



} else {
	header('Location:index.php?page=manage_user&p='.$p);
	exit();
}

?>