<?php
date_default_timezone_set('Asia/Jakarta');
header('Content-Type: application/json'); 
include_once 'connection/connect.php';

$tanggal = date('Y-m-d');
$result = mysqli_query($mysqli, "
   SELECT MAX(nomor) AS nomor FROM antrian WHERE DATE(waktu)='$tanggal' LIMIT 1
");

foreach ($result as $key => $value) {
	$nomor = $value['nomor'];
}

$kuota = mysqli_query($mysqli, "
   SELECT kuota FROM kuota WHERE DATE(tanggal)='$tanggal' LIMIT 1
");

if (mysqli_num_rows($kuota) == 0) {
 	$quota = 0;
	$is_quota = false;
} else {
 foreach ($kuota as $key => $value) {
   $quota = (int)$value['kuota'];
	$is_quota = true;
 }
}

$base_url = "https://antrian.casie.cloud/assets/img_print/";
//$base_url = "http://192.168.1.3/web_antrian_dinsos/assets/img_print/";
$img_url  = $base_url.$nomor.".png";


echo json_encode([
   "message" => "success",
	"data" => [
		'is_error'       => false,
		'pesan'          => "Berhasil mengambil antrian",
		'currect_number' => (int)$nomor,
		'number'         => $nomor+1,
		'is_kuota'       => $is_quota,
		'kuota'          => $quota,
		'tanggal'        => $tanggal,
		'next_number'    => $nomor+2,
		'image_url'		  => $img_url,
	],
	"status_code" => 200,
]);
