﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelFormError = function(config) {
    config = config || {};

    Ext.apply(config, {
        items: {
            layout: 'anchor',
            boxMinWidth: 700,
            defaults: {
                anchor: '-50'
            },
            bodyStyle: { padding: '5px' },
            border: false,
            autoScroll: true,
            items: [{
                xtype: 'fieldset',
                title: 'Avsender',
                labelWidth: 120,
                defaults: {
                    xtype: 'textfield',
                    blankText: 'Feltet er påkrevd',
                    allowBlank: false,
                    msgTarget: 'side',
                    anchor: '-10'
                },
                items: [{
                    id: 'error_organisation',
                    fieldLabel: 'Organisasjon'
                }, {
                    id: 'error_contactperson',
                    fieldLabel: 'Kontaktperson'
                }, {
                    id: 'error_email',
                    fieldLabel: 'E-post adresse',
                    regex: new RegExp('\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\\b', 'i'),
                    regexText: 'E-post adressen er ikke gyldig'
                }]
            }, {
                xtype: 'fieldset',
                id: 'error_selection',
                title: 'Feiltype',
                labelWidth: 120,
                items: {
                    xtype: 'radiogroup',
                    id: 'error_reason',
                    blankText: 'Velg en feiltype',
                    allowBlank: false,
                    msgTarget: 'side',
                    anchor: '-10',
                    columns: 1,
                    vertical: true,
                    items: [{
                        boxLabel: 'Feil i programmet',
                        name: 'error_type',
                        inputValue: 'Feil i programmet'
                    }, {
                        boxLabel: 'Feil i innhold (tekst)',
                        name: 'error_type',
                        inputValue: 'Feil i innhold (tekst)'
                    }, {
                        boxLabel: 'Feil i søk (manglende tilslag)',
                        name: 'error_type',
                        inputValue: 'Feil i søk (manglende tilslag)'
                    }, {
                        boxLabel: 'Ønske om forbedring',
                        name: 'error_type',
                        inputValue: 'Ønske om forbedring'
                    }, {
                        boxLabel: 'Annet',
                        name: 'error_type',
                        inputValue: 'Annet'
                    }]
                }
            }, {
                xtype: 'fieldset',
                title: 'Beskrivelse',
                defaults: {
                    anchor: '-50, -10',
                    msgTarget: 'side'
                },
                items: {
                    xtype: 'textarea',
                    id: 'error',
                    height: 350,
                    blankText: 'Feltet er påkrevd',
                    allowBlank: false
                }
            }, {
                xtype: 'button',
                text: 'Send inn',
                listeners: {
                    click: {
                        fn: function() {
                            var valid = true;
                            valid = Ext.getCmp('error_organisation') ? Ext.getCmp('error_organisation').validate() : true;
                            valid &= Ext.getCmp('error_contactperson') ? Ext.getCmp('error_contactperson').validate() : true;
                            valid &= Ext.getCmp('error_email') ? Ext.getCmp('error_email').validate() : true;
                            valid &= Ext.getCmp('error_reason') ? Ext.getCmp('error_reason').validate() : true;
                            valid &= Ext.getCmp('error') ? Ext.getCmp('error').validate() : true;

                            if (!valid) {
                                return;
                            }

                            var msg = {
                                Organisation: Ext.getCmp('error_organisation').getValue(),
                                ContactPerson: Ext.getCmp('error_contactperson').getValue(),
                                Email: Ext.getCmp('error_email').getValue(),
                                Reason: Ext.getCmp('error_reason').getValue().getGroupValue(),
                                Description: Ext.getCmp('error').getValue()
                            };
                            var successFunction = function(result, userContext, methodName) {
                                Ext.Msg.show({
                                    title: 'Send feilmelding',
                                    msg: 'Feilmelding er sendt',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.INFORMATION
                                });
                                userContext.clearFields();
                            };
                            var failureFunction = function() {
                                Ext.Msg.show({
                                    title: 'Send feilmelding',
                                    msg: 'Send feilmelding feilet',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.ERROR
                                });
                            };
                            finnkode.kith.no.ireportservice.PostError(msg, successFunction, failureFunction, this);
                        },
                        scope: this
                    }
                }
            }]
        }
    });
    FinnKodeWeb.PanelFormError.superclass.constructor.call(this, config);
    this.clearFields = function() {
        Ext.getCmp('error_organisation').reset();
        Ext.getCmp('error_contactperson').reset();
        Ext.getCmp('error_email').reset();
        Ext.getCmp('error_reason').reset();
        Ext.getCmp('error').reset();
    };
};

Ext.extend(FinnKodeWeb.PanelFormError, Ext.FormPanel);
