/*
 * 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.Area;
import pe.siso.horario.Beans.Curso;
import pe.siso.horario.Beans.Plana;
import pe.siso.horario.Services.AreaServices;
import pe.siso.horario.Services.CursoServices;
import pe.siso.horario.Services.PlanaServices;

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

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String param=request.getParameter("Accion");
      if("listadoCurso".equals(param)){
            listadoCurso(request,response);
      } else if("registrarCurso".equals(param)){
            registrarCurso(request,response);
      }else if("cambiarEstado".equals(param)){
            cambiarEstado(request,response);
      }else if("listadoCursoBusqueda".equals(param)){
            listadoCursoBusqueda(request,response);
      }else if("cargarFamilia".equals(param)){
            cargarFamilia(request,response);
      }else if("EditarCurso".equals(param)){
            EditarCurso(request,response);
      }else if("Actualizar".equals(param)){
            Actualizar(request,response);
      }else if("cargarCurso".equals(param)){
            cargarCurso(request,response);
      }else if("cargarCursoArea".equals(param)){
            cargarCursoArea(request,response);
      }else if("cargarCursoEdit".equals(param)){
            cargarCursoEdit(request,response);
      }else if("cantidadCursoUso".equals(param)){
            cantidadCursoUso(request,response);
      }else if("armarRegistro".equals(param)){
            armarRegistro(request,response);
      }else if("cargarCurso2".equals(param)){
            cargarCurso2(request,response);
      }
      
      
    }

    private void registrarCurso(HttpServletRequest request, HttpServletResponse response) {
        CursoServices servicios = new CursoServices();;
        String nivel = request.getParameter("nivel");
        String codplana = request.getParameter("codplana");
        String codFam= request.getParameter("codFam");
        servicios.registrarCurso(codplana, nivel,codFam);
    }

    private void listadoCurso(HttpServletRequest request, HttpServletResponse response) throws IOException {
       CursoServices servicios = new CursoServices();
        ArrayList<Curso> lista = null;
        
        lista=servicios.listarCurso();
        
        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=\"550\">\n" +
"                   <div class=\"datagrid\">\n" +
"                           <table >\n" +
"                                   <thead>\n" +
"                                       <tr >\n" +
"                                           <th>N°</th>\n" +
"                                           <th>ÁREA</th>\n" +
"                                           <th>PLANA</th>\n" +
"                                           <th>CURSO</th>\n" +
"                                           <th>FAMILIA</th>\n" +                        
"                                           <th>EDICIÓN</th>\n" +
"                                           <th>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).getEst_cur()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCod_cur()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCod_cur()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center'>"+cont+"</td>"
                     + "    <td>"+lista.get(i).getNom_are()+"</td>"
                     + "    <td>"+lista.get(i).getNom_pla()+"</td>"
                     + "    <td>"+lista.get(i).getNom_cur()+"</td>"
                     + "    <td>"+lista.get(i).getNom_fam()+"</td>"
                     + "    <td  align='center'><a href=\"javascript:armarEdicion("+lista.get(i).getCod_cur()+","+lista.get(i).getCod_area()+","+lista.get(i).getCod_fam()+");\" 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='4' align='center'>No Hay Cursos registrados</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>");
    }

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

    private void listadoCursoBusqueda(HttpServletRequest request, HttpServletResponse response) throws IOException {
      CursoServices servicios = new CursoServices();
        ArrayList<Curso> lista = null;
        String codArea = request.getParameter("codArea");
        String codPlana = request.getParameter("codPlana");
        lista=servicios.listarCurso(codArea, codPlana);
        
        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 Cursos</td>"
                + "                     <td align=\"right\">"
                + "                         <button class=\"btn btn-sm btn-warning\" onclick=\"agregarCurso();\" type=\"button\">\n" +
"                                               <i class=\"ace-icon fa fa-plus-circle icon-on-right bigger-110\"></i>\n" +
"                                               Agregar Curso\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\">ÁREA</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">PLANA</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CURSO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">FAMILIA</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).getEst_cur()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCod_cur()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCod_cur()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center'>"+cont+"</td>"
                      + "    <td>"+lista.get(i).getNom_are()+"</td>"
                     + "    <td>"+lista.get(i).getNom_pla()+"</td>"
                     + "    <td>"+lista.get(i).getNom_cur()+"</td>"
                      + "    <td>"+lista.get(i).getNom_fam()+"</td>"
                     + "    <td  align='center'><a href=\"javascript:armarEdicion("+lista.get(i).getCod_cur()+","+lista.get(i).getCod_area()+","+lista.get(i).getCod_fam()+");\" 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 Hay Cursos Registrados</td></tr>");
        } 
          
         pw.println("               </tbody>\n" +
"                               </table>\n" +
"                           </div>\n" +
"                       </div"
                 + "</div>");
    }

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

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

    private void EditarCurso(HttpServletRequest request, HttpServletResponse response) throws IOException {
       CursoServices servicios = new CursoServices();
       AreaServices serviciosArea = new AreaServices();
       PlanaServices serviciosPlana = new PlanaServices();
       Curso curso = new Curso();
       
       
        String codCurso = request.getParameter("codCurso");
        String codArea = request.getParameter("codArea");
        int codFam = Integer.parseInt(request.getParameter("codFam"));
        
        curso = servicios.NombreCurso(codCurso);
        
        ArrayList<Area> listaArea = null;
        ArrayList<Plana> listaPlana = null;
        ArrayList<Curso> listaCurso= null;
        listaArea = serviciosArea.cargarArea();
        listaPlana=serviciosPlana.cargarPlana(codArea);
        listaCurso=servicios.cargarFamilia();
        //String dato = request.getParameter("dato");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
       pw.println("<center> "
               + "<table width='300' border='0'>"
               
               + "          <td width='70'><b>Área: </b> <span style='color:red;'>*</span></td>"
               + "          <td  width='10'></td>"
               + "          <td> <select id='cmbAreaCurso' class='form-control validate[required]' onChange='cargarPlanaEdit()' >");
               
               for(int i=0; i<listaArea.size();i++){
                    if(listaArea.get(i).getCodigoArea()==curso.getCod_area()){
                        pw.println( " <option value='"+listaArea.get(i).getCodigoArea()+"' selected>"+listaArea.get(i).getNombreArea()+"</option>");
                    }else{
                        pw.println( " <option value='"+listaArea.get(i).getCodigoArea()+"'>"+listaArea.get(i).getNombreArea()+"</option>");
                    }
                    
                }
             
        pw.println( "           </select> </td>"
               + "      </tr>"
               + "      </tr>"
               + "      <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Plana: </b> <span style='color:red;'>*</span></td>"
               + "          <td width='10'></td>"
               + "          <td> <div id='divPlanaEdit'><select id='cmbPlanaCurso' onChange='cargarFamiliaEdit();' class='form-control validate[required]' >");
                  for(int i=0; i<listaPlana.size();i++){
                    if(listaPlana.get(i).getCod_pla()==curso.getCod_pla()){
                        pw.println( " <option value='"+listaPlana.get(i).getCod_pla()+"' selected>"+listaPlana.get(i).getNom_pla()+"</option>");
                    }else{
                        pw.println( " <option value='"+listaPlana.get(i).getCod_pla()+"'>"+listaPlana.get(i).getNom_pla()+"</option>");
                    }
                    
                }
          pw.println( "           </select> </div></td>"
               + "      </tr>"
               + "      </tr>"
               + "      <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Familia: </b> <span style='color:red;'>*</span></td>"
               + "          <td width='10'></td>"
               + "          <td> <div id='divFamiliaEdit'><select id='cmbfamCurso' class='form-control validate[required]' >");  
                        
                                 
                  for(int i=0; i<listaCurso.size();i++){
                    if(codFam==listaCurso.get(i).getCod_fam()){
                         pw.println( " <option value='"+listaCurso.get(i).getCod_fam()+"' selected>"+listaCurso.get(i).getNom_fam()+"</option>");
                    }else {
                        pw.println( " <option value='"+listaCurso.get(i).getCod_fam()+"'>"+listaCurso.get(i).getNom_fam()+"</option>");
                    }
               }                 
               pw.println( "           </select> </div> </td>"
               + "      </tr>"
               + "      </tr>"
               + "      <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Curso: </b> <span style='color:red;'>*</span></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_nom_curso' type='text' class='form-control validate[required]' value='"+curso.getNom_cur()+"' onChange='conMayusculas(this);'></td>"
               + "      </tr>"  
               + "  </table>"
               + "</center>");
        
      
    }

    private void Actualizar(HttpServletRequest request, HttpServletResponse response) {
        CursoServices servicios = new CursoServices();
        String codigoArea = request.getParameter("codigoArea");
        String codPlana = request.getParameter("codPlana");
        String codFam = request.getParameter("codFam");
        String nombre = request.getParameter("nombre");
        String codigoCur = request.getParameter("codigoCur");
        
        servicios.Actualizar(codigoCur, codigoArea, codPlana, codFam, nombre);
        
    }

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

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

    private void cargarCursoArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CursoServices servicios = new CursoServices();
        ArrayList<Curso> lista = null;
        String nombre = request.getParameter("nombre");
        String accion = request.getParameter("accion");
        String codArea = request.getParameter("codArea");
        
        lista=servicios.cargarCurso(codArea);
        
        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).getCod_cur()+"'>"+lista.get(i).getNom_cur()+"</option>");
        }

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

    private void cargarCursoEdit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CursoServices servicios = new CursoServices();
        ArrayList<Curso> lista = null;
        String nombre = request.getParameter("nombre");
        String accion = request.getParameter("accion");
        int codCur = Integer.parseInt(request.getParameter("codCur"));
        
        lista=servicios.cargarCurso();
        
        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++) {
            if(codCur==lista.get(i).getCod_cur()){
                pw.println("<option value='"+lista.get(i).getCod_cur()+"' selected>"+lista.get(i).getNom_cur()+"</option>");
            }else{
                pw.println("<option value='"+lista.get(i).getCod_cur()+"'>"+lista.get(i).getNom_cur()+"</option>");
            }
            
        }

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

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

    private void armarRegistro(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CursoServices servicios = new CursoServices();
       AreaServices serviciosArea = new AreaServices();
       PlanaServices serviciosPlana = new PlanaServices();
       Curso curso = new Curso();
       
        String codArea = request.getParameter("codArea");
        int codPlana = Integer.parseInt(request.getParameter("codPlana"));
        
        
        String nombre2 = request.getParameter("nombre2");
        
        int codigoA=Integer.parseInt(codArea);
        //curso = servicios.NombreCurso(codCurso);
        
        ArrayList<Area> listaArea = null;
        ArrayList<Plana> listaPlana = null;
        ArrayList<Curso> listaCurso= null;
        listaArea = serviciosArea.cargarArea();
        listaPlana=serviciosPlana.cargarPlana(codArea);
        listaCurso=servicios.cargarFamilia();
        //String dato = request.getParameter("dato");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
       pw.println("<center> "
               + "<table  width='300' border='0'>"
               
               + "          <td width='70'><b>Área</b> <span style='color:red;' >*</span></td>"
               + "          <td width='10'></td>"
               + "          <td> <select id='cmbAreaCurso' class=' form-control validate[required]' onChange='cargarPlanaAgregar()' >");
               
               for(int i=0; i<listaArea.size();i++){
                    if(listaArea.get(i).getCodigoArea()==codigoA){
                        pw.println( " <option value='"+listaArea.get(i).getCodigoArea()+"' selected>"+listaArea.get(i).getNombreArea()+"</option>");
                    }else{
                        pw.println( " <option value='"+listaArea.get(i).getCodigoArea()+"'>"+listaArea.get(i).getNombreArea()+"</option>");
                    }
                    
                }
             
        pw.println( "           </select> </td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Plana</b> <span style='color:red;' >*</span></td>"
               + "          <td width='10'></td>"
               + "          <td> <div id='divPlanaAgregar'><select id='cmbPlanaCur' class='form-control validate[required]' >");
                  for(int i=0; i<listaPlana.size();i++){
                    if(listaPlana.get(i).getCod_pla()==codPlana){
                        pw.println( " <option value='"+listaPlana.get(i).getCod_pla()+"' selected>"+listaPlana.get(i).getNom_pla()+"</option>");
                    }else{
                        pw.println( " <option value='"+listaPlana.get(i).getCod_pla()+"'>"+listaPlana.get(i).getNom_pla()+"</option>");
                    }
                    
                }
          pw.println( "           </select> </div></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td colspan='3' height='15'></td>"
               + "      </tr>" 
               + "      <tr>"
               + "          <td><b>Familia </b><span style='color:red;' >*</span></td>"
               + "          <td width='10'></td>"
               + "          <td> <div id='divFamiliaAgregar'> <select id='cmbfamCur' class='form-control validate[required]' >");  
                
               pw.println("<option value=''>[Seleccione]</option>");       
                                 
                  for(int i=0; i<listaCurso.size();i++){
                     pw.println( " <option value='"+listaCurso.get(i).getCod_fam()+"'>"+listaCurso.get(i).getNom_fam()+"</option>");
                   
               }                 
               pw.println( "           </select> </div> </td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td colspan='3' height='15'></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <td><b>Curso </b><span style='color:red;' >*</span></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_nom_cur' type='text' class='form-control validate[required]' value='' onChange='conMayusculas(this);'></td>"
               + "      </tr>"  
               + "  </table>"
               + "</center>");
        
    }  

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

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