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

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

  
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String param=request.getParameter("Accion");
       if("listarPlana".equals(param)){
            listarPlana(request,response);
        }else if("listarPlanaBusqueda".equals(param)){
            listarPlanaBusqueda(request,response);
        }else if("registrarPlana".equals(param)){
            registrarPlana(request,response);
        } else if("cambiarEstado".equals(param)){
            cambiarEstado(request,response);
        }else if("cargarPlana".equals(param)){
            cargarPlana(request,response);
        }else if("armarEdicion".equals(param)){
            armarEdicion(request,response);
        }else if("Actualizar".equals(param)){
            Actualizar(request,response);
        }else if("cantidadUsoPlana".equals(param)){
            cantidadUsoPlana(request,response);
        }else if("armarRegistro".equals(param)){
            armarRegistro(request,response);
        }
    }

    private void listarPlana(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PlanaServices servicios = new PlanaServices();
        ArrayList<Plana> lista = null;
            lista= servicios.listarPlana();
        
        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>COLOR</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_plana()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCod_pla()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCod_pla()+");\" 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 align='center'> <span style='background-color:"+lista.get(i).getColor()+";  color:"+lista.get(i).getColor()+"; border: 1px solid black; ' >----</span></td>"
                     + "    <td  align='center'><a href=\"javascript:armarEdicion("+lista.get(i).getCod_pla()+");\" 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 Planas registradas</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>"); 
    }

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

    private void registrarPlana(HttpServletRequest request, HttpServletResponse response) {
        PlanaServices servicios = new PlanaServices();
        String plana = request.getParameter("plana");
        String codarea = request.getParameter("codarea");
        String color = request.getParameter("color");
        servicios.registrarPlana(plana, codarea, color);
        
    }
                    
    private void cargarPlana(HttpServletRequest request, HttpServletResponse response) throws IOException {
     PlanaServices servicios = new PlanaServices();
        ArrayList<Plana> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codArea = request.getParameter("codArea");
        
        lista=servicios.cargarPlana(codArea);
        
        
        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_pla()+"'>"+lista.get(i).getNom_pla()+"</option>");
        }

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

    private void listarPlanaBusqueda(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PlanaServices servicios = new PlanaServices();
        ArrayList<Plana> lista = null;
        String codArea = request.getParameter("codArea");
        lista=servicios.listarPlana(codArea);
        
        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 Planas</td>"
                + "                     <td align=\"right\">"
                + "                         <button class=\"btn btn-sm btn-warning\" onclick=\"agregar_plana();\" type=\"button\">\n" +
"                                               <i class=\"ace-icon fa fa-plus-circle icon-on-right bigger-110\"></i>\n" +
"                                               Agregar Plana\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\">PLANA</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">COLOR</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_plana()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCod_pla()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCod_pla()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center'>"+cont+"</td>"
                     + "    <td>"+lista.get(i).getNom_pla()+"</td>"
                     + "    <td align='center'> <span style='background-color:"+lista.get(i).getColor()+";  color:"+lista.get(i).getColor()+"; border: 1px solid black; ' >----</span></td>"
                     + "    <td  align='center'><a href=\"javascript:armarEdicion("+lista.get(i).getCod_pla()+");\" 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='5' align='center'>No hay planas registradas</td></tr>");
        } 
          
         pw.println("               </tbody>\n" +
"                               </table>\n" +
"                           </div>" +
"			</div>" +
"                   </div>");
    }

    private void armarEdicion(HttpServletRequest request, HttpServletResponse response) throws IOException {
     PlanaServices servicios = new PlanaServices();
     AreaServices serviciosArea = new AreaServices();
     
        Plana plana = new Plana();
        String codPlana = request.getParameter("codPlana");
        String colores = request.getParameter("colores");
        
        plana = servicios.NombrePlana(codPlana);
        
        ArrayList<Area> listaArea = null;
        
        listaArea = serviciosArea.cargarArea();
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
       pw.println("<table  width='100%' >"
               + "<tr>"
               + "  <td height='15'></td>"
               + "</tr>"
               + "<tr>"
               + "<td align='center'> "
               + "<table  border='0'>"
               + " <tr>"
               + "          <td ><b>Color: </b> <font color='#FF0000'>*</font></td>"
               + "          <td width='10' ></td>"
                + "          <td > <input style='background-color:"+plana.getColor()+"; color:white;' type='text' value='"+plana.getColor()+"' size='10' id='edit_color' onclick='startColorPicker(this)' class='form-control validate[required]' >"
               + "          <!--<span class='labs-demo clearfix'> " +
                 "            <input id='edit_color' type='text' value='"+plana.getColor()+"' class='validate[required], kolorPicker' onchange='cambiaColor();'>\n" +
                 "                           </span>-->"
               + "          </td>"
               + "</tr>"
               + "<!--<tr>"
               + "<td > <b>Color actual: </b></td>"
               + "          <td width='10' ></td>"
               + "<td align='left' colspan='2'> "
               + "   <div id='colorAct' style='background-color:"+plana.getColor()+";  color:"+plana.getColor()+"; border: 1px solid black;'  >----</div> "
               + "</td> "
               + "</tr>"

               + "<td > <b>Color nuevo: </b></td>"
               + "          <td width='10' ></td>"
               + "      <td><div id='colores' style=' color:#FFFFFF ;border: 1px solid black;' >--</div> "
               + "</td>-->"
               + "<tr>"
               + "  <td height='12'></td>"
               + "</tr>"
               +"</tr>"
              
               + "      <tr>"
               + "          <td><b>Área: </b>  <font color='#FF0000'>*</font></td>"
               + "          <td width='10'></td>"
               + "          <td> <select id='cmbAreas' class='form-control validate[required]' >");
               
                for(int i=0; i<listaArea.size();i++){
                    if(listaArea.get(i).getCodigoArea()==plana.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 height='12'></td>"
               + "</tr>"
               + "      <tr>"
               + "          <td><b>Plana: </b> <font color='#FF0000'>*</font></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_nom' type='text' class='form-control validate[required,custom[onlyLetterSp]]' value='"+plana.getNom_pla()+"' onChange='conMayusculas(this);'></td>"
               + "      </tr>" 
               + "  </table>"
               + "</td>"
               + "</tr>"
               + "</table>");
        
      
        

       
    }

    private void Actualizar(HttpServletRequest request, HttpServletResponse response) {
        PlanaServices servicios = new PlanaServices();
        String nombre = request.getParameter("nombre");
        String codPlana = request.getParameter("codPlana");
        String codArea = request.getParameter("codArea");
        String color=request.getParameter("color");
        servicios.Actualizar(codPlana, nombre, codArea,color);
    }

    private void cantidadUsoPlana(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PlanaServices servicios = new PlanaServices();
        String codPlana = request.getParameter("codPlana");
        int cantidad=servicios.cantidadUsoPlana(codPlana);
        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 {
       PlanaServices servicios = new PlanaServices();
        AreaServices serviciosArea = new AreaServices();
        ArrayList<Area> listaArea = null;
        int codigoArea= Integer.parseInt(request.getParameter("codigoArea"));
        listaArea = serviciosArea.cargarArea();
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        

       pw.println("<table  width='100%' border='0'>"
               + "<tr>"
               + "  <td height='15'></td>"
               + "</tr>"
               + "<tr>"
               + "<td align='center'> "
               + "<table  border='0'>"
               + " <tr> "
               + "          <td ><b>Seleccione Color: </b> <font color='#FF0000'>*</font></td>"
               + "          <td width='10' ></td>"
               + "          <td > <input  type='text' size='10' id='color' onclick='startColorPicker(this)' class='validate[required]' >"
               + "          <!--<span class='labs-demo clearfix'> " +
                 "            <input id='color' type='text' value='#FFFFFF' class='validate[required], kolorPicker' onchange='cambiarColor2();'>\n" +
                 "             </span>-->"

               +
                 "     <!--<span id='muestraColor' style=' background-color:#FFF ;color:#FFF ;border: 1px solid black;'>\n" +
                 "     ----   \n" +
                 "     </span>-->\n" +
                 "     </td>"
             
               + "</tr>"
               + "<tr><td colspan='3' height='12'></td></tr>"
              
               + "      <tr>"
               + "          <td><b>Nombre de Área: </b> <font color='#FF0000'>*</font></td>"
               + "          <td width='10'></td>"
               + "          <td> <select id='cmbAreaRegistro' class='form-control validate[required]' >");
               
                for(int i=0; i<listaArea.size();i++){
                    if(listaArea.get(i).getCodigoArea()==codigoArea){
                        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='12'></td></tr>"
               + "      <tr>"
               + "          <td><b>Nombre de Plana: </b> <font color='#FF0000'>*</font></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_new_plana' type='text' size='21' class='form-control validate[required,custom[onlyLetterSp]]' onChange='conMayusculas(this);'> </td>"
               + "      </tr>" 
               + "  </table>"
               + "</td>"
               + "</tr>"
               + "</table>");
        
    }


   

}
