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

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

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param = request.getParameter("Accion");
        if ("listadoPeriodoAcademico".equals(param)) {
            listadoPeriodoAcademico(request, response);
        } else if ("registarPeriodoAcademico".equals(param)) {
            registarPeriodoAcademico(request, response);
        } else if ("cambiarEstado".equals(param)) {
            cambiarEstado(request, response);
        } else if ("armarEdicion".equals(param)) {
            armarEdicion(request, response);
        } else if ("actualizarPeriodoAcademico".equals(param)) {
            actualizarPeriodoAcademico(request, response);
        } else if ("listarAnio".equals(param)) {
            listarAnio(request, response);
        } else if ("listarPeriodoAcademico".equals(param)) {
            listarPeriodoAcademico(request, response);
        }
    }

    private void listadoPeriodoAcademico(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();
        ArrayList<PeriodoAcademico> lista = new ArrayList<PeriodoAcademico>();

        lista = servicios.listarPeriodoAcademico();

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        pw.println("<div style=\"border: 1px solid #CCCCCC;\">\n"
                + "                       <div class=\"table-header\">\n"
                + "                           Listado de Periodo Académico\n"
                + "                       </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\">PERIODO ACADÉMICO</th>\n"
                + "                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">FECHA INICIO</th>\n"
                + "                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">FECHA TERMINO</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++) {

            String estado = "";
            if (lista.get(i).getEstadoPeriodo() == 1) {
                estado = "<a href=\"javascript:validar_estado(0," + lista.get(i).getCodigoPeriodo() + ");\" class=\"regular\" >\n"
                        + "<img src=\"imagenes/icons/lightbulb.png\" />\n"
                        + "</a>";
            } else {
                estado = "<a href=\"javascript:validar_estado(1," + lista.get(i).getCodigoPeriodo() + ");\" class=\"regular\" >\n"
                        + "<img src=\"imagenes/icons/lightbulb_off.png\" />\n"
                        + "</a>";
            }

            String iniAc = lista.get(i).getFechaInicioPeriodo();
            String finAc = lista.get(i).getFechaFinalPeriodo();
            String[] iniA = iniAc.split("-");
            String[] finA = finAc.split("-");
            String iniDia = iniA[2];
            String iniMes = iniA[1];
            String iniAnio = iniA[0];
            String finDia = finA[2];
            String finMes = finA[1];
            String finAnio = finA[0];
            String ini = iniDia + "/" + iniMes + "/" + iniAnio;
            String fin = finDia + "/" + finMes + "/" + finAnio;

            pw.println("   <td align='center' width='20'>" + cont + "</td>"
                    + "    <td>" + lista.get(i).getNombrePeriodo() + "</td>"
                    + "    <td align='center' >" + ini + "</td>"
                    + "    <td align='center' >" + fin + "</td>"
                    + "    <td  align='center' width='50'><a href=\"javascript:armarEdicion(" + lista.get(i).getCodigoPeriodo() + ");\" class=\"regular\" >\n"
                    + "            <img src=\"imagenes/icons/page_edit.png\" />\n"
                    + "        </a>"
                    + "    </td>"
                    + "    <td align='center' valign='middle' width='50'>" + estado + "</td>"
                    + " </tr>");
            cont++;
        }

        if (cont == 1) {
            pw.println("<tr><td colspan='6' align='center'>No hay registro</td></tr>");
        }

        pw.println("               </tbody>\n"
                + "                               </table>\n"
                + "                           </div>"
                + "    </div>"
                + "</div>"
                + "<input type='hidden' id='cant_est' value='" + lista.size() + "'>");

    }

    private void registarPeriodoAcademico(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String nombre = request.getParameter("nombre");
        String iniAc = request.getParameter("ini");
        String finAc = request.getParameter("fin");
        String[] iniA = iniAc.split("/");
        String[] finA = finAc.split("/");
        String iniDia = iniA[0];
        String iniMes = iniA[1];
        String iniAnio = iniA[2];
        String finDia = finA[0];
        String finMes = finA[1];
        String finAnio = finA[2];
        String ini = iniAnio + "-" + iniMes + "-" + iniDia;
        String fin = finAnio + "-" + finMes + "-" + finDia;
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();
        int dato = servicios.registrarPeriodoAcademico(nombre, ini, fin);

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        pw.println(dato);
    }

    private void cambiarEstado(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();

        String salida = "";
        String dato = request.getParameter("dato");
        String codigo = request.getParameter("codigo");

        if (dato.equals("1")) {
            servicios.cambiarEstado(dato, codigo);
            salida = "1";
        } else {
            salida = "0";
        }

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        pw.println(salida);

    }

    private void armarEdicion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();
        String codigo = request.getParameter("codigo");
        PeriodoAcademico periodo = new PeriodoAcademico();
        periodo = servicios.armarEdicion(codigo);

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        String iniAc = periodo.getFechaInicioPeriodo();
        String finAc = periodo.getFechaFinalPeriodo();
        String[] iniA = iniAc.split("-");
        String[] finA = finAc.split("-");
        String iniDia = iniA[2];
        String iniMes = iniA[1];
        String iniAnio = iniA[0];
        String finDia = finA[2];
        String finMes = finA[1];
        String finAnio = finA[0];
        String ini = iniDia + "/" + iniMes + "/" + iniAnio;
        String fin = finDia + "/" + finMes + "/" + finAnio;

        pw.println("<center>"
                + "     <table>"
                + "         <tr>"
                + "             <td height='20' colspan='3'></td>"
                + "         </tr>"
                + "         <tr>"
                + "             <td>Periodo Académico: <span style='color:red;'>*</span></td>"
                + "             <td width='10'></td>"
                + "             <td><input class='form-control validate[required]' type='text' id='txt_new_nom' value='" + periodo.getNombrePeriodo() + "'></td>"
                + "         </tr>"
                + "         <tr>"
                + "             <td height='15' colspan='3'></td>"
                + "         </tr>"
                + "         <tr>"
                + "             <td>Fecha inicio: <span style='color:red;'>*</span></td>"
                + "             <td width='10'></td>"
                + "             <td>"
                + "                 <div class=' input-group'>"
                + "                     <input class=' form-control  validate[required] input-mask-date' id='txt_new_ini' style='text-align: center;' onblur='validaFecha(this.value);' type='text' placeholder=' dd/mm/yyyy' value='" + ini + "'><!-- input-sm -->"
                + "                     <span class='input-group-addon'><i class='fa fa-calendar'></i></span>"
                + "                 </div>"
                + "             </td>"
                + "         </tr>"
                + "         <tr>"
                + "             <td height='15' colspan='3'></td>"
                + "         </tr>"
                + "         <tr>"
                + "             <td>Fecha termino: <span style='color:red;'>*</span></td>"
                + "             <td width='10'></td>"
                + "             <td>"
                + "                 <div class=' input-group'>"
                + "                     <input class=' form-control  validate[required] input-mask-date' id='txt_new_fin' style='text-align: center;' onblur='validaFecha(this.value);' type='text' placeholder=' dd/mm/yyyy' value='" + fin + "'><!-- input-sm -->"
                + "                     <span class='input-group-addon'><i class='fa fa-calendar'></i></span>"
                + "                 </div>"
                + "             </td>"
                + "         </tr>"
                + "     </table>"
                + "</center>");

    }

    private void actualizarPeriodoAcademico(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();
        String codigo = request.getParameter("codigo");
        String nombre = request.getParameter("nombre");
        String iniAc = request.getParameter("ini");
        String finAc = request.getParameter("fin");
        String[] iniA = iniAc.split("/");
        String[] finA = finAc.split("/");
        String iniDia = iniA[0];
        String iniMes = iniA[1];
        String iniAnio = iniA[2];
        String finDia = finA[0];
        String finMes = finA[1];
        String finAnio = finA[2];
        String ini = iniAnio + "-" + iniMes + "-" + iniDia;
        String fin = finAnio + "-" + finMes + "-" + finDia;
        int dato = servicios.actualizarPeriodoAcademico(nombre, ini, fin, codigo);

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        pw.println(dato);
    }

    private void listarAnio(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();
        ArrayList<PeriodoAcademico> lista = new ArrayList<PeriodoAcademico>();
        String accion = request.getParameter("accion");
        String nombre = request.getParameter("nombre");

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        lista = servicios.listarAnio();

        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 selected value='" + lista.get(i).getCodigoPeriodo() + "'>" + lista.get(i).getAnio() + " (Activo)</option>");
        }
        pw.println("</select>");
    }

    private void listarPeriodoAcademico(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PeriodoAcademicoServices servicios = new PeriodoAcademicoServices();
        ArrayList<PeriodoAcademico> lista = new ArrayList<PeriodoAcademico>();

        lista = servicios.listarPeriodoAcademico();
        String id = request.getParameter("nombre");
        String accion = request.getParameter("accion");

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        
        pw.println("<select class='form-control validate[required]' id='" + id + "' onchange='"+accion+"'>");
        pw.println("<option value=\"\">[SELECCIONE PERIODO]</option>");
        for (int i = 0; i < lista.size(); i++) {
            if(lista.get(i).getEstadoPeriodo() == 1){
                pw.println("<option value='"+lista.get(i).getCodigoPeriodo()+"' selected>"+lista.get(i).getNombrePeriodo()+" - ACTIVO</option>");
            }else{
               pw.println("<option value='"+lista.get(i).getCodigoPeriodo()+"'>"+lista.get(i).getNombrePeriodo()+"</option>"); 
            }
            
        }
        pw.println("</select>");

    }

}
