<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.0
*/

session_name('portal_absa');session_start();if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?exit;}
require "../conexion/conectar.php";

$conexion = new CONDB();
$conexion->conectar();

$headerNames = explode(",",$_POST['headerNames']);
$optionsCol = explode(";",$_POST['optionsCol']);

for($i = 0; $i < count($optionsCol); $i++){
	$optionCol = explode("|",$optionsCol[$i]);
	$optNameCol[$i]["name"] = trim($optionCol[0]);
	$optNameCol[$i]["options"] = trim($optionCol[1]);
}
$key_grid_datos="";
$optionsForm = explode(";;",$_POST['optionsForm']);
$fieldKeys="";
if($_POST['optionsForm'] != ""){
	for($i = 0; $i < count($optionsForm); $i++){
		$optForm = explode("|",$optionsForm[$i]);
		if($optForm[1] == "true"){
			if($fieldKeys == ""){
				$fieldKeys=$optForm[0];
				$key_grid_datos=$optForm[0];
			}else{
				$fieldKeys.="|".$optForm[0];
			}
		}
	}	
}
$_SESSION['query'] = $_POST['query'];
$sql = str_replace("\\","",$_SESSION['query']);

$result = mysqli_query($conexion->CONEXION,$sql);

$ren=0;
while ($property = mysqli_fetch_field($result)) {
	if($property->type == 254){
			$t = ($property->length/3);
		}else{
			$t = $property->length;
		}
		if($t < 50){
			$size=$t;
		}else{
			$size=50;
		}
		switch($property->type){
			case 254 : $tipo='char';
			case 3 : $tipo='int';
			default : $tipo='char';
		}
		$Fields[$i]['name']=$property->name;
		$Fields[$i]['maxLenght']=$t;
		$Fields[$i]['size']=$size;
		$Fields[$i]['type']=$tipo;
}
 
$hiddenCol = explode(",",$_POST['hiddenCol']);
?>					  
	<style>
		th,td{font-size:12px}
	</style>
	<script>
	$j_6(document).ready(function() {
		$j_6("#<?=$_POST['idGrid']?>").jqGrid({
				url:"<?=$_POST['urlDatos']?>?key=<?=$key_grid_datos?>&ocultos=<?=$_POST['hiddenCol']?>",
				datatype: "json",
				colNames:[
				<?for($i = 0; $i < count($headerNames); $i++){?>
						<?if($i==count($headerNames)-1){?>
							'<?=$headerNames[$i]?>'
						<?}else{?>
							'<?=$headerNames[$i]?>',
						<?}?>
					<?}?>
				
				],
				colModel:[
					<?for($i = 0; $i < count($optionsCol); $i++){
						$options="";
						$options=$optNameCol[$i]["options"];
						?>
							<?if($i==count($optionsCol)-1){?>
								{name:'<?=$optNameCol[$i]['name']?>',index:'<?=$optNameCol[$i]['name']?>'<?if($options != ""){echo ", ".str_replace("\\","",$options);}?>}
							<?}else{?>
								{name:'<?=$optNameCol[$i]['name']?>',index:'<?=$optNameCol[$i]['name']?>'<?if($options != ""){echo ", ".str_replace("\\","",$options);}?>},
							<?}?>
					<?	
					  }?>
				],
				rowNum:<?=$_POST['rowNum']?>,
				rowTotal: 6000,
				width:<?=$_POST['width']?>,
				rowList:[10,20,30],
				pager: '#<?=$_POST['idPagina']?>',
				sortname: '<?=$_POST['sortname']?>',
				viewrecords: false,
				sortorder: "desc",
				loadonce: true,
				caption: "<?=$_POST['sheetName']?>",
				shrinkToFit: false,
				rownumbers: false,
				gridview: true,
				height:'<?=$_POST['height']?>',
				editurl: 'grids/editurl.php',
				loadError : function(xhr, st, str){alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);}
				/*loadComplete: function(data){
								var total = data.records;
								for(var i=0; i < total; i++){
									if(data.rows[i].cell[2] == "Administrador"){
									$j_6("#" + data.rows[i].id).find("td").css("background-color", "#D6D6D6");
									}
								}
				}*/
			});
			$j_6("#<?=$_POST['idGrid']?>").jqGrid('navGrid','#<?=$_POST['idPagina']?>',
			{edit:<?=$_POST['edit']?>,add:<?=$_POST['add']?>,del:<?=$_POST['del']?>,excel:true,exceltext:'Excel'},
			{},
			{},
			{},
			{multipleSearch:true, multipleGroup:true, showQuery: true}
			);
			
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'',title:'Exportar a Excel',onClickButton : function(e){
			exportExcel('<?=$_POST['idGrid']?>','<?=$_POST['headerNames']?>','<?=$_POST['ignoreColNamesExcel']?>','<?=$_POST['sheetName']?>','<?=$_POST['formName']?>','1');
			}});
			
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('filterToolbar',{defaultSearch:'cn',stringResult:true});
			$j_6("#<?=$_POST['idGrid']?>").jqGrid('setFrozenColumns');
			<?if(strtoupper($_POST['add']) == "TRUE"){?>
				$j_6("#add_<?=$_POST['idGrid']?>").click(function(){
					ventanaModal('[<?=$_POST['fileForm']?>,<?=$_POST['widthForm']?>,<?=$_POST['heightForm']?>]','<?=$_POST['optionsForm']?>','','<?=$_POST['captionForm']?>','add','<?=$_POST['tabla']?>','','','<?=$_POST['reloadGridAfterSave']?>');
				});
			<?}?>
			<?if(strtoupper($_POST['edit']) == "TRUE"){?>
				$j_6("#edit_<?=$_POST['idGrid']?>").click(function(){
					var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
					var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);

					ventanaModal2('[configClientes.php,800,300','',ret.rfc_cliente);
				});
			<?}?>
			<?if(strtoupper($_POST['del']) == "TRUE"){?>
				$j_6("#del_<?=$_POST['idGrid']?>").click(function(){
					var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
					var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
					
					var fieldKeys='<?=$fieldKeys?>';
					var valKeys="";
					<?
						$keys = explode("|",$fieldKeys);
						for($i = 0; $i < count($keys); $i++){
					?>
					if(valKeys == "")
						valKeys=ret.<?=$keys[$i]?>;
					else
						valKeys=valKeys+"|"+ret.<?=$keys[$i]?>;
					<?}?>
					ventanaModal('[<?=$_POST['delForm']?>,<?=$_POST['widthConfirm']?>,<?=$_POST['heightConfirm']?>]','','','<table><tr><td valign="center"><img src="Imagenes/Help.png" width="35" height="32"/></td><td width="95%" valign="center"><?=$_REQUEST['captionConfirm']?></td></tr></table>','del','<?=$_POST['tabla']?>',valKeys,fieldKeys,'<?=$_POST['reloadGridAfterSave']?>');
				});
			<?}?>
			$j_6('#loading').hide();
	});
	</script>
	<script src="grids/funcionesJQGrid.js" type="text/javascript"></script>
	<form name="<?=$_POST['formName']?>" id="<?=$_POST['formName']?>">
		<input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
		<input type="hidden" name="file_name" id="file_name" value="" />
		<input type="hidden" name="header" id="header" value="" />
		<input type="hidden" name="sheetName" id="sheetName" value="" />
		<input type="hidden" name="ignorarCol" id="ignorarCol" value="" />
                <input type="hidden" name="noSesion" id="noSesion" value="" />
		</form>
		<table id="<?=$_POST['idGrid']?>"></table>
		<div id="<?=$_POST['idPagina']?>"></div>
    