<?php
include "../db.php";

$page=$_GET['page'] ?? 1;
$search=$_GET['search'] ?? '';
$limit=$_GET['limit'] ?? 10;

$offset=($page-1)*$limit;

$where="WHERE menu_name LIKE '%$search%'";

$total=$conn->query("SELECT COUNT(*) as c FROM menus $where")->fetch_assoc()['c'];

$query="SELECT * FROM menus $where ORDER BY id DESC LIMIT $limit OFFSET $offset";

$result=$conn->query($query);

ob_start();
include "menus_table.php";
$table=ob_get_clean();

/* PAGINATION */

$pages=ceil($total/$limit);

$pagination="";

for($i=1;$i<=$pages;$i++){
$pagination.="<button onclick='goPage($i)'>$i</button>";
}

echo json_encode([
"table"=>$table,
"pagination"=>$pagination
]);