<?
session_name('portal_absa');
session_start();
require "../conexion/conectar.php";
require "../libs/funciones.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'];

$hiddenCol = explode(",",$_POST['hiddenCol']);
?>


	<style>
		th,td{font-size:12px}
		.formTD{
			border: 1px solid #EEEEEE; 
			padding: 5px 5px 5px 5px;
			
		}
	</style>
	
	<table width="100%" cellspacing="0">
		<tr style="background-color: white">
			<?for($i=0; $i <=20; $i++){
				if($etiqueta = getINI("MOD_NP_STATUS_".$i."=","../config.ini")){
					if($etiqueta != ""){
							?>
							<td class="formTD" width="1%">
								<img src="Imagenes/circulo_<?=$i?>.png" width="24" height="24" />
							</td>
							<td class="formTD" width="9%">
								<font style="font-size: 10px; border-color: #fbca93"><?=$etiqueta?></font></b>
							</td>
							<?
					}
				}
			}?>
		</tr>
	</table>
	<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: 3000,
				width:<?=$_POST['width']?>,
				rowList:[10,20,30],
				pager: '#<?=$_POST['idPagina']?>',
				sortname: '<?=$_POST['sortname']?>',
				viewrecords: false,
				sortorder: "desc",
				loadonce: false,
				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);
					alert("Type: "+st+"; Response: "+ xhr.status + " "+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");
									}
								}
				}*/
			});
			$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']?>');
			}});*/
			
			$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/docs16.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom', title:'Ver Expediente',onClickButton : function(e){
				//ventanaModalMax('[formularios/form_expediente.php,800,600]');
				var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
				var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
				
				if (typeof ret.uuid == 'undefined')
						{$j_6.messager.alert("Error.","Debe de seleccionar un documento","error");return false;}
						
				/*if(window.showModalDialog){
					window.showModalDialog("formularios/form_expediente.php?idexp="+ret.idexp,"Expediente","dialogWidth:800px;dialogHeight:600px");
				}else{*/
					var height1 = $j_6(window).height();
					var width1 = $j_6(window).width();
					
					var left = (width1-800)/2;
					var top = (height1-600)/2;
					
					$j_6("body").addClass("loading");
					
					mipopup = window.open("formularios/form_expediente_Pago.php?uuid="+ret.uuid,"Expediente","directories=0,menubar=0,location=0,status=0,toolbar=0,width="+width1+",height="+height1);
					//mipopup.moveTo(left,top);
					mipopup.moveTo(0,0);
				//}
			}});
                        <?if($_SESSION['sesion_editar_expediente'] == "1"){?>
                            $j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/edit.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom', title:'Editar documento',onClickButton : function(e){
				var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
				var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
				if (typeof ret.uuid == 'undefined')
				{$j_6.messager.alert("Error.","Debe de seleccionar un documento","error");return false;}
                                
                                ventanaModal3("[formularios/form_notificacion_pago.php,800,350]",ret.uuid);
                            }});
                            $j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/addDoc.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom', title:'Agregar documento',onClickButton : function(e){
				ventanaModal3("[formularios/form_notificacion_pago.php,800,350]");
                            }});
                            $j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/cancel.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom', title:'Cancelar documento',onClickButton : function(e){
				var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
				var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
				if (typeof ret.uuid == 'undefined')
				{
                                    $j_6.messager.alert("Error.","Debe de seleccionar un documento","error");return false;
                                }else{
                                    $j_6.messager.confirm("Confirmar","¿Esta seguro de cancelar el documento?",function(r){
                                        if(r){
                                            $j_6.post("extras/cancela_pago.php",{doce_uuid: ret.uuid},function(data){
                                                $j_6("#result").html(data);
                                            });
                                        }else{
                                            return false;
                                        }
                                    });
                                }
                            }});
                        <?}?>
			$j_6('#loading').hide();
	});
	</script>
	<script src="grids/funcionesJQGrid.js" type="text/javascript"></script>
	<br/>
	<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="" />
	</form>
		<center>
			<table id="<?=$_POST['idGrid']?>"></table>
			<div id="<?=$_POST['idPagina']?>"></div>
		</center>