/*
 * 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 org.json.JSONArray;
import pe.siso.horario.Beans.Area;
import pe.siso.horario.Services.AreaServices;

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

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String param = request.getParameter("Accion");
        if ("listarAreas".equals(param)) {
            listarAreas(request, response);
        } else if ("cambiarEstado".equals(param)) {
            cambiarEstado(request, response);
        } else if ("registrarAreas".equals(param)) {
            registrarAreas(request, response);
        } else if ("armarEdicion".equals(param)) {
            armarEdicion(request, response);
        } else if ("cargarArea".equals(param)) {
            cargarArea(request, response);
        } else if ("Actualizar".equals(param)) {
            Actualizar(request, response);
        } else if ("cantidadUsoArea".equals(param)) {
            cantidadUsoArea(request, response);
        } else if ("consultarAreas".equals(param)) {
            consultarAreas(request, response);
        } else if ("registraAreaDocente".equals(param)) {
            registraAreaDocente(request, response);
        } else if ("cargarAreaJson".equals(param)) {
            cargarAreaJson(request, response);
        }

    }

    private void listarAreas(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AreaServices servicios = new AreaServices();
        ArrayList<Area> lista = null;

        lista = servicios.listarAreas();

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

        pw.println("<div style=\"border: 1px solid #CCCCCC;\">  "
                + "                       <div class=\"table-header\">  "
                + "                           Listado de Áreas  "
                + "                       </div>  "
                + "                       <div>  "
                + "                           <div class=\"dataTables_wrapper form-inline no-footer\" id=\"sample-table-2_wrapper\">  "
                + "                               <table class=\"table table-striped table-bordered table-hover dataTable no-footer\" id=\"sample-table-2\" role=\"grid\" aria-describedby=\"sample-table-2_info\" >  "
                + "                                   <thead>  "
                + "                                       <tr role=\"row\">  "
                + "                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\" >N°</th>  "
                + "                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ÁREA</th>  "
                + "                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">EDICIÓN</th>  "
                + "                                           <th tabindex=\"0\" class=\"center\" rowspan=\"1\" colspan=\"1\">ESTADO</th>  "
                + "                                       </tr>  "
                + "                                   </thead>  "
                + "                                   <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).getEstaArea() == 1) {
                estado = "<a href=\"javascript:validar_estado(0," + lista.get(i).getCodigoArea() + ");\" class=\"regular\" >  "
                        + "<img src=\"imagenes/icons/lightbulb.png\" />  "
                        + "</a>";
            } else {
                estado = "<a href=\"javascript:validar_estado(1," + lista.get(i).getCodigoArea() + ");\" class=\"regular\" >  "
                        + "<img src=\"imagenes/icons/lightbulb_off.png\" />  "
                        + "</a>";
            }

            pw.println("    <td align='center'>" + cont + "</td>"
                    + "    <td>" + lista.get(i).getNombreArea() + "</td>"
                    + "    <td  align='center'><a href=\"javascript:armarEdicion(" + lista.get(i).getCodigoArea() + ");\" class=\"regular\" >  "
                    + "            <img src=\"imagenes/icons/page_edit.png\" />  "
                    + "        </a>"
                    + "    </td>"
                    + "    <td align='center' valign='middle'>" + estado + "</td>"
                    + " </tr>");
            cont++;
        }

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

        pw.println("           </tbody>  "
                + "                           </table>  "
                + "			</div>"
                + "                   </div>"
                + "               </div>");
    }

    private void cambiarEstado(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //String codArea=request.getParameter("codArea");
        String cod_area = request.getParameter("cod_area");
        AreaServices servicios = new AreaServices();
        int cantidad = servicios.cantidadAreaUso(cod_area);
        String dato = request.getParameter("dato");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        if (cantidad == 0) {
            servicios.cambiarEstado(dato, cod_area);
            pw.println("0");
        } else {
            pw.println("1");
        }

    }

    private void registrarAreas(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AreaServices servicios = new AreaServices();
        String nombreArea = request.getParameter("Tcentro");
        int dato = servicios.registrarAreas(nombreArea);
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        if (dato == 0) {
            pw.println("1");
        } else if (dato > 0) {
            pw.println("0");
        } else {
            pw.println("2");
        }
    }

    private void armarEdicion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AreaServices servicios = new AreaServices();
        Area area = new Area();
        String cod_area = request.getParameter("codarea");
        area = servicios.NombreAreas(cod_area);

        //String dato = request.getParameter("dato");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        pw.println("<center>"
                + "  <table border='0'>"
                + "      <tr>"
                + "          <td colspan='4' height='30'></td>"
                + "      </tr>"
                + "      <tr>"
                + "          <td><b>Nombre de Área: </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='" + area.getNombreArea() + "' onChange='conMayusculas(this);' > </td>"
                + "          <td width='20'></td>"
                + "      </tr>"
                + "  </table>"
                + "</center>");

    }

    private void cargarArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AreaServices servicios = new AreaServices();
        ArrayList<Area> lista = null;
        String accion = request.getParameter("accion");

        lista = servicios.cargarArea();

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

        pw.println("<select id='cmbArea' class='chosen-select 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).getCodigoArea() + "'>" + lista.get(i).getNombreArea() + "</option>");
        }

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

    private void Actualizar(HttpServletRequest request, HttpServletResponse response) {
        AreaServices servicios = new AreaServices();
        String nombre = request.getParameter("nombre");
        String cod_area = request.getParameter("cod_area");
        servicios.Actualizar(cod_area, nombre);
    }

    private void cantidadUsoArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cod_area = request.getParameter("cod_area");
        AreaServices servicios = new AreaServices();
        int cantidad = servicios.cantidadAreaUso(cod_area);

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        if (cantidad == 0) {
            pw.println("0");
        } else {
            pw.println("1");
        }
    }

    private void consultarAreas(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String codDoc = request.getParameter("codDoc");
        AreaServices servicios = new AreaServices();
        Area area = new Area();
        area = servicios.datosArea(codDoc);
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        if (area.getCodigoArea() > 0) {
            pw.println("<input id='idAreasDocente' type='hidden' value='1' >");
            pw.println("<input id='idAreasSelect' type='hidden' value='" + area.getCodigoArea() + "' >");
            pw.println("<div class='widget-box widget-color-dark'>  "
                    + "                                        <div class='widget-header'>  "
                    + "                                            <h4 class='widget-title'>ÁREA</h4>  "
                    + "                                        </div>  "
                    + "                                        <div class='widget-body'>  "
                    + "                                            <div class='widget-main no-padding'>  "
                    + "                                                <form id='registroAreaFormulario' class='formular' method='post'>  "
                    + "                                                    <fieldset>  "
                    + "                                                        <div class='col-sm-6'>  "
                    + "                                                            <br>  "
                    + "                                                            <div class='alert alert-block alert-success'>  "
                    + "                                                                 <button type='button' class='close' data-dismiss='alert'>  "
                    + "                                                                    <i class='ace-icon fa fa-times'></i>  "
                    + "                                                                </button>  "
                    + "                                                                <strong>ÁREA!</strong> <br> "
                    + "                                                                " + area.getNombreArea() + "  "
                    + "                                                                <br>  "
                    + "                                                            </div>  "
                    + "                                                        </div>  "
                    + "                                                        <div class='col-sm-6'>  "
                    + "                                                            <br>  "
                    + "                                                            <div class='alert alert-warning'>  "
                    + "                                                                <button type='button' class='close' data-dismiss='alert'>  "
                    + "                                                                    <i class='ace-icon fa fa-times'></i>  "
                    + "                                                                </button>  "
                    + "                                                                <strong>NOTA!</strong>  "
                    + "                                                                Seleccione su area perteneciente para poder registrar su disponibilidad horaria.  "
                    + "                                                                <br>  "
                    + "                                                            </div>  "
                    + "                                                        </div>  "
                    + "                                                    </fieldset>  "
                    + "  "
                    + "                                                </form>  "
                    + "                                            </div>  "
                    + "                                        </div>   "
                    + "                                    </div>");
        } else {
            pw.println("<input id='idAreasDocente' type='hidden' value='0' >");
            pw.println("<input id='idAreasSelect' type='hidden' value='0' >");
            pw.println("<div class='widget-box widget-color-dark'>  "
                    + "                                        <div class='widget-header'>  "
                    + "                                            <h4 class='widget-title'>ÁREA</h4>  "
                    + "                                        </div>  "
                    + "                                        <div class='widget-body'>  "
                    + "                                            <div class='widget-main no-padding'>  "
                    + "                                                <form id='registroAreaFormulario' class='formular' method='post'>  "
                    + "                                                    <fieldset>  "
                    + "                                                        <div class='col-sm-6'>  "
                    + "                                                            <label for='form-field-select-3'>¿A qué área asiste? <span style='color: red;'>(*)</span></label>  "
                    + "                                                            <br>  "
                    + "                                                            <span >  "
                    + "                                                                <select id='cmbAreaSelect' class='form-control validate[required]'>  "
                    + "                                                                    <option value=''>[Seleccione]</option>  ");

            pw.println("<option value='1'>MATEMÁTICA</option>");
            pw.println("<option value='2'>COMUNICACIÓN</option>");
            pw.println("<option value='3'>CIENCIAS SOCIALES</option>");
            pw.println("<option value='4'>C.T.A.</option>");

            pw.println("                                                        </select>     "
                    + "                                                            </span>  "
                    + "                                                            <br>  "
                    + "                                                        </div>  "
                    + "                                                        <div class='col-sm-6'>  "
                    + "                                                            <br>  "
                    + "                                                            <div class='alert alert-warning'>  "
                    + "                                                                <button type='button' class='close' data-dismiss='alert'>  "
                    + "                                                                    <i class='ace-icon fa fa-times'></i>  "
                    + "                                                                </button>  "
                    + "                                                                <strong>NOTA!</strong>  "
                    + "                                                                Seleccione su area perteneciente para poder registrar su disponibilidad horaria.  "
                    + "                                                                <br>  "
                    + "                                                            </div>  "
                    + "                                                        </div>  "
                    + "                                                    </fieldset>  "
                    + "                                                    <div class='form-actions center'>  "
                    + "                                                        <button class='btn btn-sm btn-primary' type='button' onclick='validar_seleccion();'>  "
                    + "                                                            <i class='ace-icon fa fa-save icon-on-right bigger-110'></i>  "
                    + "                                                            Seleccionar  "
                    + "                                                        </button>  "
                    + "                                                    </div>  "
                    + "  "
                    + "                                                </form>  "
                    + "                                            </div>  "
                    + "                                        </div>   "
                    + "                                    </div>");
        }

    }

    private void registraAreaDocente(HttpServletRequest request, HttpServletResponse response) {
        String codArea = request.getParameter("cmbAreaSelect");
        String codDocente = request.getParameter("codDocente");
        AreaServices servicios = new AreaServices();
        int dato = 0;
        dato = servicios.registrarAreaDocente(codArea, codDocente);

    }

    private void cargarAreaJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AreaServices servicios = new AreaServices();
        ArrayList<Area> lista = null;

        lista = servicios.cargarArea();

        response.setContentType("application/json");
        PrintWriter pw = response.getWriter();

        JSONArray jArray = new JSONArray(lista);
        pw.print(jArray);
    }

}
