<?php
$negocio = $_POST['negocio'];
$cant = $_POST['cant'];
$codigo = $_POST['codigo'];
$recib = $_POST['recib'];

require_once('../../Connections/odonto_connection1.php');
require_once('../../inc/utils.php');

$sql="SELECT cod_pre,cant,recibido FROM pedidos WHERE negocio = '".$negocio."' AND mesa = '".$codigo."' ORDER BY id_ped ASC";
$result0 = mysqli_query($odonto_connection,$sql);
$row_rs_ingreso0 = mysqli_fetch_assoc($result0);
$row_cnt = mysqli_num_rows($result0);

if($row_cnt >= 1){
	echo "<div class='row text-center'>";
	$suma=0;
	echo "<table class='table' style='background-color:black;opacity:0.99'><b><font style='color:white' >";
	do{
		$id_pre=$row_rs_ingreso0['cod_pre'];
		$cant=$row_rs_ingreso0['cant'];
		$recibido=$row_rs_ingreso0['recibido'];
		$cant_total += $cant;
		$recibido_total += $recibido;
		$sql="SELECT precio,menu,observa FROM precios WHERE id_pre = '".$id_pre."'";
		$result = mysqli_query($odonto_connection,$sql);
		$row_rs_ingreso = mysqli_fetch_assoc($result);

		$subtotal = $cant * $row_rs_ingreso['precio'];
		$suma = $subtotal + $suma;
		$suma = round($suma,2);
			echo "<tr style='font-size:0.9rem;color:white'><td>".$cant." - ".eliminar_tildes($row_rs_ingreso[menu]);
			if($row_rs_ingreso['observa'] != 'nota' && $row_rs_ingreso['observa']) echo " (".$row_rs_ingreso['observa'].")";
			if($recibido == 0) {
				echo "<a href='#' onclick=myBorrar($id_pre,'$codigo') style='color:white;font-size:0.7rem'>&nbsp;&nbsp;&nbsp;<i class='fas fa-trash'></i></a>";
			}
			else {
				if($recibido != $cant) echo "<a href='#' onclick=myBorrar1($id_pre,$recibido,'$codigo') style='color:white;font-size:0.7rem'>&nbsp;&nbsp;&nbsp;<i class='fas fa-trash'></i></a>";
			}
			
			echo "</td>";
			echo "<td>$".$subtotal;
			echo "</td></tr>";
	}while($row_rs_ingreso0= mysqli_fetch_assoc($result0));
		
		echo "</font></b>";
		echo "</div>";
		
		echo  "<tr><td style='text-align:right;color:white'>Total:</td><td style='color:white'>$".$suma."</td></tr>";
		
		$q = "SELECT propina FROM datos WHERE negocio LIKE '$negocio' LIMIT 1";
		$rs= mysqli_query($odonto_connection,$q) or die("Error en $q");
		$row = mysqli_fetch_assoc($rs);
		$propina=$row[propina];
		if($propina != 1 && !$id_delivery){
			$propina = $suma * 0.08;
			$propina = round($propina,2);
			echo  "<tr style='text-align:right;color:white'><td>Propina estipulada: </td><td>$".$propina."</td></tr>";
		}

		$q = "SELECT costo_envio FROM datos WHERE negocio LIKE '$negocio' LIMIT 1";
		$rs= mysqli_query($odonto_connection,$q) or die("Error en $q");
		$row = mysqli_fetch_assoc($rs);
		$costo_envio=$row['costo_envio'];
		if($costo_envio && $id_delivery){
			echo  "<tr><td style='text-align:right;color:white'>Costo Envio:</td><td style='color:white'>$ ".$costo_envio."</td></tr>";
		}

	echo "</table>";
	
	$q = "SELECT descrip,hora FROM avisos WHERE codigo LIKE '$codigo' AND limpiado = 0 AND aviso like '%Pedido en%' ORDER BY id_avi ASC";
	$rs= mysqli_query($odonto_connection,$q) or die("Error en $q");
	$num_rows = mysqli_num_rows($rs);
	$row = mysqli_fetch_assoc($rs);
	$primero = 1;
	if($num_rows){
		echo "<font class=texto_sombra>";
		echo "Pedidos realizados:<br>";
		do{
			$hora = $row[hora];
			$hora = explode(" ",$hora);
			$hora =$hora[1];
			echo "<i class='far fa-clock'></i>".$hora." <i class='fas fa-arrow-circle-right'></i> ".$row[descrip]."<br>";
			if($primero == 1) echo "Agregados:<br>";
			$primero = 0;
		}while($row = mysqli_fetch_assoc($rs));
		echo "</font>";
	}	
}
		
?>
