<?php
if(isset($_POST["bexp"])) {		
	$varCod = (isset($_POST["cod"]))?$_POST["cod"]:NULL;
	$varNom = (isset($_POST["nam"]))?$_POST["nam"]:NULL;
	$sql = "call spQuery_Ruta('$varCod', '$varNom')";
	$_SESSION["expor_sql_1"] =  $sql;
	$_SESSION["expor_sql_2"] = NULL;
	$varTitulo = (isset($_POST["titu"]))?$_POST["titu"]:"Reporte";
	$dir = "export.php?page=$varTitulo";
	header("location: $dir");
	}
?>
<script language="javascript">	
	function EditarRegistro(varLink, cod, nom) {		
		var Ruta = varLink + '?opt=EDIT&valor='+cod;
		window.location=Ruta;
		}
			
	function EliminarRegistro(varLink, form, cod, nom) {		
		if(confirm('¿ESTA SEGURO DE ELIMINAR EL REGISTRO?\n'+cod+' - '+nom)) {
			var Ruta = varLink + '?opt=DROP&valor='+cod;
			window.location=Ruta;
			}
		}
</script>

<?php
function MyTable($obj, $Titulo, $Opcion, $Valor, $TamFila) {
	$Link = $_SERVER['PHP_SELF'];
	$MyTabla = "_sv_ruta";
	$myID = "id_ruta";
	$varCod = (isset($_POST["cod"]))?$_POST["cod"]:NULL;
	$varNom = (isset($_POST["nam"]))?$_POST["nam"]:NULL;
	if(!isset($Opcion)) {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"400", "clase"=>"FormTitulo", "titulo"=>$Titulo." - BUSQUEDA",
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$Titulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"80",
											 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "valor"=>$_POST["cod"], 
											 "alinear"=>"center", "ancho"=>"70")),								   
									   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$_POST["nam"], "ancho"=>"280"))									   
									   ),
						"boton"=>array(array("tipo"=>"submit", "id"=>"bcon", "name"=>"bcon", "ancho"=>"80", "alto"=>"22", "texto"=>"Buscar"),
									   array("tipo"=>"submit", "id"=>"bexp", "name"=>"bexp", "ancho"=>"80", "alto"=>"22", "texto"=>"Exportar"),								  
									   array("tipo"=>"button", "id"=>"bnew", "name"=>"bnew", "ancho"=>"80", "alto"=>"22", "texto"=>"(+) Nuevo", "click"=>"location='$Link?opt=NEW'"),
									   )
						);
		echo $obj->setTablaForm($varTabla);
		$sql = "call spQuery_Ruta('$varCod', '$varNom')";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"600", "sql_cab"=>$sql,	"pages"=>20,
						"conte"=>array(array("titulo"=>"CRUT",    "ancho"=>"30" , "alinear"=>"center", "clase"=>"sortable-numeric", "tipo"=>"texto"), 
									   array("titulo"=>"RUTA", "ancho"=>"500", "clase"  =>"sortable"),									   
									   array("titulo"=>"FCREADO", "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"FUPDATE", "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO", "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"UUPDATE", "ancho"=>"100", "oculto"=>"SI")
									   ), 
						"boton"=>array(array("tipo"=>"img", "id"=>"bbus", "name"=>"bbus", "img"=>"images/b_table_edit.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>EDITAR</b> EL REGISTRO<br><b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
												   				),
											 "click"=>array("funcion"=>"EditarRegistro",
															"param"=>array(array("valor"=>$Link, "tipo"=>"TEXTO"),
																		   array("valor"=>0, "tipo"=>"INDEX"),
																		   array("valor"=>1, "tipo"=>"INDEX") 
																		  )
															)
											 
											),
									   array("tipo"=>"img", "id"=>"badd", "name"=>"badd", "img"=>"images/b_table_delete.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>ELIMINAR</b> EL REGISTRO<br><b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
												   				),
											 "click"=>array("funcion"=>"EliminarRegistro",
															"param"=>array(array("valor"=>$Link, "tipo"=>"TEXTO"),
																		   array("valor"=>"this", "tipo"=>"NUM"),
																		   array("valor"=>0, "tipo"=>"INDEX"),
																		   array("valor"=>1, "tipo"=>"INDEX") 
																		   )
															)
											 )
									)
						);
		echo $obj->SetGridTabla($varTabla);	
		}
	elseif($Opcion=="NEW" || $Opcion=="EDIT") {
		if($Opcion=="NEW") {
			$SubTit = $Titulo." - NUEVO";
			$Permite = $_SESSION["_FINS"];
			$BloqCod = "SI";
			$Fila = NULL;
			}
		else {
			$SubTit =  $Titulo." - EDITAR";
			$Permite = $_SESSION["_FUPD"];
			$BloqCod = "SI";
			$sql = "select * from $MyTabla where $myID = $Valor";		
			$obj->AbrirConexion();
			$rs = $obj->Query($sql);
			$obj->CerrarConexion();
			$Fila = $obj->FetchArray($rs);
			}
		if($Permite<>"S") {
			echo "<script language='Javascript'>";
			echo "window.location='$Link'";
			echo "</script>\n";
			}
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"480", "clase"=>"FormTitulo", "titulo"=>$SubTit,
					"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST"),						
					"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$Titulo),
								   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
								  ),
					"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "clase"=>"inputDisable",
										 "valor"=>$Fila[$myID], "alinear"=>"center", "locked"=>"SI", "ancho"=>"70")),								   
								   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$Fila["nombre"], "ancho"=>"350")),					   							   ),
					"boton"=>array(array("tipo"=>"submit", "id"=>"bsav", "name"=>"bsav", "ancho"=>"80", "alto"=>"22", 
										 "texto"=>"Grabar", "click"=>"return confirm('¿ DESEAR GUARDAR LOS DATOS ?')"),			  
								   array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"80", "alto"=>"22", 
										 "texto"=>"Regresar", "click"=>"location='$Link'"),
								)
					);
		echo $obj->setTablaForm($varTabla);
		}
	elseif($Opcion=="DROP") {
		if($_SESSION["_FDEL"]<>"S") {
			echo "<script language='Javascript'>";
			echo "window.location='$Link'";
			echo "</script>\n";
			}
		$sql = "update $MyTabla set _estado='X' where $myID = $Valor";
		$obj->AbrirConexion();
		$obj->Query($sql);
		$obj->CerrarConexion();	
		echo "<script language='Javascript'>";
		echo "window.location='$Link'";
		echo "</script>\n";
		}
	if(isset($_POST["bsav"])) {	
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varCod = ($_POST["cod"]=="")? "NULL" : "$_POST[cod]";
		$varNam = ($_POST["nam"]=="")? "NULL" : "'$_POST[nam]'";
		$sql = "call spMante_Ruta('$_POST[opt]', $varCod, $varNam, 	$varUsr);";		
		$obj->AbrirConexion();
		$obj->Query($sql);
		$obj->CerrarConexion();	
		echo "<script language='Javascript'>";
		echo "window.location='$Link'";
		echo "</script>\n";
		}
	} 
?>