﻿Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelFormQuestion = 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: 'question_organisation',
                    fieldLabel: 'Organisasjon'
                }, {
                    id: 'question_contactperson',
                    fieldLabel: 'Kontaktperson'
                }, {
                    id: 'question_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: 'question_codebookset',
                title: 'Kodeverk',
                labelWidth: 120,
                items: {
                    xtype: 'radiogroup',
                    id: 'question_codebooks',
                    blankText: 'Velg et kodeverk',
                    allowBlank: false,
                    msgTarget: 'side',
                    columns: 2,
                    vertical: true,
                    anchor: '-10',
                    items: [{
                        boxLabel: 'ICD-10',
                        inputValue: 'ICD-10',
                        name: 'codebook_id'
                    }, {
                        boxLabel: 'NCMP',
                        inputValue: 'NCMP',
                        name: 'codebook_id'
                    }, {
                        boxLabel: 'NCSP',
                        inputValue: 'NCSP',
                        name: 'codebook_id'
                    }, {
                        boxLabel: 'BUP',
                        inputValue: 'BUP',
                        name: 'codebook_id'
                    }, {
                        boxLabel: 'ICPC-2',
                        inputValue: 'ICPC-2',
                        name: 'codebook_id'
                    }, {
                        boxLabel: 'ICF-CY',
                        inputValue: 'ICF-CY',
                        name: 'codebook_id'
                    }, {
                        boxLabel: 'Annet',
                        inputValue: 'Annet',
                        name: 'codebook_id'
                    }]                
                }
            }, {
                xtype: 'fieldset',
                title: 'Spørsmål',
                defaults: {
                    anchor: '-50, -10',
                    msgTarget: 'side'
                },
                items: {
                    xtype: 'textarea',
                    id: 'question',
                    height: 350,
                    blankText: 'Feltet er påkrevd',
                    allowBlank: false
                }
            }, {
                xtype: 'button',
                text: 'Send inn',
                listeners: {
                    click: {
                        fn: function() {
                            var valid = true;
                            valid = Ext.getCmp('question_organisation') ? Ext.getCmp('question_organisation').validate() : true;
                            valid &= Ext.getCmp('question_contactperson') ? Ext.getCmp('question_contactperson').validate() : true;
                            valid &= Ext.getCmp('question_email') ? Ext.getCmp('question_email').validate() : true;
                            valid &= Ext.getCmp('question_codebooks') ? Ext.getCmp('question_codebooks').validate() : true;
                            valid &= Ext.getCmp('question') ? Ext.getCmp('question').validate() : true;

                            if (!valid) {
                                return;
                            }
                            
                            var msg = {
                                Organisation: Ext.getCmp('question_organisation').getValue(),
                                ContactPerson: Ext.getCmp('question_contactperson').getValue(),
                                Email: Ext.getCmp('question_email').getValue(),
                                Codebook: Ext.getCmp('question_codebooks').getValue().getGroupValue(),
                                Question: Ext.getCmp('question').getValue()
                            };
                            var successFunction = function(result, userContext, methodName) {
                                Ext.Msg.show({
                                    title: 'Send spørsmål',
                                    msg: 'Spørsmål er sendt',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.INFORMATION
                                });
                                userContext.clearFields();
                            };
                            var failureFunction = function() {
                                Ext.Msg.show({
                                    title: 'Send spørsmål',
                                    msg: 'Send spørsmål feilet',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.ERROR
                                });
                            };
                            finnkode.kith.no.ireportservice.PostQuestion(msg, successFunction, failureFunction, this);
                        },
                        scope: this
                    }
                }
            }]
        }
    });
    FinnKodeWeb.PanelFormQuestion.superclass.constructor.call(this, config);
    this.clearFields = function() {
        Ext.getCmp('question_organisation').reset();
        Ext.getCmp('question_contactperson').reset();
        Ext.getCmp('question_email').reset();
        Ext.getCmp('question_codebooks').reset();
        Ext.getCmp('question').reset();       
    };
};

Ext.extend(FinnKodeWeb.PanelFormQuestion, Ext.Panel);
