<?
ob_clean();
include("../../startup.php"); 
###########################################  	END  FILTER CONITIONS 	#####################################

$header = "id,company_name,sector,first_name,last_name,profession,target_market,phone_no,email,password,address1,address2,address3,city,county,postcode,created_date,interventions,sales_process,consultant,contact_source,prospect_status,type_of_prospect,vcp,g,a,i,n,s,itr\n";

$data  = $header;
$sql = "SELECT * FROM job $where ORDER BY id DESC";

$rs = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($rs)){
	$sector = "";
	if(isset($row["sector"]) && $row["sector"] != 0){
		$sector = Keycode::getkeycodeById($row["sector"]);
	}
	$county = "";
	if(isset($row["county"]) && $row["county"] != 0){
		$county =  $gsi->getCountyById($row['county']);
	}
	$contact_source = "";
	if(isset($row["contact_source"]) && $row["contact_source"] != 0){
		$contact_source = Keycode::getkeycodeById($row["contact_source"]);
	}
	$prospect_status = "";
	if(isset($row["prospect_status"]) && $row["prospect_status"] != 0){
		$prospect_status = Keycode::getkeycodeById($row["prospect_status"]);
	}
	$type_of_prospect = "";
	if(isset($row["type_of_prospect"]) && $row["type_of_prospect"] != 0){
		$type_of_prospect = Keycode::getkeycodeById($row["type_of_prospect"]);
	}
	$vcp = "";
	if(isset($row["vcp"]) && $row["vcp"] != 0){
		$vcp =  $gsi->getVcpTypeById($row['vcp']);
	}
	
	$common_date = ($row['common_date'] != "0000-00-00 00:00:00") ? date("d-m-Y",strtotime($row['common_date'])) : "";
	$consultant_string = "";
	$sql_for_enquiry_admin = "SELECT * FROM job_admin_mapping WHERE job_id = ".$row['id']." ";
	$result_for_enquiry_admin = mysqli_query($conn,$sql_for_enquiry_admin);
	while($row_for_enquiry_admin = mysqli_fetch_array($result_for_enquiry_admin)){
		$consultant_string .= ucfirst(utility::getTableFieldById("admin","name","id",$row_for_enquiry_admin["admin_id"]))."#";
	}
	$consultant_string = substr($consultant_string,0,-1);
	
	$data .= '"'.$row['id'].'"'.",".'"'.$row['company_name'].'"'.",".'"'.$sector.'"'.",".'"'.$row['first_name'].'"'.",".'"'.$row['last_name'].'"'.",".'"'.$row['profession'].'"'.",".'"'.$row['target_market'].'"'.",".'"'.$row['phone_no'].'"'.",".'"'.$row['email'].'"'.",".'"'.$row['password'].'"'.",".'"'.$row['address1'].'"'.",".'"'.$row['address2'].'"'.",".'"'.$row['address3'].'"'.",".'"'.$row['city'].'"'.",".'"'.$county.'"'.",".'"'.$row['postcode'].'"'.",".'"'.$common_date.'"'.",".'"'.$row['interventions'].'"'.",".'"'.$row['sales_process'].'"'.",".'"'.$consultant_string.'"'.",".'"'.$contact_source.'"'.",".'"'.$prospect_status.'"'.",".'"'.$type_of_prospect.'"'.",".'"'.$vcp.'"'.",".'"'.$row['g'].'"'.",".'"'.$row['a'].'"'.",".'"'.$row['i'].'"'.",".'"'.$row['n'].'"'.",".'"'.$row['s'].'"'.",".'"'.$row['itr'].'"'."\n";
}

$file_name = "client_details_export.csv";
ob_clean();
echo $data;
header("Content-type: application/csv"); 
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
die;
?>