<?php
include '../connect/cms-config.php' ;
include '../requires/session.php' ;
include '../requires/function.php' ;

// set value in variable
$place 			= escapeString($_POST['place']) ;
$staff_id  		= escapeString($_POST['staff_id']) ;
$type	  		= escapeString($_POST['type']) ;
$group_id		= escapeString($_POST['group_id']) ;
$formula_group = escapeString($_POST['formula_group']);

// default variable
$content 	= '' ;
$result 	= 'failed' ;


if (!empty($type)){
	switch ($type) {
		case 'staff':
			// array for seaching query
			$staff_list = [];
			$staff_id = ( empty($staff_id) ? '0' : $staff_id );
			if ( $place > 0 ){	

				$query = "SELECT * FROM system_orgChart where chart_id = '".$place."'";
				//echo $query;
				$query = $mysqli->query($query);
				if ( $query ->num_rows > 0 ){
					$row   = $query->fetch_array(MYSQLI_ASSOC);
					$old_staff_group[1]["old"] = $row['staff_id'];
					// $old_staff_group[1] = [
					// 	"old" => $row['staff_id'],
					// 	"new" => $staff_id,
					// ];
				}
				$old_staff_group[1]["new"] = $staff_id;

				$query = "UPDATE system_orgChart set staff_id = '".$staff_id."' where chart_id = '".$place."'";

				if (!$mysqli->query($query)){
					$result = "failed";
				}else{
					$result = "success";
				}
			}


			break;
		case 'staff_update':
			// array for seaching query
			$staff_list = [];

			if ( $staff_id > 0 && $group_id > 0 ){	

				$old_staff_group[0] = false;
				$check_group = $mysqli->query("SELECT * FROM staff
											  WHERE deleted_at IS NULL AND staff_id = '".$staff_id."' LIMIT 1") ;
				if ( $check_group->num_rows > 0 ){
					$row = $check_group->fetch_array(MYSQLI_ASSOC);
					if ( $row['group_id'] != $group_id ){
						$old_staff_group[0] = true;
						$old_staff_group[1] = [
							"old" => $row['group_id'],
							"new" => $group_id,
							];
					}
					
				}

				$query = "UPDATE staff set group_id = '".$group_id."' where staff_id = '".$staff_id."'";
				
				if (!$mysqli->query($query)){
					$result = "failed";
				}else{
					$result = "success";
				}
			}

			break;
		case "working_hours":
			$old_staff_group[0] = false;
			if ( $place > 0 && $group_id > 0 ){	
				$query = "SELECT * FROM system_formula where chart_id = '".$place."' AND formula_group = '".$formula_group."'";
				//echo $query;
				$query = $mysqli->query($query);
				if ($query->num_rows > 0){
					$row = $query->fetch_array(MYSQLI_ASSOC);
					if ( $row['group_id'] != $group_id ){
						$old_staff_group[0] = true;
						$old_staff_group[1] = [
							"old" => $row['group_id'],
								"new" => $group_id,
							"formula" => $formula_group,
							];
					}
					if (!$mysqli->query("UPDATE system_formula set group_id = '".$group_id."' where chart_id = '".$place."' AND formula_group = '".$formula_group."'")){
						$result = "failed";
					}else{
						$result = "success";
					}
				}else{
					if ( $row['group_id'] != $group_id ){
						$old_staff_group[0] = true;
						$old_staff_group[1] = [
							"old" => $row['group_id'],
							"new" => $group_id,
							"formula" => $formula_group,
							
							];
					}
					if (!$mysqli->query("INSERT INTO system_formula (group_id, chart_id, formula_group) VALUES ('".$group_id."', '".$place."', '".$formula_group."')")){
						$result = "failed";
					}else{
						$result = "success";
					}
				}

			}
			break;

		case 'update_type':
			// array for seaching query
			$exchange = escapeString($_POST['exchange']);
			if ( $group_id > 0 ){	

				$query = "UPDATE system_orgChart set type = '".$exchange."', staff_id = '' where chart_id = '".$group_id."'";
				
				if (!$mysqli->query($query)){
					$result = "failed";			
				}else{
					$query = "UPDATE system_formula set group_id = '' where chart_id = '".$group_id."'";
					if (!$mysqli->query($query)){
						$result = "failed";
					}else{
						$result = "success";
					}	
				}
			}

			break;
		case 'remark_update':
			// array for seaching query
			$remark = escapeString($_POST['remark']);
			if ( $group_id > 0 ){	

				$query = "UPDATE system_orgChart set remark = '".$remark."' where chart_id = '".$group_id."'";
				
				if (!$mysqli->query($query)){
					$result = "failed";			
				}else{
					$result = "success";						
				}
			}

			break;
		default:
			# code...
			break;
	}
}



// set in array
$array['data'] = $data ;
$array['result'] = $result ;

// echo array
echo json_encode($array);
?>