<?php	ob_start();
require_once('../preheader1.php'); // <-- this include file MUST go first before any HTML/output
include ('../ajaxCRUD.class.php'); // <-- this include file MUST go first before any HTML/output
$user=$_SESSION['MM_Username'];
if(!isset($user)) header("Location:" . "../../index.php?deslogeo");
require_once('../../Connections/odonto_connection1.php');

$query_rs_todos = "SELECT negocio,categ,sucursal FROM ingreso WHERE user LIKE '$user'";
$rs_ingreso0 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
$row_rs_ingreso0 = mysqli_fetch_assoc($rs_ingreso0);
$categ=$row_rs_ingreso0[categ];
$negocio=$row_rs_ingreso0[negocio];
$sucursal=$row_rs_ingreso0[sucursal];

if(isset($_GET[id_avi])){$id_avi=$_GET[id_avi];
$query_rs_todos = "UPDATE avisos SET avisado = 1 WHERE id_avi = $id_avi";
$rs_ingreso2 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
}

if(isset($_GET['id_categ'])) $id_categ_elegido = $_GET['id_categ'];

?>
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<script>

</script>
</head>

<?php   

if($id_categ_elegido) {
	
	$q = "SELECT categoria FROM categorias WHERE negocio LIKE '$negocio' AND id_categ = $id_categ_elegido";
	$rs0 = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	$row0 = mysqli_fetch_assoc($rs0);
	$nombre_categoria = $row0['categoria'];			
				
	$tabla1 = new ajaxCRUD("$nombre_categoria", "precios", "id_pre", "../");
	$tabla1->defineRelationship("id_categ", "categorias", "id_categ", "categoria", "id_categ ASC", 0, "WHERE negocio LIKE '$negocio'"); //use your own table - this table (tabla1Relationship) not included in the installation script
	$tabla1->omitPrimaryKey();
	$aux1="../../../imagenes_productos/".$negocio."/";
	$aux2="../../../imagenes_productos/".$negocio."/";
	$allowedExts = array("png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff","txt");
	$tabla1->setFileUpload("imagen", $aux1, $aux2,$allowedExts);
	$tabla1->appendUploadFilename("id_pre");
	$tabla1->formatFieldWithFunction('imagen', 'makeImg');
	function makeImg($val)
	{
		global $negocio;
		if($val) return "<img src=\"../../../imagenes_productos/".$negocio."/$val\" width=\"100\" height=\"100\" />";
		else return "<img src=\"../../../imagenes_productos/sin_imagen.png\" width=\"100\" height=\"100\" />";
	}
	$tabla1->displayAs("id_categ", "Categoria");
	$tabla1->displayAs("menu", "Descripcion");
	$tabla1->displayAs("observa", "Nota");
	$tabla1->displayAs("precio", "Precio");
	$tabla1->displayAs("publicar", "Local");
	$tabla1->displayAs("delivery", "Delivery");
	$tabla1->omitFieldCompletely("categoria");
	$tabla1->omitFieldCompletely("sucursal");
	$tabla1->omitFieldCompletely("negocio");
	$tabla1->omitAddField("orden");
	$tabla1->omitAddField("id_categ");
	
	$tabla1->setTextboxWidth('menu', 50);
	$tabla1->emptyTableMessage = "";
	$tabla1->addValueOnInsert("negocio", "$negocio");
	$tabla1->addValueOnInsert("sucursal", "$sucursal");
	$tabla1->addValueOnInsert("sucursal", "$id_categ_elegido");
	
	$q = "SELECT MAX(orden) AS LargestOrden FROM precios WHERE negocio = '$negocio' AND id_categ = $id_categ_elegido";
	$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	$row = mysqli_fetch_assoc($rs);
	$LargestOrden = $row[LargestOrden];
	$LargestOrden = $LargestOrden + 1;
	$tabla1->addValueOnInsert("orden", $LargestOrden);
	
	
	$tabla1-> addWhereClause("WHERE (negocio = '$negocio' AND id_categ = $id_categ_elegido)");
	$tabla1->addOrderBy("ORDER BY orden ASC");

	$query_rs_todos = "SELECT id_categ,categoria FROM categorias WHERE negocio LIKE '$negocio'";
	$rs_ingreso55 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
	$row_rs_ingreso55 = mysqli_fetch_assoc($rs_ingreso55);
	$allowableValues = array();

	do{   
		$id_categ = $row_rs_ingreso55['id_categ'];
		$categoria = $row_rs_ingreso55['categoria'];
		$allowableValues[] = array(0 => "$id_categ", 1 => "$categoria");
	} while($row_rs_ingreso55 = mysqli_fetch_assoc($rs_ingreso55));

	$tabla1->defineAllowableValues("id_categ", $allowableValues);

	$q = "SELECT id_pre,orden FROM precios WHERE negocio = '$negocio' AND id_categ = $id_categ_elegido";
	$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	$row = mysqli_fetch_assoc($rs);
	$orden_orig = array();
	do{
		$orden_array[] = $row['orden'];
		$orden_orig_array[$row['id_pre']] = $row['orden'];
	}while($row = mysqli_fetch_assoc($rs));
	//sort($orden_array);
	$tabla1->defineAllowableValues("orden", $orden_array);
    $tabla1->disallowDelete();
	$tabla1->onUpdateExecuteCallBackFunction("orden","doMyLogicAfterUpdating");
	$numRows = $tabla1->getNumRows();
	$tabla1->defineCheckbox("publicar", "si", "no");
	$tabla1->defineCheckbox("delivery", "si", "no");
	$tabla1->displayAddFormTop();
	$tabla1->deleteText = "Borrar";
	$tabla1->addText= "AGREGAR";
	$tabla1->setCSSFile('greysuit.css');
	//$tabla1->formatFieldWithFunction('precio', 'makeBlue');
	function doMyLogicAfterUpdating($array){
	  global $negocio;
	  global $orden_orig_array;
	  global $id_categ_elegido;
	  $id = $array['id_pre']; //example index - field in db
	  $orden = $array['orden']; //example index - field in db
	  $orden_orig = serialize($orden_orig_array);
	  header("Location:auxi_orden.php?negocio=$negocio&id_pre=$id&orden=$orden&orden_original=$orden_orig&id_categ=$id_categ_elegido");
	}
	function makeBold($val){
		if ($val == "") return "-----";
		return "<font style='font-size:10px'><b>$val</b></font>";
	}
	function makeBlue($val){
		return "<span style='color: blue;'>$val</span>";
	}
}
?>	

