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

/**
 *
 * @author fcelestino
 */
public class ServletTipoGrado extends HttpServlet {
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param=request.getParameter("Accion");
        if("listadoTipoGrado".equals(param)){
            listadoTipoGrado(request,response);
        }else if("listadoTipoGradoBusqueda".equals(param)){
            listadoTipoGradoBusqueda(request,response);
        }else if("cargarTipoGrado".equals(param)){
            cargarTipoGrado(request,response);
        }else if("registarTipoGrado".equals(param)){
            registarTipoGrado(request,response);
        }else if("cambiarEstado".equals(param)){
            cambiarEstado(request,response);
        }else if("armarEditar".equals(param)){
            armarEditar(request,response);
        }else if("actualizar".equals(param)){
            actualizar(request,response);
        }else if("cargarTipoGradoBlo".equals(param)){
            cargarTipoGradoBlo(request,response);
        }else if("cantidadTipoGrado".equals(param)){
            cantidadTipoGrado(request,response);
        }
        
    }

    private void listadoTipoGrado(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TipoGradoServices servicios = new TipoGradoServices();
        ArrayList<TipoGrado> lista = null;
        
        lista=servicios.listarTipoGrado();
        
        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 Aulas</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\">TIPO GRADO</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).getEstadoTipoGrado()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCodigoTipoGrado()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCodigoTipoGrado()+");\" 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>"+lista.get(i).getNombreTipoGrado()+"</td>"
                     + "    <td  align='center'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoTipoGrado()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='7' align='center'>No Tipo centros registrados</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>");
    }

    private void listadoTipoGradoBusqueda(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TipoGradoServices servicios = new TipoGradoServices();
        ArrayList<TipoGrado> lista = null;
        String codCentro = request.getParameter("codCentro");
        String codnivel = request.getParameter("codnivel");
        lista=servicios.listarTipoGrado(codnivel, 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 Aulas</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\">TIPO GRADO</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).getEstadoTipoGrado()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCodigoTipoGrado()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCodigoTipoGrado()+");\" 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>"+lista.get(i).getNombreTipoGrado()+"</td>"
                     + "    <td  align='center'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoTipoGrado()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='7' align='center'>No Tipo centros registrados</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>");
    }

    private void cargarTipoGrado(HttpServletRequest request, HttpServletResponse response) throws IOException { 
        TipoGradoServices servicios = new TipoGradoServices();
        ArrayList<TipoGrado> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codCentro = request.getParameter("codCentro");
        String codNivel = request.getParameter("codNivel");
        
        lista=servicios.cargarTipoGrado(codCentro, codNivel);
        
        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).getCodigoTipoGrado()+"'>"+lista.get(i).getNombreTipoGrado()+"</option>");
        }

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

    private void registarTipoGrado(HttpServletRequest request, HttpServletResponse response) {
        TipoGradoServices servicios = new TipoGradoServices();      
        String tipogrado = request.getParameter("tipogrado");
        String codigoNivel = request.getParameter("codNivel");
        String codigoCentro = request.getParameter("codCentro");
        servicios.registrarTipoGrado(tipogrado, codigoCentro, codigoNivel);
        //response.setContentType("text/html");
        //PrintWriter pw = response.getWriter();
    }

    private void cambiarEstado(HttpServletRequest request, HttpServletResponse response) {
        TipoGradoServices servicios = new TipoGradoServices();
        String dato = request.getParameter("dato");
        String codTipoGrado = request.getParameter("codTipoGrado");
        servicios.cambiarEstado(dato, codTipoGrado);
    }
    
    private void armarEditar(HttpServletRequest request, HttpServletResponse response) throws IOException {
       TipoGradoServices servicios=new TipoGradoServices();
       
       TipoGrado tipo = new TipoGrado();
       String codTipoGrado = request.getParameter("codTipoGrado");
       String nombre = request.getParameter("nombre");
       tipo=servicios.NombreTipo(codTipoGrado);
        
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<center>"
               + "  <table border='0'>"
               + "      <tr>"
               + "          <td><b>Tipo Centro </b></td>"
               + "          <td width='10'></td>"
               + "          <td><label>"+tipo.getNombreTipoCentro()+"</label></td>"
               + "      </tr>"  
               + "      <tr>"  
               + "          <td colspan='3' height='15'></td>"  
               + "      </tr>"  
               + "      <tr>"   
               + "          <td><b>Centro:</b></td>"
               + "          <td width='10'></td>"
               + "          <td><label>"+tipo.getNombreCentro()+"</label></td>"
               + "      </tr>"
               + "      <tr>"  
               + "          <td colspan='3' height='15'></td>"  
               + "      </tr>" 
               + "      <tr>"
               + "          <td><b>Tipo Nivel:</b></td>"
               + "          <td width='10'></td>"
               + "          <td><label>"+tipo.getNombreNivel()+"</label></td>"
               + "      </tr>"
               + "      <tr>"  
               + "          <td colspan='3' height='15'></td>"  
               + "      </tr>" 
               + "      <tr>"
               + "          <br>"  
               + "          <td><b>Tipo Grado:</b></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_nom_tipo' type='text' class='form-control validate[required,custom[onlyLetterSp]]' value='"+tipo.getNombreTipoGrado()+"' onChange='conMayusculas(this);'></td>"
               + "      </tr>"
               + "  </table>"
               + "</center>");  
    }

    private void actualizar(HttpServletRequest request, HttpServletResponse response) {
      TipoGradoServices servicio= new TipoGradoServices();
      String nombre= request.getParameter("nombre");
      String codTipoGrado= request.getParameter("codTipoGrado");
      servicio.actualizar(codTipoGrado,nombre);
      
      
    }

    private void cargarTipoGradoBlo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TipoGradoServices servicios = new TipoGradoServices();
        ArrayList<TipoGrado> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codCentro = request.getParameter("codCentro");
        String codNivel = request.getParameter("codNivel");
        String codSede = request.getParameter("codSede");
        String codTipoCentro = request.getParameter("codTipoCentro");
        
        lista=servicios.cargarTipoGrado(codSede, codTipoCentro, codCentro, codNivel);
        
        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).getCodigoTipoGrado()+"'>"+lista.get(i).getNombreTipoGrado()+"</option>");
        }

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

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

}
