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

/**
 *
 * @author fcelestino
 */
public class ServletNivel extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param=request.getParameter("Accion");
        if("listadoNivel".equals(param)){
            listadoNivel(request,response);
        }else if("listadoNivelBusqueda".equals(param)){
            listadoNivelBusqueda(request,response);
        }else if("registarNivel".equals(param)){
            registarNivel(request,response);
        }else if("cambiarEstado".equals(param)){
            cambiarEstado(request,response);
        }else if("cargarNivel".equals(param)){
            cargarNivel(request,response);
        }else if("cargarNivelGeneral".equals(param)){
            cargarNivelGeneral(request,response);
        }else if("armarEditar".equals(param)){
            armarEditar(request,response);
        }else if("actualizar".equals(param)){
            actualizar(request,response);
        }else if("cargarNivelBlo".equals(param)){
            cargarNivelBlo(request,response);
        }else if("cargarNivelSec".equals(param)){
            cargarNivelSec(request,response);
        }else if("cantidadUsoNivel".equals(param)){
            cantidadUsoNivel(request,response);
        }else if("listadoNivelBusquedaRegistro".equals(param)){
            listadoNivelBusquedaRegistro(request,response);
        }
    }
    
    

    private void listadoNivel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        
        lista=servicios.listarNivel();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n" +
"                           <table width=\"100%\" >"
                + "             <tbody>"
                + "                 <tr>"
                + "                     <td>Listado de Niveles</td>"
                + "                     <td align=\"right\">"
                + "                     </td>"
                + "                     <td width=\"10\"></td>"
                + "                 </tr>"
                + "             </tbody>"
                + "         </table>" +
"                       </div>\n" +
"                       <div>\n" +
"                           <div class=\"dataTables_wrapper form-inline no-footer\" id=\"sample-table-2_wrapper\">\n" +

