Ext.define("App.view.CustomerOrderWindow", {
    extend: "Ext.window.Window",
    alias: "widget.customerOrderWindow",
    xtype: "customerOrderWindow",
    title: App.localeRes("Customer Order List"),
    height: "100%",
    width: "99%",
    resetFocusPosition: true,
    focusable:true,
    modal: true,
    params: {},

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

    reloadWindow: function (id) {
        var me = this;
        var f = me.down("form");
        var refreshBtn = me.down("#refreshBtn");
        var customerOrderGrid = me.down("customerOrderGrid");
        var customerOrderItemGrid = me.down("customerOrderItemGrid");
        customerOrderItemGrid.getStore().removeAll();
        f.load({
            url: App.url("/customer/load"),
            method: 'POST',
            params:{
                id: id
            },
            success: function(form, action) {
                if(!form.getValues().id) return;
                var params = {
                    custId: form.getValues().id
                };
                customerOrderGrid.setGridParams(params);
                refreshBtn.setHidden(false);
            }
        });
    },   

    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: "vbox",
                    tabIndex: 1,
                    hidden: !App.isGranted(['ORDER_MAINTAIN']),
                    items: [
                        {
                            xtype:'form',
                            width: '100%',
                            margin: '4 4 2 4',
                            padding: '4 16 4 16',
                            layout: 'hbox',
                            items:[
                                {
                                    xtype: 'hidden',
                                    name: 'id',
                                },
                                {
                                    xtype: 'textfield',
                                    fieldLabel: App.localeRes('Customer'),
                                    name: 'customerName',
                                    readOnly: true,
                                    width: 300,
                                    margin: '0 8 0 0',
                                    fieldStyle: 'background-color: #eee;'
                                },
                                {
                                    xtype: 'textfield',
                                    fieldLabel: App.localeRes('Phone'),
                                    name: 'phoneNum',
                                    readOnly: true,
                                    width: 300,
                                    margin: '0 8 0 8',
                                    fieldStyle: 'background-color: #eee;'
                                },
                                {
                                    xtype: 'textfield',
                                    fieldLabel: App.localeRes('Email'),
                                    name: 'email',
                                    readOnly: true,
                                    flex: 1,
                                    margin: '0 8 0 8',
                                    minWidth: 300,
                                    fieldStyle: 'background-color: #eee;'
                                },
                                {
                                    xtype:"button",
                                    text: App.localeRes('Details'),
                                    handler: function(button, event){
                                        App.loadModuleWin('customerWindow', App.localeRes('Customer'), {
                                            params: { id: me.params.id },
                                            callbackOnClose: function() {
                                                me.reloadWindow(me.params.id);
                                            }
                                        });
                                    }
                                }
                            ],
                        },
                        {
                            layout: "hbox",
                            padding: '0 0 0 0',
                            margin: 2,
                            flex: 2,
                            items:[
                                {
                                    xtype: 'customerOrderGrid',
                                    padding: '0 8 0 8',
                                    margin: '4 4 4 2',
                                    width: 1020,
                                    height: '100%',
                                },
                                {
                                    xtype:'customerOrderItemGrid',
                                    padding: '16 8 0 8',
                                    margin: '4 2 4 4',
                                    height: '100%',
                                    width: 480,
                                }
                            ]
                        } 
                    ]
                },
            ],
            listeners: {
                show: function () {
                    me.reloadWindow(me.params.id);
                },
                close: function (window, opts) {
                    if (me.callbackOnClose) {
                        me.callbackOnClose();
                    }
                }
            }
        });
        me.callParent(arguments);
    }
});
