<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_template_document'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string","section_ids"));
    
	$page_action = $_REQUEST['page_action'];	
	$id = $_REQUEST['id'];	
	$job_id = $_REQUEST['job_id'];	
	$document_template_id = $_REQUEST['document_template_id'];	
	$section_ids = implode(",",$_REQUEST['section_ids']);	
	$updated_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO document_template_job_mapping(job_id,document_template_id,section_ids,updated_on,updated_by) VALUES('$job_id','$document_template_id','$section_ids','$updated_on','$updated_by')";
		mysqli_query($conn,$sql);
	}else{
		$sql= "UPDATE document_template_job_mapping SET job_id = '$job_id',document_template_id = '$document_template_id', section_ids = '$section_ids', updated_on = '$updated_on', updated_by = '$updated_by' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_template_document'){
	$template_document_id = $_REQUEST['template_document_id'];
	mysqli_query($conn,"DELETE FROM document_template_job_mapping WHERE id = '$template_document_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit_template_document'){
	$id = $_REQUEST['template_document_id'];
	
	$sql= "SELECT * FROM document_template_job_mapping WHERE id = '$id' ";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_assoc($result_edit);
	
	$data_array = array();
	foreach($row_edit as $key => $value){
		$data_array[$key] = $value;
	}
	$returnArray["data_array"] = $data_array;
	$returnArray["status"] = "success";
	
    echo json_encode($returnArray, JSON_FORCE_OBJECT);
}else{
	////////////// For Listing data through JSON ///////////////////////////
	$id=$_REQUEST['id'];
    $query_for_document_template = "SELECT * FROM document_template_job_mapping WHERE job_id='$id' ORDER BY id ASC ";
	$sql_for_document_template = mysqli_query($conn,$query_for_document_template);
	$result_for_document_template = mysqli_num_rows($sql_for_document_template);
	$document_template = "";
	if($result_for_document_template > 0){
		$document_template .= '{';
		$document_template .= '"data" : [';
		while($row_for_document_template = mysqli_fetch_array($sql_for_document_template)){
			$section_name_array = array();
			$section_ids_array = explode(",",$row_for_document_template['section_ids']);
			foreach($section_ids_array as $section_id){
				array_push($section_name_array,ucfirst(utility::getTableFieldById('document_template_section','section','id',$section_id)));
			}
			
			//$document_template .= '{"client":"'  . $gsi->getJobUserById($row_for_document_template['job_id']) . '",';
			$document_template .= '{"client":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_for_document_template['job_id'])) . '",';
			$document_template .= '"document_template":"'  . ucfirst(utility::getTableFieldById('document_template','document_template','id',$row_for_document_template['document_template_id'])) . '",';
			$document_template .= '"section_ids":"'  . implode(", ",$section_name_array) . '",';
			$document_template .= '"id":"'  . $row_for_document_template['id'] . '"},';
		}
		$document_template = substr($document_template,0,-1);
		$document_template .= "]";
		$document_template .= '}';
	}else{
		$document_template .= '{';
		$document_template .= '"data" : [';
		$document_template .= "]";
		$document_template .= '}';
	}
	echo $document_template;
}
die;
?>