
Ext.ns('am.lib');
Ext.ns('am.data');

/*
Ext.override(Ext.form.DateField, {
    onMenuHide: function() {
        this.focus();
        this.focus(false, 60);
        this.menuEvents('un');
    }
});
*/

am.data.baseURL = '/bridge/mnet/WorkShop/';

am.lib.BookBriefInfo = Ext.extend(Ext.Panel, {

    initComponent: function() {

        Ext.apply(this, {
            renderTo: 'ws-info',
            frame: false,
            border: false,
            autoLoad: {
                url: am.data.baseURL + 'getBriefInfo',
                method: 'GET',
                params: {
                    wsid: document.URL.toQueryParams().wsid
                }
            }
        });

        am.lib.BookBriefInfo.superclass.initComponent.call(this);
    }
});

am.lib.BookFullInfo = Ext.extend(Ext.Panel, {

    initComponent: function() {

        Ext.apply(this, {
            renderTo: 'ws-info',
            frame: false,
            border: false,
            autoLoad: {
                url: am.data.baseURL + 'getFullInfo',
                method: 'GET',
                params: {
                    wsid: document.URL.toQueryParams().wsid
                }
            }
        });

        am.lib.BookFullInfo.superclass.initComponent.call(this);
    }
});

am.lib.BookForm = Ext.extend(Ext.form.FormPanel, {

    store: null,

    initComponent: function() {

        Ext.apply(this, {
            renderTo: 'ws-form',
            frame: true,
            title: 'Bestill verkstedstime',
            width: 560,
            monitorValid: true,
            items: [{
                layout: 'column',
                items: [{
                    columnWidth:.5,
                    style: 'margin-right: 7px',
                    /*items: [{ // Pick services
                        id: 'fservices',
                        layout: 'form',
                        frame: true,
                        border: true,
                        title: 'Velg tjeneste',
                        items: [{
                            xtype: 'label',
                            id: 'fs-load',
                            html: 'Loading...'
                        }] // Pick time
                    }]*/
                    items: [{ // Contact info
                        id: 'fcontact',
                        layout: 'form',
                        frame: true,
                        border: true,
                        title: 'Kontaktinformasjon',
                        /*items: [{
                            xtype: 'label',
                            id: 'fc-load',
                            html: 'Loading...'
                        }]*/
                        items: [{
                            'xtype'         : 'textfield',
                            'fieldLabel'    : 'Navn/Firmanavn*',
                            'name'          : 'ContactName',
                            'allowBlank'    : false
                        },{
                            'xtype'         : 'textfield',
                            'fieldLabel'    : 'Adresse',
                            'name'          : 'ContactAddress'
                        },{
                            'xtype'         : 'textfield',
                            'fieldLabel'    : 'Sted',
                            'name'          : 'ContactPlace'
                        },{
                            'xtype'         : 'textfield',
                            'fieldLabel'    : 'E-post*',
                            'name'          : 'ContactEmail',
                            'allowBlank'    : false,
                            'vtype'         : 'email'
                        },{
                            'xtype'         : 'textfield',
                            'fieldLabel'    : 'Mobil*',
                            'name'          : 'ContactMobile',
                            'allowBlank'    : false
                        }]
                    }]
                },{
                    columnWidth:.5,
                    items: [{
                        id: 'fcarinfo',
                        layout: 'form',
                        frame: true,
                        border: true,
                        title: 'Informasjon om bilen',
                        /*items: [{
                            xtype: 'label',
                            id: 'fr-load',
                            html: 'Loading...'
                        }]*/
                        items: [{
                            'xtype'           : 'combo'
                            ,'id'             : 'CarBrandCombo'
                            ,'fieldLabel'     : 'Velg bilmerke'
                            ,'name'           : 'CarBrand'
                            ,'hiddenName'     : 'CarBrandID'
                            ,'mode'           : 'local'
                            ,'allowBlank'     :  true
                            ,'emptyText'      : 'Velg bilmerke'
                            ,'forceSelection' : true
                            ,'minChars'       : 2
                            ,'typeAhead'      : true
                            ,'triggerAction'  : 'all'
                            ,'lazyRender'     : true
                            ,'valueField'     : 'v'
                            ,'displayField'   : 'd'
                            ,store: new Ext.data.JsonStore({
                                url: am.data.baseURL + 'getCarBrands',
                                root: 'records',
                                totalProperty: 'totalCount',
                                idProperty: 'CarBrandID',
                                fields: [
                                    {name: 'CarBrandID', mapping: 'v'},
                                    {name: 'Title', mapping: 'd'}
                                ],
                                autoLoad: true
                            })
                        },{
                            'xtype'         : 'textfield',
                            'fieldLabel'    : 'Reg. nummer',
                            'name'          : 'CarRegNum'
                        }]
                    }/*,{
                        id: 'ftime',
                        layout: 'form',
                        frame: true,
                        border: true,
                        title: 'Når passer det?',
                        items: [{
                            'xtype'         : 'datefield',
                            'fieldLabel'    : 'Velg dato',
                            'name'          : 'BookDate',
                            'format'        : 'Y-m-d'
                        }]
                    }*/]
                }]
            },{
                layout: 'form',
                items: [
                    {
                        id: 'fnotes',
                        layout: 'form',
                        frame: true,
                        border: true,
                        title: 'Tilleggsopplysninger',
                        /*items: [{
                            xtype: 'label',
                            id: 'fn-load',
                            html: 'Loading...'
                        }]*/
                        items: [{
                            'xtype'         : 'textarea',
                            'name'          : 'Notes',
                            'hideLabel'     : true,
                            'width'         : 535,
                            'height'        : 150
                        }]
                    }/*,{
                        xtype: 'checkbox',
                        id: 'fagreement',
                        boxLabel: 'Jeg godtar at AutoMester kan lagre disse opplysningene.'
                    }*/,{
                        layout: 'form',
                        id: 'ws-other',
                        items: [{
                            xtype: 'textfield',
                            name: 'Other'
                        }]
                    }
                ]
            }],
            buttons:[{
                 text: 'Send'
                ,formBind: true
                ,scope: this
                ,handler: this.submit
                ,disabled: true
            }]
        });

        am.lib.BookForm.superclass.initComponent.call(this);
    },

	onFormLoadSuccess: function(result, request) {
		console.log('succ');
	},

	onFormLoadFailure: function(result, request) {
		console.log('fail');
	},

    submit: function() {

        //if (Ext.getCmp('fagreement').getValue()) {
            this.getForm().submit({
                 url: am.data.baseURL + 'bookTime'
                 ,params: {
                    wsid: document.URL.toQueryParams().wsid
                 }
                ,scope: this
                ,success: this.onSubmitSuccess
                ,failure: this.onSubmitFailure
                ,timeout: 5000
                ,waitMsg: 'Sender din reservasjon...'
            });
        //} else {
        //    Ext.Msg.alert('Information', 'In order to submit the form you have to check agreement checkbox');
        //}
    },

    onSubmitSuccess: function(form, action) {
        Ext.Msg.alert('Informasjon', action.result.message, function() {
			window.location = String.format('{0}&type=map&wsid={1}', am.data.basePageURL, document.URL.toQueryParams().wsid);
		});
    },

    onSubmitFailure: function(form, action) {
        Ext.Msg.alert('Error', action.result.message);
    }
});

