<?php 


function rml_noheader($template,$no,$param,$headerkey){
	$data = array();		
	$data['scenarioKey']="77E3760DB647A79B03F245C55FE7D3A2";
	$data['destinations'][0]['to']['phoneNumber']=$no;
	$data['whatsApp']['templateName']=$template;
	$data['whatsApp']['templateData']=$param;
	$data['whatsApp']['language']='en';	
	//echo json_encode($data); 
	rml_sendWhatsapp($data,$headerkey);	
}

function rml_imageheader($template,$no,$param,$media,$mediatype,$btn1,$btn2,$headerkey){	
	$data = array();		
	$data['phone']=$no;
	$data['media']['type'] = "media_template";
	$data['media']['template_name'] = $template;
	$data['media']['lang_code'] = 'en';
	
	if($mediatype == 'MEDIA'){
	$data['media']['header'][0]['image']['link']=base_url('uploads/broadcast/'.$media);
	}elseif($mediatype == 'VIDEO'){
	$data['media']['header'][0]['video']['link']=base_url('uploads/broadcast/'.$media);
	}
	elseif($mediatype == 'DOCUMENT'){
	$data['media']['header'][0]['document']['link']=base_url('uploads/broadcast/'.$media);
	}
	$i=0;
	if($param)
	{
		foreach($param as $text){
		$data['media']['body'][$i]['text']=$text;
		$i++;
		}
	}
	
	if($btn1!=null ){
		$data['media']['button'][0]['button_no']="0";
		$data['media']['button'][0]['url']=$btn1;
	}
	elseif($btn2!=null && $btn1==null){
		$data['media']['button'][0]['button_no']="1";
		$data['media']['button'][0]['url']=$btn2;
	}
	elseif($btn2!=null && $btn1!=null){
		$data['media']['button'][1]['button_no']="1";
		$data['media']['button'][1]['url']=$btn1;
	}
	
	
	json_encode($data); 
	rml_sendWhatsapp($data,$headerkey);	
}

function rml_sendWhatsapp($params,$headerkey)
	{
		$CI = get_instance();
$url = 'https://apis.rmlconnect.net/wba/v1/messages';
		//$headerkey="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYnV5bnh0NiIsInVzZXJuYW1lIjoiYnV5bnh0NiIsImV4cCI6MTkzNjk2MDY1OCwiZW1haWwiOiJ1ZGF5QGJ1eW54dC5pbiIsIm9yaWdfaWF0IjoxNjIxNjAwNjU4LCJjdXN0b21lcl9pZCI6IkZCeVNXZzlvT2ZoayJ9.O4CFD6oepQP9qRig04MHZE0z1keJYb7-7YtaLUr9Pmg";
		//$headerkey = $rmlheader;
		
		$curlOptions = array( CURLOPT_RETURNTRANSFER => TRUE);
		$curlOptions[CURLOPT_CUSTOMREQUEST] = 'POST';
		$curlOptions[CURLOPT_URL] = $url;
		$requestHeaders = array(
				'Authorization:'.$headerkey,
				'Content-Type: application/json',
				'Accept: application/json'
		);
		
		$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
		$curlOptions[CURLOPT_HTTPHEADER] = $requestHeaders;
		$curl = curl_init();
		curl_setopt_array($curl, $curlOptions);
		$response = curl_exec($curl);
		curl_close($curl);
		
		$rsp = json_decode($response, true);
		//print_r($rsp);
		//print_r($rsp['status']);
		
		$contact = preg_replace("/[^0-9]/","",$params['phone']);
		$contact = substr($contact,2);
		
		if($rsp['status'] == 'processing'){
			$CI->Helper_model->updatemessageresponse($contact,1,$response);
			return 1;
		}
		else{
			$CI->Helper_model->updatemessageresponse($contact,3,$response);
			return 0;
		}
		
	}


?>