<?php require_once("../../db.php"); ?>
<?php 
	
	try {
		$core = Core::getInstance();
		$stmt_prep = $core->dbh->prepare("SELECT * FROM myclients WHERE client_number = :client_number");
		$stmt_prep -> bindParam("client_number", $_POST["txt_bulk_send"]);
		$stmt_prep -> execute();
		$row_count_prep = $stmt_prep -> rowCount();
		$results = $stmt_prep -> fetchAll(PDO::FETCH_ASSOC);
		
		if ($row_count_prep < 1) {
			die('{"send":"false","reason":"Zahtjev za slanjem poruke poslan, ali broj ne postoji u bazi."}');
		} else {
			$client_id = $results[0]["client_id"];
			try {
	
				$core = Core::getInstance();
				$stmt = $core->dbh->prepare("INSERT INTO messages (mess_client_id, mess_message, mess_whosent) values (:mess_client_id, :mess_message, :mess_whosent)");
				$mess_whosent = "1";
				$stmt -> bindParam("mess_client_id", $client_id, PDO::PARAM_INT);
				$stmt -> bindParam("mess_message", $_POST["blk_message"], PDO::PARAM_STR);
				$stmt -> bindParam("mess_whosent", $mess_whosent, PDO::PARAM_STR);
				
				
				$stmt -> execute();
				$row_count = $stmt -> rowCount();
				
				if ($row_count > 0) {
					echo '{"send":"true","reason":"ok"}';
					$last_insert_id = $core->dbh->lastInsertId();
					//$client_number = getClientNumber($_POST["inp_rnd_clientID"]);
					pushSMS($_GET["ag"], $_POST["txt_bulk_send"], $_POST["blk_message"], $last_insert_id);
				}
				
				
			} catch(PDOException $pe) {
				
				trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
				
			}
		}
	} catch(PDOException $pe) {
		trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);		
	}

?>