-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmunicipios.php
219 lines (198 loc) · 6.27 KB
/
municipios.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
require_once( 'admin.php' );
?>
<!doctype html>
<html lang="es">
<head>
<?php include 'basic_css.php' ?>
<title>Gestión de municipios</title>
<script id="lista-items-tmpl" type="text/template">
<div class="list-group">
{{#municipios}}
<a id="item-link-{{id}}" href="#" class="list-group-item" onclick="mostrarItem('{{id}}')">{{nombre}}</a>
{{/municipios}}
</div>
</script>
<script id="options-estado-tmpl" type="text/template">
{{#estados}}
<option value="{{id}}" label="{{nombre}}" />
{{/estados}}
</script>
</head>
<body>
<!-- Empieza el encabezado -->
<?php include 'header.php' ?>
<!-- Termina el encabezado -->
<div class="container">
<h1>Gestión de municipios</h1>
<!-- Nuevo/Editar usuario -->
<div class="row">
<div class="col-md-3">
<div class="panel panel-default" >
<div class="panel-heading">
<button class="btn btn-default" type="button" onclick="cargarLista()"><span class="glyphicon glyphicon-refresh"></span></button> Lista de municipios
</div>
<div id="lista-items" class="item-list" >
</div>
</div>
</div>
<div class="col-md-9">
<div class="panel panel-default hidden-xs">
<div class="panel-heading">
<div class="btn-group">
<button id="boton-recargar" class="btn btn-default" type="button"
onclick="recargarItem()"><span class="glyphicon glyphicon-refresh"></span> Recargar</button>
<button class="btn btn-default" type="button"
onclick="nuevoItem()"><span class="glyphicon glyphicon-plus-sign"></span> Nuevo</button>
<button class="btn btn-default" type="button"
onclick="borrarItem()"><span class="glyphicon glyphicon-minus-sign"></span> Eliminar</button>
</div>
<div class="btn-group pull-right">
<button class="btn btn-default" type="button"
onclick="guardarItem()"><span class="glyphicon glyphicon-floppy-disk"></span> Guardar</button>
<button class="btn btn-default" type="button"
onclick="cancelar()"><span class="glyphicon glyphicon-ban-circle"></span> Cancelar</button>
</div>
</div>
<!-- Comienza el formulario -->
<div class="panel-body item-form">
<form id="frm-item" class="form-horizontal" role="form">
<!-- Campos no visibles -->
<input id="id" name="id" type="hidden" class="form-control" />
<!-- Campos editables -->
<div class="form-group">
<label for="estado" class="col-lg-3 control-label">Estado</label>
<div class="col-lg-9">
<select id="estado" name="estado" class="form-control">
</select>
</div>
</div>
<div class="form-group">
<label for="nombre" class="col-lg-3 control-label">Nombre</label>
<div class="col-lg-9">
<input id="nombre" name="nombre" type="text" class="form-control" required />
</div>
</div>
</form>
</div>
</div>
<!-- Mensajes -->
<div id="mensajes">
</div>
</div>
</div>
</div>
<!-- Empieza el pie -->
<?php include 'footer.php' ?>
<!-- Terminal el pie -->
<!-- Empieza javascript -->
<?php include 'basic_js.php' ?>
<script type="text/javascript">
// Url del controlador
var control = 'municipio_controller.php';
// Accion de la pantalla, ninguna por defecto
var gblAccion = 'guardar';
var selId = null;
$(document).ready(function() {
// Actualizar la lista de estados
cargarLista();
// La lista de estados
$.getJSON('estado_controller.php',
{accion: 'lista'},
function(json) {
$('#estado').html(Mustache.to_html($('#options-estado-tmpl').html(), json));
});
});
function guardarItem() {
// Tiene que haber una accion definida
if (gblAccion == null) {
return; // Do nothing
}
if (gblAccion == 'eliminar') {
$.post(control,
{accion: 'eliminar', id: selId},
function(json) {
if (json.resultado) {
cargarLista();
uxSuccessAlert('Municipio eliminado correctamente');
} else {
uxErrorAlert('No se pudo eliminar el municipio. ' + json.error );
}
});
} else { // Guardar o actualizar un item
var params = $('#frm-item').serializeArray();
params.push( {name: 'accion', value: gblAccion} );
$.post(control,
params,
function(json) {
if ( json.resultado ) {
clearForm();
cargarLista();
uxSuccessAlert('El municipio se ha guardado correctamente');
} else {
// Mostrar error
uxErrorAlert('No se pudo guardar el municipio');
}
});
}
}
function cargarLista() {
$.getJSON(control,
{accion: 'lista'},
function(json){
$('#lista-items').html(Mustache.to_html($('#lista-items-tmpl').html(), json));
nuevoItem();
});
}
function mostrarItem(itemId) {
$.getJSON(control,
{accion: 'item', id: itemId},
function(json){
if ( json.resultado ) {
$('#id').val(json.item.id);
$('#estado').val(json.item.id_estado);
$('#nombre').val(json.item.nombre);
$('#estado').focus();
// Hacer seleccion visible
$('a[class~=active]').removeClass('active');
$('#item-link-' + itemId).addClass('active');
// Accion global: Guardar el item
gblAccion = 'guardar';
selId = itemId; // Item en vista
} else {
// Mostrar error
gblAccion = null; // No hay accion posible
selId = null;
uxErrorAlert('No se encontro el municipio');
}
});
}
function recargarItem() {
if (selId != null && selId.length != 0 ){
mostrarItem(selId);
} else {
console.log('No hay que recargar');
}
}
function nuevoItem() {
$('a[class~=active]').removeClass('active'); // Limpiar seleccion
gblAccion = 'guardar'; // Configurar accion
selId = null;
clearForm(); // Limpiar el formulario
enableForm(true); // Habilitar el formulario
}
function borrarItem() {
if (selId != null) {
gblAccion = 'eliminar';
enableForm(false);
} else {
console.log('No existe selección');
}
}
function cancelar() {
gblAccion = 'guardar';
enableForm(true); //
}
</script>
</body>
</html>