<?php
/*
$username="root";
$password="salim";
$database="sms";
$servername="localhost";*/

$username="saliqcok_smsuser";
$password="sms@2017";
$database="saliqcok_sms";
$servername="localhost";



$db = new mysqli($servername, $username, $password, $database);
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
	exit();
}



function get_user_Level(){
  session_start();
  $userLevel=$_SESSION['position'];
  return $userLevel;
}


function list_wards(){
	global $db;// = new mysqli($servername, $username, $password, $database);
    $branchlist="<option value=''>Select Ward</option>";
    $bsql="Select distinct Contact_Ward from sms_contacts";
    $branchres=$db->query($bsql);
    if($branchres->num_rows>0){
      // echo 'total rows='.$branchres->num_rows;
        while($brow=$branchres->fetch_array(MYSQLI_ASSOC)){
			$ival= $brow["Contact_Ward"];
			$iname= $brow["Contact_Ward"];
        	$newline="<option value='".$ival."'>".$iname."</option>";
			if($newline==''){
			}else{
			$branchlist=$branchlist.$newline;
			}
        }

    }else{
		$branchlist="<option value=''>No Branches Set</option>";

	}
 return $branchlist;
}


function add_newcontactxx(){
	global $db;// = new mysqli($servername, $username, $password, $database);
    $branchlist="<option value=''>Select User Level</option>";

        $bsql="Select level_Name from user_levels order by level_Name asc";
    $branchres=$db->query($bsql);
    if($branchres->num_rows>0){
      // echo 'total rows='.$branchres->num_rows;
        while($brow=$branchres->fetch_array(MYSQLI_ASSOC)){
			$ival= $brow["level_Name"];
			$iname= $brow["level_Name"];

      $newline="<option value='".$ival."'>".$iname."</option>";

			$branchlist=$branchlist.$newline;

        }

    }else{
		$branchlist="<option value=''>No User Levels Set</option>";

	}
 return $branchlist;
}


function add_newcontact($Contact_Name,$Contact_Tel,$Contact_Ward){
	$saveSQL="insert into sms_contacts(Contact_Name,Contact_Tel,Contact_Ward)values('$Contact_Name','$Contact_Tel','$Contact_Ward')";
  executeQuery($saveSQL);
}




function contact_recipients($wardName){
  	global $db;
  if($wardName==""){
    $inventorysql="Select Contact_Name,Contact_Tel,Contact_Ward from sms_contacts order by Contact_Ward asc";
  }else{
    $inventorysql="Select Contact_Name,Contact_Tel,Contact_Ward from sms_contacts where Contact_Ward='$wardName'";
  }
  //$inventorysql="Select Contact_Name,Contact_Tel,Contact_Ward from sms_contacts order by Contact_Ward asc";
  $recipients="";
	 $itemlist=$db->query($inventorysql);
    if($itemlist->num_rows>0){
        while($itemrow=$itemlist->fetch_array(MYSQLI_ASSOC)){

			$Contact_Tel=replace_fChar($itemrow["Contact_Tel"]);

if($recipients==""){
  $recipients=$recipients.$Contact_Tel;
}else{
  $recipients=$recipients.",".$Contact_Tel;
}


        }


    }else{


	}
	return $recipients;
}



function replace_fChar($mobileNo){
  $mobno=$mobileNo;
  	$fchar=substr($mobno,0,1);
  	//echo "<script>alert($fchar)</script>";
    if($fchar==0){
      $nolen=strlen($mobno);
    	$rem=$nolen-1;
    	$restno=substr($mobno,1,$rem);
    }else{
      $restno=$mobno;
    }

  	//echo "<script>alert($nolen)</script>";
  	$mobileNo= "+254".$restno;
    return $mobileNo;
}
function contactlist_table(){
	global $db;
	$tableStr="<table class='table table-condensed table-bordered'>
    <thead>
      <tr><th>ID</th> <th>Contact Name</th><th>Telephone No:</th><th>Ward:</th></tr></thead>
    <tbody>";
	$inventorysql="Select ContactID,Contact_Name,Contact_Tel,Contact_Ward from sms_contacts order by Contact_Ward asc";
	 $itemlist=$db->query($inventorysql);
    if($itemlist->num_rows>0){
        while($itemrow=$itemlist->fetch_array(MYSQLI_ASSOC)){
			$ContactID=$itemrow["ContactID"];
			$Contact_Name= $itemrow["Contact_Name"];
			$Contact_Tel= $itemrow["Contact_Tel"];
			$Contact_Ward= $itemrow["Contact_Ward"];

			$newline="<tr>
			<td>".$ContactID."</td><td>".$Contact_Name."</td><td>".$Contact_Tel."</td><td>".$Contact_Ward."</td></tr/>";
			if($newline==''){
			}else{
			$tableStr=$tableStr.$newline;
			}
        }
		$tableStr=$tableStr."</tbody></table>";

    }else{
		$tableStr=$tableStr."</tbody></table>";

	}
	return $tableStr;
}



function executeQuery($ExecuteQuery){
  global $db;
     if(!mysqli_connect_errno()){
       try {
          mysqli_query($db, $ExecuteQuery);
       } catch (Exception $e) {

       }


       }
}



function get_Qty($selectSQL,$retvalName){
//echo $selectSQL;
	 global $db;// = new mysqli($servername, $username, $password, $database);
    $returndata=0;
    $bsql=$selectSQL;
    $branchres=$db->query($bsql);
    if($branchres->num_rows>0){
      // echo 'total rows='.$branchres->num_rows;
        while($brow=$branchres->fetch_array(MYSQLI_ASSOC)){
			$Out=$brow[$retvalName];
			$outTransfer=$Out;
        }

    }else{

$outTransfer=0;
	}
 return $outTransfer;


}



function validateUsers($username,$password){
	global $db;// = new mysqli($servername, $username, $password, $database)
    //create_Audittable();
    $loginSQL="select UserName,loginpass,FullNames,User_Position from login where UserName='$username' and loginpass='$password'";
    $branchres=$db->query($loginSQL);
    $reply=array('status'=>$loginSQL);
                 //echo json_encode($reply);
                // exit();
    if($branchres->num_rows==1){
      // echo 'total rows='.$branchres->num_rows;
        while($brow=$branchres->fetch_array(MYSQLI_ASSOC)){
                        $username= $brow["FullNames"];
                        $userPosition=$brow["User_Position"];
			session_start();
                        $_SESSION['loginname']=$username;
                        $_SESSION['position']=$userPosition;
                        //postTranslog("Login Module","Logged into the System");
                        $reply=array('status'=>'success');
                        echo json_encode($reply);
        }

    }else{
		$reply=array('status'=>"failed",'mesaage'=>"Invalid Login Credentials");
                       echo json_encode($reply);

	}

}


?>
