<?php
header('Content-type: application/json');
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');
$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {
	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		$response = array();


		if(isset($_POST['accept_invoice'])){

			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');

			$sql = "UPDATE project_invoice SET flag = '1' WHERE id = $id";
          	$result = mysqli_query($conn, $sql);

          	if ($result) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}

			echo json_encode($response);

		}

		if(isset($_POST['remove_invoice'])){

			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');

			$sql = "UPDATE project_invoice SET flag = '2' WHERE id = $id";
          	$result = mysqli_query($conn, $sql);

          	if ($result) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}

			echo json_encode($response);

		}

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}

}
?>