<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="validar.jsp" %>
<!DOCTYPE html>
<html lang="es">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta charset="utf-8" />
        <title>:::SISTEMA DE HORARIOS:::</title>

        <meta name="description" content="overview &amp; stats" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />



        <link rel="stylesheet" href="../assets/css/bootstrap.css" />
        <link rel="stylesheet" href="../assets/css/font-awesome.css" />
        <link rel="stylesheet" href="../assets/css/jquery-ui.css" />
        <link rel="stylesheet" href="../assets/css/ace-fonts.css" />
        <link rel="stylesheet" href="../assets/css/ace.css" class="ace-main-stylesheet" id="main-ace-style" />
        <link href="css/success.css" rel="stylesheet" type="text/css"/>

        <script src="../assets/js/ace-extra.js"></script>



        <!-- inline scripts related to this page -->
        <script type="text/javascript" language="javascript">

            /*$(document).ready( function() {
             jQuery("#registroEncuesta").validationEngine();
             });*/
        </script>
    </head>

    <body class="no-skin" onLoad="cargar();">
        <!-- #section:basics/navbar.layout -->
        <div id="navbar" class="navbar navbar-default">
            <script type="text/javascript">
                try {
                    ace.settings.check('navbar', 'fixed')
                } catch (e) {
                }
            </script>

            <div class="navbar-container" id="navbar-container">
                <%@ include file="banner.jsp" %>
            </div><!-- /.navbar-container -->
        </div>

        <!-- /section:basics/navbar.layout -->
        <div class="main-container" id="main-container">
            <script type="text/javascript">
                try {
                    ace.settings.check('main-container', 'fixed')
                } catch (e) {
                }
            </script>

            <!-- #section:basics/sidebar -->
            <div id="sidebar" class="sidebar                  responsive">
                <script type="text/javascript">
                    try {
                        ace.settings.check('sidebar', 'fixed')
                    } catch (e) {
                    }
                </script>
                <input id="codModulo" type="hidden" value="<%=menu!=null?menu.getCodModulo():""%>">
                <input id="codCategoria" type="hidden" value="<%=menu!=null?menu.getCodCategoria():""%>">
                <%@ include file="menu.jsp" %>
                <!-- #section:basics/sidebar.layout.minimize -->
                <div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
                    <i class="ace-icon fa fa-angle-double-left" data-icon1="ace-icon fa fa-angle-double-left" data-icon2="ace-icon fa fa-angle-double-right"></i>
                </div>
                <!-- /section:basics/sidebar.layout.minimize -->
                <script type="text/javascript">
                    try {
                        ace.settings.check('sidebar', 'collapsed')
                    } catch (e) {
                    }
                </script>
            </div>

            <!-- /section:basics/sidebar -->
            <div class="main-content">
                <div class="main-content-inner">
                    <!-- #section:basics/content.breadcrumbs -->
                    <div class="breadcrumbs" id="breadcrumbs">
                        <script type="text/javascript">
                            try {
                                ace.settings.check('breadcrumbs', 'fixed')
                            } catch (e) {
                            }
                        </script>

                        <ul class="breadcrumb">
                            <li>
                                <i class="ace-icon fa fa-home home-icon"></i>
                                <a href="principal.jsp">INICIO</a>
                            </li>
                            <li class="active">ASIGNACIÓN</li>
                        </ul><!-- /.breadcrumb -->
                    </div>

                    <!-- /section:basics/content.breadcrumbs -->
                    <div class="page-content">

                        <div class="page-header">
                            <h1>
                                Asignación
                                <small>
                                    <i class="ace-icon fa fa-angle-double-right"></i>
                                    Docente a Plana
                                </small>
                            </h1>
                        </div><!-- /.page-header -->

                        <div class="row">





                            <!-- ----------- CONTENEDOR DE FORMULARIOS--------INI----- -->
                            <div class="col-sm-12" style="width:100%;">

                                <div  class="col-sm-3"></div>
                                <div class="col-sm-6">
                                    <div class="widget-box widget-color-blue">
                                        <div class="widget-header">
                                            <h4 class="widget-title">Búsqueda de docentes</h4>
                                        </div>

                                        <div class="widget-body">
                                            <div class="widget-main no-padding">
                                                <form id="formulario" method="post">
                                                    <fieldset>
                                                        <!--                                                        <div class="col-sm-4">
                                                                                                                    <label for="form-field-select-3">Periodo de Horario <span style="color: red;">*</span></label>
                                                                                                                    <div id="divPeriodoH">
                                                                                                                        <select class="form-control validate[required]" id="cmbPeriodoH" onchange="cargarPeriodoEncuesta();
                                                                                                                                ">   
                                                                                                                            <option value="">[Seleccione]</option>
                                                                                                                        </select>
                                                                                                                    </div> 
                                                                                                                </div>-->
                                                        <div class="col-sm-6">
                                                            <label for="form-field-select-3">Área <span style="color: red;">*</span></label>
                                                            <div id="divArea">
                                                                <select class="form-control" id="cmbAula" onchange="limpiar();">   
                                                                    <option value="">[Seleccione]</option>
                                                                </select>
                                                            </div> 
                                                        </div>
                                                        <div class="col-sm-6">
                                                            <label for="form-field-select-3">Apellidos del docente </label>
                                                            <input type="text" id="txt_bus_doc"  class="form-control validate[custom[onlyLetterSp]]"  onchange="conMayusculas(this);"> 
                                                        </div>
                                                        <div class="col-sm-12 hidden-xs" style="height: 10px;"></div>
                                                        <div class="col-sm-12 text-right">
                                                            <span style="color: red;">*</span><label for="form-field-select-3">Campos Obligatorios</label>                                                            
                                                        </div>
                                                    </fieldset>

                                                    <div class="form-actions center">
                                                        <button class="btn btn-sm btn-info" type="button" onclick="validar_campos();">
                                                            <i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
                                                            Buscar
                                                        </button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div  class="col-sm-3"></div>

                            </div>
                            <div class="col-sm-12" style="width:100%; height: 40px;"></div>
                            <div  class="col-sm-12" style="width:100%;">
                                <div  class="col-sm-12">
                                    <!-- -------------TABLA---INI------------ -->
                                    <form id="listarDocenteArea"  method="post">
                                        <div id='divDocente'></div>
                                    </form>
                                    <!-- -------------TABLA---FIN------------ -->
                                </div>
                            </div>
                            <!-- ----------- CONTENEDOR DE FORMULARIOS--------FIN----- -->




                            <!-- ::::::::::-ALERTAS-::::::::::::::: -->
                            <input type="hidden" id="ape">
                            <input id='pagina' type='hidden'>

                            <div id="dialog-noti1" class="hide">
                                <br>
                                Se actualizó con éxito.
                                <br><br>
                            </div>

                            <div id="dialog-confor1" class="hide">
                                <br>
                                ¿Ésta seguro de asignar estas áreas al Docente?
                                <br><br>
                            </div>

                            <div id="dialog-confor2" class="hide">
                                <br>
                                ¿Desea quitar el área asignada al Docente?
                                <br><br>
                            </div>

                            <div id="dialog-advertencia1" class="hide">
                                <br>
                                Debe seleccionar al menos una plana para realizar la asignación.
                                <br><br>
                            </div>

                            <div id="dialog-advertencia2" class="hide">
                                <br>
                                No se pude desvincular del área al docente, debido a que está asignado a un horario.
                                <br><br>
                            </div>


                            <div id="dialog-Alerta2" class="hide">
                                <br>
                                Ocurrió un error en el registro.
                                <br><br>
                            </div>

                            <div id="dialog-edit" class="hide">
                                <br>
                                <form id="RegistroDatos"  method="post">
                                    <div class="sttxt2" id="divRegistroDatos"></div>
                                </form>
                                <br><br>
                            </div>

                            <div id="dialog-cargando" class="hide">
                                <table cellpadding="0" cellspacing="0" width="100%">
                                    <tr><td align="center"><img src="imagenes/cargando.gif" border=0></td></tr>
                                </table>
                            </div>



                            <!-- ::::::::::-ALERTAS-::::::::::::::: -->


                        </div><!-- /.row -->
                    </div><!-- /.page-content -->
                </div>
            </div><!-- /.main-content -->

            <div class="footer">
                <%@ include file="footer.jsp" %>
            </div>

            <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
                <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
            </a>
        </div><!-- /.main-container -->

        <!-- basic scripts -->

        <!--[if !IE]> -->
        <script type="text/javascript">
            window.jQuery || document.write("<script src='../assets/js/jquery.js'>" + "<" + "/script>");
        </script>

        <!-- <![endif]-->

        <!--[if IE]>
