<?php  

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

   if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
  {
			
			
	   $_SESSION['timestamp'] = time(); 
	   		
			
		$mobile = $_REQUEST['mobile'];	
		$account_holder_id = $_REQUEST['account_holder_id'];
		$uniqueid = $_REQUEST['uniqueid'];
		
		
		$custQ = mysqli_query($conn,"select *from `customer` where account_holder_id = '$account_holder_id' ");
		$custData = mysqli_fetch_assoc($custQ);
		
		
		
	   
	   
		  if($_SESSION['login_as'] == 'admin')
		  {
			  
			  
			  $advisor = $custData['advisor'];
			  
		  
		     $advisorQ  = mysqli_query($conn,"select *from `advisors` where name='$advisor'  limit 1  ");
		     $advisorData = mysqli_fetch_assoc($advisorQ);
			 
			 
			 $advisorData['apisipid'] = $_SESSION['apisipid'];
		  
		  
		  
		  
		  
		  }
		  else
		  {
            $login_name = $_SESSION['login_name'];
		  
		     $advisorQ  = mysqli_query($conn,"select *from `advisors` where name='$login_name'  ");
		     $advisorData = mysqli_fetch_assoc($advisorQ);
		  
		  }
		  
		  $advisor_id = $advisorData['id'];
		  
	
  $customerTypeQ = mysqli_query($conn,"select *from `customer_type` where id in (select customer_type_id from advisors where id=$advisor_id )   ");
	   $typeData = mysqli_fetch_assoc($customerTypeQ);
	   $customer_type = $typeData['name'];
	   
	   


	   
		  
		 $login_statusQ = mysqli_query($conn,"select *from `advisor_login_status` where advisor_id = $advisor_id order by id desc limit 1 ");
	   $loginStatusData = mysqli_fetch_assoc($login_statusQ);
	   
		  
	  if(($loginStatusData['login_status'] == 'logged_in' && $loginStatusData['login_status_type'] == 'manual') || $advisorData['is_paused'] == 'on'  ||  $_SESSION['login_as'] == 'admin' )
	   {
		   
		   
	 if($advisorData['is_busy_on_call'] == '' ||  $_SESSION['login_as'] == 'admin' )
	   {
		  
		  
		  
			//$uniqueid = uniqid();

			$DATA = array();
			$DATA['customer_mobile'] = $mobile;
			$DATA['customer_name'] = $custData['name'];
			$DATA['emp_code'] = $advisorData['username'];
			$DATA['apisipid'] = $advisorData['apisipid'];
			$DATA['uniqueid'] = $uniqueid;
			$DATA['advisor_mobile'] = $advisorData['mobile'];
			$DATA['advisor_name'] = $advisorData['name'];
			$DATA['account_holder_id'] = $account_holder_id;
			$DATA['is_hit'] = '0';
			$DATA['init_type'] = 'manual';
		    $DATA['customer_type'] = $customer_type;

			
			
			
			
	        insertTable($conn,'call_init',$DATA,'add_submit');
			
			
			
			mysqli_query($conn,"update `advisors` set is_busy_on_call = 'on' where id=$advisor_id ");
			
			
			
			echo "Call Initiated Successfully";
			
	   }
	   
	   else
	   {
		   echo "Sorry. You are busy right now.";
	   }
	
	
	   }
	   else
	   {
		    echo "Sorry.your logged in status not in manual call";
	   }
	
			
			
  }


?>