/*
 * 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 javax.servlet.http.HttpSession;
import pe.siso.horario.Beans.Bloque;
import pe.siso.horario.Beans.Grupo;
import pe.siso.horario.Services.BloqueServices;

/**
 *
 * @author sistem16user
 */
public class ServletBloque extends HttpServlet {

    
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String param=request.getParameter("Accion");
        if("cargarBloque".equals(param)){
            cargarBloque(request,response);
        }else if("BloqueBusqueda".equals(param)){
            BloqueBusqueda(request,response);
        }else if("registroBloque".equals(param)){
            registroBloque(request,response);
        }else if("listarBloque".equals(param)){
            listarBloque(request,response);
        }else if("listarBloqueBusqueda".equals(param)){
            listarBloqueBusqueda(request,response);
        }else if("mostrarDetalleBloque".equals(param)){
            mostrarDetalleBloque(request,response);
        }else if("verEstado".equals(param)){
            verEstado(request,response);
        }else if("editarDetallebloque".equals(param)){
            editarDetallebloque(request,response);
        }else if("actualizarBloque".equals(param)){
            actualizarBloque(request,response);
        }else if("cargarBloqueSesion".equals(param)){
            cargarBloqueSesion(request,response);
        }else if("cargarBloqueSesion".equals(param)){
            cargarBloqueSesion(request,response);
        }else if("BloqueBusqueda_Gen".equals(param)){
            BloqueBusqueda_Gen(request,response);
        }else if("armarBloqueHorario".equals(param)){
            armarBloqueHorario(request,response);
        }else if("registrarGrupoHorario".equals(param)){
            registrarGrupoHorario(request,response);
        }else if("listarBloqueBusqueda2".equals(param)){
            listarBloqueBusqueda2(request,response);
        }else if("armarBloqueHorarioEditar".equals(param)){
            armarBloqueHorarioEditar(request,response);
        }else if("sessionBloqueHorario".equals(param)){
            sessionBloqueHorario(request,response);
        }else if("armarBloqueHorarioParaActualizar".equals(param)){
            armarBloqueHorarioParaActualizar(request,response);
        }else if("EditarGrupoHorario".equals(param)){
            EditarGrupoHorario(request,response);
        }
        
    }

    private void cargarBloque(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Bloque> lista = null;
        String accion = request.getParameter("accion");
        
        lista=servicios.cargarBloque();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<select id='cmbBloque' 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_blo()+"'>"+lista.get(i).getNom_blo()+"</option>");
        }
        pw.println("</select>");
    }

    private void BloqueBusqueda(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Bloque> lista = null;
        int cantcod = Integer.parseInt(request.getParameter("cod"));
        //String codPlana = request.getParameter("codPlana");
        //lista=servicios.BusquedaBloque(cod);
        
        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>Bloque</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\">BLOQUE</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">INICIO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">FIN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CANT. MIN.</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        

        int cont= 1;
        int contblo= 1;
        int contrec= 1;
        
        int cantTotal=cantcod+(cantcod-1);
        String disabled="";
       
        for(int i=0; i< cantTotal; i++) {
             if(i>0){
                disabled="disabled"; 
             }
            if(cont%2==0){
                pw.println("<tr style='background-color:#C2E6F7;' >"
                        + "     <td align='center'>"+cont+"</td>"
                        + "     <td align='center' style='vertical-align: middle;'> RECESO </td>");
                contrec++;
            }else{
                pw.println("<tr>"
                        + "     <td width='30' align='center'>"+cont+"</td>"
                        + "     <td width='100'  align='center'  style='vertical-align: middle;'> <input type='hidden' id='txt_pos_"+cont+"' value='"+cont+"'> CURSOS </td>");
                contblo++;
            
            }
            
             pw.println("   <td align='center' width='100' > <input type='text' id='txt_ini_"+cont+"' size='6'  class='validate[required]' onChange='calculo_min("+cont+",1)' "+disabled+"> [HH:MM]</td>"
                     + "    <td align='center' width='100' > <input type='text' id='txt_fin_"+cont+"' size='6'  class='validate[required]' onChange='calculo_min("+cont+",2)'  > [HH:MM] <input type=hidden id='cont' value='"+cont+"'> </td>"
                     /*+ "    <td align='center'> <input type='text' id='txt_cant_"+cont+"' size='7'  class='validate[required]' disabled ></td>"*/
                     + "    <td align='center' width='80'  style='vertical-align: middle;'> <div id='div_cant_"+cont+"'> </div><input type='hidden' id='txt_cant_"+cont+"' size='7'  class='validate[required]'  ></td>"
                   
                     + " </tr>");
            cont++;
            
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='6' align='center'>No hay bloques registrados</td></tr>");
        } 
          int cantBlo = cont-1;
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>"
                 + "<input type='hidden' id='cantBlo' value='"+cantBlo+"' ><br><center>"
                 + "<button class=\"btn btn-sm btn-primary\" onclick=\"validar_registro();\" type=\"button\">\n" +
"                       <i class=\"ace-icon fa fa-check icon-on-right bigger-110\"></i>\n" +
"                       Agregar Bloque\n" +
"                   </button><br><br></center>");
    }

    private void registroBloque(HttpServletRequest request, HttpServletResponse response) {
        BloqueServices servicios = new BloqueServices();
        Bloque claseBloque= new Bloque();
        String codBlo= request.getParameter("codBlo");
        String codTur= request.getParameter("codTur");
        String bloq=request.getParameter("bloq");
        
        
        int codigoBloque=Integer.parseInt(request.getParameter("codBlo"));
        
        //-------------------crear sesion bloque-----------------
        HttpSession session_actual = request.getSession(true);
        claseBloque.setCod_blo(codigoBloque);
        session_actual.setAttribute("sesionBloque", claseBloque);
        System.out.println(codigoBloque);
        //------------------------------------------------------
        servicios.registrarBloque(codBlo,bloq,codTur);
    }

    private void listarBloque(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Bloque> lista = null;
        
        lista=servicios.mostrarBloque();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<table  cellpadding=\"0\" cellspacing=\"0\">\n" +
"	    <tr>\n" +
"               <td height=\"10\"></td>\n" +
"           </tr>\n" +
"           <tr><td width=\"380\">\n" +
"                   <div class=\"datagrid\">\n" +
"                           <table >\n" +
"                                   <thead>\n" +
"                                       <tr >\n" +
"                                           <th>N° GRUPO</th>\n" +
"                                           <th>DESCRIPCIÓN</th>\n" +
"                                           <th>INICIO</th>\n" +                 
"                                           <th>FIN</th>\n" +                 
"                                           <th>CANT. MIN</th>\n" +                 
"                                           <th>ACCIÓN</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 >");
            }
          pw.println("      <td align='center' width='5'><b>"+lista.get(i).getCod_gru()+"</td>"
                     + "    <td align='center' width='55'>"+lista.get(i).getNom_blo()+"</td>"
                     + "    <td align='center' width='15'>"+lista.get(i).getHor_ini()+"</td>"
                     + "    <td align='center' width='15'>"+lista.get(i).getHor_fin()+"</td>"
                     + "    <td align='center' width='15'>"+lista.get(i).getCan_min()+"</td>"
                     + "    <td  align='center' width='15'><a href=\"javascript:editar("+lista.get(i).getCod_blo()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
      
                     + " </tr>");
            cont++;
        
        
        
        }
            if(cont==1){
            pw.println("<tr><td colspan='4' align='center'>No hay bloques registrados</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>");  
    }

    private void listarBloqueBusqueda(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Bloque> lista = null;
       // String codDetalle= request.getParameter("codDetalle");
        String codigoBloque= request.getParameter("codigoBloque");
        
        lista=servicios.BusquedaListaBloque(codigoBloque);
        
       
        
        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%\" height='60' >"
                + "             <tbody>"
                + "                 <tr>"
                + "                     <td>Listado de Bloques creados</td>"
                + "                     <td align=\"right\">"
                + "                         <button class=\"btn btn-sm btn-warning\" onclick=\"agregarBloque();\" type=\"button\">\n" +
"                                               <i class=\"ace-icon fa fa-check icon-on-right bigger-110\"></i>\n" +
"                                               Agregar Bloque\n" +
"                                           </button>"
                + "                     </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\">GRUPO DE BLOQUE</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">TURNO DE BLOQUE</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ESTADO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ACCIÓN</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
         
        
        int cont= 1;
        
        for(int i=0; i< lista.size(); i++) {
        //int codDetalle=lista.get(i).getCod_det_blo();
        String codigoGrupo= lista.get(i).getCod_gru();
        //String codDet=Integer.toString(codDetalle);
        int dato=servicios.estado(codigoGrupo);
        if(cont%2==0){
                pw.println("<tr class=\"alt\" >");
            }else{
                pw.println("<tr >");
            }
       
        String estado="";
        String turno="";
         if(dato==0){
                estado="<span style='color:#478FCA;'>" +
                            "Editable" +
                        "</span>";
            }else{
                estado="<b> <font color='red'>En uso</font></b>";
            }
         
            if(lista.get(i).getTur_gru().equals("1")){
                turno="MAÑANA";
            }else if(lista.get(i).getTur_gru().equals("2")){
                turno="TARDE";
            }else if(lista.get(i).getTur_gru().equals("3")){
                turno="NOCHE";
            }else if(lista.get(i).getTur_gru().equals("4")){
                turno="MAÑANA - TARDE";
            }else if(lista.get(i).getTur_gru().equals("5")){
                turno="TARDE - NOCHE";
            }else if(lista.get(i).getTur_gru().equals("6")){
                turno="MAÑANA - TARDE - NOCHE";
            }else{
                turno="--";
            }
            
             pw.println("   <td align='center' width='5'><b>"+cont+"</td>"
                     + "    <td align='center' width='45'>"+lista.get(i).getNom_gru()+"</td>"
                     + "    <td align='center' width='45'>"+turno+"</td>"
                     + "    <td align='center' width='5'>"+estado+"</td>"
                     + "    <td align='center' width='5'><a href=\"javascript:verBloque("+lista.get(i).getCod_blo()+','+lista.get(i).getCod_gru()+','+lista.get(i).getTur_gru()+");\" class=\"regular\" >\n"
                     + "    <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     
                     + " </tr>");
            cont++;
        
        
        
        }
            if(cont==1){
            pw.println("<tr><td colspan='5' align='center'>No hay bloques registrados</td></tr>");
        } 
          
         pw.println("       </tbody>\n" +
"			</table>");
          
         pw.println("       </div>"
                 + "    </div>"
                 + "</div>");
         
         
         
        //----rompiendo la sesion en la busqueda de bloques 
        HttpSession session_actual = request.getSession(true);
        session_actual.getAttribute("sesionBloque");
        
        if (session_actual.getAttribute("sesionBloque") != null) {
            session_actual.removeAttribute("sesionBloque");

        }
         
    }

    private void mostrarDetalleBloque(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices(); 
        //Bloque bloque = new Bloque();
        ArrayList<Bloque> lista = null;
        String codigoBloque = request.getParameter("codigoBloque");
        String codigoGrupo= request.getParameter("codigoGrupo");
      
        lista = servicios.detallebloque(codigoBloque,codigoGrupo);
       
        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>Bloque</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\" >GRUPO DE BLOQUE</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">HORA INICIO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">HORA FIN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CANT. MIN.</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 style='background-color:#C2E6F7;' >" +
                                   "    <td align='center' width='15'> RECESO</td>");
                    }else{
                        pw.println("<tr >" +
                                   "    <td align='center' width='15'> CURSOS </td>");
                    }     
                        pw.println(   //"    <td align='center' width='15'>"+lista.get(i).getNom_gru()+"</td>"
                                      "    <td align='center' width='15'>"+lista.get(i).getHor_ini()+"  [HH:MM]</td>"
                                    + "    <td align='center' width='15'>"+lista.get(i).getHor_fin()+"  [HH:MM]</td>"
                                    + "    <td align='center' width='15'>"+lista.get(i).getCan_min()+"</td>"
 //                                 + "    <td><input type='hidden' id='txt_codDet' value='"+lista.get(i).getCod_det_blo()+"'></td>"
                                    + " </tr>");
                        cont++;
               
                    }   
                    if(cont==1){
                        pw.println("<tr><td colspan='4' align='center'>No hay bloques registrados</td></tr>");
        } 
          
                        pw.println( "                   </tbody>\n" +
                                    "			</table>\n" +
                                    "			</div>\n" +
                                    "			</td></tr></table>");
    }

    private void verEstado(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices services= new BloqueServices();
       String codigoGrupo=request.getParameter("codigoGrupo");
       //String codigoDetalle=request.getParameter("codigoDetalle");
       int dato= services.estado(codigoGrupo);
       response.setContentType("text/html");
       PrintWriter pw = response.getWriter();
        if(dato==0){
            pw.println("0");
        }else {
            pw.println("1");
        }
    }

    private void editarDetallebloque(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices(); 
        //Bloque bloque = new Bloque();
        ArrayList<Bloque> lista = null;
        String codigoBloque = request.getParameter("codigoBloque");
        String codigoGrupo= request.getParameter("codigoGrupo");
        String codigoTurno= request.getParameter("codigoTurno");
        String disabled="";
        lista = servicios.detallebloque(codigoBloque,codigoGrupo);
       
        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>Bloques</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\" >GRUPO DE BLOQUE</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">HORA INICIO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">HORA FIN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CANT. MIN.</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
         
        
               int cont=1;     
               for(int i=0; i< lista.size(); i++) {
                   //String codigoTurno=lista.get(i).getTur_gru();
                    if(i>0){
                        disabled="disabled"; 
                    }
                    if(cont%2==0){
                        pw.println("<tr  style='background-color:#C2E6F7;' >" +
                                   "    <td align='center' style='vertical-align:middle' > RECESO <input type='hidden' id='txt_codDet_"+cont+"' value='"+lista.get(i).getCod_det_blo()+"' style='text-align:center;'></td>");
                    }else{
                        pw.println("<tr >" +
                                  "    <td align='center' style='vertical-align:middle' > CURSOS <input type='hidden' id='txt_codDet_"+cont+"' value='"+lista.get(i).getCod_det_blo()+"' style='text-align:center;'></td>");
                    }     
                        pw.println(   //"    <td align='center' style='vertical-align:middle' >"+lista.get(i).getNom_gru()+"<input type='hidden' id='txt_codDet_"+cont+"' value='"+lista.get(i).getCod_det_blo()+"' style='text-align:center;'></td>"
                                      "    <td align='center'><input class='validate[required]' type='text' value='"+lista.get(i).getHor_ini()+"' id='txt_hini_"+cont+"' size='7' style='text-align:center;' onChange='calcularMin("+cont+",1,"+codigoTurno+")' "+disabled+" />  [HH:MM]</td>"
                                    + "    <td align='center'><input class='validate[required]' type='text' value='"+lista.get(i).getHor_fin()+"' id='txt_hfin_"+cont+"' size='7' style='text-align:center;' onChange='calcularMin("+cont+",2,"+codigoTurno+")'/>  [HH:MM]</td>"
                                    /*+ "    <td align=center></td>"*/
                                    + "    <td align='center' style='vertical-align:middle' ><div id='div_cantmin_"+cont+"' >"+lista.get(i).getCan_min()+" </div> <input type='hidden' value='"+lista.get(i).getCan_min()+"' id='txt_cantmin_"+cont+"' size='7' style='text-align:center;'/></td>"
 //                                   + "    <td></td>"
                                    + " </tr>");
                        cont++;
               
                    }   
                    if(cont==1){
                        pw.println("<tr><td colspan='4' align='center'>No hay bloques registrados</td></tr>");
                    } 
                    int cantBlo = cont-1;
                        pw.println( "                   </tbody>\n" +
                                    "			</table>\n" +
                                    "			</div>\n" +
                                    "			</td></tr></table>"
                                  + "<input type='hidden' id='cantBlo' value='"+cantBlo+"' >"); 
                        
     
    }

    private void actualizarBloque(HttpServletRequest request, HttpServletResponse response) {
        BloqueServices servicios = new BloqueServices();;
        //String codDetalle= request.getParameter("codDetalle");
        String bloDetalle=request.getParameter("bloDetalle");
        servicios.actualizarDetalleBloque(bloDetalle);
    }

    private void cargarBloqueSesion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Bloque> lista = null;
        String accion = request.getParameter("accion");
        int codigoBloque = Integer.parseInt(request.getParameter("codigoBloque"));
        
        lista=servicios.cargarBloque();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<select id='cmbBloque' class='form-control validate[required]' onChange='"+accion+"'>");
         pw.println("<option value=''>[Seleccione]</option>");
        
        for(int i=0; i< lista.size(); i++) {
           if(codigoBloque==lista.get(i).getCod_blo()){
                pw.println("<option value='"+lista.get(i).getCod_blo()+"' selected>"+lista.get(i).getNom_blo()+"</option>");
           }else{
                pw.println("<option value='"+lista.get(i).getCod_blo()+"'>"+lista.get(i).getNom_blo()+"</option>");
           }
           
        }

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

    private void BloqueBusqueda_Gen(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Bloque> lista = null;
        int cantcod = Integer.parseInt(request.getParameter("cod"));
        //String codPlana = request.getParameter("codPlana");
        //lista=servicios.BusquedaBloque(cod);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        
        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n" +
"                           <table width=\"300\" >"
                + "             <tbody>"
                + "                 <tr>"
                + "                     <td>Bloque por día</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\">INICIO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">FIN</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CANT. MIN.</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
        int cont= 1;
        String disabled="";
        for(int i=1; i<= cantcod; i++){
            
            pw.println("<tr>"
                    + "     <td align='center'width='30'>"+cont+"</td>"
                    + "     <td align='center' width='100' > <input type='text' id='txt_ini_"+cont+"' size='6'  class='validate[required]' onChange='calculo_min("+cont+",1)' > [HH:MM]</td>"
                    + "     <td align='center' width='100' > <input type='text' id='txt_fin_"+cont+"' size='6'  class='validate[required]' onChange='calculo_min("+cont+",2)'  > [HH:MM] <input type=hidden id='cont' value='"+cont+"'> </td>"
                    + "     <td align='center' width='80'  style='vertical-align: middle;'> <div id='div_cant_"+cont+"'> </div><input type='hidden' id='txt_cant_"+cont+"' size='7'  class='validate[required]'  ></td>"
                    + " </tr>");
            cont++;
        }
        
        
        if(cont==1){
            pw.println("<tr><td colspan='4' align='center'>No hay bloques registrados</td></tr>");
        } 
         
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>"
                 + "<input type='hidden' id='cantBlo' value='"+cantcod+"' ><br><center>"
                 + "<button class=\"btn btn-sm btn-primary\" onclick=\"validar_registro();\" type=\"button\">\n" +
"                       <i class=\"ace-icon fa fa-clock-o icon-on-right bigger-110\"></i>\n" +
"                       Agregar las horas\n" +
"                   </button><br><br></center>");
    }

    private void armarBloqueHorario(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cadena = request.getParameter("cadena");
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        String[] array1 = cadena.split("!");
        int x =0;
        
        for(int i=0; i<array1.length;i++){
            String[] array2 = array1[i].split("#");
            if(x<Integer.parseInt(array2[0])){
                x=Integer.parseInt(array2[0]);
            }
           
        }
        
        
        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n HORARIO" +
"                       </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\" >LUNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MARTES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MIÉRCOLES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">JUEVES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">VIERNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">SÁBADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
        
        
      
        String HorIni="";
        String HorFin="";
        int contenido = 0;
        for(int t=1; t<=x;t++){
            pw.println("<tr>");
            for(int i=1; i<=6;i++){
                for(int j=0; j<array1.length;j++){
                    String[] array2 = array1[j].split("#");
                    if(array2[2].equals(""+i)){
                        if(Integer.parseInt(array2[0])>=t){
                            String[] horas  = array2[3].split("¡");
                            int h = t-1;
                            String[] hora   = horas[h].split("-");
                            HorIni=hora[0];
                            HorFin=hora[1];
                            contenido=1;
                        } 
                    } 
                }
                
                if(contenido==1){
                    pw.println("<td align='center' style=' vertical-align:middle;'>"+HorIni+"<br>"+HorFin+"</td>");
                }else{
                    pw.println("<td align='center' style='color:red; background-color:#F5A9A9; vertical-align:middle;'>SIN HORARIO</td>");
                }
                    
                contenido=0;     

            }
            pw.println("</tr>");
        }
        
        
            
        
        
        pw.println("</table><br>"
                + "<center>"
                 + "<button class=\"btn btn-sm btn-primary\" onclick=\"registrarHorario();\" type=\"button\">\n" +
"                       <i class=\"ace-icon fa fa-save icon-on-right bigger-110\"></i>\n" +
"                       Registrar bloque\n" +
"                   </button>"
                + "</center><br>"
                + "<input type='hidden' id='codBloqMax'value='"+x+"'>");
    }

    private void registrarGrupoHorario(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cadena = request.getParameter("cadena");
        String cantMax = request.getParameter("cantMax");
        String txtTurno = request.getParameter("txtTurno");
        String txtDia = request.getParameter("txtDia");
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        BloqueServices servicios = new BloqueServices();
        int dato = servicios.registrarGrupoHorario(cadena, cantMax,txtTurno,txtDia);
        
        pw.println(dato);
    }

    private void listarBloqueBusqueda2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BloqueServices servicios = new BloqueServices();
        ArrayList<Grupo> lista = null;
       // String codDetalle= request.getParameter("codDetalle");
        String codigoBloque= request.getParameter("codigoBloque");
        
        lista=servicios.BusquedaListaBloqueHorario(codigoBloque);
        
       
        
        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%\" height='60' >"
                + "             <tbody>"
                + "                 <tr>"
                + "                     <td>Listado de Bloques creados </td>"
                + "                     <td align=\"right\">"
                + "                         <button id=\"btnAdBloque\" class=\"btn btn-sm btn-warning\" onclick=\"agregarBloque();\" type=\"button\">\n" +
