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

session_name('portal_absa');
session_start();
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]);
}

$optionsForm = explode(";;",$_POST['optionsForm']);
$fieldKeys="";
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];
		}
	}
}	
$sql = str_replace("\\","",$_POST['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']);
$_SESSION['query'] = $_POST['query'];
?>
	<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,
				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:true,add:true,del:true,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"){?>
				<?if(trim(strtoupper($_SESSION['usuario_p_prov_absa'])) == "ADMIN"){?>
					$j_6("#add_<?=$_POST['idGrid']?>").click(function(){
						ventanaModal2('[<?=$_POST['fileForm']?>,<?=$_POST['widthForm']?>,<?=$_POST['heightForm']?>]','<?=$_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);
					
					/*var rfcs = ["<?=str_replace(',','","',trim($_SESSION['sesion_ref_rfc_receptor']))?>"];
					
					<?if(trim(strtoupper($_SESSION['usuario_p_prov_absa'])) != "ADMIN"){?>
					
					if(rfcs.indexOf(ret.rfc) == -1){
						alert("No tiene los permisos para modificar el receptor");
						return false;
					}
					<?}?>*/
					
					ventanaModal2('[<?=$_POST['fileForm']?>,<?=$_POST['widthForm']?>,<?=$_POST['heightForm']?>]','<?=$_POST['reloadGridAfterSave']?>',ret.idReceptor);
				});
			<?}?>
			<?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 rfcs = ["<?=str_replace(',','","',trim($_SESSION['sesion_ref_rfc_receptor']))?>"];
					
					<?if(trim(strtoupper($_SESSION['usuario_p_prov_absa'])) != "ADMIN"){?>
					
					if(rfcs.indexOf(ret.rfc) == -1){
						alert("No tiene los permisos para eliminar el receptor");
						return false;
					}
					<?}?>*/
					
					fieldKeys='idReceptor';
					valKeys=ret.idReceptor;
					
					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"><?=$_POST['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>
    