<script type="text/javascript">
window.jQuery || document.write("<script src='../assets/js/jquery1x.js'>"+"<"+"/script>");
</script>
<![endif]-->
        <script type="text/javascript">
            if ('ontouchstart' in document.documentElement)
                document.write("<script src='../assets/js/jquery.mobile.custom.js'>" + "<" + "/script>");
        </script>
        <script src="../assets/js/bootstrap.js"></script>

        <!-- page specific plugin scripts -->
        <script src="../assets/js/jquery-ui.js"></script>
        <script src="../assets/js/jquery.ui.touch-punch.js"></script>

        <!--[if lte IE 8]>
          <script src="../assets/js/excanvas.js"></script>
        <![endif]-->
        <script src="../assets/js/jquery-ui.custom.js"></script>
        <script src="../assets/js/jquery.ui.touch-punch.js"></script>
        <!-- ace scripts -->
        <script src="../assets/js/ace/elements.scroller.js"></script>
        <script src="../assets/js/ace/elements.colorpicker.js"></script>
        <script src="../assets/js/ace/elements.fileinput.js"></script>
        <script src="../assets/js/ace/elements.typeahead.js"></script>
        <script src="../assets/js/ace/elements.wysiwyg.js"></script>
        <script src="../assets/js/ace/elements.spinner.js"></script>
        <script src="../assets/js/ace/elements.treeview.js"></script>
        <script src="../assets/js/ace/elements.wizard.js"></script>
        <script src="../assets/js/ace/elements.aside.js"></script>
        <script src="../assets/js/ace/ace.js"></script>
        <script src="../assets/js/ace/ace.ajax-content.js"></script>
        <script src="../assets/js/ace/ace.touch-drag.js"></script>
        <script src="../assets/js/ace/ace.sidebar.js"></script>
        <script src="../assets/js/ace/ace.sidebar-scroll-1.js"></script>
        <script src="../assets/js/ace/ace.submenu-hover.js"></script>
        <script src="../assets/js/ace/ace.widget-box.js"></script>
        <script src="../assets/js/ace/ace.settings.js"></script>
        <script src="../assets/js/ace/ace.settings-rtl.js"></script>
        <script src="../assets/js/ace/ace.settings-skin.js"></script>
        <script src="../assets/js/ace/ace.widget-on-reload.js"></script>
        <script src="../assets/js/ace/ace.searchbox-autocomplete.js"></script>
        <script src="../assets/js/ace/ace.searchbox-autocomplete.js"></script>

        <script src="js/blockui.min.js" type="text/javascript"></script>
        <link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css"/>
        <!--<link rel="stylesheet" href="css/template.css" type="text/css"/>-->


        <!-- inline scripts related to this page -->
        <script type="text/javascript">

            jQuery(function ($) {



            });

            //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

            $(document).ready(function () {
                $("#formulario").validationEngine();
            });



            function segundaCarga() {
//                cargarPeriodoHorario();
                cargarArea();
            }

