Ext.define("App.view.MeetingForm", {
    extend: "Ext.form.Panel",
    xtype: "meetingForm",
    params: {},
    layout: "vbox",
    scrollable: true,

    onSave: function () {
        var me = this,
        f = me.getForm();

        if (f.isValid()) {
            f.submit({
                url: App.url("/meeting/save"),
                method: 'POST',
                submitEmptyText: false,
                success: function (form, action) {
                    App.showMsg('Saved');
                    if(me.callbackOnSave)
                        me.callbackOnSave();
                }
            });
        }
    },

    initComponent: function () {
        var me = this;

        Ext.apply(me, {
            bbar: {
                defaultButtonUI: 'default',
                layout: 'hbox',
                padding: 0,
                items: [
                    '->',
                    {
                        xtype: 'button',
                        text: App.localeRes('Save & Close'),
                        ui: "blue",
                        margin: '0 0 16 0',
                        formBind: true,
                        handler: function(button, event){
                            me.onSave();
                        }
                    }
                ]
            },
            items: [
                {
                    xtype: 'hidden',
                    name: 'id'
                },
                {
                    xtype: 'datetimeField',
                    fieldLabel: App.localeRes("Date"),
                    name: 'date',
                    width: '100%',
                    minValue: '09:00',
                    maxValue: '19:00',
                    allowBlank: false,
                    value: new Date(),
                    increment: 15
                },
                {
                    xtype: 'meetingTypeCombo',
                    fieldLabel: App.localeRes("Type"),
                    name: 'type',
                    width: '100%',
                },
                {
                    xtype: 'customerCombo',
                    fieldLabel: App.localeRes("Customer"),
                    name: 'custId',
                    width: '100%',
                    listeners: {
                        select: function(combo, newValue, oldValue){
                            if(newValue == null) return;
                            var orderCombo = combo.up('form').down("orderCombo");
                            var store = orderCombo.getStore();
                            Ext.apply(store.getProxy(), {
                                extraParams: {
                                    custId: newValue.id
                                }
                            });
                            store.load();
                        }
                    }
                },
                {
                    xtype: 'orderCombo',
                    fieldLabel: App.localeRes("Order"),
                    name: 'orderId',
                    width: '100%',
                    listeners: {
                        select: function(combo, newValue, oldValue){
                            if(newValue == null) return;
                            var customerCombo = combo.up('form').down("customerCombo");
                            customerCombo.setValue(newValue.get("custId"));
                        }
                    }
                },
                {
                    xtype: 'textarea',
                    fieldLabel: App.localeRes("Description"),
                    name: 'remarks',
                    width: '100%',
                    height: 80,
                    maxLength: 200
                },
                {
                    xtype: 'staffCombo',
                    fieldLabel: App.localeRes("Staff"),
                    name:"staffName",
                    width: '100%',
                    maxLength: 30,
                }
            ],
        });

        me.callParent(arguments);
    }
});
