<?php
require_once("../includes/bootstrap.php");

if (!headers_sent())
    ob_start("ob_gzhandler");

$sesion = new sesion(); // inicio la sesion

$sesion->set('permiso', permisos($_POST['privilegio']));
$permisos = str_split($sesion->get('permiso'));
#var_dump($permisos);
?>



<script  type="text/javascript">

    $(function () {
        var cbjor = $("#txtjornadas"), cbgru = $("#txtgrupo"), cbSedes = $("#txtsedes"),
                gridMatriculas = $("#list_Matriculas"),
                gridListaEstudNoMatric = $("#listado_estudiantes"),
                estmat = [],
                cont = 0;

        colores = ["#2DC6E5", "#57ED91", "#BEE554", "#F49666", " #F7874F", "#75F488", "#E1BBF9"];
        $("#btnPasaIzqaDer, #btnPasaDeraIzq").button();
        $("#btnPasaIzqaDer").on("click", function () {
            var dts = gridListaEstudNoMatric.getGridParam('selarrrow');
            if (dts) {
                var izx = 0
                while (izx < dts.length) {
                    var filC = dts[izx];
                    var retx = gridListaEstudNoMatric.getRowData(filC);
                    gridListaEstudNoMatric.delRowData(filC);

                    gridMatriculas.addRowData(filC, retx);

                    $("#" + filC).css("background-color", colores[retx.color]).removeClass("ui-widget-content");
                    estmat[cont] = filC;
                    gridMatriculas.setCell(filC, 'indice', cont++);

                    izx;
                }

//                
            }

        });
        $("#btnPasaDeraIzq").on("click", function () {
            var dts = gridMatriculas.getGridParam('selarrrow');
            if (dts) {
                var izx = 0
                while (izx < dts.length) {
                    var filC = dts[izx];
                    var retx = gridMatriculas.getRowData(filC);
                    if (!retx.color) {
                        izx++;
                        continue;
                    }

                    gridMatriculas.delRowData(filC);

                    gridListaEstudNoMatric.addRowData(filC, retx);

                    $("#" + filC).css("background-color", colores[retx.color]).removeClass("ui-widget-content");
                    estmat[cont] = filC;
                    gridListaEstudNoMatric.setCell(filC, 'indice', cont++);

                    izx;
                }

//                
            }

        });


        $(document).ready(function () {

            var lastsel3;
            var datos = new Array();
            var cod_grupo = "";
            gridMatriculas.jqGrid({
                caption: 'Estudiantes matriculados',
                headertitles: true,
                height: 415, //tamaño  alto de la gridListaEstudNoMatric
                width: 430,
                shrinkToFit: false,
                url: 'mod_matriculas/list_matriculas.php',
                datatype: "json",
                rowNum: 1500,
                multiselect: true,
                hidegrid: false,
                rowList: [15, 30, 50],
                pager: null,
                sortname: 'apellido1_est',
                viewrecords: true,
                sortorder: "asc",
                toolbar: [true, 'top'],
                colNames: [
                    '<?php echo $dtsLang['globals']['num_doc'] ?>',
                    '<?php echo $dtsLang['mod_matriculas']['estudiante'] ?>',
                    '', ''
                ],
                colModel: [
                    {name: 'numedocumento_est', index: 'numedocumento_est', resizable: true, width: 150, sortable: false},
                    {name: 'nomape_est', index: 'nomape_est', resizable: true, width: 220, search: false, sortable: false},
                    {name: 'color', index: 'color', width: 200, hidden: true},
                    {name: 'indice', index: 'indice', width: 200, hidden: true}
                ],
                ondblClickRow: function (rowid) {
                    var ret = gridMatriculas.getRowData(rowid);
                    if (!ret.color) {
                        jAlert(dtsLang['mod_matriculas']['err_elim_est_mat'], dtsLang['project']['institucion'], "adv");
                        return false;
                    }
                    gridMatriculas.delRowData(rowid);
                    gridListaEstudNoMatric.addRowData(rowid, ret);
                    $("#" + rowid).css("background-color", colores[ret.color])
                            .removeClass("ui-widget-content");
                    delete estmat[ret.indice];
                },
                beforeSelectRow: function (row, e) {
                    var ret = gridMatriculas.getRowData(row);
                    if (!ret.color) {
                        jAlert(dtsLang['mod_matriculas']['err_elim_est_mat'], dtsLang['project']['institucion'], "adv");
                        return false;
                    }
                    return true;
                },
                loadComplete: function () {
                    gridMatriculas.setGridParam({
                        datatype: 'local'
                    });
                }

            });

<?php if (in_array("A", $permisos) && in_array("M", $permisos)) { ?>

                var tdnew = {
                    pag: "t_list_Matriculas",
                    title: '<?php echo $dtsLang['global']['del'] . " " . $dtsLang['mod_matriculas']['matest'] ?>',
                    empresa: '<?php echo $dtsLang['project']['institucion'] ?>',
                    buttonicon: 'ui-btn-save',
                    onClickButton: function () {
                        jConfirm('<?php echo $dtsLang['mod_matriculas']['matest'] ?>', dtsLang.project.empresa, function (r) {
                            if (!r)
                                return  false;
                            $.ajax({
                                url: 'mod_matriculas/opera_matriculas.php',
                                data: {dt: estmat.join(), grupo: cbgru.val()},
                                dataType: 'json',
                                beforeSend: function () {
                                    $.blockUI({message: "Matriculando estudiantes"});
                                },
                                success: function (rta) {
                                    estmat = [];
                                    $.unblockUI();
                                    notifica_ok("Los estudiantes se matricularon correctamente");
                                }
                            });
                        });
                    }
                };
                newBottonGrid(gridMatriculas, tdnew)
<?php } ?>
            var grupo_selec = '';
            gridListaEstudNoMatric.jqGrid({
                caption: 'Estudiantes por matricular',
                headertitles: true,
                height: 415,
                mtype: "GET",
                width: 420,
                url: 'mod_matriculas/list_estudiantes.php',
                datatype: "local",
                hidegrid: false,
                multiselect: true,
                rowNum: 1500,
                rowList: [15, 30, 50],
                pager: null,
                toolbar: [true, 'top'],
                viewrecords: true,
                sortorder: "asc",
                colNames: [
                    '<?php echo $dtsLang['globals']['num_doc'] ?>',
                    '<?php echo $dtsLang['mod_matriculas']['estudiante'] ?>',
                    '',
                    ''
                ],
                colModel: [
                    {name: 'numedocumento_est', index: 'numedocumento_est', resizable: true, width: 150, sortable: false},
                    {name: 'nomape_est', index: 'nomape_est', resizable: true, width: 220, sortable: false, search: false},
                    {name: 'gradoant', index: 'gradoant', width: 200, hidden: true},
                    {name: 'color', index: 'color', width: 200, hidden: true}
                ],
                grouping: true,
                groupingView: {
                    groupField: ["gradoant"],
                    groupCollapse: false,
                    groupText: ['<b>{0} ({1} Estudiantes)</b>'],
                    groupSummary: [false],
                    showSummaryOnHide: false,
                    groupColumnShow: [false],
                },
                ondblClickRow: function (rowid) {
                    var ret = gridListaEstudNoMatric.getRowData(rowid);
                    gridListaEstudNoMatric.delRowData(rowid);
                    gridMatriculas.addRowData(rowid, ret);
                    $("#" + rowid).css("background-color", colores[ret.color])
                            .removeClass("ui-widget-content");
                    estmat[cont] = rowid;
                    gridMatriculas.setCell(rowid, 'indice', cont++);

                    //gridMatriculas.sortGrid('nomape_est',true);
                    // 
                    // var nmat=$("#list_MatGrupos").getCell(cod,'nom_asi');
                    //id:$("#id").val(),
                    // jQuery("#list_MatGrupos").addRowData(rta['id'],newDt);
                },
                loadComplete: function (data) {

                    gridListaEstudNoMatric.setGridParam({
                        datatype: 'local'
                    });

                    try {
                        var cod = data.rows[0].cell[2];
                        var color = 0
                        $.each(data.rows, function (i, item) {
                            if (item.cell[2] !== cod) {
                                color++;
                                cod = item.cell[2];
                            }
                            gridListaEstudNoMatric.setCell(item.id, 'color', color);
                            $("#" + item.id, gridListaEstudNoMatric).css("background-color", colores[color]);
                            $("#" + item.id, gridListaEstudNoMatric).removeClass("ui-widget-content");
                        });
                    } catch (e) {
                    }
                }
            });

            //******************************************************
            cbSedes.change(function () {

                cd_sede = $(this).val();

                if (cd_sede != '0x') {

                    cbjor.attr("disabled", true).addClass("cargando");

                    $.post("includes/cargar_selec_normal.php", {
                        codigo: 'b1',
                        cod_cnds: cd_sede,
                        mopc: 0
                    }, function (dt) {
                        cbjor.html(dt)
                                .attr("disabled", false)
                                .removeClass("cargando")
                                .trigger('change')
                    })

                } else {

                    cbjor.html("<option value='0x'>Seleccionar...</option>")

                    cbgru.html("<option value='0x'>Seleccionar...</option>").trigger('change')

                }

            });
            //---------------------------------------------------------------
            cbgru.change(function () {
                var v = $(this).val()
                if (v != '0x') {
                    busqueda($("option:selected", this).attr("id_grado"), v);
                } else {
                    $("#listado_estudiantes,#list_Matriculas").clearGridData(true);
                    delete $("#listado_estudiantes").jqGrid('getGridParam', 'postData')['grupo'];
                    delete $("#list_Matriculas").jqGrid('getGridParam', 'postData')['grupo'];
                    $("#nomGrupo").text('');
                }

            });
            //----------------------------------------------------------------
            cbjor.change(function () {
                cd_jor = $(this).val();
                if (cd_jor !== '0x') {
                    cbgru.attr("disabled", true).addClass("cargando");
                    cn = "cod_jornada=" + cd_jor;
                    $.post("mod_matriculas/cargar_select_grupos.php", {
                        sede: cd_sede,
                        cmpcnd: '',
                        cons: 1,
                        cd_jor: cd_jor
                    }, function (dt) {
                        cbgru.html(dt)
                                .attr("disabled", false).removeClass("cargando")
                                .trigger('change');
                    });
                }
                else {
                    cbgru.html("<option value='0x'>Seleccionar...</option>");
                }

            });

            //---------------------------------------------------------------------	

            busqueda = function (ide_grado, cod_grupo) {
                if (parseInt(gridListaEstudNoMatric.getGridParam("postData")['grado']) !== parseInt(ide_grado)) {
                    $("#nomGrupo").text($("option:selected", cbgru).parent()[0].label);
                    gridListaEstudNoMatric.setGridParam({
                        postData: {
                            grado: ide_grado
                        }, datatype: 'json'
                    }).trigger("reloadGrid");
                }
                var titulo = $("option:selected", cbgru);
                var t = titulo.parent()[0].label;
                if (t) {

                    t = "<em>" + t + " " + titulo.html() + "</em> - Jornada: <em>" + $("#txtjornadas option:selected").html() + "</em> - Sede: <em>" + $("#txtsedes option:selected").html() + "</em>";

                    gridMatriculas.jqGrid('setCaption', '<?php echo $dtsLang['matriculas']['tituloGrilla'] ?> ' + t);

                }
                gridMatriculas.jqGrid('setGridParam', {postData: {
                        page: 1, grupo: cod_grupo

                    }, datatype: 'json'
                }).trigger("reloadGrid");
            }
        });
    });

