Ext.define("App.view.CustomerWindow", {
    extend: "Ext.window.Window",
    alias: "widget.customerWindow",
    xtype: "customerWindow",
    title: App.localeRes("Customer"),
    height: "95%",
    width: "95%",
    modal: true,
    params: {},

    resizable: true,
    layout: "fit",
    constrainHeader: true,

    reloadWindow: function (id) {
        var me = this;
        me.down("customerForm").loadForm({
            id: id
        });
    },

    initComponent: function () {
        var me = this;

        me.tools= [
            {
                xtype: 'button',
                tooltip: 'Refresh',
                itemId: 'refreshBtn',
                iconCls:'x-fa fa-sync ',
                title: 'Refresh',
                margin: '0 20 0 20',
                ui: "header",
                hidden: !(me.params && me.params.id),
                handler: function() {
                    if(!me.params) return;
                    if(!me.params.id) return;
                    me.reloadWindow(me.params.id);
                }
            }
        ];
        Ext.apply(me, {
            items: [
                {
                    layout: "hbox",
                    tabIndex: 0,
                    items: [
                        {
                            xtype: "customerForm",
                            height: '100%',
                            width: "50%",
                            padding: '0 16 0 16',
                            params: {},
                            callbackOnSave: function(id){
                                me.reloadWindow(id);
                            },
                            callbackOnLoad: function (formData) {
                                if(!formData.id) return;
                                var params = {
                                    custId: formData.id
                                };
                                var refreshBtn = me.down("#refreshBtn");
                                var newOrderBtn = me.down("#newOrderBtn");
                                var customerMeetingGrid = me.down("customerMeetingGrid"),
                                customerPhotoPanel = me.down("#customerPhotoPanel");

                                customerMeetingGrid.params = params;
                                customerPhotoPanel.params = params;

                                customerMeetingGrid.loadStore(formData.id);
                                customerPhotoPanel.loadPanel(formData.id);
                                
                                customerMeetingGrid.setDisabled(false);
                                customerPhotoPanel.setDisabled(false);
                                refreshBtn.setHidden(false);
                                newOrderBtn.setHidden(false);
                            }
                        },
                        {
                            xtype: 'container',
                            layout: 'vbox',
                            height: '100%',
                            width: "50%",
                            items: [
                                {
                                    xtype: 'photoPanel',
                                    itemId: 'customerPhotoPanel',
                                    disabled: true,
                                    width: '100%',
                                    padding: '0 16 0 16',
                                    margin: '4 8 4 8',
                                    flex: 1,
                                    refType: 'customer',
                                    url: App.url("/customer/image-list.json"),
                                },
                                {
                                    xtype: 'customerMeetingGrid',
                                    disabled: true,
                                    width: '100%',
                                    padding: '0 16 0 16',
                                    margin: '4 8 4 8',
                                    flex: 1
                                },
                              
                            ]
                        }
                    ]
                }
            ],
            listeners: {
                show: function () {
                    me.reloadWindow(me.params.id);
                },
                close: function (window, opts) {
                    if (me.callbackOnClose) {
                        me.callbackOnClose();
                    }
                }
            }
        });
        me.callParent(arguments);
    }
});
