<?php
function TableTrabajador($obj, $Titulo, $Opcion, $Valor, $TamFila) {
$Link = $_SERVER['PHP_SELF'];
if(!isset($Opcion)) {
	$sqlcar = "select id_cargo, nombre FROM _bp_cargo order by nombre";
	$Form ='{
			"id":"formulario", "name":"formulario", "metodo":"GET", "valida":"return validaForm(this)",
			"titulo":"'.$Titulo.'", "tam_tabla":"400", "cla_titu":"FormTitulo",
			"tam_campo1":"100", "tam_campo2":"300",
			"cla_campo1":"FormCampo1","cla_campo2":"interior",
			"contenido":[
				 {"tipo":"text", "id":"nam", "name":"nam", "texto":"Nombre :", "ancho":"280", "valor":"'.htmlentities($_GET[nam]).'"},
				 {"tipo":"select", "id":"car", "name":"car", "texto":"Cargo :", "ancho":"280", "valor":"'.$_GET[car].'", 
				  "combo":{"sql":"'.$sqlcar.'", "titulo":" --- Seleccionar CARGO --- ", "todos":" --- TODOS LOS CARGOS ---",
				  		   "sincod":"SI"}},
				 {"tipo":"select_simple", "id":"est", "name":"est", "texto":"Estado :", "ancho":"120", "valor":"'.$_GET[est].'", 
				  "combo":{"sql":{"1":"ACTIVO","2":"INACTIVO", "3":"VACACIONES", "4":"PERMISO"}}}			 
				 ],
			"botones":[
				 {"tipo":"submit", "id":"buscar", "name":"buscar", "texto":"Buscar", "ancho":"80", "alto":"22"}'.$BotNuevo.'
				 ]
		    }';
	echo $obj->SetForm($Form); 
	if(isset($_GET[buscar])) {
		$rs_cab = array(0=>array("Tamano"=>20, "Titulo"=>"CTRA", "Negrita"=>"SI", "Alinear"=>"center"),
						1=>array("Tamano"=>300 , "Titulo"=>"NOMBRE TRABAJADOR"),
						2=>array("Tamano"=>20 , "Titulo"=>"CCAR", "Alinear"=>"center"),
					    3=>array("Tamano"=>150 , "Titulo"=>"CARGO"),
					    4=>array("Tamano"=>50 , "Titulo"=>"ESTADO", "Alinear"=>"center")
					   );
		$Boton = array(0=>array(0 => array("Link"=>$Link, "Boton"=>"b_table_edit.png", "Permite"=>$_SESSION["_FUPD"], 
											"Mensaje"=>"Haga Click para <b>Editar</b> el Vendedor"),
								1 => array("Campo"=>"opt","Valor"=>"EDIT", "Tipo"=>"IN"),
								2 => array("Campo"=>"valor","Valor"=>0)								
								)
						);	
		
		$sql = "call spQuery_Trabajador('$_GET[nam]', '$_GET[car]', '$_GET[est]')";
		echo $obj->SetGrid(750,$rs_cab,$sql,NULL,4,NULL,NULL, NULL, "IZQ", $Boton);  
		}
	}