</script>



<table align="center">

    <tr><td colspan=3>
            <table>
                <tr clas class="tit1">
                    <td><?php echo $dtsLang['globals']['name_sede'] ?></td>
                    <td><?php echo $dtsLang['globals']['name_jornada'] ?></td>
                    <td><?php echo $dtsLang['globals']['grupos'] ?> &nbsp;<span class="text4" id="nomGrupo"></span></td>
                </tr>
                <tr><td>
                        <select name="txtsedes" class="campos" id="txtsedes" style="width:250px" >
                            <?php
                            $cmps = "cod_sede, initcap(nom_sede) as 'nom_sede'";
                            $ord = 'ORDER BY cod_sede ASC';
                            $tabla = "adm_sedes";
                            $cmpcnd = "";
                            $mselc = 0;
                            $default = '';
                            mostrar_cont_selec($cmps, $tabla, $cmpcnd, $ord, $default, false, '', 0);
                            ?>

                        </select></td>

                    <td>
                        <select name="txtjornadas" class="campos" id="txtjornadas" style="width:160px">
                            <option value="0x">Seleccionar...</option>
                        </select>
                    </td>
                    <td>
                        <select name="txtgrupo" class="campos" id="txtgrupo" style="width:160px">
                            <option value="0x">Seleccionar...</option>
                        </select>
                    </td>
                </tr>
            </table>
        </td></tr>
    <tr>
        <td>
            <table id="listado_estudiantes"></table>
            <div id="pager_estudiantes"></div>	
        </td>
        <td width="20px">
            <button id="btnPasaIzqaDer">>></button>
            <br><br>
            <button id="btnPasaDeraIzq"><<</button>
        </td>
        <td>
            <table id="list_Matriculas"></table>
            <div id="pager_grupos"></div>
        </td>
    </tr>
</table>

