<?php
//include("../../database.inc.php"); //database.inc.php is databse connection file which will be included
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='change_status'){
  $page_id = $_REQUEST['page_id'];  
  $new_status = ($_REQUEST['status'] == 0) ? 1 : 0; 
  if($page_id){
    $update_page = "UPDATE pages SET live ='".$new_status."' WHERE id='".$page_id."' ";
    if(mysqli_query($conn,$update_page)){
      echo "Page status changed";
    }
  }else{
    echo "Page status not changed";
  }
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='check_country_exist'){
	$country = $_REQUEST['country'];
	$id = $_REQUEST['id'];
	$where = "";
	if(isset($id) && $id !=""){
		$where = " AND id != '$id' ";
	}
	$returnArray = array();
	$sql = "SELECT * FROM country WHERE country = '$country' $where ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray['status'] = "failure";
	}else{
		$returnArray['status'] = "success";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_route_details'){ 
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
	$table_name = "set_route_details";
	$id					 = $_REQUEST['id'];
	$page_action		 = $_REQUEST['page_action_part_used'];
	$set_id		 = $_REQUEST['set_id'];
	$route_no = $_REQUEST['route_no'];
    $route_desc = $_REQUEST['route_desc'];
    $part_no = $_REQUEST['part_no'];
    $previous_part_no = $_REQUEST['previous_part_no'];
    $operation = $_REQUEST['operation'];
    $machine_lock = isset($_REQUEST['machine_lock']) ? 1 : 0;
    $machine_high_priority = $_REQUEST['machine_high_priority'];
    $machine_medium_priority = $_REQUEST['machine_medium_priority'];
    $machine_low_priority = $_REQUEST['machine_low_priority'];
    $machine_rate = $_REQUEST['machine_rate'];
    $set_time = $_REQUEST['set_time'];
    $operation_time = $_REQUEST['operation_time'];
    $operation_unit = 0;
    $parts_per_operation = $_REQUEST['parts_per_operation'];
    $workers_needed = $_REQUEST['workers_needed'];
   $labour_rate = isset($_REQUEST['labour_rate']) && $_REQUEST['labour_rate'] !== '' ? (float) $_REQUEST['labour_rate'] : 0;

    $notes = $_REQUEST['notes'];
    
	$created_by					= $_SESSION['user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	$updated_by					= $_SESSION['user_id'];
	
	$hidden_document_upload 	= $_REQUEST['hidden_document_upload'];
	if(isset($_FILES['document_upload']['name']) && $_FILES['document_upload']['name'] !=""){
		$path = $_FILES['document_upload']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf" || $ext == "doc" || $ext == "docx" || $ext == "xls" || $ext == "xlsx"){
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			 $uploaddir = __DIR__ . '/../../route_documents/';
			
			 $old_document	=	$uploaddir.$hidden_document_upload;	
			if(file_exists($old_document)){
				unlink($old_document);
			}
			$photo_after_complete_array = explode(".",$_FILES['document_upload']['name']);
			$document_upload = $id."".$photo_after_complete_array[0]."".$random_number.".".$photo_after_complete_array[1];
		    $uploadfile = $uploaddir . $document_upload;
			move_uploaded_file($_FILES['document_upload']['tmp_name'], $uploadfile);
			
			$tmp = ucfirst(str_replace("_"," ", $table_name));
		}else{
			$document_upload = $hidden_document_upload;
		}
    }else{
		$document_upload = $hidden_document_upload;
	}
	  
	list($hours, $minutes) = explode(':', $operation_time);
		$hours = (int) $hours;  
		$minutes = (int) $minutes;  
		$total_hours = $hours + ($minutes / 60);  
		$labor_cost = $total_hours * $labour_rate;  

	if($page_action == "add"){
		    $sql = "INSERT INTO $table_name (route_no,set_id,route_desc, part_no, previous_part_no, operation, machine_lock, 
            machine_high_priority, machine_medium_priority, machine_low_priority, machine_rate, set_time, operation_time,operation_unit, 
            parts_per_operation, workers_needed, labour_rate, document_upload, notes,created_on,created_by,updated_by) 
            VALUES ('$route_no','$set_id','$route_desc', '$part_no', '$previous_part_no', '$operation', '$machine_lock', 
            '$machine_high_priority', '$machine_medium_priority', '$machine_low_priority', '$machine_rate', '$set_time', 
            '$operation_time', '$operation_unit','$parts_per_operation', '$workers_needed', '$labour_rate', '$document_upload', '$notes', '$created_on', '$created_by', '$updated_by')";
         mysqli_query($conn, $sql);
		 
	}else{
	        $sql = "UPDATE $table_name SET route_desc = '$route_desc', part_no = '$part_no', previous_part_no = '$previous_part_no',
            operation = '$operation', machine_lock = '$machine_lock', machine_high_priority = '$machine_high_priority',
            machine_medium_priority = '$machine_medium_priority', machine_low_priority = '$machine_low_priority', machine_rate = '$machine_rate',
            set_time = '$set_time', operation_time = '$operation_time',operation_unit = '$operation_unit', parts_per_operation = '$parts_per_operation',
            workers_needed = '$workers_needed', labour_rate = '$labour_rate', document_upload = '$document_upload', notes = '$notes', updated_by = '$updated_by' WHERE set_id = $set_id";
    mysqli_query($conn, $sql);
	 
	}
}
die;
?>