<body style="background-color:#0D1749"/>

	<?php	require_once('menu.php');?>
	<br>
	
	<?php	require_once('botones.php');?>
	
<div class="container-fluid text-center">
	<div class="row">
		<div class="col">
			<a href="negocio_3_categorias.php" class="btn btn-outline-light btn-sm" role="button" style="width:15rem"><b><i class="fas fa-certificate"></i> Categorias</b></a></p>
		</div>
	</div>
	<div class="row">
		<div class="col">
			<?php 
			if($id_categ_elegido){
				$q = "SELECT categoria FROM categorias WHERE negocio LIKE '$negocio' AND id_categ = $id_categ_elegido";
				$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
				$row = mysqli_fetch_assoc($rs);
				echo "<h5>".$row['categoria']."</h5>";
				$tabla1->showTable();
				}
			?>
		</div>
	</div>

	<div class="row">
		<div class="col">
			<?php 
			$q = "SELECT id_categ,categoria FROM categorias WHERE negocio LIKE '$negocio' ORDER BY orden ASC";
			$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
			$row = mysqli_fetch_assoc($rs);
			do{
				echo "<a href='negocio_3.php?id_categ=".$row['id_categ']."' style='color:white'><small>".$categoria=$row[categoria]."</small></a><br>";
			}while($row = mysqli_fetch_assoc($rs));
			?>
		</div>
	</div>
	</div>

</body>
</html>

