/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.siso.horario.Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pe.siso.horario.Beans.UbigeoDocente;
import pe.siso.horario.Services.UbigeoDocenteServices;

/**
 *
 * @author sistem02user
 */
public class ServletUbigeo extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param=request.getParameter("Accion");
        if("cargarDepartamento".equals(param)){
            cargarDepartamento(request,response);
        }else if("cargarProvincia".equals(param)){
            cargarProvincia(request,response);
        }else if("cargarDistrito".equals(param)){
            cargarDistrito(request,response);
        }else if("listarDistritos".equals(param)){
            listarDistritos(request,response);
        }
    }

    private void cargarDepartamento(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UbigeoDocenteServices servicios = new UbigeoDocenteServices();
        ArrayList<UbigeoDocente> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        
        lista=servicios.cargarDepartamento();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<select id='"+nombre+"' class='form-control validate[required]' onChange='"+accion+"'>");
            pw.println("<option value=''>[SELECCIONE]</option>");
                for(int i=0; i< lista.size(); i++) {
            pw.println("<option value='"+lista.get(i).getCod_dep_ubi()+"' >"+lista.get(i).getNom_dep_ubi()+"</option>");
                }
        pw.println("</select>");
    }

    private void cargarProvincia(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UbigeoDocenteServices servicios = new UbigeoDocenteServices();
        ArrayList<UbigeoDocente> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        int codDepartamento = Integer.parseInt(request.getParameter("codDepartamento"));
        
        lista=servicios.cargarProvincia(codDepartamento);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<select id='"+nombre+"' class='form-control validate[required]' onChange='"+accion+"'>");
            pw.println("<option value=''>[SELECCIONE]</option>");
        for(int i=0; i< lista.size(); i++) {
            pw.println("<option value='"+lista.get(i).getCod_pro_ubi()+"' >"+lista.get(i).getNom_pro_ubi()+"</option>");
        }
        pw.println("</select>");
    }

    private void cargarDistrito(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UbigeoDocenteServices servicios = new UbigeoDocenteServices();
        ArrayList<UbigeoDocente> lista = null;
        
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        int codDepartamento = Integer.parseInt(request.getParameter("codDepartamento"));
        int codProvincia = Integer.parseInt(request.getParameter("codProvincia"));
        
        lista=servicios.cargarDistrito(codProvincia, codDepartamento);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<select id='"+nombre+"' class='form-control validate[required]' onChange='"+accion+"'>");
            pw.println("<option value=''>[SELECCIONE]</option>");
        for(int i=0; i< lista.size(); i++) {
            pw.println("<option value='"+lista.get(i).getCod_dis_ubi()+"' >"+lista.get(i).getNom_dis_ubi()+"</option>");
                  
        }
        pw.println("</select>");
    }

    private void listarDistritos(HttpServletRequest request, HttpServletResponse response) {
        UbigeoDocenteServices servicios = new UbigeoDocenteServices();
        ArrayList<UbigeoDocente> lista = null;
        
        int codDepartamento = Integer.parseInt(request.getParameter("codDepartamento"));
        int codProvincia = Integer.parseInt(request.getParameter("codProvincia"));
        
        lista=servicios.cargarDistrito(codProvincia, codDepartamento);
        
        
    }


}
