var settingsScript = settingsScript || function () {
    var isInit = false;
    var rollNo = '';
    var init = function () {
        if (isInit) return;
        isInit = true;
        handler();
    };

    var handler = function () {
        getAppVersion();
        globalScript.initValidatorSimple();

        $('body').on('submit', '#app_version_form', function (e) {
            e.preventDefault();
            if ($('#submit_btn').hasClass('disabled')) {
                return false;
            }
            $('.add_errors').hide();
            $('.add_errors ul').html('');
            var obj = Object.create(globalScript.SAVE_APP_VERSION);
            obj.data = $('#app_version_form').serialize();
            globalScript.ajaxCall(obj).done(function (response) {
                if (response.errors && response.errors.length !== 0) {
                    $('.add_errors').show();
                    _.each(response.errors, function (v, k) {
                        $('.add_errors ul').append('<li>' + v + '</li>');
                    });
                } else if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    toastr.success(response._metadata.message);
                }
            });
        });
    };

    var getAppVersion = function () {
        var obj = Object.create(globalScript.GET_APP_VERSION);
        globalScript.ajaxCall(obj).done(function (response) {
            if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                $('#app_version').val(response.records.app_version);
                setTimeout(function () {
                    globalScript.initValidatorSimple();
                }, 500)
            }
        })
    };

    return {
        'init': init
    }
}();

$(document).ready(function () {
    settingsScript.init();
});
