﻿Ext.onReady(function() {
    Ext.BLANK_IMAGE_URL = '/lib/resources/images/default/s.gif';

    check_login = new TK24Backend();
    check_login.load({
        params: {realm: 'ENDUSER_MNGM', action: 'CheckLogin', request: Ext.util.JSON.encode({id_reseller: _resellerid})}
    });

    check_login.on('load',function(store,records,options) {
        _or_enabled = records[0].data['or_enabled'];
        if(_or_enabled)
            RenderRegisterPage();
        else
            document.location = '/';
    });


    RenderRegisterPage = function() {
    
        var snstore = new TK24Backend({
            baseParams: {realm: 'ENDUSER_MNGM',action: 'GetAvailableSN', request: Ext.util.JSON.encode({id_reseller: _resellerid})}
        });

        
        var email1Field = new Ext.form.TextField({
            name: 'email1',
            fieldLabel: 'Podaj swój adres email',
            allowBlank: false
        })
        var email2Field = new Ext.form.TextField({
            name: 'email2',
            fieldLabel: 'Potwórz adres email',
            allowBlank: false
        })
        var password1Field = new Ext.form.TextField({
            name: 'password1',
            fieldLabel: 'Wprowadź hasło',
            allowBlank: false,
            inputType: 'password'
        })
        var password2Field = new Ext.form.TextField({
            name: 'password2',
            fieldLabel: 'Powtórz hasło',
            allowBlank: false,
            inputType: 'password'
        })

        var nameField = new Ext.form.TextField({
            name: 'name',
            fieldLabel: 'Imię i Nazwisko lub nazwa firmy',
            allowBlank: false
        })
        var cityField = new Ext.form.TextField({
            name: 'city',
            fieldLabel: 'Miejscowość'
        });
        var streetField = new Ext.form.TextField({
            name: 'street',
            fieldLabel: 'Ulica'
        });
        var streetNumberField = new Ext.form.TextField({
            name: 'street_number',
            fieldLabel: 'Numer ulicy'
        });
        var flatNumberField = new Ext.form.TextField({
            name: 'flat_number',
            fieldLabel: 'Numer lokalu'
        });
        var postcodeField = new Ext.form.TextField({
            name: 'postcode',
            fieldLabel: 'Kod pocztowy'
        });

        // Custom rendering Template
        var comboTpl = new Ext.XTemplate(
            '<tpl for=".">',
            '<div class="x-combo-list-item"><b>{sn}</b></div>',
            '</tpl>'
        );

        var snCombo = new Ext.form.ComboBox({
            store: snstore,
            editable: false,
            triggerAction: 'all',
            fieldLabel: 'Wybierz strefę numeracyjną',
            valueField: 'sn',
            displayField: 'name',
            disabled: false,
            width: 100,
            tpl: comboTpl,
            emptyText: 'Strefa numeracyjna...'
        });




        var register_backend = new TK24Backend();

        register_backend.on('load',function(store,records,options) {
            loadMask.hide();
            if(store.reader.BSsuccess == true) {
                register_form.setVisible(false);
                Ext.MessageBox.alert('Rejestracja poprawna','Na podany adres email została wysłana wiadomość z linkiem aktywacyjnym.',
                function() {
                    document.location='/';
                });
            } else {
                Ext.MessageBox.alert('Błąd',store.reader.BSErrorMsg);
            }
        });
        
        registrationBtnHandler = function() {
            loadMask.show();
            register_backend.load({
                params: {
                    realm: 'ENDUSER_MNGM',
                    action: 'PrepRegistration',
                    request: Ext.util.JSON.encode({
                        email1: email1Field.getValue(),
                        email2: email2Field.getValue(),
                        password1: password1Field.getValue(),
                        password2: password2Field.getValue(),
                        name: "24ps-televoice_"+nameField.getValue(),
                        city: cityField.getValue(),
                        street: streetField.getValue(),
                        street_number: streetNumberField.getValue(),
                        flat_number: flatNumberField.getValue(),
                        postcode: postcodeField.getValue(),
                        sn: snCombo.getValue(),
                        id_reseller: 9
                    })
                }
            });
        }

        register_form = new Ext.form.FormPanel({
            collapsible: false,
            width: 600,
            autoHeight: true,
            frame: true,
            title: 'Rejestracja',
            autoScroll: true,
            labelWidth: 200,
            region: 'center',
            bodyStyle: 'padding-left:10px;padding-top: 15px;',
            labelPad: 5,
            defaultType: 'textfield',
            defaults: {
                width: 350,
                labelSeparator: ':',
                style: 'margin-top: 10px',
                labelStyle: 'margin-top: 10px'
            },
            items: [
                nameField,email1Field,email2Field,password1Field,password2Field,cityField,streetField,streetNumberField,flatNumberField,postcodeField,snCombo
            ],
            buttons: [
                {text: 'Zarejestruj się', handler: registrationBtnHandler}
            ],
            renderTo: 'registration-panel',
            id: 'registration-form-panel'
        });
            
        var loadMask = new Ext.LoadMask(register_form.body, {msg:"Rejestracja użytkownika, proszę czekać..."});
        
    }
    
});


