$(function() {
    var dtsGrafs = null;
    var tbsInfo = $("#tabsInfoEstd");

    $("#lyDtsEstud").layout();
    var tbEstud = $('#tabDtsEstud');
    var idEst = null;
    var nper = "";
    var tipUser;
    var cod_docente;

    var nreg = 0;
    $("#acorPerdiodos").accordion();
    $('#wndGrafs').window({
        title: 'Grafico',
        resizable: false,
        modal: true,
        maximized: false,
        closed: true,
        minimizable: false,
        iconCls: 'icon-grafico'
    });
    $(document).ready(function() {
        var tipUser = parseInt($("#codtpu").val());
        var cod_docente = $("#rusid").val();
        nper = $("#txtnumper").val();
        if (tipUser == 7) {
            var dtest = $.parseJSON($("#dtsEstud").val());
            $("#dtsEstud").remove();
            $.ajax({
                url: "mod_info_estudiante/template_estudiante.php",
                data: {id: dtest.cd_est, nper: nper, cdgrup: dtest.cod_grupo},
                beforeSend: function() {
                    $("*", tbEstud).remove();
                },
                success: function(rta) {
                    tbEstud.html(rta);
                    $("#tabsInfoEstd").tabs();
                }
            })
        }
        var gridEstudInfo = $("#tbDtsEst").jqGrid({
            caption: '',
            height: 'auto',
            width: 185,
            url: '',
            datatype: "local",
            rowNum: 30,
            hidegrid: false,
            pager: null,
            viewrecords: false,
            sortorder: "asc",
            shrinkToFit: true,
            pgbuttons: false,
            pginput: false,
            colNames: ['Id', 'Estudiantes'],
            colModel: [
                {name: "id", index: "id", sortable: false, editable: false, resizable: false, hidden: true},
                {name: 'nombre', index: 'nombre', sortable: false, editable: true, resizable: false, classes: 'opera'}
            ],
            onSelectRow: function(row) {
                idEst = row;
                var dt = gridEstudInfo.getRowData(idEst);
                $.ajax({
                    url: "mod_info_estudiante/template_estudiante.php",
                    data: {id: dt.id, nper: nper, cdgrup: $("#md_n_txtgrupo").val()},
                    beforeSend: function() {
                        $("*", tbEstud).remove();
                    },
                    success: function(rta) {
                        tbEstud.html(rta);
                        $("#tabsInfoEstd").tabs();
                    }
                })
            }
        });
        $("tr[role=rowheader]").hide();


        if (tipUser != 7) {
            /*----------------------------------------------------------------------------------------------------*/
            $("#md_n_txtsedes").change(function() {
                cd_sede = $(this).val();
                if ($.trim(cod_docente).length == 0) {
                    cdcn = "a28";
                    cnd = cd_sede + "|";
                } else {
                    cdcn = "a25";
                    cnd = cod_docente + "|" + cd_sede;
                }
                $.ajax({
                    type: "POST",
                    url: "includes/cargar_selec_normal.php",
                    data: {cod_cnds: cnd, codigo: cdcn, mopc: '0'},
                    beforeSend: function() {
                        $.messager.progress({
                            title: dtsLang["globals"]["tit_msg_espere"],
                            msg: dtsLang["globals"]["msg_load_jornadas"],
                            interval: 100,
                        });
                    },
                    success: function(dt) {
                        $("#md_n_txtjornadas").html(dt);
                        $.messager.progress('close');
                        $("#md_n_txtjornadas").trigger("change");
                    },
                });
            }).trigger("change");

            /*----------------------------------------------------------------------------------------------------*/

            $("#md_n_txtjornadas").change(function() {
                var jor = $(this).val();
                $.ajax({
                    type: "POST",
                    url: "mod_notas_opt/cargar_select_grupos.php",
                    data: {sede: $("#md_n_txtsedes").val(), jornada: jor},
                    beforeSend: function() {
                        $.messager.progress({
                            title: dtsLang["globals"]["tit_msg_espere"],
                            msg: dtsLang["globals"]["msg_load_grupos"],
                            interval: 100,
                        });
                    },
                    success: function(rta) {
                        $("#md_n_txtgrupo").html(rta);
                        $.messager.progress('close');
                        $("#md_n_txtgrupo").trigger("change");
                    }
                });
            });

            /*----------------------------------------------------------------------------------------------------*/

            $("#md_n_txtgrupo").change(function() {
                cd_grupo = $(this).val();
                if ($.trim(cod_docente).length == 0) {
                    cdcn = "a29";
                    cnd = cd_grupo + "|";
                } else {
                    cdcn = "a26";
                    cnd = cd_grupo + "|" + cod_docente;
                }

                gridEstudInfo.clearGridData();
                $.ajax({
                    url: 'mod_info_estudiante/list_estudiantes.php',
                    type: "POST",
                    dataType: "json",
                    data: {
                        cdgrup: cd_grupo,
                    },
                    success: function(rta) {
                        for (v in rta['est']) {
                            gridEstudInfo.addRowData(rta['est'][v].id, rta['est'][v]);
                        }
                        nper = rta['nv'];
                    }
                });
            })
        }
    });
});
var cnfGraf;
var chart = null;
function viewGrafico(periodo) {
    cnfGraf = {
        chart: {
            renderTo: 'contGrafs',
            type: 'bar'
        },
        title: {
            text: 'REPRESENTACIÓN GRAFICA DE LAS NOTAS POR MATERIA'
        },
        subtitle: {
            text: ''
        },
        xAxis: {
            min: 0,
            minRange: 2,
            categories: [],
            labels: {
                align: 'right',
                style: {
                    fontSize: '10px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Valor numérico de la nota'
            }
        },
        legend: {
            enabled: false
        },
        tooltip: {
            formatter: function() {
                return '' +
                        this.x + ': ' + this.y + '';
            }
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [],
    };
    cnfGraf.xAxis.categories = dtsGrafs[periodo]['cat'];
    cnfGraf.series.push({name: 'xx', data: dtsGrafs[periodo]['dts']});

    $('#wndGrafs').window('open');
    if (chart)
        chart.destroy();
    chart = new Highcharts.Chart(cnfGraf);
}

function verDtllsNts(id, nom_mat) {
    var id = "#dtllsNotas" + id;
    var conten = $(id);
    $.messager.progress({
        title: dtsLang["globals"]["tit_msg_espere"],
        msg: dtsLang["globals"]["msg_load_grupos"],
        interval: 150,
    });
    var dts = $.parseJSON(conten.text());
    var cad = '<table class="table table-bordered table-condensed">';
    for (x in dts) {
        var dtsNotas = dts[x]['notas'];
        cad += '<tr>';
        cad += '<td><strong>Desempeño: </strong>' + dts[x]['nomdesempenio'] + '</td>';
        cad += '<td><strong>Descripcion: </strong>' + dts[x]['descsaber'] + '</td>';
        cad += '<td><strong></strong></td>';
        cad += '</tr>';
        cad += '<tr><td colspan="3">';
        cad += '<table class="griddetalles" class="table table-bordered table-condensed">';
        cad += '<thead>';
        cad += '</thead><tbody>';
        for (xn in dtsNotas) {
            cad += '<tr>';
            cad += '<td>' + dtsNotas[xn]['nn'] + '</td>';
            cad += '<td>' + dtsNotas[xn]['fn'] + '</td>';
            cad += '<td>' + dtsNotas[xn]['detn'] + '</td>';

            cad += '<td>' + dtsNotas[xn]['dn'] + '</td>';
            cad += '<td>' + dtsNotas[xn]['dl'] + '</td>';

            cad += '<td>' + dtsNotas[xn]['rn'] + '</td>';
            cad += '<td>' + dtsNotas[xn]['rl'] + '</td>';
            cad += '<td>' + dtsNotas[xn]['rf'] + '</td>';
            cad += '<td>' + dtsNotas[xn]['ro'] + '</td>';
            cad += '</tr>';
        }
        cad += '</tbody></table>';
        cad += '</td></tr>';
        cad += '<tr><td colspan="3">&nbsp;</td></tr>';
    }
    ;

    cad += '</table>';

    $('#wndDetNotas').window({
        width: 1000,
        height: 500,
        content: cad,
        title: nom_mat,
        resizable: false,
        modal: true,
        maximized: false,
        minimizable: false,
    });
    $(".griddetalles").datagrid({
        nowrap: false,
        singleSelect: true,
        columns: [[
                {field: 'nomn', title: 'Nombre', width: 150, rowspan: 2},
                {field: 'fecn', title: 'Fecha', width: 90, rowspan: 2},
                {field: 'code', title: 'Descripción', width: 150, rowspan: 2},
                {title: 'Informacion Notas', colspan: 2},
                {title: 'Informacion Recuperación', colspan: 4}
            ], [
                {field: 'notorg', title: 'Nota', width: 65, align: 'right'},
                {field: 'notlet', title: 'Letra', width: 65},
                {field: 'nrec', title: 'Nota', width: 50},
                {field: 'nrecl', title: 'Letra', width: 50},
                {field: 'nfrec', title: 'Fecha', width: 90},
                {field: 'norec', title: 'Observacion', width: 190},
            ]]
    });
    $.messager.progress('close');
}