Ext.define('TradePortal.store.DrillDownInfo', {
    extend: 'Ext.data.Store',
    model: 'TradePortal.model.DrillDownInfo',
    storeId: 'DrillDownInfo',
    proxy: {
        type : 'jsonajax',
        url: BASE_URL  + '/fluid/FluidAppServerProxy?ipApi=presenter&ipService=Frameworks.Interfaces.Mobile.TradePortal&ipFunction=getTransactionTP',
        reader: {
            type: 'json',
            root: 'dataset.TransactionDetailsTP'
        }
    },
    listeners: {
        load: {
            fn: function (store, records, options) {
                if (Ext.getCmp('statements_drilldown_window') != undefined) {
                    Ext.getCmp('statements_drilldown_window').setLoading(false);
                }
                Ext.getCmp('viewport').setLoading(false);
                if (store.data.items.length > 0) {
                    Ext.getStore('DrillDown').loadRawData(store.proxy.reader.jsonData);
                    Ext.widget('drilldownwindow');
                    Ext.getCmp('drilldown_window').setTitle(records[0].data.transactionType + ' ' + records[0].data.transactionNumber);

                    if (Ext.getStore('getPriceViewOptionsTP').last().data.EmailRetail) {
                        Ext.getCmp('drilldownEmailRetail').show();
                        Ext.getCmp('drilldownPrint').hide();
                    } else {
                        Ext.getCmp('drilldownPrint').show();
                        Ext.getCmp('drilldownEmailRetail').hide();
                    }

                    if (records[0].data.transactionType == "Quote" || records[0].data.transactionType == "Order") {
                        if (Ext.getStore('getPriceViewOptionsTP').last().data.ShowOrderProvisional) {
                            Ext.getCmp('drilldowngrid').columns[6].setVisible(true);
                            Ext.getCmp('drilldowngrid').columns[5].setText("Nett Price");
                            Ext.getCmp('drilldowngrid').columns[7].setText("List Total");
                        } else {
                            Ext.getCmp('drilldowngrid').columns[6].setVisible(false);
                            Ext.getCmp('drilldowngrid').columns[5].setText("Unit Price");
                            Ext.getCmp('drilldowngrid').columns[7].setText("Line Price");
                        }
                    } else {
                        if (Ext.getStore('getPriceViewOptionsTP').last().data.ShowInvoiceProvisional) {
                            Ext.getCmp('drilldowngrid').columns[6].setVisible(true);
                            Ext.getCmp('drilldowngrid').columns[5].setText("Nett Price");
                            Ext.getCmp('drilldowngrid').columns[7].setText("List Total");
                        } else {
                            Ext.getCmp('drilldowngrid').columns[6].setVisible(false);
                            Ext.getCmp('drilldowngrid').columns[5].setText("Unit Price");
                            Ext.getCmp('drilldowngrid').columns[7].setText("Line Price");
                        }
                    }

                    if (records[0].data.transactionType == "Order" || records[0].data.transactionType == "Quote") {
                        Ext.getCmp('drilldownRequestChange').show();
                    }
                    if (records[0].data.transactionType == "Quote") {
                        Ext.getCmp('acceptQuote').show();
                    }

                    if (records[0].data.transactionType == "Order" || records[0].data.transactionType == "Quote") {
                        Ext.getCmp('invoiceComment').hide();
                        Ext.getCmp('changeComment').hide();
                        Ext.getCmp('invoiceHeader').hide();
                        Ext.getCmp('invoicePagination').hide();
                    } else {
                        Ext.getCmp('invoiceComment').setValue(Ext.customParam.userComment);
                        Ext.getCmp('invoiceComment').show();
                        Ext.getCmp('changeComment').show();
                        if (Ext.customParam.transactionNumbers.length > 0) {
                            Ext.getCmp('invoiceHeader').show();
                            Ext.getCmp('invoicePagination').show();
                            if (Ext.customParam.currentInvoiceIndex == 1) {
                                Ext.getCmp('previous_invoice').disable();
                            } else {
                                Ext.getCmp('previous_invoice').enable();
                            }
                            if (Ext.customParam.currentInvoiceIndex == Ext.customParam.transactionNumbers.length) {
                                Ext.getCmp('next_invoice').disable();
                            } else {
                                Ext.getCmp('next_invoice').enable();
                            }
                            Ext.getCmp('invoice_pagination').update('Invoice ' + Ext.customParam.currentInvoiceIndex + ' of ' + Ext.customParam.transactionNumbers.length);
                        } else {
                            Ext.getCmp('invoiceHeader').hide();
                            Ext.getCmp('invoicePagination').hide();
                        }
                    }


                } else {
                    Ext.Msg.alert('Alert', 'Info is not available.', Ext.emptyFn);
                }

                if (Ext.getStore('getPriceViewOptionsTP').data.last().get("ShowInvoiceRRP")) {
                    Ext.getCmp('drilldowngrid').columns[4].setVisible(true);
                } else {
                    Ext.getCmp('drilldowngrid').columns[4].setVisible(false);
                }
            }
        }
    }

});