"                               <table class=\"table table-striped table-bordered table-hover dataTable no-footer\" id=\"sample-table-2\" role=\"grid\" aria-describedby=\"sample-table-2_info\" >\n" +
"                                   <thead>\n" +
"                                       <tr role=\"row\">\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\" >N°</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">TIPO CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">NIVEL</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">EDICIÓN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ESTADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
        

        int cont= 1;
        for(int i=0; i< lista.size(); i++) {
           
            if(cont%2==0){
                pw.println("<tr class=\"alt\" >");
            }else{
                pw.println("<tr >");
            }
            
            String estado="";
            if(lista.get(i).getEstadoNivel()==1){
                estado="<a href=\"javascript:validar_estadoTotal(0,"+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estadoTotal(1,"+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center'>"+cont+"</td>"
                     + "    <td>"+lista.get(i).getNombreTipoCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreNivel()+"</td>"
                     + "    <td  align='center' width='30'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle' width='30'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='4' align='center'>No hay Tipo centros registrados</td></tr>");
        } 
          
        pw.println("   </tbody>\n" +
                   "</table>");
        
        pw.println("        </div>\n" +
                   "    </div>"
                +  "</div>");
    }

    private void listadoNivelBusqueda(HttpServletRequest request, HttpServletResponse response) throws IOException {
        NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        String codTipoCentro = request.getParameter("codTipoCentro");
        String codCentro = request.getParameter("codCentro");
        lista=servicios.listarNivel(codTipoCentro, codCentro);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n" +
"                           <table width=\"100%\" >"
                + "             <tbody>"
                + "                 <tr>"
                + "                     <td>Listado de Niveles</td>"
                + "                     <td align=\"right\">"
                + "                     </td>"
                + "                     <td width=\"10\"></td>"
                + "                 </tr>"
                + "             </tbody>"
                + "         </table>" +
"                       </div>\n" +
"                       <div>\n" +
"                           <div class=\"dataTables_wrapper form-inline no-footer\" id=\"sample-table-2_wrapper\">\n" +

"                               <table class=\"table table-striped table-bordered table-hover dataTable no-footer\" id=\"sample-table-2\" role=\"grid\" aria-describedby=\"sample-table-2_info\" >\n" +
"                                   <thead>\n" +
"                                       <tr role=\"row\">\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\" >N°</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">TIPO CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">NIVEL</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">EDICIÓN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ESTADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
    
    
    
        int cont= 1;
        for(int i=0; i< lista.size(); i++) {
           
            if(cont%2==0){
                pw.println("<tr class=\"alt\" >");
            }else{
                pw.println("<tr >");
            }
            
            String estado="";
            if(lista.get(i).getEstadoNivel()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center'  >"+cont+"</td>"
                     + "    <td>"+lista.get(i).getNombreTipoCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreNivel()+"</td>"
                     + "    <td  align='center' width='30'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle' width='30'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='6' align='center'>No hay Tipo centros registrados</td></tr>");
        } 
          
        pw.println( "   </tbody>\n" +
                    "</table>");
        
        pw.println( "       </div>\n" +
                    "   </div>"
                +   "</div>");
        
        
    }

    private void registarNivel(HttpServletRequest request, HttpServletResponse response) {
        NivelServices servicios = new NivelServices();;
        String nivel = request.getParameter("nivel");
        String codCentro = request.getParameter("codCentro");
        servicios.registrarNivel(codCentro, nivel);
    }

    private void cambiarEstado(HttpServletRequest request, HttpServletResponse response) {
        NivelServices servicios = new NivelServices();
        String dato = request.getParameter("dato");
        String codNivel = request.getParameter("codNivel");
        servicios.cambiarEstado(dato, codNivel);
    }

    private void cargarNivel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codCentro = request.getParameter("codCentro");
        
        lista=servicios.cargarNivel(codCentro);
        
        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).getCodigoNivel()+"'>"+lista.get(i).getNombreNivel()+"</option>");
        }

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



    private void cargarNivelGeneral(HttpServletRequest request, HttpServletResponse response) throws IOException {
        NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        
        lista=servicios.cargatNivel();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<select id='"+nombre+"' class='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).getCodigoNivel()+"'>"+lista.get(i).getNombreNivel()+"</option>");
        }

         pw.println("</select>");
    }
    
    private void armarEditar(HttpServletRequest request, HttpServletResponse response) throws IOException{
       NivelServices servicios=new NivelServices();
       
       Nivel nivel = new Nivel();
       String codNivel = request.getParameter("codNivel");
       String nombre = request.getParameter("nombre");
       nivel=servicios.nombreNivel(codNivel);
        
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<center> "
               + "<table border='1'>"
               + "      <br>"  
               + "      <tr>"
               + "          <td><b>Tipo Centro </b></td>"
               + "          <td width='10'></td>"
               + "          <td><label>"+nivel.getNombreTipoCentro()+"</label></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Centro </b></td>"
               + "          <td width='10'></td>"
               + "          <td><label>"+nivel.getNombreCentro()+"</label></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Nivel </b> <span style='color:'>*</span></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_nom_nivel' type='text' class='form-control validate[required,custom[onlyLetterSp]]' value='"+nivel.getNombreNivel()+"' onChange='conMayusculas(this);'></td>"
               + "      </tr>"  
               + "  </table>"
               + "</center>");   
    }

    private void actualizar(HttpServletRequest request, HttpServletResponse response) throws IOException {
       NivelServices servicios=new NivelServices();
       String nombre = request.getParameter("nombre");
       String codNivel = request.getParameter("codNivel");
       servicios.actualizar(codNivel,nombre);
        
       
    }

    private void cargarNivelBlo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codCentro = request.getParameter("codCentro");
        String codSede = request.getParameter("codSede");
        String codTipoCentro = request.getParameter("codTipoCentro");
        
        lista=servicios.cargarNivel(codSede, codTipoCentro, codCentro);
        
        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).getCodigoNivel()+"'>"+lista.get(i).getNombreNivel()+"</option>");
        }

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

    private void cargarNivelSec(HttpServletRequest request, HttpServletResponse response) throws IOException {
       NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codCentro = request.getParameter("codCentro");
        
        lista=servicios.cargarNivelSec(codCentro);
        
        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).getCodigoNivel()+"'>"+lista.get(i).getNombreNivel()+"</option>");
        }

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

    private void cantidadUsoNivel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        NivelServices servicios = new NivelServices();
        String codNivel = request.getParameter("codNivel");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        int dato=servicios.cantidadUsoNivel(codNivel);
        if (dato==0){
            pw.println("0");
        }else{
            pw.println("1");
        }
       
       
    }

    private void listadoNivelBusquedaRegistro(HttpServletRequest request, HttpServletResponse response) throws IOException {
         NivelServices servicios = new NivelServices();
        ArrayList<Nivel> lista = null;
        String codCentro = request.getParameter("codCentro");
        //String codCentro = request.getParameter("codCentro");
        lista=servicios.listarNivel(codCentro);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        
        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n" +
"                           <table width=\"100%\" >"
                + "             <tbody>"
                + "                 <tr>"
                + "                     <td>Listado de Niveles</td>"
                + "                     <td align=\"right\">"
                + "                     </td>"
                + "                     <td width=\"10\"></td>"
                + "                 </tr>"
                + "             </tbody>"
                + "         </table>" +
"                       </div>\n" +
"                       <div>\n" +
"                           <div class=\"dataTables_wrapper form-inline no-footer\" id=\"sample-table-2_wrapper\">\n" +

"                               <table class=\"table table-striped table-bordered table-hover dataTable no-footer\" id=\"sample-table-2\" role=\"grid\" aria-describedby=\"sample-table-2_info\" >\n" +
"                                   <thead>\n" +
"                                       <tr role=\"row\">\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\" >N°</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">TIPO CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">NIVEL</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">EDICIÓN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ESTADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        

        int cont= 1;
        for(int i=0; i< lista.size(); i++) {
           
            if(cont%2==0){
                pw.println("<tr class=\"alt\" >");
            }else{
                pw.println("<tr >");
            }
            
            String estado="";
            if(lista.get(i).getEstadoNivel()==1){
                estado="<a href=\"javascript:validar_estadoBusqueda(0,"+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estadoBusqueda(1,"+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center'  >"+cont+"</td>"
                     + "    <td>"+lista.get(i).getNombreTipoCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreNivel()+"</td>"
                     + "    <td  align='center' width='30'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoNivel()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle' width='30'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='6' align='center'>No Tipo centros registrados</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>");
    }
    

}