<?php
include("../../../include/startup.php"); 

ob_clean();
function is_cyclic($parent,$child){
	global $conn;
	$adj_list=array();
	$adj_list[]=$parent;
	$stack[]=$child;
	
	while(count($stack)>0){
		$id=$stack[0];
		$adj_list[]=$id;
		array_splice($stack, 0, 1);
		$sql="select * from product_link where parent_id='".$id."'";
		$res=mysqli_query($conn,$sql);
		while($row=mysqli_fetch_assoc($res)){
			if(in_array($row["child_id"], $adj_list)){
				//echo "Cyclic";
				return 1;
			}else{
				$stack[]=$row["child_id"];
			}
			
		}
		
	}
	//echo "Not Cyclic ".$parent."->".$child;
	return 0;
}

function get_parent_list($id){
	global $conn;
	$parent_list=array();
	$stack=array();
	$stack[]=$id;
	while(count($stack)>0){
		$id=$stack[0];
		$parent_list[]=$id;
		array_splice($stack, 0, 1);
		$sql="select * from product_link where parent_id='".$id."'";
		$res=mysqli_query($conn,$sql);
		while($row=mysqli_fetch_assoc($res)){
			$stack[]=$row["child_id"];
		}
		
	}
	return implode(",",$parent_list);
}
function calculate_child_cost($id){
	global $conn;
	$total_cost=0;
	$total_labour_cost=0;
	
	$stack=array();
	$stack[]=$id;
	while(count($stack)>0){
		$id=$stack[0];
		array_splice($stack, 0, 1);
		$sql="select * from product_name where id='".$id."'";
		$res=mysqli_query($conn,$sql);
		$row=mysqli_fetch_assoc($res);
		$total_cost=$total_cost + ($row["cost"] * $row["qty"]);
		$total_labour_hours=$total_labour_hours + ($row["labour_hours"]);
		$total_labour_cost=$total_labour_cost + ($row["labour_cost"] * 1);
		$total_final_cost = $total_cost + $total_labour_cost;
		$sql="select * from product_link where parent_id='".$id."'";
		$res=mysqli_query($conn,$sql);
		while($row=mysqli_fetch_assoc($res)){
			$stack[]=$row["child_id"];
		}
		
	}
	
	return array($total_labour_hours,$total_cost,$total_labour_cost,$total_final_cost);
}
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_edit_job_part_used'){
	$id = $_REQUEST['id'];
	$sql= "SELECT * FROM product_extra_info 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){
		if($key == "created_on"){
			$value = utility::getFormatedDateReturn($value);
		}else{
			$data_array[$key] = $value;
		}
	}
	
	$returnArray["data_array"] = $data_array;
	$returnArray["status"] = "success";
	echo json_encode($returnArray, JSON_FORCE_OBJECT);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_job_part_used'){
	$extra_id = $_REQUEST['extra_id'];
	mysqli_query($conn,"DELETE FROM product_extra_info WHERE id = '$extra_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_item_images'){
	$id = $_REQUEST['imageid'];
	$product_image = $_REQUEST['product_image'];
		$dir = "../product_images/";
		if(file_exists($dir.$product_image)){ 
			unlink($dir.$product_image);				
		}
	mysqli_query($conn,"DELETE FROM item_images WHERE id = '$id' ");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_leave_details'){
	$dataArray = array();
	$dataArray["status"] = 0;
	$leave_id = $_REQUEST['leave_id'];
    $query = "SELECT id,is_authorised,employee_name_id,rejection_reason, leave_holiday_start_date, leave_holiday_end_date, leave_holiday_type 
              FROM leave_holiday_management 
              WHERE id = $leave_id";
	       $result = mysqli_query($conn, $query);

    if ($result && mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $dataArray["id"]  = $row['id'];
        $dataArray["employee_name_id"]  = $gsi->getEmployeeById(ucfirst($row['employee_name_id']));
		$dataArray["leave_holiday_type"]  = ($row['leave_holiday_type'] == 1 ? 'Holiday' : 'Leave');
		$dataArray["is_authorised"]  = $row['is_authorised'];
		$dataArray["rejection_reason"]  = $row['rejection_reason'];
		$dataArray["from"]  =  date("d-m-Y H:i", strtotime($row['leave_holiday_start_date'])) ;
		$dataArray["to"]  =  date("d-m-Y H:i", strtotime($row['leave_holiday_end_date'])) ;
		$dataArray["status"]		= 1;  
    }
	echo json_encode($dataArray);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='reject_leave'){
	$dataArray = array();
	$dataArray["status"] = 0;
	$leave_id = $_REQUEST['leave_id'];
	$rej_date_time					= date("Y-m-d H:i:s");
	$reason = $_REQUEST['reason'];
	$admin_user = $_SESSION['admin_user_id'];
    $sql1= "UPDATE leave_holiday_management SET is_authorised = '2' ,rejection_reason ='$reason',authorised_by ='$admin_user' ,auth_date_time ='$rej_date_time' WHERE id = '$leave_id'";
	mysqli_query($conn,$sql1);
	$dataArray["status"]		= 1;  
	echo json_encode($dataArray);

}else if (isset($_REQUEST['action']) && $_REQUEST['action'] =='submit_leave_action') {
    $dataArray = ["status" => 0];
    $leave_id = $_REQUEST['leave_id'];
    $action_type = $_REQUEST['action_type']; 
    $rejection_reason = $_REQUEST['rejection_reason'] ?? null;
    $admin_user = $_SESSION['admin_user_id'];
    $action_date_time = date("Y-m-d H:i:s");

    if ($action_type == 1) { 
        $sql = "UPDATE leave_holiday_management 
                SET is_authorised = '1', authorised_by = '$admin_user',rejection_reason = '$rejection_reason', auth_date_time = '$action_date_time' 
                WHERE id = '$leave_id'";
        mysqli_query($conn, $sql);
        $dataArray["status"] = 1; // Status for approved
    } else { 
        $sql = "UPDATE leave_holiday_management 
                SET is_authorised = '2', rejection_reason = '$rejection_reason', authorised_by = '$admin_user', auth_date_time = '$action_date_time' 
                WHERE id = '$leave_id'";
        mysqli_query($conn, $sql);
        $dataArray["status"] = 2; // Status for rejected
    }

    echo json_encode($dataArray);
}


die;
?>