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

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

app.state.ConfigurationSubProfileEdit = function()
{
    var state = app.core.StateManager.createState( 'configuration-subprofile-edit' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent();

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Account',
            'getAccountRelatedData',
            null,
            function( res )
            {
                var profile = new app.model.Profile( res.profile, res.own_teams );
                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'configuration-subprofile-edit',
                        {
                            p : profile
                        }
                    )
                );

                $content.find( '[data-id="btn-add-subprofile"]' ).click( function()
                {
                    var fnAddSubProfile = function()
                    {
                        var $modalContent = app.core.View.getModalContent(),
                            $form = $modalContent.find( '[data-id="form-subprofile"]' ).first(),
                            isValid = app.util.Form.bootstrapValidate( $form );

                        if ( isValid )
                        {
                            app.core.View.closeModal();
                            app.gui.PageLoader.show();
                            app.core.Rpc.call(
                                'Profile',
                                'addSubProfile',
                                {
                                    subProfileName : $form.find( '[data-id="input-subprofile-name"]' ).first().val(),
                                    subProfileType : $form.find( '[data-id="select-subprofile-type"]' ).first().val()
                                },
                                function( res )
                                {
                                    if ( res && res.hasOwnProperty( 'profile' ) )
                                    {
                                        app.model.SessionUser.udpateUserProfile( res.profile );
                                    }
                                    app.core.View.toastSuccess( 'Unterprofil erfolgreich erstellt.' );
                                    app.core.Controller.reload();
                                }
                            );
                        }

                    }.bind( this );

                    app.core.View.showModal(
                        {
                            'title' : 'Neues Unterprofil hinzufügen',
                            'body' : app.core.View.getTemplate( 'configuration-subprofile-modal-form', { sp : null } ),
                            'okButtonText' : "Speichern",
                            'onConfirm' : fnAddSubProfile
                        }
                    );
                });

                $content.find( '[data-type="editSubProfile"]' ).click( function()
                {
                    const childProfileId = $(this).attr( 'data-subprofile-id' ),
                          childProfile = profile.getChildProfileById( childProfileId );

                    var fnUpdateSubprofile = function()
                    {
                        var $modalContent = app.core.View.getModalContent(),
                            $form = $modalContent.find( '[data-id="form-subprofile"]' ).first(),
                            isValid = app.util.Form.bootstrapValidate( $form );

                        if ( isValid )
                        {
                            app.core.View.closeModal();
                            app.gui.PageLoader.show();
                            app.core.Rpc.call(
                                'Profile',
                                'updateSubProfile',
                                {
                                    subProfileId : childProfileId,
                                    subProfileName : $form.find( '[data-id="input-subprofile-name"]' ).first().val(),
                                    subProfileType : $form.find( '[data-id="select-subprofile-type"]' ).first().val()
                                },
                                function( res )
                                {
                                    if ( res && res.hasOwnProperty( 'profile' ) )
                                    {
                                        app.model.SessionUser.udpateUserProfile( res.profile );
                                    }
                                    app.core.View.toastSuccess( 'Unterprofil erfolgreich aktualisiert.' );
                                    app.core.Controller.reload();
                                }
                            );
                        }

                    }.bind( this );

                    app.core.View.showModal(
                        {
                            'title' : 'Unterprofil bearbeiten',
                            'body' : app.core.View.getTemplate( 'configuration-subprofile-modal-form', { sp : childProfile } ),
                            'okButtonText' : "Speichern",
                            'onConfirm' : fnUpdateSubprofile
                        }
                    );
                });

                $content.find( '[data-type="deleteSubProfile"]' ).click( function()
                {

                    const subProfileId = $( this ).attr( 'data-subprofile-id' );
                    app.core.View.confirm(
                        "Soll dieses Unterprofil wirklich gelöscht werden? Alle Profildaten inkl. der Terminzusagen werden unwiderrufbar gelöscht.",
                        function()
                        {

                            app.gui.PageLoader.show();
                            app.core.Rpc.call(
                                'Profile',
                                'deleteSubProfile',
                                {
                                    subProfileId : subProfileId
                                },
                                function( res )
                                {
                                    app.core.View.toastSuccess( 'Unterprofile erfolgreich gelöscht' );
                                    app.core.Controller.reload();
                                }
                            );
                        },
                        "Ja, löschen"
                    );
                    return false;
                });

                $content.find( '[data-id="btn-save"]' ).click( function()
                {
                    let $form = $content.find('[data-id="form-profile"]').first(),
                        birthday = null,
                        teamProps = profile.getTeamProp(),
                        isValid = app.util.Form.bootstrapValidate($form);

                    if (isValid)
                    {
                        app.gui.PageLoader.show();

                        let newChildProfile = null,
                            newChildProfileName = $.trim( $form.find('[data-id="input-new-childprofile-name"]').first().val() ),
                            newChildProfileType = $form.find('[data-id="select-new-chiildprofile-type"]').first().val()

                        if ( newChildProfileName.length > 0 )
                        {
                            newChildProfile = {
                                "name" : newChildProfileName,
                                "type" : newChildProfileType
                            };
                        }

                        app.core.Rpc.call(
                            'Profile',
                            'update',
                            {
                                status : $form.find( '[data-id="input-status"]' ).first().val(),
                                firstName: $form.find('[data-id="input-firstname"]').first().val(),
                                lastName : $form.find('[data-id="input-lastname"]').first().val(),
                                birthday : birthday,
                                childProfiles : [],
                                newChildProfile : newChildProfile,
                                teamProps : teamProps
                            },
                            function( res )
                            {
                                app.gui.PageLoader.hide();
                                app.core.View.toastSuccess( _lc( 'PROFILE_SUCCESSFULLY_UPDATED' ) );
                                app.core.Controller.redirect( '#/configuration/profile' );
                            }
                        );

                    }
                });

                app.gui.PageLoader.hide();

            }
        );
    };

    return state;
};