/*
 * 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.Centro;
import pe.siso.horario.Beans.TipoCentro;
import pe.siso.horario.Services.CentroServices;
import pe.siso.horario.Services.TipoCentroServices;

/**
 *
 * @author fcelestino
 */
public class ServletCentro extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param=request.getParameter("Accion");
        if("listadoCentro".equals(param)){
            listadoCentro(request,response);
        }else if("listadoCentroBusqueda".equals(param)){
            listadoCentroBusqueda(request,response);
        }else if("registarCentro".equals(param)){
            registarCentro(request,response);
        }else if("cambiarEstado".equals(param)){
            cambiarEstado(request,response);
        }else if("cargarCentro".equals(param)){
            cargarCentro(request,response);
        }else if("armarEditar".equals(param)){
            armarEditar(request,response);
        }else if("actualizar".equals(param)){
            actualizar(request,response);
        }else if("cargarCentroBlo".equals(param)){
            cargarCentroBlo(request,response);
        }else if("cantidadUsoCentro".equals(param)){
            cantidadUsoCentro(request,response);
        }
        
    }

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

"                               <table class=\"table table-striped table-bordered table-hover dataTable no-footer\" id=\"sample-table-2\" role=\"grid\" aria-describedby=\"sample-table-2_info\" >\n" +
"                                   <thead>\n" +
"                                       <tr role=\"row\">\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\" >N°</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">TIPO CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">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).getEstadoCentro()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCodigoCentro()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCodigoCentro()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center' width='25'>"+cont+"</td>"
                     + "    <td >"+lista.get(i).getNombreTipoCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreCentro()+"</td>"
                     + "    <td  align='center' width='40'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoCentro()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle' width='40'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='5' align='center'>No hay centros registrados</td></tr>");
        } 
          
         pw.println("   </tbody>\n" +
                    "</table>");
         
         pw.println("       </div>" +
                    "   </div>"
                 +  "</div>");
    }

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

"                               <table class=\"table table-striped table-bordered table-hover dataTable no-footer\" id=\"sample-table-2\" role=\"grid\" aria-describedby=\"sample-table-2_info\" >\n" +
"                                   <thead>\n" +
"                                       <tr role=\"row\">\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\" >N°</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">TIPO CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">CENTRO</th>\n" +
"                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">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).getEstadoCentro()==1){
                estado="<a href=\"javascript:validar_estado(0,"+lista.get(i).getCodigoCentro()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb.png\" />\n" +
                        "</a>";
            }else{
                estado="<a href=\"javascript:validar_estado(1,"+lista.get(i).getCodigoCentro()+");\" class=\"regular\" >\n" +
                            "<img src=\"imagenes/icons/lightbulb_off.png\" />\n" +
                        "</a>";
            }
            
             pw.println("    <td align='center' width='25'>"+cont+"</td>"
                     + "    <td>"+lista.get(i).getNombreTipoCentro()+"</td>"
                     + "    <td>"+lista.get(i).getNombreCentro()+"</td>"

                     + "    <td  align='center' width='40'><a href=\"javascript:armarEdicion("+lista.get(i).getCodigoCentro()+");\" class=\"regular\" >\n"
                     + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                     + "        </a>"
                     + "    </td>"
                     + "    <td align='center' valign='middle' width='40'>"+estado+"</td>"
                     + " </tr>");
            cont++;
        }
        
        if(cont==1){
            pw.println("<tr><td colspan='5' align='center'>No hay centros registrados</td></tr>");
        } 
          
         pw.println("</tbody>\n" +
"			</table>\n" +
"			</div>\n" +
"			</td></tr></table>");
    }

    private void registarCentro(HttpServletRequest request, HttpServletResponse response) {
        CentroServices servicios = new CentroServices();
        String centro = request.getParameter("centro");
        String codTipoCentro = request.getParameter("codTipoCentro");
        servicios.registrarCentro(centro, codTipoCentro);
    }

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

    private void cargarCentro(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CentroServices servicios = new CentroServices();
        ArrayList<Centro> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codTipoCentro = request.getParameter("codTipoCentro");
        
        lista=servicios.cargarCentro(codTipoCentro);
        
        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).getCodigoCentro()+"'>"+lista.get(i).getNombreCentro()+"</option>");
        }

         pw.println("</select>");
    }
    
    private void armarEditar(HttpServletRequest request, HttpServletResponse response) throws IOException {
       CentroServices servicios=new CentroServices();
       TipoCentroServices servicios2 = new TipoCentroServices();
       Centro centro = new Centro();
       
       
        String codCentro = request.getParameter("codCentro");
        String nombre = request.getParameter("nombre");
        centro=servicios.nombreCentro(codCentro);
        
        ArrayList<TipoCentro> lista= null;
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<table  width='100%'>"
               + "<tr>"
               + "<td align='center'> "
               + "<table>"
          
               + "      <tr>"
               + "          <td><b>Tipo Centro </b></td>"
               + "          <td width='10'></td>"
               + "          <td><label>"+centro.getNombreTipoCentro()+"</label></td>"  
               + "      </tr>"
               + "      </tr>"
               + "          <td colspan='3' height='15' ></td>"
               + "      </tr>"
               + "      <tr>"
               + "          <br>"  
               + "          <td><b>Centro </b> <span style='color:red;'>*</span></td>"
               + "          <td width='10'></td>"
               + "          <td><input id='txt_nom_centro' type='text' class='form-control validate[required,custom[onlyLetterSp]]' value='"+centro.getNombreCentro()+"' onChange='conMayusculas(this);'></td>"
               + "      </tr>"  
               + "</table>"
               + "</td>"
               + "</tr>"
               + "</table>"); 
    }

    private void actualizar(HttpServletRequest request, HttpServletResponse response) {
        CentroServices servicios = new CentroServices();
        String nombre = request.getParameter("nombre");
        String codCentro = request.getParameter("codCentro");
        
        servicios.actualizar(codCentro, nombre);
    }

    private void cargarCentroBlo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CentroServices servicios = new CentroServices();
        ArrayList<Centro> lista = null;
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");
        String codTipoCentro = request.getParameter("codTipoCentro");
        String codSede = request.getParameter("codSede");
        
        lista=servicios.cargarCentro(codSede, codTipoCentro);
        
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
         pw.println("<select id='"+nombre+"' class='form-control' onChange='"+accion+"'>");
         pw.println("<option value=''>[Seleccione]</option>");
        for(int i=0; i< lista.size(); i++) {
            pw.println("<option value='"+lista.get(i).getCodigoCentro()+"'>"+lista.get(i).getNombreCentro()+"</option>");
        }

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

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

}
