<?php

include('../db_con.php');
include 'methodlist.php';

session_start();
//var_dump($_POST);

$skuList = explode(' ', $_POST['updateids']);
//print_r($str_arr);

$dp_id = $_POST['docpos_id'];
//print_r($skuList);
$skuList=array_values(array_unique(array_filter($skuList)));
//print_r($skuList);
$correctid=0;

$updatedlist=array();

for ($i = 0; $i < count($skuList); $i++) {

            //$key = array_search($array[$i], $rules);
            //echo $skuList[$i];
     

//var_dump($_POST);

	if($_POST['idtype']==1){

		$sql = "select app_id, doc_position, email, phone_no, passport_no, refer_no, parent from applicant_table where refer_no= '$skuList[$i]'";
	}

	if($_POST['idtype']==2){

		$sql = "select app_id, doc_position, email, phone_no, passport_no, refer_no, parent from applicant_table where passport_no= '$skuList[$i]' ";
	}
	
	$result = mysqli_query($con,$sql);
	$row = mysqli_fetch_array($result);

    if($row["app_id"]!=NULL){  // only matched appid doc position will be updtaed 
    
    	$correctid++;
    	
    	/// get all updated application list to array_push
    	if($_POST['idtype']==1)	array_push($updatedlist,$row["refer_no"]);
	    if($_POST['idtype']==2) array_push($updatedlist,$row["passport_no"]);
    	
    	$last_position=get_position_name($row["doc_position"], $con);
    	$new_position=get_position_name($_POST["docpos_id"], $con);
    
    	// update current doc position in applicant table 
    	$sql ="UPDATE `applicant_table` SET `doc_position`='$dp_id'  WHERE `app_id`='$row[app_id]'";
    	mysqli_query($con,$sql);
    
    	//check for same position entry for same applicantion. if yes, delete previous one 
        $sql = "select docpos_log_id from docpos_log_table where docpos_log_appid= '$row[app_id]' AND docpos_log_dposid= '$dp_id'";
        $result = mysqli_query($con,$sql);
        $delrow=mysqli_num_rows($result);
        if($delrow>0){
            
            $sql = "delete from docpos_log_table where docpos_log_appid= '$row[app_id]' AND docpos_log_dposid= '$dp_id'";
            $result = @mysqli_query($con,$sql);
        }


        $userid=get_session_user_id($con);
    
    	// update doc position in doc log table 
    	$sql ="insert into `docpos_log_table` (docpos_log_appid, docpos_log_dposid, docpos_log_userid) values ('$row[app_id]', '$dp_id', '$userid') ";
    	mysqli_query($con,$sql);
    
    	ver_update_history('Document position has been updated from <b>'.$last_position.'</b> to <b>'.$new_position.'</b> by', $row["app_id"],$con);
    
        //get email and phone number of all parent and dependant applicant  
        if($row["parent"]!=0){
            
            $xsql = "select email, phone_no from applicant_table where app_id= '$row[parent]' ";
            $xresult = mysqli_query($con,$xsql);
	        $xrow = mysqli_fetch_array($xresult);
	        $sendemail=$xrow["email"];
            $sendsms=$xrow["phone_no"];
        }
        else{
            $sendemail=$row["email"];
            $sendsms=$row["phone_no"];
        }
        
        //echo $sendsms."".$sendemail;
        
    	if($new_position=="Ready for Pickup"){
    	
    	////// send email to email address /////
    
    		$toEmail = $sendemail;
    
    		$subject = "Your Visa Application Status has been Updated";
    
    		$headers  = 'MIME-Version: 1.0' . "\r\n";
    		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    		$headers .= 'From: Saimon Visa Application Center <no-reply@saimongroup.com>' . "\r\n";
    
    		$msg_box="Dear Applicant,<br/><br/>
    		Your Passport No:".$row["passport_no"]." is ready for delivery. Please collect your passport between 9AM-4PM (Sun-Thurs) from Thai Visa Application Center. 
            <br/><br/>    		   
    		Thanks. 
    		<br/> Customer Support Team
    		<br/>Saimon Group"; 
    
    		mail($toEmail,$subject,$msg_box,$headers);
    
    		///////////// ends //////////
    		
    		////// send sms to phone number /////
    
        	$to=$sendsms;
        	//$message="Dear Applicant, (Passport No:".$row["passport_no"].") Your Visa Application status has been updated and your passport is ready for delivery. You do not need to contact our customer support. Simply, visit your pre-selected Thai Visa Application Center location to pick up your passport. Thank you for your interest in Saimon Group. Regards - Customer Support Service, Thai Visa Application Center, Saimon Group"; 
    		$message="Your Passport no. ".$row["passport_no"]." is ready for delivery. Please collect your passport between 9AM-4PM (Sun-Thurs) from Thai Visa Center. Thanks. Saimon Group";
    		//$message = strip_tags($msg_box);
        	
        // 	$token = "79432c9e0afad786d880b96957985439";
        // 	$url = "http://sms.greenweb.com.bd/api.php";
        // 	$data= array(
        // 	'to'=>"$to",
        // 	'message'=>"$message",
        // 	'token'=>"$token"
        // 	); 
        // 	// Add parameters in key value
        // 	$ch = curl_init(); // Initialize cURL
        // 	curl_setopt($ch, CURLOPT_URL,$url);
        // 	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        // 	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 	$smsresult = curl_exec($ch);
        	
        	//$url='https://vas.banglalinkgsm.com/sendSMS/sendSMS?msisdn='.$to.'&message='.$message.'&userID=SgroupBLsms%20&passwd=32eec9ea89ba1af42b5f3e6b8d179180&sender=SaimonGrp';
           // $url = preg_replace("/ /", "%20", $url);
           // $response = file_get_contents($url);
    
    		///////////// ends //////////
    	}
   	
    	
    }


}
     $totalapp=count($skuList);
	echo "<div class='note note-success'>
			<p>".$correctid." of ".$totalapp." Application document status has been updated.</p>";
	echo "  <p>Document position has been updated for following applications: ".implode(", ",$updatedlist)."</p></div>";							

//header('Location: docposupdate.php?docposid='.$dp_id.'&msg='.$totalapp.'');

?>