<?php
	$cpanel_cabeza=mysql_query("select * from tabla_menu where eliminar=0 and abreviacion='$control_menu'",$link);
	$ver_cpanel_cabeza=mysql_fetch_array($cpanel_cabeza);
		$control_id_menu=$ver_cpanel_cabeza['id'];
		$cpanel_sub_menu=mysql_query("select * from  tabla_menu_sub where eliminar=0 and abreviacion='$menu' and cod_menu='$control_id_menu'",$link);
		$ver_cpanel_sub_menu=mysql_fetch_array($cpanel_sub_menu);
		$tabla_control=$ver_cpanel_sub_menu['nombre_tabla_sql'];
		$tabla_control_id=$ver_cpanel_sub_menu['id'];
?>
                    <div class="pageheader">
                        <div class="media">
                            <div class="pageicon pull-left">
                                <i class="fa <?php echo $ver_cpanel_cabeza['figura']; ?>"></i>
                            </div>
                            <div class="media-body">
                                <ul class="breadcrumb">
                                    <li><a href="#"><i class="glyphicon glyphicon-cog"></i></a></li>
                                    <li><?php echo $ver_cpanel_cabeza['nombre_menu']; ?></li>
                                </ul>
                                <h4><?php echo $ver_cpanel_sub_menu['nombre_menu']; ?> | 
									<?php if($ver_usuarios['privilegios']=='1'){ echo 'Administrador';} ?>
									<?php if($ver_usuarios['privilegios']=='2'){ echo 'Gerente';} ?>
									<?php if($ver_usuarios['privilegios']=='3'){ echo 'Cajero';} ?>
								</h4>
                            </div>
                        </div>
                    </div>
