﻿var ScheduleSubject = function () {
      
    this._url = null;
    this._subjectId = null;

    //------- Init
    this.init = function (url, subjectId) {
        this._url = url;
        this._subjectId = subjectId;
    }

    //------- Init options
    this.setOptionsData = function (controlId, lstData, valueDefault, hasAll) {
        //Empty
        $(controlId).empty();
        //All
        if (hasAll) {
            $(controlId).append($('<option>', { value: "", text: "Todos" }));
        }
        //For
        $.each(lstData, function (index, value) {
            $(controlId).append($('<option>', { value: value, text: value }));
        });
        //Select value
        if (valueDefault != null) {
            $(controlId).val(valueDefault);
        }
    }

    //------- Load schedule
    this.loadSchedule = function() {

        let self = this;
        let selectedGroup = $("#optionsGroup").val();
        var activeSemesterElement = $("#tabSemester li.active");
        var activeSemester = activeSemesterElement.attr("data-semestre");

        $("#loading").show();

        $.ajax({
            url: self._url,
            type: "GET",
            data: {
                "id": self._subjectId,
                "group": selectedGroup,
                "semester": activeSemester
            },
            success: function (response) {
                self.setSchedule(response);
                $("#loading").hide();
            },
            error: function (xhr, status, error) {
                console.error("Error al cargar la página:", error);
                $("#loading").hide();
                $("#divTable").empty();
                $("#divTable").append("Se ha producido un error.Inténtelo de nuevo más tarde");
            }
        });
    }

    //------- Set schedule
    this.setSchedule = function (data) {

        let divId = "#divTable";
        $(divId).empty();

        //#Data
        let tableStr = '';
        if (data.LstSchedule != null && data.LstSchedule.length > 0) {
            tableStr = '' +
                '<table class="asignaturas horarios"' +
                '<thead>' +
                '    <tr>' +
                '        <th>Fecha</th>' +
                '        <th>Hora</th>' +
                '        <th>Aula</th>' +
                '        <th>Profesor</th>' +
                '        <th>Grupo</th>' +
                '    </tr> '
            '</thead>';
            tableStr += '<tbody>';
            //Schedule
            $.each(data.LstSchedule, function (i, schedule) {
                //Subjects
                $.each(schedule.LstSubjectModel, function (i, itemSubject) {
                    tableStr += '    <tr>';

                    tableStr += '    <td>';
                    tableStr += '    <span class="lugar"><strong>' + itemSubject.DayOfWeekName + '</strong>' + ' ' + schedule.DateStr + '</span>';
                    tableStr += '    </td>';

                    tableStr += '    <td>';
                    tableStr += '        ' + itemSubject.StartEndStr;
                    tableStr += '    </td> ';

                    tableStr += '    <td>';
                    tableStr += '    <span class="lugar"><strong>' + itemSubject.Rooms + '</span>';
                    tableStr += '    </td>';

                    tableStr += '    <td>';
                    tableStr += '        ' + itemSubject.Teachers;
                    tableStr += '    </td>';

                    tableStr += '    <td>';
                    tableStr += '      ' + itemSubject.Groups;
                    tableStr += '    </td>';

                    tableStr += '    </tr>';
                });
            });
            tableStr += '</tbody>';
            tableStr += '</table>';
        }
        else {
            tableStr = '<div style="color: red; font-weight: bold;">' + data.MsgInfo + '</div>';
        }
        //#DIV
        $(divId).append(tableStr);
    }

    //------- Loading
    this.loading = function (load) {
        if (load) {
            $("#loading").show();
        } else {
            $("#loading").hide();
        }
    }

    //------- Load
    this.load = function(data) {
        this.loading(true);
        this.setSchedule(data);
        this.setOptionsData("#optionsGroup", data.LstGroups, null, true);
        this.loading(false);
    }
}

