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

$result = 'failed' ;

// set value in variable
$list_staff_id = escapeString($_POST['list_staff_id']) ;
$list_date 	   = escapeString($_POST['list_date']) ;
$list_health   = escapeString($_POST['list_health']) ;

if ( $list_staff_id != '' && $list_date != '' && $list_health != '' ){

	$mysqli_page = $mysqli->query("SELECT * FROM staff_health
								   WHERE staff_id = '".$list_staff_id."' AND request_date = '".$list_date."' LIMIT 1");
	if ( $mysqli_page->num_rows == 0 ){
		$mysqli->query( "INSERT INTO staff_health
						 ( staff_id, request_date, created_at ) VALUES
						 ( '".$list_staff_id."', '".$list_date."', '".TODAYDATE."' )" ) ;
		$health_id   = $mysqli->insert_id ;
	}else{
		$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
		$health_id   = $row_page['health_id'] ;
	}

	$mysqli->query("UPDATE staff_health SET
					degree  			= '".escapeString($_POST['list_health'])."',
					updated_at 			= '".TODAYDATE."'
					WHERE health_id 	= '".$health_id."'") ;

	$result = 'success' ;
}

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

// echo array
echo json_encode($array) ;

?>