//            function cargarPeriodoHorario() {
//                var periodo = "<%=periodo!=null?periodo.getCodigoPeriodo():""%>";
//                var accion = "limpiar();";
//                var nombre = "cmbPeriodoH";
//                $.ajax({
//                    type: 'POST',
//                    url: '../ServletReporteEncuesta?Accion=listarPeriodoHorario',
//                    data: 'accion=' + accion + '&nombre=' + nombre + '&periodo=' + periodo,
//                    beforeSend: function () {
//                        cargando();
//                    }, success: function (resultado) {
//                        cerrar_cargando();
//                        $("#divPeriodoH").html(resultado);
//                    }, complete: function () {
//
//                    }
//                });
//            }

            function cargarArea() {
                $.ajax({
                    type: 'POST',
                    url: '../ServletArea?Accion=cargarAreaJson',
                    data: null,
                    datatype: 'application/json',
                    beforeSend: function () {

                    }, success: function (resultado) {
                        var datos = resultado;
                        var cb = "<select class=\"form-control validate[required]\" name=\"cbArea\" onchange=\"limpiar()\" id=\"cbArea\" >";
                        cb += "<option value=\"\">[Seleccione]</option>";
//                        cb += "<option value=\"0\">SIN ÁREA</option>";
                        for (var i = 0; i < datos.length; i++) {
                            cb += "<option value=\"" + datos[i].codigoArea + "\">" + datos[i].nombreArea + "</option>";
                        }
                        cb += "</select>";
                        $("#divArea").html(cb);
                    }, complete: function () {
                    }
                });
            }

            function validar_campos() {
                var estado = $('#formulario').validationEngine('validate');
                if (estado) {
                    listarDocentes(1);
                }
            }

            function listarDocentes(pagina) {
                var codigoArea = $("#cbArea").val();
                var codigoPeriodo = "<%=periodo!=null?periodo.getCodigoPeriodo():""%>";
                var apellidoDocente = $("#txt_bus_doc").val().trim();
                var cantidad = 15;
                $.ajax({
                    type: 'POST',
                    url: '../ServletDocente?Accion=listarDocentePorArea',
                    data: 'codigoArea=' + codigoArea + '&cantidad=' + cantidad + '&pagina=' + pagina + '&codigoPeriodo=' + codigoPeriodo + '&apellidoDocente=' + apellidoDocente,
                    beforeSend: function () {
                        cargando();
                    }, success: function (resultado) {
                        cerrar_cargando();
                        $("#divDocente").html(resultado);
                    }, complete: function () {
                        $("#pagina").val(pagina);
                    }
                });
            }


            function validar_asignar() {
                var cantidad = $('#cantidad').val();
                var codPlanas = $('#codPlanas').val();
                var codigosP = codPlanas.split("#");
                var res = "";
                var resp = "";
                for (var i = 1; i <= cantidad; i++) {
                    for (var j = 0; j < codigosP.length; j++) {
                        resp = $("input[name='" + codigosP[j] + "_docente_" + i + "']:checked").val();
                        if (resp != "" && resp != undefined) {
                            if (res == "") {
                                res += resp;
                            } else {
                                res += "#" + resp;
                            }
                        }
                        resp = "";
                    }
                }

                if (res == "") {
                    alerta1();
                } else {
                    alerta2(res);
                }
            }

            function alerta1() {
                var mensaje = "Debe seleccionar al menos una plana para realizar la asignación.";
                bootbox.dialog({
                    headerClass: "modal-bg-warning",
                    title: "<i class='ace-icon fa fa-exclamation-triangle  white'></i> <span class='white'>Advertencia</span>",
                    size: "small",
                    message: mensaje,
                    buttons: {
                        si: {
                            label: "<i class='fa fa-check bigger-110'></i>&nbsp; Aceptar",
                            className: "btn-warning  btn-sm",
                            callback: function () {
                            }
                        }
                    }
                });
            }


            function alerta2(res) {
                dialogo = bootbox.dialog({
                    headerClass: "modal-bg-primary",
                    title: "<i class='ace-icon fa fa-check-circle white'></i> <span class='white'>Conformidad</span>",
                    size: "small",
                    message: "¿Está seguro de asignar la(s) plana(s)?",
                    buttons: {
                        si: {
                            label: "<i class='fa fa-check bigger-110'></i>&nbsp; Si",
                            className: "btn-primary  btn-sm",
                            callback: function () {
                                registrarAsignacion(res);
                            }
                        }, no: {
                            label: "<i class='fa fa-times bigger-110'></i>&nbsp; No",
                            className: "btn-danger  btn-sm",
                            callback: function () {

                            }
                        }
                    }
                });
            }

            function registrarAsignacion(res) {
                var codigoPeriodo = "<%=periodo!=null?periodo.getCodigoPeriodo():""%>";
                $.ajax({
                    type: 'POST',
                    url: '../ServletDocente?Accion=registrarPlanaDocente',
                    data: 'datos=' + res + '&codigoPeriodo=' + codigoPeriodo,
                    beforeSend: function () {
                        cargando();
                    }, success: function (resultado) {
                        cerrar_cargando();
                        notificacionRegistrarAsignacion();
                    }, complete: function () {

                    }
                });


            }

            function notificacionRegistrarAsignacion() {
                bootbox.dialog({
                    headerClass: "modal-bg-success",
                    title: "<i class='ace-icon fa fa-tags white'></i> <span class='white'>Notificación</span>",
                    size: "small",
                    message: "La asignación se realizó con éxito.",
                    buttons: {
                        si: {
                            label: "<i class='fa fa-check bigger-110'></i>&nbsp; Aceptar",
                            className: "btn-success btn-sm",
                            callback: function () {
                                var pagina = $("#pagina").val();
                                listarDocentes(pagina);
                            }
                        }
                    }
                });
            }


            function verificarCruces(codigoPlana, codigoDocente, dato, cont) {
                var codigoArea = $("#cbArea").val();
                var codigoPeriodo = "<%=periodo!=null?periodo.getCodigoPeriodo():""%>";
                $.ajax({
                    type: 'POST',
                    url: '../ServletDocente?Accion=verificarCrucesPorPeriodo',
                    data: 'codigoArea=' + codigoArea + '&codigoDocente=' + codigoDocente + '&codigoPeriodo=' + codigoPeriodo,
                    success: function (resultado) {
                        ;
                        if (resultado > 0) {
                            alerta4();
                        } else {
                            alerta5(codigoPlana, codigoDocente, dato, cont);
                        }
                    }, complete: function () {
                    }
                });
            }

            function alerta4() {
                var mensaje = "No se pude desvincular de la plana al docente, debido a que está asignado a un horario.";
                bootbox.dialog({
                    headerClass: "modal-bg-warning",
                    title: "<i class='ace-icon fa fa-exclamation-triangle  white'></i> <span class='white'>Advertencia</span>",

                    message: mensaje,
                    buttons: {
                        si: {
                            label: "<i class='fa fa-check bigger-110'></i>&nbsp; Aceptar",
                            className: "btn-warning  btn-sm",
                            callback: function () {
                            }
                        }
                    }
                });
            }

            function alerta5(codigoPlana, codigoDocente, dato, cont) {
                bootbox.dialog({
                    headerClass: "modal-bg-primary",
                    title: "<i class='ace-icon fa fa-check-circle white'></i> <span class='white'>Conformidad</span>",
                    size: "small",
                    message: "¿Está seguro de deseleccionar la plana?",
                    buttons: {
                        si: {
                            label: "<i class='fa fa-check bigger-110'></i>&nbsp; Si",
                            className: "btn-primary  btn-sm",
                            callback: function () {
                                quitarPlanaSeleccion(codigoPlana, codigoDocente, dato, cont);
                            }
                        }, no: {
                            label: "<i class='fa fa-times bigger-110'></i>&nbsp; No",
                            className: "btn-danger  btn-sm",
                            callback: function () {

                            }
                        }
                    }
                });
            }

            function quitarPlanaSeleccion(codigoPlana, codigoDocente, dato, cont) {
                var codigoPeriodo = "<%=periodo!=null?periodo.getCodigoPeriodo():""%>";
                $("#div_" + codigoPlana + "_" + codigoDocente + "").html("<input type='checkbox' name='" + dato + "_docente_" + cont + "' value='" + codigoPlana + "_" + codigoDocente + "'  />");
                $.ajax({
                    type: 'POST',
                    url: '../ServletDocente?Accion=quitarPlanaDocente',
                    data: 'codigoPlana=' + codigoPlana + '&codigoDocente=' + codigoDocente + '&codigoPeriodo=' + codigoPeriodo,
                    beforeSend: function () {
                        cargando();
                    }, success: function (resultado) {
                        cerrar_cargando();
                        if (resultado > 0) {
                            notificacionQuitarPlanaSeleccion(codigoPlana, codigoDocente, dato, cont);
                        } else {
                            alert("error");
                        }
                    }, complete: function () {

                    }
                });
            }

            function notificacionQuitarPlanaSeleccion(codigoPlana, codigoDocente, dato, cont) {
                bootbox.dialog({
                    headerClass: "modal-bg-success",
                    title: "<i class='ace-icon fa fa-tags white'></i> <span class='white'>Notificación</span>",
                    size: "small",
                    message: "Se eliminó la asignación con éxito.",
                    buttons: {
                        si: {
                            label: "<i class='fa fa-check bigger-110'></i>&nbsp; Aceptar",
                            className: "btn-success btn-sm",
                            callback: function () {
                                $("#div_" + codigoPlana + "_" + codigoDocente + "").html("<input type='checkbox' name='" + dato + "_docente_" + cont + "' value='" + codigoPlana + "_" + codigoDocente + "'  />");
                            }
                        }
                    }
                });
            }

            function limpiar() {
                $("#divDocente").html("");
            }
            //-------------------------------

            function cargando() {
                $.blockUI({
                    message: '<div class="thecube"> <div class="cube c1"></div> <div class="cube c2"></div> <div class="cube c4"></div> <div class="cube c3"></div> </div>',
//        message: '<div class="loading" id="g-spinner" style="zoom: 1;"><div class="circle c1"></div><div class="circle c2"></div><div class="circle c3"></div><div class="circle c4"></div></div>',
//        message: '<div class="js-loading-indicator triple" style="width: 192px; height: 192px; top: 64px; left: 208px;"></div>',

                    baseZ: 9000,
                    overlayCSS: {
                        backgroundColor: '#1b2024',
                        opacity: 0.8,
                        cursor: 'wait'
                    },
                    css: {
                        zindex: '9999',
                        border: 0,
                        color: '#fff',
                        padding: 0,
                        backgroundColor: 'transparent'
                    }
                });
            }

            function cerrar_cargando() {
                $.unblockUI();
            }

            function conMayusculas(field) {
                field.value = field.value.toUpperCase();
            }

            function enter(e) {
                tecla = (document.all) ? e.keyCode : e.which;
                if (tecla == 13)
                    validar_busqueda();
            }

            $('input').keypress(function (e) {
                if (e.which == 13) {
                    return false;
                }
            });






        </script>

        <!-- the following scripts are used in demo only for onpage help and you don't need them -->

        <link rel="stylesheet" href="../assets/css/ace.onpage-help.css" />
        <link rel="stylesheet" href="../docs/assets/js/themes/sunburst.css" />

        <script type="text/javascript"> ace.vars['base'] = '..';</script>
        <script src="../assets/js/ace/elements.onpage-help.js"></script>
        <script src="../assets/js/ace/ace.onpage-help.js"></script>
        <script src="../docs/assets/js/rainbow.js"></script>
        <script src="../docs/assets/js/language/generic.js"></script>
        <script src="../docs/assets/js/language/html.js"></script>
        <script src="../docs/assets/js/language/css.js"></script>
        <script src="../docs/assets/js/language/javascript.js"></script>

        <script src="js/bootbox.min.js" type="text/javascript"></script>

        <script src="js/languages/jquery.validationEngine-es.js" type="text/javascript" charset="utf-8"></script>
        <script src="js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>

</html>
