diff --git a/app/(private)/AsignacionMesas/page.tsx b/app/(private)/AsignacionMesas/page.tsx
index 322de7d..9d87bb6 100644
--- a/app/(private)/AsignacionMesas/page.tsx
+++ b/app/(private)/AsignacionMesas/page.tsx
@@ -7,7 +7,6 @@ import Toggle from "@/app/Components/Global/Toggle/Toggle";
import { envConfig } from "@/app/lib/config";
import { GetRegisterStudent } from "@/app/lib/getRegisterStudent";
-import axios from "axios";
async function getInscripcion(idCuenta: number) {
try {
@@ -26,11 +25,14 @@ export default async function Page(props: {
searchParams?: Promise<{
key?: string;
numAcount?: string;
+ table?:number
}>;
}) {
const params = await props.searchParams;
const key = params?.key;
const numAcount = params?.numAcount ?? null;
+ const table = params?.table ?? null;
+
let student: any = null;
let platica: any = null;
@@ -98,7 +100,7 @@ export default async function Page(props: {
{
key: "Liberar",
label: "Cancelar mesa",
- content: ,
+ content: ,
},
]}
/>
diff --git a/app/Components/CheckBoxMesa.tsx b/app/Components/CheckBoxMesa.tsx
index 4fbee81..b285af6 100644
--- a/app/Components/CheckBoxMesa.tsx
+++ b/app/Components/CheckBoxMesa.tsx
@@ -1,24 +1,123 @@
"use client";
-import { useState } from "react";
+import { useEffect, useState } from "react";
import SearchUser from "./Global/SearchUser/searchUser";
-import SearchEquipo from "./SearchEquipo";
import Information from "./Global/Information/information";
import SearchMesa from "./SearchMesa";
+import { getTableByCount } from "../lib/getTableByCount";
+import toast from "react-hot-toast";
+import axios from "axios";
+import { envConfig } from "../lib/config";
interface props {
numAcount: string | null;
- student: {
- id_cuenta: number;
- nombre: string;
- carrera:{
- carrera:string;
- }
- };
+ table: number | null;
}
-export default function CheckBoxMesa({ numAcount, student }: props) {
- const [modo, setModo] = useState<"Mesa" | "Cuenta" | null>(null);
+async function getEquipoId(idEquipo: number) {
+ try {
+ const res = await axios.get(
+ `${envConfig.apiUrl}/bitacora-mesa/table/${idEquipo}`,
+ );
+ return res.data;
+ } catch (error: any) {
+ if (axios.isAxiosError(error)) {
+ return {
+ error: error.response?.data?.message || "Error al consultar equipo",
+ };
+ }
+
+ return { error: "Error desconocido" };
+ }
+}
+
+export default function CheckBoxMesa({ numAcount, table }: props) {
+ const [modo, setModo] = useState<"Mesa" | "Cuenta" | null>("Cuenta");
+ const [bitacora, setBitacora] = useState(null);
+ const [tiempoRestante, setTiempoRestante] = useState("");
+ const [minutos, setMinutos] = useState();
+
+ const fetchByCuenta = async (idCuenta: number) => {
+ const result = await getTableByCount(idCuenta);
+
+ if (result?.error) {
+ toast.error(result.error);
+ setBitacora(null);
+ } else {
+ setBitacora(result);
+ }
+ };
+
+ const fetchByEquipo = async (idEquipo: number) => {
+ const result = await getEquipoId(idEquipo);
+
+ if (result?.error) {
+ toast.error(result.error);
+ setBitacora(null);
+ } else {
+ setBitacora(result);
+ }
+ };
+
+ useEffect(() => {
+ if (!numAcount) return;
+ const idCuenta = parseInt(numAcount);
+ if (isNaN(idCuenta)) return;
+
+ fetchByCuenta(idCuenta);
+ }, [numAcount]);
+
+ useEffect(() => {
+ if (!table) return;
+ const idEquipo = table;
+ if (isNaN(idEquipo)) return;
+
+ fetchByEquipo(idEquipo);
+ }, [table]);
+
+ useEffect(() => {
+ if (!bitacora) return;
+
+ const entrada = new Date(bitacora.tiempo_entrada).getTime();
+ const asignadoMs = bitacora.tiempo_asignado * 60 * 1000;
+
+ const interval = setInterval(() => {
+ const ahora = Date.now();
+ const restante = entrada + asignadoMs - ahora;
+
+ if (restante <= 0) {
+ setTiempoRestante("agotado");
+ clearInterval(interval);
+ } else {
+ const minutos = Math.floor(restante / 60000);
+ setMinutos(minutos + 1);
+ const segundos = Math.floor((restante % 60000) / 1000);
+ setTiempoRestante(`${minutos} minutos con ${segundos} segundos`);
+ }
+ }, 1000);
+
+ return () => clearInterval(interval);
+ }, [bitacora]);
+
+
+ const handleButton = async (e: React.MouseEvent) => {
+ e.preventDefault();
+
+ await axios.patch(
+ `${envConfig.apiUrl}/bitacora-mesa/cancelar/${bitacora.id_bitacora_mesa}`,
+ { tiempo_asignado: minutos },
+ );
+
+ toast.success("Tiempo cancelado");
+
+ if (modo === "Cuenta" && numAcount) {
+ fetchByCuenta(parseInt(numAcount));
+ }
+
+ if (modo === "Mesa" && table) {
+ fetchByEquipo(table);
+ }
+ };
return (
<>
@@ -48,10 +147,23 @@ export default function CheckBoxMesa({ numAcount, student }: props) {
{modo === "Cuenta" && }
{modo === "Mesa" && }
- {modo === "Mesa" && }
- {student && (
-
+ {bitacora && (
+ <>
+
+
+ >
)}
>
);
diff --git a/app/Components/SearchMesa.tsx b/app/Components/SearchMesa.tsx
index fa7f550..ecda544 100644
--- a/app/Components/SearchMesa.tsx
+++ b/app/Components/SearchMesa.tsx
@@ -1,6 +1,7 @@
"use client";
-import axios from 'axios';
-import { useEffect, useState } from 'react';
+import axios from "axios";
+import { usePathname, useRouter, useSearchParams } from "next/navigation";
+import { useEffect, useState } from "react";
interface Mesa {
id_mesa: number;
@@ -20,11 +21,12 @@ function Mesas({ id_cuenta }: Props) {
setLoading(true);
- axios.get(`/cuenta/${id_cuenta}`)
- .then(res => {
+ axios
+ .get(`/cuenta/${id_cuenta}`)
+ .then((res) => {
setMesas(res.data);
})
- .catch(err => console.error(err))
+ .catch((err) => console.error(err))
.finally(() => setLoading(false));
}, [id_cuenta]);
@@ -38,7 +40,7 @@ function Mesas({ id_cuenta }: Props) {
No hay mesas
) : (
- {mesas.map(mesa => (
+ {mesas.map((mesa) => (
-
Mesa {mesa.id_mesa} {mesa.nombre && `- ${mesa.nombre}`}
@@ -50,12 +52,19 @@ function Mesas({ id_cuenta }: Props) {
}
export default function SearchMesa() {
- const [inputValue, setInputValue] = useState('');
+ const [inputValue, setInputValue] = useState("");
const [idCuenta, setIdCuenta] = useState(null);
+ const router = useRouter();
+ const pathname = usePathname();
+ const searchParams = useSearchParams();
const buscar = (e: React.FormEvent) => {
e.preventDefault();
- setIdCuenta(Number(inputValue));
+ const params = new URLSearchParams(searchParams.toString());
+ if (inputValue) {
+ params.set("table", `${inputValue}`);
+ router.push(`${pathname}?${params.toString()}`);
+ }
};
return (
@@ -76,8 +85,6 @@ export default function SearchMesa() {
-
- {idCuenta && }
>
);
}
diff --git a/app/lib/getTableByCount.ts b/app/lib/getTableByCount.ts
new file mode 100644
index 0000000..debf82a
--- /dev/null
+++ b/app/lib/getTableByCount.ts
@@ -0,0 +1,19 @@
+import axios from "axios";
+import { envConfig } from "./config";
+
+export async function getTableByCount(idCuenta: number) {
+ try {
+ const res = await axios.get(
+ `${envConfig.apiUrl}/bitacora-mesa/cuenta/${idCuenta}`,
+ );
+ return res.data;
+ } catch (error: any) {
+ if (axios.isAxiosError(error)) {
+ return {
+ error: error.response?.data?.message || "Error al consultar equipo",
+ };
+ }
+
+ return { error: "Error desconocido" };
+ }
+}
\ No newline at end of file