/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.ConfigurationAttendanceLog = function()
{
    var state = app.core.StateManager.createState( 'configuration-attendance-log' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent(),
            groups = [],
            playerHistoryXhr = null,
            profile = app.model.SessionUser.getUserProfile(),
            fnUpdateAppointmentLog = function()
            {
                if ( !profile )
                {
                    return;
                }

                if ( playerHistoryXhr )
                {
                    playerHistoryXhr.abort();
                }

                playerHistoryXhr = app.core.Rpc.call(
                    'Team',
                    'getProfileAppointmentLog',
                    {
                        groupId : $content.find( '[data-id="select-team-id"]' ).first().val(),
                        profileId : profile.getId(),
                        startDt : $content.find( '[data-id="input-appointment-log-from"]' ).first().val(),
                        endDt : $content.find( '[data-id="input-appointment-log-until"]' ).first().val()
                    },
                    function( res)
                    {
                        $content.find( '[data-id="container-appointment-log"]' ).first().html(
                            app.core.View.getTemplate(
                                'group-member-management-body-appointment-log',
                                {
                                    logs : res.appointmentLog
                                }
                            )
                        );
                    },
                    function()
                    {
                        // Do nothing.
                    }
                );
            };

        app.gui.PageLoader.show();

        app.core.View.setContent(
            app.core.View.getTemplate(
                'configuration-attendance-log',
                {
                    p : profile,
                    groups : app.model.SessionUser.getGroups()
                }
            )
        );

        $content.on( 'click', '[data-type="appointment-log-item"]', function()
        {
            app.core.Controller.redirect( '#/appointment/' + $(this).attr( 'data-appointment-id' ) );
        });

        $content.on( 'click', '[data-id="btn-update-appointment-log"]', function()
        {
            app.core.View.setContentLoader(
                $content.find( '[data-id="container-appointment-log"]' ).first()
            );
            fnUpdateAppointmentLog();
        });

        app.gui.PageLoader.hide();

        fnUpdateAppointmentLog();
    };

    return state;
};