/* Minification failed. Returning unminified contents.
(1,1): run-time error CSS1019: Unexpected token, found '('
(1,11): run-time error CSS1031: Expected selector, found '('
(1,11): run-time error CSS1025: Expected comma or open brace, found '('
(82,2): run-time error CSS1019: Unexpected token, found ')'
(82,3): run-time error CSS1019: Unexpected token, found '('
(82,10): run-time error CSS1031: Expected selector, found ')'
(82,10): run-time error CSS1025: Expected comma or open brace, found ')'
 */
(function (window) {
    function RegisterRequesterController(options) {
        this._setOptions(options || {});
        this._init();
    }

    RegisterRequesterController.prototype = {

        _setOptions: function (options) {
            this.Url = options.Url;
            this.Model = options.Model;
            this.DefaultType = options.DefaultType;
        },

        _init: function () {
            this.form = $('#registerRequestUser');
            this.submitButton = this.form.closest('section').find('[data-type="submitForm"]');

            this.form.on('change', '[name="Type"]', $.proxy(this.onChangeType, this));
            this.submitButton.on('click', $.proxy(this.onButtonClick, this));

            this.form.find('[name="Type"]').val(this.DefaultType).trigger('change');
        },

        /* Events */

        onChangeType: function (e) {
            var url = this.Url.RequesterInfo.replace('{type}', e.currentTarget.value);

            $.ajax({
                type: 'GET',
                url: url,
                beforeSend: $.proxy(function () {
                    waitSpinner('show', 'Загрузка данных');
                }, this),
                success: $.proxy(function (content) {
                    waitSpinner('hide');
                    this.form.find('#requesterInfo').html(content);

                    for (var modelField in this.Model) {
                        if (this.Model.hasOwnProperty(modelField)) {
                            var value;

                            switch (modelField) {
                                case 'CertificateIssueDate':
                                case 'PassportIssueDate': {
                                    value = moment(new Date(this.Model[modelField])).format('DD.MM.YYYY');
                                    break;
                                }

                                default: {
                                    value = this.Model[modelField];
                                }
                            }

                            this.form.find('#requesterInfo [name="' + modelField + '"]').val(value);
                        }
                    }

                    setTimeout($.proxy(function () {
                        initControls(this.form.find('#requesterInfo'));

                        this.form.find('#requesterInfo .datepicker').each(function () {
                            $(this).datepicker();
                        });
                    }, this), 500);
                }, this),
                error: $.proxy(function (xhr) {
                    waitSpinner('hide');
                    showError(xhr, xhr.statusText);
                }, this)
            });
        },

        onButtonClick: function (e) {
            this.form.submit();
        }
    };

    window.RegisterRequesterController = RegisterRequesterController;

})(window);