Ext.onReady(function() {

    var wsid = document.URL.toQueryParams().wsid;
    var type = document.URL.toQueryParams().type;

    if (type == 'info') {

        am.data.bi = new am.lib.BookFullInfo();

    } else if (type == 'map') {

		am.data.bi = new am.lib.BookBriefInfo();

		Ext.Ajax.request({
			 url: am.data.baseURL + 'getGmapPosition'
			,params: {
				wsid: document.URL.toQueryParams().wsid
			}
			,method: 'GET'
			,success: function (result, request) {

				var data = Ext.decode(result.responseText);

				if (data.lat && data.lng) {

					var myLatlng = new google.maps.LatLng(
						parseFloat(data.lat),
						parseFloat(data.lng)
					);

					var myOptions = {
					  zoom: 12,
					  center: myLatlng,
					  mapTypeId: google.maps.MapTypeId.ROADMAP
					}

					var mapEl = document.getElementById("ws-map");
					mapEl.style.height = '400px';
					var map = new google.maps.Map(mapEl, myOptions);

					var marker = new google.maps.Marker({
						position: myLatlng,
						title: data.name,
						map: map,
						draggable: false
					});
				}
			}
		});

    } else if (type == 'book') {

        am.data.bi = new am.lib.BookBriefInfo();
        am.data.bf = new am.lib.BookForm();
    }
});

