<?php
ob_start();
session_start(); 
include('../db_con.php');
include('methodlist.php');

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

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

		//$id = intval($_REQUEST['id']);
		$appid=get_parent_id($appid,$con);
		$deluserid=get_session_user_id($con);
		
		// get all data from application table and insert to delete table
		$dels=@mysqli_query($con,"select * from applicant_table where app_id='$appid'");
		$delsrow=mysqli_fetch_array($dels);
		
		$sql = "INSERT INTO `delete_table`(`surname`, `givenname`, `salu`, `phone_no`, `email`, `homeaddress`,`deliverycenter`,`passporttype`,`passport_no`, `passissuedate`,`passexpdate`,`gender`, nationality, created_date, delete_date, takenby, total_fee, birth_date, placeofbirth, `paymode`, paydoneby, delitypeid, appstatus, paystatus, doc_position, visa_type, agent, agent_name, v_uid, refer_no, parent,deleteby,delnotes) VALUES ('$delsrow[surname]','$delsrow[givenname]','$delsrow[salu]','$delsrow[phone_no]','$delsrow[email]','$delsrow[homeaddress]','$delsrow[deliverycenter]','$delsrow[passporttype]','$delsrow[passport_no]','$delsrow[passissuedate]','$delsrow[passexpdate]','$delsrow[gender]','$delsrow[nationality]', '$delsrow[created_date]', NOW(),'$delsrow[takenby]','$delsrow[total_fee]','$delsrow[birth_date]','$delsrow[placeofbirth]','$delsrow[paymode]','$delsrow[paydoneby]','$delsrow[delitypeid]','$delsrow[appstatus]','$delsrow[paystatus]','$delsrow[doc_position]','$delsrow[visa_type]','$delsrow[agent]','$delsrow[agent_name]','$delsrow[v_uid]','$delsrow[refer_no]','$delsrow[parent]','$deluserid','$_REQUEST[delnotes]')";
		//echo $sql;
		$result = @mysqli_query($con,$sql);
				
		// 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"];
         	  	
         	  	// get all data from application table and insert to delete table
        		$dels=@mysqli_query($con,"select * from applicant_table where app_id='$appid'");
        		$delsrow=mysqli_fetch_array($dels);
        		
        		$sql = "INSERT INTO `delete_table`(`surname`, `givenname`, `salu`, `phone_no`, `email`, `homeaddress`,`deliverycenter`,`passporttype`,`passport_no`, `passissuedate`,`passexpdate`,`gender`, nationality, created_date, delete_date, takenby, total_fee, birth_date, placeofbirth, `paymode`, paydoneby, delitypeid, appstatus, paystatus, doc_position, visa_type, agent, agent_name, v_uid, refer_no, parent,deleteby,delnotes) VALUES ('$delsrow[surname]','$delsrow[givenname]','$delsrow[salu]','$delsrow[phone_no]','$delsrow[email]','$delsrow[homeaddress]','$delsrow[deliverycenter]','$delsrow[passporttype]','$delsrow[passport_no]','$delsrow[passissuedate]','$delsrow[passexpdate]','$delsrow[gender]','$delsrow[nationality]', '$delsrow[created_date]', NOW(),'$delsrow[takenby]','$delsrow[total_fee]','$delsrow[birth_date]','$delsrow[placeofbirth]','$delsrow[paymode]','$delsrow[paydoneby]','$delsrow[delitypeid]','$delsrow[appstatus]','$delsrow[paystatus]','$delsrow[doc_position]','$delsrow[visa_type]','$delsrow[agent]','$delsrow[agent_name]','$delsrow[v_uid]','$delsrow[refer_no]','$delsrow[parent]','$deluserid','$_REQUEST[delnotes]')";
        		//echo $sql;
        		$result = @mysqli_query($con,$sql);
        		

         	  	$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.'));
		}
	}




?>