elseif($Opcion=="NEW" || $Opcion=="EDIT") {
	if($Opcion=="NEW") {
		$SubTit = "NUEVO";
		$Permite = $_SESSION["_FINS"];
		}
	else {
		$SubTit = "EDITAR";
		$Permite = $_SESSION["_FUPD"];
		$sql = "select * from _bp_trabajador where id_trabajador=$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";
		}
	$sqlrub = "select id_rubro, nombre FROM _bp_rubro order by nombre";
	$sqlare = "select id_area, nombre FROM _bp_area order by nombre";
	$sqlcar = "select id_cargo, nombre FROM _bp_cargo order by nombre";
	$sqlncar = "select id_nivel_cargo, nombre FROM _bp_nivel_cargo order by nombre";
	$sqlnins = "select id_nivel_instruccion, nombre FROM _bp_nivel_instruccion order by nombre";
	$sqlpro = "select id_profesion, nombre FROM _bp_profesion order by nombre";
	$Form ='{
			"id":"formulario", "name":"formulario", "metodo":"POST", "valida":"return validaForm(this)",
			"titulo":"MANTENIMIENTO DE TRABAJADORES - '.$SubTit.'", "tam_tabla":"500", "cla_titu":"FormTitulo",
			"tam_campo1":"120", "tam_campo2":"380",
			"cla_campo1":"FormCampo1","cla_campo2":"interior",
			"oculto":[
				 {"tipo":"hidden", "id":"opt", "name":"opt", "valor":"'.$Opcion.'"}
				 ],
			"contenido":[
				 {"tipo":"text", "id":"cod", "name":"cod", "texto":"Codigo :", "ancho":"70", 
				  "valor":"'.$Fila[id_trabajador].'", "locked":"SI", "alinear":"center"},
				 {"tipo":"text", "id":"apa", "name":"apa", "texto":"Apellido Paterno :", 
				  "ancho":"250", "valor":"'.htmlentities($Fila[apellido_paterno]).'"},
				 {"tipo":"text", "id":"ama", "name":"ama", "texto":"Apellido Materno :", 
				  "ancho":"250", "valor":"'.htmlentities($Fila[apellido_materno]).'"},
				 {"tipo":"text", "id":"nom", "name":"nom", "texto":"Nombres :", "ancho":"250", "valor":"'.htmlentities($Fila["nombre"]).'"},
				 {"tipo":"select", "id":"rub", "name":"rub", "texto":"Rubro :", "ancho":"300", "valor":"'.$Fila[id_rubro].'", 
				  "combo":{"sql":"'.$sqlrub.'", "titulo":" --- Seleccionar Rubro --- ", "sincod":"SI"}},
				 {"tipo":"select", "id":"are", "name":"are", "texto":"Area :", "ancho":"300", "valor":"'.$Fila[id_area].'", 
				  "combo":{"sql":"'.$sqlare.'", "titulo":" --- Seleccionar Area --- ", "sincod":"SI"}},
				 {"tipo":"select", "id":"car", "name":"car", "texto":"Cargo :", "ancho":"300", "valor":"'.$Fila[id_cargo].'", 
				  "combo":{"sql":"'.$sqlcar.'", "titulo":" --- Seleccionar Cargo --- ", "sincod":"SI"}},
				 {"tipo":"select", "id":"niv", "name":"niv", "texto":"Nivel Cargo :", "ancho":"300", 
				  "valor":"'.$Fila[id_nivel_cargo].'", 
				  "combo":{"sql":"'.$sqlncar.'", "titulo":" --- Seleccionar Nivel Cargo --- ", "sincod":"SI"}},
				 {"tipo":"select", "id":"gra", "name":"gra", "texto":"Nivel Instruccion :", "ancho":"300", 
				  "valor":"'.$Fila[id_nivel_instruccion].'", 
				  "combo":{"sql":"'.$sqlnins.'", "titulo":" --- Seleccionar Nivel Instruccion --- ", "sincod":"SI"}},
				 {"tipo":"select", "id":"pro", "name":"pro", "texto":"Profesion :", "ancho":"300", 
				  "valor":"'.$Fila[id_profesion].'", 
				  "combo":{"sql":"'.$sqlpro.'", "titulo":" --- Seleccionar Profesion --- ", "sincod":"SI"}},
				 {"tipo":"select_simple", "id":"est", "name":"est", "texto":"Estado :", "ancho":"120", "valor":"'.$Fila[estado_activo].'", 
				  "combo":{"sql":{"1":"ACTIVO","2":"INACTIVO", "3":"VACACIONES", "4":"PERMISO"}}}
				 ],
			"botones":[
				 {"tipo":"submit", "id":"save", "name":"save", "texto":"Grabar", "ancho":"100", "alto":"22"},
				 {"tipo":"button", "id":"regre", "name":"regre", "texto":"Regresar", "ancho":"100", "alto":"22", 
				  "click":"location=\''.$Link.'\'"}
				 ]
		    }';
	echo $obj->SetForm($Form); 
	}
if(isset($_POST["save"])) {
	if(isset($_SESSION["_CUSE"])) $Usuario = $_SESSION["_CUSE"];
	else $Usuario = 1;
	if($_POST[opt]=="EDIT") {
		$sql = "update _bp_trabajador set
				apellido_paterno = '$_POST[apa]', apellido_materno = '$_POST[ama]', 
				nombre = '$_POST[nom]', id_rubro = $_POST[rub], id_area = $_POST[are], 
				id_cargo = $_POST[car], id_nivel_cargo = $_POST[niv], 
				id_nivel_instruccion = $_POST[gra], id_profesion = $_POST[pro],
				estado_activo = $_POST[est]
				where id_trabajador = $_POST[cod]";					
		$obj->AbrirConexion();
		$obj->Query($sql);
		$obj->CerrarConexion();	
		}
	echo "<script language='Javascript'>";
	echo "window.location='$Link'";
	echo "</script>\n";
	}
} 
?>