"                                               <i class=\"ace-icon fa fa-check icon-on-right bigger-110\"></i>\n" +
"                                               Agregar Bloque\n" +
"                                           </button>"
                + "                     </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\">GRUPO DE BLOQUE</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ESTADO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ACCIÓN</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
         
        int dato=0;
        int cont= 1;
        String nombreMostrar="";
        
        for(int i=0; i< lista.size(); i++) {

           pw.println("<tr >");
            
       
        String estado="";
        String turno="";
         if(dato==0){
                estado="<span style='color:#478FCA;'>" +
                            "Editable" +
                        "</span>";
            }else{
                estado="<b> <font color='red'>En uso</font></b>";
            }
         
       
            
             pw.println("   <td align='center' width='5'><b>"+cont+"</td>"
                     + "    <td align='center' width='45'>"+lista.get(i).getNombreGrupo()+"</td>"
                     + "    <td align='center' width='5'>"+estado+"</td>"
                     + "    <td align='center' width='5'><a href=\"javascript:verBloque('"+lista.get(i).getDiplayName()+"',"+lista.get(i).getCodigo()+","+lista.get(i).getMaximoGrupo()+",'"+lista.get(i).getCadena()+"','"+lista.get(i).getNombreGrupo()+"');\" class=\"regular\" >\n"
                     + "    <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     
                     + " </tr>");
            cont++;
        
        
        
        }
            if(cont==1){
            pw.println("<tr><td colspan='4' align='center'>No hay bloques registrados</td></tr>");
        } 
          
         pw.println("       </tbody>\n" +
"			</table>");
          
         pw.println("       </div>"
                 + "    </div>"
                 + "</div>");
         
         
         
        //----rompiendo la sesion en la busqueda de bloques 
        HttpSession session_actual = request.getSession(true);
        session_actual.getAttribute("sesionBloque");
        
        if (session_actual.getAttribute("sesionBloque") != null) {
            session_actual.removeAttribute("sesionBloque");

        }
    }

    private void armarBloqueHorarioEditar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String maxGru = request.getParameter("maxGru");
        String codigo = request.getParameter("codigo");
        String cadena = request.getParameter("cadena");
        String nombreGru = request.getParameter("nombreGru");
        String displayName = request.getParameter("displayName");
        System.out.println("display"+displayName.length());
        String nombreGrupo = "";
        if(displayName.length() == 4){
            nombreGrupo = nombreGru;
        }else{
           nombreGrupo = displayName;
         }
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        String[] array1 = cadena.split("!");
        int x =0;
        
        for(int i=0; i<array1.length;i++){
            String[] array2 = array1[i].split("#");
            if(x<Integer.parseInt(array2[0])){
                x=Integer.parseInt(array2[0]);
            }
           
        }
				
				if(codigo.equals("56") ){
					        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n HORARIO "+nombreGrupo+" " +
"                       </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\" >LUNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MARTES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MIÉRCOLES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">JUEVES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">VIERNES</th>\n" +
//"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">SÁBADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
									
									        String HorIni="";
        String HorFin="";
        int contenido = 0;
        for(int t=1; t<=x;t++){
            pw.println("<tr>");
            for(int i=1; i<=5;i++){
                for(int j=0; j<array1.length;j++){
                    String[] array2 = array1[j].split("#");
                    if(array2[2].equals(""+i)){
                        if(Integer.parseInt(array2[0])>=t){
                            String[] horas  = array2[3].split("¡");
                            int h = t-1;
                            String[] hora   = horas[h].split("-");
                            HorIni=hora[0];
                            HorFin=hora[1];
                            contenido=1;
                        } 
                    } 
                }
                
                if(contenido==1){
                    pw.println("<td align='center' style=' vertical-align:middle;'>"+HorIni+"<br>"+HorFin+"</td>");
                }else{
                    pw.println("<td align='center' style='color:red; background-color:#F5A9A9; vertical-align:middle;'>SIN HORARIO</td>");
                }
                    
                contenido=0;     

            }
            pw.println("</tr>");
        }
					
					
				}else{
					
					    pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n HORARIO "+nombreGrupo+" " +
"                       </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\" >LUNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MARTES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MIÉRCOLES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">JUEVES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">VIERNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">SÁBADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
									
			String HorIni="";
        String HorFin="";
        int contenido = 0;
        for(int t=1; t<=x;t++){
            pw.println("<tr>");
            for(int i=1; i<=6;i++){
                for(int j=0; j<array1.length;j++){
                    String[] array2 = array1[j].split("#");
                    if(array2[2].equals(""+i)){
                        if(Integer.parseInt(array2[0])>=t){
                            String[] horas  = array2[3].split("¡");
                            int h = t-1;
                            String[] hora   = horas[h].split("-");
                            HorIni=hora[0];
                            HorFin=hora[1];
                            contenido=1;
                        } 
                    } 
                }
                
                if(contenido==1){
                    pw.println("<td align='center' style=' vertical-align:middle;'>"+HorIni+"<br>"+HorFin+"</td>");
                }else{
                    pw.println("<td align='center' style='color:red; background-color:#F5A9A9; vertical-align:middle;'>SIN HORARIO</td>");
                }
                    
                contenido=0;     

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

        
        
        
      

        
        
            
        
        
        pw.println("</table><br>"
                + "<br>"
                + "<input type='hidden' id='codBloqMax'value='"+x+"'>");
    }

    private void sessionBloqueHorario(HttpServletRequest request, HttpServletResponse response) {
        String maxGru = request.getParameter("maxGru");
        int codigo = Integer.parseInt(request.getParameter("codigo"));
        String cadena = request.getParameter("cadena");
        String nombreGru = request.getParameter("nombreGru");
        Grupo grupo =new Grupo();
        HttpSession session_actual = request.getSession(true);
        
        grupo.setCodigo(codigo);
        grupo.setMaximoGrupo(maxGru);
        grupo.setCadena(cadena);
        grupo.setNombreGrupo(nombreGru);
        session_actual.setAttribute("sessionGrupoHorario", grupo);

        
        
    }

    private void armarBloqueHorarioParaActualizar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cadena = request.getParameter("cadena");
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        String[] array1 = cadena.split("!");
        int x =0;
        
        for(int i=0; i<array1.length;i++){
            String[] array2 = array1[i].split("#");
            if(x<Integer.parseInt(array2[0])){
                x=Integer.parseInt(array2[0]);
            }
           
        }
        
        
        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n" +
"                       <div class=\"table-header\">\n HORARIO" +
"                       </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\" >LUNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MARTES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">MIÉRCOLES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">JUEVES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">VIERNES</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">SÁBADO</th>\n" +
"                                       </tr>\n" +
"                                   </thead>\n" +
"                                   <tbody>");
        
        
        
      
        String HorIni="";
        String HorFin="";
        int contenido = 0;
        for(int t=1; t<=x;t++){
            pw.println("<tr>");
            for(int i=1; i<=6;i++){
                for(int j=0; j<array1.length;j++){
                    String[] array2 = array1[j].split("#");
                    if(array2[2].equals(""+i)){
                        if(Integer.parseInt(array2[0])>=t){
                            String[] horas  = array2[3].split("¡");
                            int h = t-1;
                            String[] hora   = horas[h].split("-");
                            HorIni=hora[0];
                            HorFin=hora[1];
                            contenido=1;
                        } 
                    } 
                }
                
                if(contenido==1){
                    pw.println("<td align='center' style=' vertical-align:middle;'>"+HorIni+"<br>"+HorFin+"</td>");
                }else{
                    pw.println("<td align='center' style='color:red; background-color:#F5A9A9; vertical-align:middle;'>SIN HORARIO</td>");
                }
                    
                contenido=0;     

            }
            pw.println("</tr>");
        }
        
        
            
        
        
        pw.println("</table><br>"
                + "<center>"
                 + "<button class=\"btn btn-sm btn-primary\" onclick=\"registrarHorario();\" type=\"button\">\n" +
"                       <i class=\"ace-icon fa fa-pencil icon-on-right bigger-110\"></i>\n" +
"                       Editar bloque\n" +
"                   </button>"
                + "</center><br>"
                + "<input type='hidden' id='codBloqMax'value='"+x+"'>");
    }
    
    
    private void EditarGrupoHorario(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String codigo = request.getParameter("codigo");
        String cadena = request.getParameter("cadena");
        String cantMax = request.getParameter("cantMax");
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        BloqueServices servicios = new BloqueServices();
        int dato = servicios.EditarGrupoHorario(cadena, cantMax, codigo);
        
        pw.println(dato);
    }
    
    
   
   

}
