{# @copyright   Copyright (C) 2010-2024 Combodo SARL #}
{# @license     http://opensource.org/licenses/AGPL-3.0 #}

// Personal informations form
var oContactFormFieldSet = $('#{{ oContactForm.id }} > .form_fields').field_set({{ oContactForm.fieldset|json_encode()|raw }});
$('#{{ oContactForm.id }}').portal_form_handler({
    formmanager_class: "{{ oContactForm.formmanager_class|escape('js') }}",
    formmanager_data: {{ oContactForm.formmanager_data|json_encode()|raw }},
    field_set: oContactFormFieldSet,
    endpoint: "{{ oContactForm.renderer.GetEndpoint()|raw }}"
});

// Preferences form
var oPreferencesFormFieldSet = $('#{{ oPreferencesForm.id }} > .form_fields').field_set({{ oPreferencesForm.fieldset|json_encode()|raw }});
$('#{{ oPreferencesForm.id }}').portal_form_handler({
    formmanager_class: "{{ oPreferencesForm.formmanager_class|escape('js') }}",
    formmanager_data: {{ oPreferencesForm.formmanager_data|json_encode()|raw }},
    field_set: oPreferencesFormFieldSet,
    endpoint: "{{ oPreferencesForm.renderer.GetEndpoint()|raw }}"
});

{% if oPasswordForm is not null %}
// Password form
var oPasswordFormFieldSet = $('#{{ oPasswordForm.id }} > .form_fields').field_set({{ oPasswordForm.fieldset|json_encode()|raw }});
$('#{{ oPasswordForm.id }}').portal_form_handler({
    formmanager_class: "{{ oPasswordForm.formmanager_class|escape('js') }}",
    formmanager_data: {{ oPasswordForm.formmanager_data|json_encode()|raw }},
    field_set: oPasswordFormFieldSet,
    endpoint: "{{ oPasswordForm.renderer.GetEndpoint()|raw }}"
});
{% endif %}

// Picture form
// - JQuery upload widget
$('#picture-form #picture').fileupload({
            dataType: 'json',
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent)
        })
        .on('fileuploadsend', function(oEvent, oData){
            $('.user_profile_picture .form_alerts .alert-error').hide()
            $('#page_overlay .overlay_content .content_loader').clone().prependTo('.user_profile_picture .panel-body');
        })
        .on('fileuploadalways', function(oEvent, oData){
            $('.user_profile_picture .content_loader').remove();
        })
        .on('fileuploaddone', function(oEvent, oData){
            if( (oData._response.result.form !== undefined) && (oData._response.result.form.validation.valid === true) )
            {
                // Retrieving picture url
                var sPictureUrl = oData._response.result.form.picture_url;
                // Replacing form preview image
                $('#picture-form .preview img').attr('src', sPictureUrl);
                // Replacing menu image
                $('#topbar .user_photo, #sidebar .user_photo').css('background-image', 'url("' + sPictureUrl + '")');
            }
        })
        .on('fileuploadfail', function(oEvent, oData){
            if( (oData._response.jqXHR.responseJSON !== undefined) && (oData._response.jqXHR.responseJSON.error_message !== undefined) )
            {
                $('.user_profile_picture .form_alerts .alert-error').show().text(oData._response.jqXHR.responseJSON.error_message);
            }
        });
// - Undo button
/*$('#user-profile-wrapper .actions .btn_undo').on('click', function(oEvent){
    //console.log('Picture undo trigger');
});*/
// - Reset button
$('#user-profile-wrapper .actions .btn_reset').on('click', function(oEvent){
    //console.log('Picture reset trigger');
});

// Submit button
$('#user-profile-wrapper .form_buttons .form_btn_submit').off('click').on('click', function(oEvent){
    oEvent.preventDefault();

    // Resetting feedback
    $('#user-profile-wrapper .form_alerts .alert').hide();
    $('#user-profile-wrapper .form_alerts .alert > p').remove();
    $('#user-profile-wrapper .form_field').removeClass('has-error');
    $('#user-profile-wrapper .form_field .help-block > p').remove();

    // Submiting contact form through AJAX
    $('#{{ oContactForm.id }}').portal_form_handler('submit', oEvent);

    // Submiting preferences form through AJAX
    $('#{{ oPreferencesForm.id }}').portal_form_handler('submit', oEvent);

    {% if oPasswordForm is not null %}
    // Submiting password form through AJAX
    // Only if fields are filled
    $('#{{ oPasswordForm.id }} :password').each(function(iIndex, oElem){
        if($(oElem).val() !== '')
        {
            $('#{{ oPasswordForm.id }}').portal_form_handler('submit', oEvent);
            return false;
        }
    });
    {% endif %}
});