<!-- PANEL ADMINISTRATIVO -->					
<div class="contentpanel">
 <?php 
 		include 'mensajes.php';
		if (isset($_POST['submit'])){
			foreach($_POST as $campo => $value)
			 {
				$limpiar_variable=strip_tags($value);
				//echo $campo.$limpiar_variable;
				if (empty($limpiar_variable)){
						echo'
							<div class="alert alert-danger fade in">
								<i class="fa-fw fa fa-times"></i>
								<strong>Error!</strong> Llamar al administrador del sistema <b>ERROR: 10</b>
							</div>
						';
					break;
				}
			}
			if($_GET['proceso']=="1NS3RT"){
				$contar_registro = mysql_query("SELECT * FROM $tabla_control");
				$cont_registro=mysql_num_rows($contar_registro);
				$contruc_table = mysql_query("SHOW COLUMNS FROM $tabla_control");
				$cont=mysql_num_rows($contruc_table);
					if ( $cont> 0) {
						 $campos.="(";
						while ($fila = mysql_fetch_assoc($contruc_table)) {
							$i++;
							if($i>1){
								$tipo_campo=$fila['Field'];
								$campos.="".$tipo_campo;
							}
							if($i<$cont and $i>1 ){
								$campos.=",";
							}
						}
					}
				$campos.=") values (";
				$conta=count($_POST);
				$i=0;
				foreach($_POST as $campo => $value) {
					$i++;
					$campos_tabla.="'".$value."'";
					if($i<$conta){
						$campos_tabla.=",";
					}
				 }
				 $campos_tabla=$campos_tabla.");";
				 $sql="INSERT INTO $tabla_control ". $campos.$campos_tabla;
				 mysql_query("$sql",$link);
				$contar_registro_insert = mysql_query("SELECT * FROM $tabla_control");
				$cont_registro_insert=mysql_num_rows($contar_registro_insert);
				if($cont_registro==$cont_registro_insert){
					echo'
                        <div class="alert alert-warning fade in">
                            <i class="fa-fw fa fa-warning"></i>
                                <strong>Advertencia !</strong> Datos ya registrados en la base de datos.
                        </div>
					';
				} else {
					echo'
                        <div class="alert alert-success fade in">
                            <i class="fa-fw fa fa-check"></i>
                               <strong>Exelente !</strong> Muchas gracias datos registrados.
                        </div>

					';					
				}
				/*echo'
                        <div class="alert alert-success fade in">
                            <i class="fa-fw fa fa-check"></i>
                               <strong>Exelente !</strong> Muchas gracias datos registrados.
                        </div>
				';*/	
			}
		if($_GET['proceso']=="3DIT4R"){
			$id_control=strip_tags($_GET['token']);
			foreach($_POST as $campo => $value)
			 {
				mysql_query("UPDATE $tabla_control SET $campo='$value' where  id='$id_control'",$link);
				//echo "update $tabla_control set $campo='$value' where  id='$id_control'";
			 }
			 echo'
                <div class="alert alert-success fade in">
                    <i class="fa-fw fa fa-success"></i>
                        <strong>Enhorabuena !</strong>Los Datos fueron editados 
                </div>
			';
		}
		}
		
		if($_GET['proceso']=="D3L3T3"){
			$id_control=strip_tags($_GET['token']);
			mysql_query("update $tabla_control set eliminar='1'  where  id='$id_control'",$link);
					echo'
                        <div class="alert alert-warning fade in">
                            <i class="fa-fw fa fa-warning"></i>
                               <strong>Advertencia !</strong> El registro fue eleminado de la Base de Datos.
                        </div>	
					';
		}

 ?>

	<div class="row row-stat">
		<div class="col-md-12">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <div class="panel-btns">
                                            <a href="#" class="panel-minimize tooltips" data-toggle="tooltip" title="Minimize Panel"><i class="fa fa-minus"></i></a>
                                            <a href="#" class="panel-close tooltips" data-toggle="tooltip" title="Close Panel"><i class="fa fa-times"></i></a>
                                        </div><!-- panel-btns -->
                                        <h4 class="panel-title"><?php echo $ver_cpanel_sub_menu['titulo']; ?></h4>
                                        <p><?php echo $ver_cpanel_sub_menu['descripcion']; ?>.</p>
                                    </div><!-- panel-heading -->
                                    <div class="panel-body nopadding">
									<form action="<?php echo $url_global; if($_GET['proceso']=='') { echo'&proceso=1NS3RT';} else { echo'&proceso=3DIT4R&token='.$_GET['id'].''; }?>" method="POST" class=" form-bordered">
										<div class="form-group">
											<?php
												if($_GET['proceso']=='C0N5ULT4R'){
													$id_control=bindec($_GET['token']);
													$consultar_sql=mysql_query("select * from  $tabla_control where id='$id_control'",$link);
													$ver_consultar_sql=mysql_fetch_array($consultar_sql);
													//echo "select * from  $tabla_control where id='$id_control'";
												}
												/*$resultado = mysql_query("SHOW COLUMNS FROM $tabla_control");
													if (mysql_num_rows($resultado) > 0) {
													while ($fila = mysql_fetch_assoc($resultado)) {
													$tipo_campo=$fila['Type'];
													$campo_cultar=$fila['Field'];
													$value_campo=$ver_consultar_sql[$campo_cultar];
												*/
												$armar_form=mysql_query("select * from tabla_menu_sub_html where cod_menu_sub='$tabla_control_id'",$link);
												while($html_form=mysql_fetch_array($armar_form)){ 
												$tipo_campo=$html_form['elements'];
												$name_campo=$html_form['name_field'];
											?>
											<?php if($tipo_campo=='input'){ ?>	
                                                <div class="<?php echo $html_form['class_div']; ?>">
													<input	class='form-control'
															type='<?php echo $html_form['type']; ?>' 
															name='<?php echo $html_form['name_field']; ?>'   
															id='<?php echo $html_form['name_id']; ?>'
															value='<?php if($ver_consultar_sql[$name_campo]=='') { echo $html_form['value']; } else { echo $ver_consultar_sql[$name_campo]; }?>' 
															<?php if ($html_form['type']=='text') { ?>															
																placeholder='<?php echo $html_form['field_description']; ?>' 
															<?php } ?>	
															<?php if ($html_form['autocomplete']=='1'){ echo 'autocomplete="off"'; } ?>
															<?php if ($html_form['autofocus']=='1'){ echo 'autofocus'; } ?>
															<?php if ($html_form['required_field']=='1'){ echo 'required="required"'; } ?>
															<?php if ($html_form['onchange']=='1'){ echo 'onChange="javascript:this.value=this.value.toUpperCase();"'; } ?>
															<?php if ($html_form['type']=='text') {?>
																maxlength="<?php echo $html_form['character_length_max']; ?>"
																minlength="<?php echo $html_form['character_length_min']; ?>"
															<?php } ?>
															<?php if ($html_form['type']=='number') {?>
																max="<?php echo $html_form['character_length_max']; ?>"
																min="<?php echo $html_form['character_length_min']; ?>"
															<?php } ?>
															<?php if ($html_form['type']!='number' and $html_form['pattern']!='' ) {?>
																pattern="<?php echo $html_form['pattern']; ?>"
															<?php } ?>
															title='<?php echo $html_form['help']; ?>'
													/>
							                    </div>
												<?php } ?>
											<?php } ?>
												
												
												
												
												
												
												
												
												
												<?php if($_GET['consultar']=='true'){ ?>
												<div class="pull-right">
													<button type="submit" name="submit" value='Guardar' class="btn btn-primary btn-rounded">GUARDAR CAMBIOS <i class="fa fa-floppy-o ml5"></i></button>
												</div>
												<?php } else { ?>
												<div class="pull-right">
														<button type="submit" name="submit" value='Guardar' class="btn btn-primary btn-rounded">GUARDAR <i class="fa fa-floppy-o ml5"></i></button>
												</div>
												<?php }  ?>
                                            </div>
										</form>  
								<div class="col-md-12">
                                <div class="table-responsive">
                                    <table class="table table-primary table-hover mb30">
                                        <thead>
                                          <tr>
											<?php
												$resultado = mysql_query("SHOW COLUMNS FROM $tabla_control");
													if (mysql_num_rows($resultado) > 1) {
													while ($fila = mysql_fetch_assoc($resultado)) {
													$tipo_campo=$fila['Type'];
											?>										  
                                            <th><?php echo $fila['Default']; ?></th>
											<?php }  } ?>	
                                          </tr>
                                        </thead>
										<tbody>
										<?php 
											$ver_contenido=mysql_query("select * from  $tabla_control where eliminar=0",$link);
											while ($ver_contenido_sql=mysql_fetch_array($ver_contenido)){
												$control_id=$ver_contenido_sql['id'];
												$i++;
										?>	
											 <tr>
											 <?php
											 $resultado = mysql_query("SHOW COLUMNS FROM $tabla_control");
												while ($fila = mysql_fetch_assoc($resultado)) {
												$campo=$fila['Field'];
												if($campo!='eliminar'){
													$campo_ver_sql=$ver_contenido_sql[$campo];
											 ?>
												<td><?php echo mb_convert_case($campo_ver_sql, MB_CASE_TITLE, "UTF-8"); ?></td>										
											<?php } } 
											if($_GET['token']==''){
												$metodo_url='&proceso='.$_GET['proceso'];
											} else {
												$metodo_url='&proceso='.$_GET['proceso'].'&token='.$_GET['token'];
											}
												$url_global_limpia = str_replace("$metodo_url", "", "$url_global");
											
											?>
											<td class="table-action">
												<a href="<?php echo $url_global_limpia;?>&proceso=C0N5ULT4R&token=<?php echo decbin($ver_contenido_sql['id']); ?>" data-toggle="tooltip" title="Edit" id="growl-warning" ><i class="fa fa-pencil"></i></a>
												<a href="<?php echo $url_global_limpia;?>&proceso=D3L3T3&token=<?php echo decbin($ver_contenido_sql['id']); ?>" data-toggle="tooltip" title="Delete" class="delete-row tooltips"><i class="fa fa-trash-o"></i></a>
											</td>
																						
											</tr>
										<?php } ?>
										</tbody> 
                                    </table>
                                </div><!-- table-responsive -->
                            </div>								
                    </div>     
            </div>
        </div>
	</div>
</div>
