<?php
include_once("includes/functions.php");
	
	$status = '';
	$adminId = $_SESSION['adminId'];	

	$userTypeArray = array( 'Select Type' => 'Select Type',
						'Active Users' => 'Active Users',
						'Active Staffs' => 'Active Staffs',
						'Active Technicians' => 'Active Technicians');
//,'Active Purchase Partners' => 'Active Purchase Partners'

	date_default_timezone_set('UTC');
	
	if(isset($_POST['Send'])) {		
		
		if(isset($_POST['message']) && $_POST['message']!=''){
			$message = trim($_POST['message']);
		} else {
			$error = 'Message should not be empty';
		}
				
		$status = trim($_POST['status']);
		if($status == 'Select Type'){$error = "Please Select Type";}
		
		$curDate = date('Y-m-d H:i:s');
		
		if(empty($error)) {
			
			if($status =='Active Users') {
				$phoneListArray = array();
				$rs = mysqli_query($con, "SELECT phone FROM tbl_user WHERE status = 'Active' ORDER BY id");
				if(mysqli_num_rows($rs)>0){
					while($row = mysqli_fetch_array($rs)){
						$phone = $row['phone'];
						if($phone!='' && $phone!='undefined' && is_numeric($phone) && strlen($phone)>9 && strlen($phone)< 14){
							$phoneListArray[] = $phone;							
						}	
					}
					$listCount = count($phoneListArray);
					if($listCount>0){
						$phoneList = implode(',', $phoneListArray);
						//mass sms to all numbers
						$smsMessage = urlencode($message);
						//type - 1 for normal and 2 for unicode
						$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phoneList.'&message='.$smsMessage.'&sender=odp&type=2&route=3');
						$smsRes = json_decode($smsRes); 
						//print_r($smsRes);
						if($smsRes->error!= 'null' && $smsRes->error!=''){
							$error = $smsRes->error;							
						} else {
							$insert = mysqli_query($con, "INSERT INTO tbl_mass_sms(user_type, message, date) VALUES ('".$status."', '".$message."', '".$curDate."')");
							//$smsMessageId = $smsRes->msg_id;
							if($insert){
								$success = 'The SMS has been sent to '.$listCount.' users';							
							} else {
								$error = 'Message has been sent. But unable to save in DB';
							}							
						}
					}
				}		
			} else if($status =='Active Staff') {
				$phoneListArray = array();
				$rs = mysqli_query($con, "SELECT phone FROM tbl_staff WHERE status = 'Active' ORDER BY id");
				if(mysqli_num_rows($rs)>0){
					while($row = mysqli_fetch_array($rs)){
						$phone = $row['phone'];
						if($phone!='' && $phone!='undefined' && is_numeric($phone) && strlen($phone)>9 && strlen($phone)< 14){
							$phoneListArray[] = $phone;							
						}	
					}
					$listCount = count($phoneListArray);
					if($listCount>0){
						$phoneList = implode(',', $phoneListArray);
						//mass sms to all numbers
						$smsMessage = urlencode($message);
						//type - 1 for normal and 2 for unicode
						$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phoneList.'&message='.$smsMessage.'&sender=odpP&type=2&route=3');
						$smsRes = json_decode($smsRes); 
						//print_r($smsRes);
						if($smsRes->error!= 'null' && $smsRes->error!=''){
							$error = $smsRes->error;							
						} else {
							$insert = mysqli_query($con, "INSERT INTO tbl_mass_sms(user_type, message, date) VALUES ('".$status."', '".$message."', '".$curDate."')");
							//$smsMessageId = $smsRes->msg_id;
							if($insert){
								$success = 'The SMS has been sent to '.$listCount.' staffs';					
							} else {
								$error = 'Message has been sent. But unable to save in DB';
							}							
						}
					}
				}
			} else if($status =='Active Technicians') {
				$phoneListArray = array();
				$rs = mysqli_query($con, "SELECT phone FROM tbl_technician WHERE status = 'Active' ORDER BY id");
				if(mysqli_num_rows($rs)>0){
					while($row = mysqli_fetch_array($rs)){
						$phone = $row['phone'];
						if($phone!='' && $phone!='undefined' && is_numeric($phone) && strlen($phone)>9 && strlen($phone)< 14){
							$phoneListArray[] = $phone;							
						}	
					}
					$listCount = count($phoneListArray);
					if($listCount>0){
						$phoneList = implode(',', $phoneListArray);
						//mass sms to all numbers
						$smsMessage = urlencode($message);
						//type - 1 for normal and 2 for unicode
						$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phoneList.'&message='.$smsMessage.'&sender=odpP&type=2&route=3');
						$smsRes = json_decode($smsRes); 
						//print_r($smsRes);
						if($smsRes->error!= 'null' && $smsRes->error!=''){
							$error = $smsRes->error;							
						} else {
							$insert = mysqli_query($con, "INSERT INTO tbl_mass_sms(user_type, message, date) VALUES ('".$status."', '".$message."', '".$curDate."')");
							//$smsMessageId = $smsRes->msg_id;
							if($insert){
								$success = 'The SMS has been sent to '.$listCount.' technicians';			
							} else {
								$error = 'Message has been sent. But unable to save in DB';
							}							
						}
					}
				}
			} else if($status =='Active Purchase Partners') {
				$phoneListArray = array();
				$rs = mysqli_query($con, "SELECT mobile FROM tbl_purchase_partner WHERE status = 'Active' ORDER BY id");
				if(mysqli_num_rows($rs)>0){
					while($row = mysqli_fetch_array($rs)){
						$phone = $row['mobile'];
						if($phone!='' && $phone!='undefined' && is_numeric($phone) && strlen($phone)>9 && strlen($phone)< 14){
							$phoneListArray[] = $phone;							
						}	
					}
					$listCount = count($phoneListArray);
					if($listCount>0){
						$phoneList = implode(',', $phoneListArray);
						//mass sms to all numbers
						$smsMessage = urlencode($message);
						//type - 1 for normal and 2 for unicode
						$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phoneList.'&message='.$smsMessage.'&sender=idpP&type=2&route=3');
						$smsRes = json_decode($smsRes); 
						//print_r($smsRes);
						if($smsRes->error!= 'null' && $smsRes->error!=''){
							$error = $smsRes->error;							
						} else {
							$insert = mysqli_query($con, "INSERT INTO tbl_mass_sms(user_type, message, date) VALUES ('".$status."', '".$message."', '".$curDate."')");
							//$smsMessageId = $smsRes->msg_id;
							if($insert){
								$success = 'The SMS has been sent to '.$listCount.' purchase partners';	
							} else {
								$error = 'Message has been sent. But unable to save in DB';
							}							
						}
					}
				}
			} 
			
			
			
		} 
	}


		
?>