<?php
//var_dump($_REQUEST);
//die();

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

@$appid = mysqli_real_escape_string($con,$_REQUEST['id']);

if(isset($_REQUEST['id'])){

		//$id = intval($_REQUEST['id']);
		$appid=get_parent_id($appid,$con);		
				
		// now delete applicant information
		$sql = "delete from applicant_table where `app_id`=$appid";		
		$result = @mysqli_query($con,$sql);
		if ($result){
			

			$docrs = mysqli_query($con,"select file, doc_type from app_docu_table where `app_id`=$appid");
    		while($docrow=mysqli_fetch_array($docrs)){
    			
    			if($docrow["doc_type"]==1) unlink("uploads/passportcopy/".$docrow["file"]);
		 		if($docrow["doc_type"]==2) unlink("uploads/visappform/".$docrow["file"]);	
    		}

			$sql = "delete from app_docu_table where `app_id`=$appid";		
			@mysqli_query($con,$sql);

    		$sql = "delete from app_his_table where `app_id`=$appid";		
			@mysqli_query($con,$sql);

			$sql = "delete from app_notes_table where `app_id`=$appid";		
			@mysqli_query($con,$sql);

			$sql = "delete from docpos_log_table where `docpos_log_appid`=$appid";		
			@mysqli_query($con,$sql);

			// now delete all dependant information
			$chldrs = mysqli_query($con,"select app_id from applicant_table where `parent`=$appid");
    		while($chldrow=mysqli_fetch_array($chldrs)){ 
        
         	  	$appid=$chldrow["app_id"];

         	  	$sql = "delete from applicant_table where `app_id`=$appid";						
				@mysqli_query($con,$sql);

				$docrs = mysqli_query($con,"select file, doc_type from app_docu_table where `app_id`=$appid");
	    		while($docrow=mysqli_fetch_array($docrs)){
	    			
	    			if($docrow["doc_type"]==1) unlink("uploads/passportcopy/".$docrow["file"]);
			 		if($docrow["doc_type"]==2) unlink("uploads/visappform/".$docrow["file"]);	
	    		}
    			$sql = "delete from app_docu_table where `app_id`=$appid";		
				@mysqli_query($con,$sql);

				$sql = "delete from app_his_table where `app_id`=$appid";		
				@mysqli_query($con,$sql);

				$sql = "delete from app_notes_table where `app_id`=$appid";		
				@mysqli_query($con,$sql);

				$sql = "delete from docpos_log_table where `docpos_log_appid`=$appid";		
				@mysqli_query($con,$sql);
			  	
			} 

			header("Location:application.php");
			
		} else {
			echo json_encode(array('msg'=>'Some errors occured.'));
		}
	}




?>