<?php 
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
include ('../inc/repfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){  
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$dtnow = date("Y-m-d h:i:s", time());



function addtosms($dty,$did,$name,$number){
if($number=='') return;    
if (!isset($_SESSION['axes_sms']) ) {
$_SESSION['axes_sms'] = array();
}

if(is_array($_SESSION['axes_sms'])){
$max=count($_SESSION['axes_sms']);     
$_SESSION['axes_sms'][$max]['dty']=$dty;
$_SESSION['axes_sms'][$max]['did']=$did;
$_SESSION['axes_sms'][$max]['name']=$name;
$_SESSION['axes_sms'][$max]['number']=$number; 
}else{
$_SESSION['axes_sms']=array();   
$_SESSION['axes_sms'][0]['dty']=$dty;
$_SESSION['axes_sms'][0]['did']=$did;
$_SESSION['axes_sms'][0]['name']=$name;
$_SESSION['axes_sms'][0]['number']=$number;    
}    
}

function remove_smsitem($pid){
$pid=intval($pid);
if(isset($_SESSION['axes_sms'])){
$max=count($_SESSION['axes_sms']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_sms'][$i]);
break;
}
}
$_SESSION['axes_sms']=array_values($_SESSION['axes_sms']);
}
}



if(isset($_POST['getsms'])){
$dty = $_POST['dty'];    
$did=intval($_POST['did']); 
if($dty=='CU'){
 $sql="SELECT cnumber,name FROM tbl_customer where id=$did";
}elseif($dty=='SU'){
 $sql="SELECT cnumber,name FROM tbl_supplier where id=$did";
}elseif($dty=='EM'){
 $sql="SELECT mobile,name FROM tbl_employe where id=$did";
}else{
   $sql="SELECT cnumber,name FROM tbl_customer where id=$did";
}

$query = mysqli_query($con,$sql);
$row=mysqli_fetch_array($query);
    $name = $row['name'];
    if($dty=='EM'){
         $number = $row['mobile'];

    }else{
         $number = $row['cnumber'];

    }
   

addtosms($dty,$did,$name,$number);   
}




if(isset($_POST['smsdata'])){
if(isset($_SESSION['axes_sms'])){
unset($_SESSION['axes_sms']);    
}    
}

if(isset($_POST['smsremove'])){
$id=floatval($_POST['smsremove']);
remove_smsitem($id);    
$max=count($_SESSION['axes_sms']);
if($max <= 0){
unset($_SESSION['axes_sms']);    
}       
}


if(isset($_POST['smsclear'])){
if(isset($_SESSION['axes_smsdata'])){
unset($_SESSION['axes_smsdata']);    
}
if(isset($_SESSION['axes_sms'])){
unset($_SESSION['axes_sms']);    
}    
}


if(isset($_POST['save_payment'])){

$mgs = remove_junk(escape($_POST['note']));
	//SMS Code Start
$sql="SELECT * FROM tbl_sms WHERE id=1 LIMIT 1";
$smsde = mysqli_query($con,$sql);
$sms=mysqli_fetch_array($smsde);


if($sms['status']==1){
$lang=$sms['language'];
$appkey=$sms['appkey'];
$senderid=$sms['senderid'];
$type=$sms['type'];
$url=$sms['url'];

 
$max=count($_SESSION['axes_sms']);
for($i=($max-1);$i>=0;$i=$i-1){
     
$dty = $_SESSION['axes_sms'][$i]['dty'];
$did=$_SESSION['axes_sms'][$i]['did'];
$name = $_SESSION['axes_sms'][$i]['name'];    
$cmobile=$_SESSION['axes_sms'][$i]['number'];




if(strlen($cmobile)>10){
if($lang==1){
     
    $url = "$url";
    $data = [
      "api_key" => "$appkey",
      "type" => "$type",
      "contacts" => "$cmobile",
      "senderid" => "$senderid",
      "msg" => "$mgs",
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);

}else{
  $url = "$url";
  $data = [
    "api_key" => "$appkey",
    "type" => "$type",
    "contacts" => "$cmobile",
    "senderid" => "$senderid",
    "msg" => "$mgs",
  ];
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $response = curl_exec($ch);
  curl_close($ch);


}

}else{
  echo json_encode(array(
      'status' => 'info',
      'message'=> 'Number is wrong!!'
      ));
      exit;
}
}

}

//SMS Code End


    unset($_SESSION['axes_smsdata']);
    unset($_SESSION['axes_sms']);   
  $efid=mysqli_affected_rows($con);   
  if($efid>0){ 
    write_activity($aid,'SMS','SMS Successfully Send','Sended');
    echo json_encode(array(
      'status' => 'success',
      'message'=> 'SMS Successfully Send!!'
      ));
      exit;
  } else {
   echo json_encode(array(
      'status' => 'info',
      'message'=> 'SMS Not Send!!'
      ));
      exit;
  }  
}
?> 