﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelFormSynonyms = function(config) {
    config = config || {};

    Ext.apply(config, {
        layout: 'form',
        items: {
            layout: 'form',
            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: 'organisation',
                    fieldLabel: 'Organisasjon'
                }, {
                    id: 'contactperson',
                    fieldLabel: 'Kontaktperson'
                }, {
                    id: '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: 'codebookset',
                title: 'Kodeverk',
                labelWidth: 120
            }, {
                xtype: 'fieldset',
                title: 'Synonymer',
                labelWidth: 180,
                defaults: {
                    anchor: '-10',
                    msgTarget: 'side'
                },
                items: [{
                    xtype: 'textfield',
                    id: 'synonymterm',
                    blankText: 'Feltet er påkrevd',
                    allowBlank: false,
                    fieldLabel: 'Nåværende term i FinnKode'
                }, {
                    fieldLabel: 'Synonymer',
                    layout: 'table',
                    border: false,
                    id: 'synonyms',
                    layoutConfig: { columns: 2 },
                    items: [{
                        width: 310,
                        xtype: 'textfield',
                        id: 'synonym0',
                        allowBlank: false
                    }, {
                        width: 40,
                        xtype: 'button',
                        id: 'synonymbutton0',
                        text: 'Legg til',
                        listeners: {
                            click: function() {
                                var synonymTextField = Ext.getCmp('synonym0');
                                var synonymText = synonymTextField.getValue();
                                if (synonymText.replace(' ', '') == '') {
                                    synonymTextField.markInvalid();
                                    return;
                                }
                                if (synonymTextField.validate()) {
                                    var synonymSet = Ext.getCmp('synonyms');
                                    var txt = new Ext.form.DisplayField({
                                        value: synonymTextField.getValue()
                                    });
                                    var btn = new Ext.Button({
                                        text: 'Fjern',
                                        width: 40,
                                        listeners: {
                                            click: function() {
                                                synonymSet.remove(txt);
                                                synonymSet.remove(btn);
                                                synonymSet.doLayout();
                                            }
                                        }
                                    });
                                    synonymTextField.setRawValue('');
                                    synonymSet.add(txt);
                                    synonymSet.add(btn);
                                    synonymSet.doLayout();
                                }
                            }
                        }
                    }]
                }]
            }, {
                xtype: 'button',
                text: 'Send inn',
                listeners: {
                    click: {
                        fn: function() {
                            var valid = true;
                            valid = Ext.getCmp('organisation') ? Ext.getCmp('organisation').validate() : true;
                            valid &= Ext.getCmp('contactperson') ? Ext.getCmp('contactperson').validate() : true;
                            valid &= Ext.getCmp('email') ? Ext.getCmp('email').validate() : true;
                            valid &= Ext.getCmp('codebooks') ? Ext.getCmp('codebooks').validate() : true;
                            var synonymSet = Ext.getCmp('synonyms');
                            if (synonymSet.items.getCount() <= 2) {
                                valid = false;
                                Ext.Msg.show({
                                    title: 'Send synonymer',
                                    msg: 'Registrer minst ett synonym',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.ERROR
                                });
                            }
                            if (Ext.getCmp('synonym0').getValue() != '') {
                                valid = false;
                                Ext.Msg.show({
                                    title: 'Legg til synonym',
                                    msg: 'Klikk \'Legg til\' for å legge til synonymet',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.INFORMATION
                                });
                            }
                            if (!valid) {
                                return;
                            }
                            
                            var synonymString = new String();
                            for (var synonymIndex = 2; synonymIndex < synonymSet.items.getCount(); synonymIndex += 2) {
                                var synonymTextField = synonymSet.items.get(synonymIndex);
                                synonymString = synonymString.concat(synonymIndex == 2? '': ',', synonymTextField.getValue());
                            }
                            var msg = {
                                Organisation: Ext.getCmp('organisation').getValue(),
                                ContactPerson: Ext.getCmp('contactperson').getValue(),
                                Email: Ext.getCmp('email').getValue(),
                                Codebook: Ext.getCmp('codebooks').getValue().getGroupValue(),
                                OriginalTerm: Ext.getCmp('synonymterm').getValue(),
                                Synonyms: synonymString
                            };
                            var successFunction = function(result, userContext, methodName) {
                                Ext.Msg.show({
                                    title: 'Send synonymer',
                                    msg: 'Synonymer er sendt',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.INFORMATION
                                });
                                userContext.clearFields();
                            };
                            var failureFunction = function() {
                                Ext.Msg.show({
                                    title: 'Send synonymer',
                                    msg: 'Send synonymer feilet',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.ERROR
                                });
                            };
                            finnkode.kith.no.ireportservice.PostSynonyms(msg, successFunction, failureFunction, this);
                        },
                        scope: this
                    }
                }
            }]
        }       
    });
    FinnKodeWeb.PanelFormSynonyms.superclass.constructor.call(this, config);
    this.clearFields = function() {
        Ext.getCmp('organisation').reset();
        Ext.getCmp('contactperson').reset();
        Ext.getCmp('email').reset();
        Ext.getCmp('codebooks').reset();
        Ext.getCmp('synonymterm').reset();
        Ext.getCmp('synonyms').items.get(0).reset();
        for (var synonymIndex = Ext.getCmp('synonyms').items.getCount() - 1; synonymIndex > 1; synonymIndex--) {
            Ext.getCmp('synonyms').remove(Ext.getCmp('synonyms').items.get(synonymIndex));
        } 
    };

    if (Loader != null && Loader.configurationLoader) {
        var settings = Loader.configurationLoader.getSettings();
        if (settings != null) {
            var codebookset = Ext.getCmp('codebookset');
            if (codebookset != null) {
                codebookset.removeAll();
                var bookIndex, arrayIndex = 0;
                var radioArray =  new Array();
                for (bookIndex = 0; bookIndex < settings.Books.length; bookIndex++) {
                    var book = settings.Books[bookIndex];
                    var bookSectionIndex;
                    for (bookSectionIndex = 0; bookSectionIndex < book.BookSections.length; bookSectionIndex++) {
                        var bookSection = book.BookSections[bookSectionIndex];
                        if (bookSection.IsSearchable) {
                            var radioButton = new Ext.form.Radio({
                                boxLabel: bookSection.Description,
                                inputValue: bookSection.Description,
                                name: 'codebook_id'            
                            });
                            radioArray[arrayIndex] = radioButton;
                            arrayIndex++;
                        }
                    }
                }
                radioArray[arrayIndex] = new Ext.form.Radio({
                    boxLabel: 'Annet',
                    inputValue: 'Annet',
                    name: 'codebook_id'
                });
                var codebooks = new Ext.form.RadioGroup({
                    id: 'codebooks',
                    blankText: 'Velg et kodeverk',
                    allowBlank: false,
                    msgTarget: 'side',
                    anchor: '-50',
                    columns: 2,
                    vertical: true,
                    items: radioArray
                });
                codebookset.add(codebooks);
                codebookset.doLayout();
            }
        }
    }
};

Ext.extend(FinnKodeWeb.PanelFormSynonyms, Ext.FormPanel);
