<?php
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_package_price'){
	$packages_id = $_REQUEST['packages_id'];
	$row = utility::getTableRowByField("fixed_packages","id",$packages_id);
	$returnArray["price"] = $row["price"];
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_hourly_packages_price'){
	$hourly_packages_id = $_REQUEST['hourly_packages_id'];
	$row = utility::getTableRowByField("hourly_packages","id",$hourly_packages_id);
	$returnArray["price"] = $row["price"];
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='send_mail'){
	include('send_mail_modal.php');
	die;
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='mail_send'){
	$job_id = $_REQUEST['job_id'];
	$admin_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$mail_format_subject = $_REQUEST['mail_format_subject'];
	$mail_format_body =  str_replace("Â","",$_REQUEST['mail_format_body']);
	$mail_format_body = str_replace($find_array,$replace_array,$mail_format_body);
	$email_id = utility::getTableFieldById("job","email","id",$job_id);
	$email_id_test = "shoebahmad999@gmail.com";
	
	$email_date = date("Y-m-d");
	$sql = "INSERT INTO email_track(job_id,email_subject,email_body,email_id,email_date) VALUES('$job_id','$mail_format_subject','$mail_format_body','$email_id','$email_date')";
	mysqli_query($conn,$sql);
	$email_track_id = mysqli_insert_id($conn);
	/***************************** File Upload *************************************************/
	$digits = 5;
	
	$file_path 		= array();
	foreach($_FILES['mail_document'] as $file_pro => $file_value){
		foreach($_FILES['mail_document'][$file_pro] as $k => $v){
			if(!(isset($v) && $v !="")){
				continue;
			}
			${$file_pro}[$k] = $v;
		}
    }
	foreach($name as $key => $file_name){
		$my_path = $_SERVER['DOCUMENT_ROOT']."/beb/admin/document_upload/";
		
		$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
		$uploaddir = '../../document_upload/';
		$document_name_array = explode(".",$file_name);
		$document_name = $document_name_array[0]."_".$random_number.".".$document_name_array[1];
		$uploadfile = $uploaddir . $document_name;
		array_push($file_path,$my_path.$document_name);
		move_uploaded_file($tmp_name[$key], $uploadfile);
		$sql= "INSERT into mail_document_upload(job_id,email_track_id,document_name) VALUES('$job_id','$email_track_id','$document_name')";
		mysqli_query($conn,$sql);
	}
	/***************************** End File Upload *************************************************/	
	//echo $functions->HTMLemailWithoutSignatureBlock($mail_format_body);
	//die;
	//////////////////////// send mail ////////////////////
	$site_email = $config->siteEmail;
	//$table ="<div style='font-size:14px;'>Job with job id $job_id confirm and you can proceed to survey this property.</div>";
	//if(utility::multi_attach_mail($email_id, $mail_format_subject, $functions->HTMLemail($mail_format_body), $file_path, $config->siteEmail,"",$to)){
	if(utility::mailSMTP($email_id, $mail_format_subject, $functions->HTMLemailWithoutSignatureBlock($mail_format_body,$admin_id), $file_path,$site_email,$site_email,"","","",$admin_id)){
		utility::mailSMTP($email_id_test, $mail_format_subject, $functions->HTMLemailWithoutSignatureBlock($mail_format_body,$admin_id), $file_path,$site_email,$site_email,"","","",$admin_id);
		echo "Mail send successfully";
	}else{
		echo "There is some error.";
	}
	die;
	///////////////// End send mail  //////////////////////
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_invoice_price'){
	$invoice_id = $_REQUEST['invoice_id'];
	$invoice_amount = utility::getTableFieldById("invoice","amount","id",$invoice_id);
	$returnArray["invoice_amount"] = $invoice_amount;
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_invoice_by_job_id'){
	$job_id = $_REQUEST['job_id'];
	echo $gsi->getInvoiceList("invoice_id","","100%",false,"form-control",$job_id); 
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_invoice_number'){
	$job_id = $_REQUEST['job_id'];
	echo $gsi->getInvoiceNumber($job_id); 
}
?>