Ext.define('TradePortal.store.HomeStatistic', {
    extend: 'Ext.data.Store',
    model: 'TradePortal.model.HomeStatistic',
    storeId: 'HomeStatistic',
    proxy: {
        type: 'jsonajax',
        url: BASE_URL + '/fluid/FluidAppServerProxy?ipApi=presenter&ipService=Frameworks.Interfaces.Mobile.TradePortal&ipFunction=getUserDetails',
        reader: {
            type: 'json',
            root: 'dataset.ttGetUserDetails'
        },
        extraParams: {
            tpa: 'tportal'
        }
    },
    listeners: {
        load: {
            fn: function (store, records, options) {
                if (store.data.items.length > 0) {
                    Ext.getStore('createCart').load({
                        params: {
                            deviceID: Ext.getStore('Config').data.last().data.device_id,
                            customerID: records[0].data.id_cust
                        }
                    });
                    Ext.getStore('CustomerDetails').load({
                        params: {
                            customerID: records[0].data.id_cust
                        }
                    });
                    Ext.getStore('CustomerContacts').load({
                        params: {
                            userName: records[0].data.id_user,
                            customerID: records[0].data.id_cust
                        }
                    });
                    Ext.getStore('getListMobileUsers').load({
                        params: {
                            deviceID: Ext.getStore('Config').data.last().data.device_id,
                            customerID: records[0].data.id_cust
                        }
                    });
                    var today = new Date();
                    todaydate = Ext.Date.format(today, 'Y-m-d');
                    var monthago = today.setMonth(today.getMonth() - 3);
                    monthagodate = Ext.Date.format(new Date(monthago), 'Y-m-d');
                    Ext.getStore('Invoices').setExtraParam('userName', records[0].data.id_user);
                    Ext.getStore('Invoices').setExtraParam('deviceID', Ext.getStore('Config').data.last().data.device_id);
                    Ext.getStore('Invoices').setExtraParam('customerID', records[0].data.id_cust);
                    Ext.getStore('Invoices').setExtraParam('fromDate', monthagodate);
                    Ext.getStore('Invoices').setExtraParam('toDate', todaydate);
                    Ext.getStore('Invoices').load({});
                    Ext.getStore('QuoteOrders').load({
                        params: {
                            userName: records[0].data.id_user,
                            deviceID: Ext.getStore('Config').data.last().data.device_id,
                            customerID: records[0].data.id_cust
                        }
                    });
                    Ext.getStore('Statements').load({
                        params: {
                            userName: records[0].data.id_user,
                            deviceID: Ext.getStore('Config').data.last().data.device_id,
                            companyID: Ext.getStore('Config').data.last().data.company_id,
                            customerID: records[0].data.id_cust
                        }
                    });
                    Ext.getStore('Downloads').load({
                        params: {
                            deviceID: Ext.getStore('Config').data.last().data.device_id
                        }
                    });
                    Ext.getStore('getCustomList').load({
                        params: {
                            custID: Ext.getStore('HomeStatistic').data.items[0].data.id_cust,
                            deviceID: Ext.getStore('Config').data.last().data.device_id
                        }
                    });
                    Ext.getStore('getCustomList2').load({
                        params: {
                            custID: Ext.getStore('HomeStatistic').data.items[0].data.id_cust,
                            deviceID: Ext.getStore('Config').data.last().data.device_id
                        }
                    });
                    Ext.getStore('getPriceBookInfo').load({
                        params: {
                            tradePortalUser: Ext.getCmp('Username').getValue(),
                            deviceID: Ext.getStore('Config').data.last().data.device_id
                        }
                    });
                    Ext.getStore('getPriceBookInfoGrid').load({
                        params: {
                            tradePortalUser: Ext.getCmp('Username').getValue(),
                            deviceID: Ext.getStore('Config').data.last().data.device_id
                        }
                    });

                    Ext.getCmp('account_breadcrumbs_title').update('<div class="breadcrumbs">Welcome ' + records[0].data.name_first + ' ' + records[0].data.name_last + '</div>');
                } else {
                    Ext.getCmp('viewport').setLoading(false);
                }
            }
        }
    }
});
