/*
 * 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.Turno;
import pe.siso.horario.Services.TurnoServices;

/**
 *
 * @author fcelestino
 */
public class ServletTurno extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param=request.getParameter("Accion");
        if("cargarTurno".equals(param)){
            cargarTurno(request,response);
        }else if("cargarTurnoBlo".equals(param)){
            cargarTurnoBlo(request,response);
        }
        
    }

    private void cargarTurno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TurnoServices servicios = new TurnoServices();
        ArrayList<Turno> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");;
        
        lista=servicios.cargarTurno();
        
        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).getCodigoTurno()+"'>"+lista.get(i).getNombreTurno()+"</option>");
        }

        pw.println("</select>");
        
    }

    private void cargarTurnoBlo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TurnoServices servicios = new TurnoServices();
        ArrayList<Turno> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codSede = request.getParameter("codSede");
        String codTipoCentro = request.getParameter("codTipoCentro");
        String codCentro = request.getParameter("codCentro");
        String codNivel = request.getParameter("codNivel");
        String codTipoGrado = request.getParameter("codTipoGrado");
        String codGrado = request.getParameter("codGrado");
        String codSeccion = request.getParameter("codSeccion");
        
        lista=servicios.cargarTurno(codSede, codTipoCentro, codCentro, codNivel, codTipoGrado, codGrado, codSeccion);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<select id='"+nombre+"' class='form-control' onChange='"+accion+"'>");
        pw.println("<option value=''>[Seleccione]</option>");
        for(int i=0; i< lista.size(); i++) {
            pw.println("<option value='"+lista.get(i).getCodigoTurno()+"'>"+lista.get(i).getNombreTurno()+"</option>");
        }

        pw.println("</select>");
    }

}
