<?php  

  session_start();
  error_reporting(0);
   include("../db.php");
	 include("../all_query.php");
		

	 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
	{
		
		
		  $account_holder_id = $_REQUEST['account_holder_id'];
		  $template_id = $_REQUEST['template_id'];
		  $text_message = $_REQUEST['text_message'];
		  
		  
		  
		   $content = '';
		  $subject = '';
		  
          if($template_id!='0')
		  {
 		 
		 $templateQ = mysqli_query($conn,"select *from `compaign_templates` where id=$template_id ");
		 $templateData = mysqli_fetch_assoc($templateQ);
		 $content = $templateData['content'];
		 $subject = $templateData['subject'];
		  }
		  else
		  {
			  $content = $text_message;
		  }
		  
		  
		  
		  $customerQ = mysqli_query($conn,"select *from `customer` where account_holder_id = '$account_holder_id' ");
	     $customerData = mysqli_fetch_assoc($customerQ);
		 
		 
		 
		 $leadQ=mysqli_query($conn,"select *from leads  where  account_holder_id = '$account_holder_id' order by id desc ");
		 
		 $total_loan = mysqli_num_rows($leadQ);
		 
		 $total_loan_amount = 0;
		 $total_tos = 0;
		 
		 
		while($leadData = mysqli_fetch_assoc($leadQ))
		{
			$total_loan_amount = $total_loan_amount + $leadData['loan_amount'];
			$total_tos = $total_tos + $leadData['tos'];
		}
		 
		 
		  
		 $leadQ=mysqli_query($conn,"select *from leads  where  account_holder_id = $account_holder_id order by id desc limit 1 ");
		 $leadData = mysqli_fetch_assoc($leadQ);
		 $lead_data = $leadData['lead_data'];
		 $data = json_decode(stripslashes($lead_data)); 
		 
	  
	  
		  $content =    str_replace('&gt;','>',$content);
		  $content =    str_replace('&lt;','<',$content);
		  $content =    str_replace('&nbsp;',' ',$content);
		  $content =    str_replace('{','<?= ',$content);
		  $content =    str_replace('}',' ?>',$content);
		  $content =    str_replace('^','\'',$content);
		  
		  
		  
		  
		  $fp = fopen("mail_content_html.php", "w");
		  fwrite($fp, $content);
		  fclose($fp);
		 
	 
	   
	        ob_start();

		   $content_html = include 'mail_content_html.php';
		   $content_html = ob_get_clean();
		   
		   
		  
		  
		  echo urlencode($content_html);
		  
		  
		  
		
	}
	
	
	?>