<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.2
Cambio: Se quitaron las funciones: open_w,close_w y maximizar_w y se pusieron en menu.php
*/

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]);
}
$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() {
			var datosGrid="detallefechaDatosGrid.php";
			
		if(navigator.appVersion.toLowerCase().indexOf("safari") != -1 && navigator.appVersion.toLowerCase().indexOf("chrome") == -1)
			var datosGridC = "comentariosDatosGrid_Safari.php";
		else
			var datosGridC="comentariosDatosGrid.php";
			
			
		$j_6("#<?=$_POST['idGrid']?>").jqGrid({
				url:"<?=$_POST['urlDatos']?>",
				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: 2000,
				width:<?=$_POST['width']?>,
				rowList:[10,20,30],
				pager: '#<?=$_POST['idPagina']?>',
				sortname: '<?=$_POST['sortname']?>',
				viewrecords: false,
				sortorder: "",
				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+ " "+xhr.responseText);},
				/*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");
									}
								}
				}*/
				subGrid: true,
				subGridOptions: {
        "plusicon"  : "ui-icon-triangle-1-e",
        "minusicon" : "ui-icon-triangle-1-s",
        "openicon"  : "ui-icon-arrowreturn-1-e",
		// load the subgrid data only once
		// and the just show/hide
		"reloadOnExpand" : false,
		// select the row when the expand column is clicked
		"selectOnExpand" : true
	},
				subGridRowExpanded: function(subgrid_id, row_id) {
					var subgrid_table_id, pager_id;
					subgrid_table_id = subgrid_id+"_t";
					pager_id = "p_"+subgrid_table_id;
					$j_6("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
					$j_6("#"+subgrid_table_id).jqGrid({
						url:"grids/"+datosGrid+"?rfcEmisor=<?=urlencode($_GET['rfcEmisor'])?>&doc=<?=urlencode($_GET['doc'])?>&serie=<?=urldecode($_GET['serie'])?>&folio=<?=urlencode($_GET['folio'])?>&uuid=<?=urlencode($_GET['uuid'])?>&id="+row_id,
						datatype: "json",
						colNames: ['Status','Evento','Fecha Evento','Concepto'],
						colModel: [
							{name:"status",index:"status",width:50},
							{name:"numeroevento",index:"numeroevento",width:50, key: true},
							{name:"fechaevento",index:"fechaevento",width:150},
							{name:"evento",index:"evento",width:450}
						],
						rowNum:20,
						pager: pager_id,
						sortname: 'numeroevento',
						sortorder: "asc",
						height: '100%',
						//Inicia subgrid 2
						subGrid: false,
						subGridOptions: {
							"plusicon"  : "ui-icon-triangle-1-e",
							"minusicon" : "ui-icon-triangle-1-s",
							"openicon"  : "ui-icon-arrowreturn-1-e",
							// load the subgrid data only once
							// and the just show/hide
							"reloadOnExpand" : false,
							// select the row when the expand column is clicked
							"selectOnExpand" : true
						}
						/*subGridRowExpanded: function(subgrid_id, row_id) {
										
										var subgrid_table_id, pager_id;
										subgrid_table_id = subgrid_id+"_t2";
										pager_id = "p2_"+subgrid_table_id;
										$j_6("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
										$j_6("#"+subgrid_table_id).jqGrid({
											url:"grids/"+datosGridC+"?rfcEmisor=<?=urlencode($_GET['rfcEmisor'])?>&doc=<?=urldecode($_GET['doc'])?>&serie=<?=urldecode($_GET['serie'])?>&folio=<?=urldecode($_GET['folio'])?>&uuid=<?=urldecode($_GET['uuid'])?>&id="+row_id,
											datatype: "json",
											colNames: ['Comentario'],
											colModel: [
												{name:"comentario",index:"comentario",width:650},
											],
											rowNum:20,
											pager: pager_id,
											sortname: 'comentario',
											sortorder: "asc",
											height: '100%',
											loadError : function(xhr, st, str){alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);}
										});
							$j_6("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
						}*/
						// Termina subgrid 2
	
					});
				$j_6("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
                                $j_6('#'+subgrid_table_id).jqGrid('navButtonAdd','#'+pager_id,{caption:'',title:'Exportar a Excel',onClickButton : function(e){
                                    exportExcel(subgrid_table_id,'Status,Numero Evento,Fecha Evento,Evento','0','Validaciones','form_valida','2');
                                }});
			}
			
				/*subGridRowExpanded: function(subgrid_id, row_id) {
					var subgrid_table_id, pager_id;
					subgrid_table_id = subgrid_id+"_t";
					pager_id = "p_"+subgrid_table_id;
					$j_6("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
					$j_6("#"+subgrid_table_id).jqGrid({
						url:"grids/"+datosGrid+"?id="+row_id,
						datatype: "json",
						colNames: ['Comentario'],
						colModel: [
							{name:"comentario",index:"comentario",width:700},
						],
						rowNum:20,
						pager: pager_id,
						sortname: 'comentario',
						sortorder: "asc",
						height: '100%'
					});
		$j_6("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
	}*/
				
				
				
				
				
				
			});
			$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']?>','2');
			}});*/
			
			$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);
					
					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['fileForm']?>,<?=$_POST['widthForm']?>,<?=$_POST['heightForm']?>]','<?=$_REQUEST['optionsForm']?>','','<?=$_REQUEST['captionForm']?>','edit','<?=$_POST['tabla']?>',valKeys,fieldKeys,'<?=$_POST['reloadGridAfterSave']?>');
				});
			<?}?>
			<?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('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/E-Mail.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom',title:'Enviar Validación',onClickButton : function(e){
			
			var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
			var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
			if(ret.tipodocto == null || ret.tipodocto == "undefined" || ret.tipodocto == ""){
				$j_6.messager.alert("Error","Debe de seleccionar un registro","error");
			}else{
				$j_6.messager.prompt('Correo a Enviar', 'Ingrese el cdfcedcorreo a quien se le enviara el reporte.', function(r){
					if (r){
						$j_6.post("extras/envia_validaciones.php",{correos: r,rfcemisor: ret.rfcemisor,serie: ret.serie,folio: ret.folio,uuid: ret.uuid,tipodocto: ret.tipodocto,
						fecharecibido: ret.fecharecibido},function(data){
						
							$j_6.messager.alert("Estado de Envio",data,"info");
							
						});
					}
				});
			}
			}});
			
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/preview.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom',title:'Ver Validación',onClickButton : function(e){
			
			var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
			var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);

			
					/*ventanaModal("extras/ventanaModal_validaciones.php",{correos: r,rfcemisor: ret.rfcemisor,serie: ret.serie,folio: ret.folio,uuid: ret.uuid,tipodocto: ret.tipodocto,
					fecharecibido: ret.fecharecibido},function(data){
					
						$j_6.messager.alert("Estado de Envio",data,"info");
						
					});*/
					
					loadDiv("extras/preview_validaciones.php","cuerpoPopup",ret.rfcemisor,ret.serie,ret.folio,ret.uuid,ret.tipodocto,ret.fecharecibido);
					
					open_w();

			}});
			
			
			$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>
        
        <form name="form_valida" id="form_valida">
            <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>
		
		<!--<script type="text/javascript" src="libs/jqueryEasy_1.2.5/jquery.easyui.min.js"></script>
		
	<div id="windowVal" class="easyui-window" closed="true" title="Validaciones" iconCls="icon-save" style="width:500px;height:200px;padding:5px;">
		<div class="easyui-layout" fit="true">
			<div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">
				<div id="cuerpoPopup"></div>
			</div>
			<div region="south" border="false" style="text-align:right;padding:5px 0;">
				<a class="easyui-linkbutton" iconCls="icon-print" href="javascript:void(0)" onclick="maximizar_w()">Imprimir</a>
				<a class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)" onclick="close_w()">Cancelar</a>
			</div>
		</div>
	</div>-->
    