Ext.define("App.view.OrderWindow", {
    extend: "Ext.window.Window",
    alias: "widget.orderWindow",
    xtype: "orderWindow",
    title: App.localeRes("Order Details"),
    height: '95%',
    width: '90%',
    modal: true,

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

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

    initComponent: function () {
        var me = this;

        me.tools= [
            {
                xtype: 'button',
                tooltip: 'Refresh',
                itemId: 'refreshBtn',
                iconCls:'x-fa fa-sync ',
                title: 'Refresh',
                ui: "header",
                margin: '0 20 0 20',
                hidden: !(me.params && me.params.id),
                handler: function() {
                    if(me.down('form').getValues().id){
                        me.reloadWindow(me.params.id);
                        return;
                    }
                    if(!me.params) return;
                    if(!me.params.id) return;
                    me.reloadWindow(me.params.id);
                }
            }
        ];

        me.items= [
            {
                xtype: 'tabpanel',
                tabBar: {
                    layout: {
                        pack: "center"
                    },
                },
                style: {
                    "border-radius": "0px"
                },
                items: [
                    {
                        title: App.localeRes("Order Details"),
                        layout: "hbox",
                        tabIndex: 0,
                        items: [
                            {
                                xtype: "orderForm",
                                padding: '4 16 0 16',
                                margin: '4 4 4 8',
                                flex: 2,
                                height: '100%',
                                params: {},
                                callbackOnSave: function(id){
                                    me.reloadWindow(id);
                                },
                                callbackOnLoad: function (formData) {
                                    if(!formData.id) return;
                                    var params = {
                                        orderId: formData.id,
                                        custId: formData.custId
                                    };
                                    var refreshBtn = me.down('#refreshBtn');
                                    var orderItemGrid = me.down("orderItemGrid"),
                                    orderPaymentGrid = me.down("orderPaymentGrid"),
                                    orderMeetingGrid = me.down("orderMeetingGrid"),
                                    orderPhotoPanel = me.down("#orderPhotoPanel");
                                    orderPhotoPanel2 = me.down("#orderPhotoPanel2");
                                    orderLogGrid = me.down("orderLogGrid");
            
                                    orderItemGrid.params = params;
                                    orderPaymentGrid.params = params;
                                    orderMeetingGrid.params = params;
                                    orderPhotoPanel.params = params;
                                    orderPhotoPanel2.params = params;
                                    orderLogGrid.params = params;
            
                                    orderItemGrid.setDisabled(false);
                                    orderPaymentGrid.setDisabled(false);
                                    orderMeetingGrid.setDisabled(false);
                                    orderPhotoPanel.setDisabled(false);
                                    orderPhotoPanel2.setDisabled(false);
                                    orderLogGrid.setDisabled(false);
                                    refreshBtn.setHidden(false);
            
                                    orderItemGrid.loadStore(formData.id);
                                    orderPaymentGrid.loadStore(formData.id);
                                    orderMeetingGrid.loadStore(formData.id);
                                    orderPhotoPanel.loadPanel(formData.id);
                                    orderPhotoPanel2.loadPanel(formData.id);
                                    orderLogGrid.loadStore(formData.id);

                                    orderItemGrid.reconfigure(orderItemGrid.getStore());
                                    orderPaymentGrid.reconfigure(orderPaymentGrid.getStore());
                                    orderMeetingGrid.reconfigure(orderMeetingGrid.getStore());
                                    orderLogGrid.reconfigure(orderLogGrid.getStore());

                                }
                            },
                            {
                                xtype: 'container',
                                layout: 'vbox',
                                flex: 3,
                                height: '100%',
                                items: [
                                    {
                                        xtype: 'orderItemGrid',
                                        disabled: true,
                                        padding: '4 16 0 16',
                                        margin: '4 8 4 4',
                                        width: '100%',
                                        flex: 1
                                    },
                                    {
                                        xtype: 'orderPaymentGrid',
                                        disabled: true,
                                        width: '100%',
                                        margin: '4 4 4 4',
                                        padding: '4 16 0 16',
                                        flex: 1
                                    }
                                ]
                            },
                        ]
                    },
                    {
                        title: App.localeRes("More"),
                        layout: "hbox",
                        tabIndex: 0,
                        items: [
                            {
                                xtype: 'orderMeetingGrid',
                                disabled: true,
                                padding: '4 16 0 16',
                                margin: '4 8 4 4',
                                height: '100%',
                                flex: 2
                            },
                            {
                                xtype: 'photoPanel',
                                itemId: 'orderPhotoPanel',
                                disabled: true,
                                padding: '4 16 0 16',
                                margin: '4 8 4 4',
                                height: '100%',
                                flex: 3,
                                refType: 'order',
                                url: App.url("/order/image-list.json"),
                            },
                        ]
                    },
                    {
                        title: App.localeRes("Log"),
                        layout: "hbox",
                        tabIndex: 0,
                        hidden: !App.isGranted(["SUPERUSER"]),
                        items: [
                            {
                                xtype: 'orderLogGrid',
                                padding: '4 16 0 16',
                                margin: '4 8 4 4',
                                width: '100%',
                                height: '100%',
                            }
                        ]
                    }
                ]
            },
            
        ];
        me.listeners= {
            show: function () {
                if(me.params && me.params.id){
                    me.reloadWindow(me.params.id);
                }else if(me.params && me.params.custId){
                    var orderForm = me.down('orderForm');
                    orderForm.down('customerCombo').setValue(me.params.custId);
                }
            },
            close: function (window, opts) {
                if (me.callbackOnClose) {
                    me.callbackOnClose();
                }
            }
        };
        me.callParent(arguments);
    }
});
