Ext.define('App.view.Report_PaymentContainer', {
    extend: 'Ext.container.Container',
    alias: 'widget.report_PaymentContainer',
    resetFocusPosition: true,
    focusable:true,
    layout: 'vbox',

    defaults: {
        width: '100%',
        margin: '2 4 2 4'
    },

    doSearch: function() {
        if (App.isGranted(['PAYMENT_REPORT']) == true) {
            var f = this.down('#searchCriteriaForm').getForm(),
                g = this.down('grid');

            Ext.apply(g.getStore().getProxy().extraParams, f.getValues());

            g.getStore().loadPage(1, {
                params: { start: 0 },
                callback: function(records, operation, success) {}
            });
        } else {
            App.showError('You Have No Authority To Process This Action!', 'Authority Issue');
        }
    },

    params: {},
    initComponent: function() {
        var me = this,
            today = new Date(),
            y = today.getFullYear(),
            m = today.getMonth() - 1,
            firstDay = new Date(y, m, 1),
            lastDay = new Date(y, m + 1, 0);

        Ext.apply(me, {
            items: [
                {
                    xtype: 'form',
                    itemId: 'searchCriteriaForm',
                    bodyPadding: 15,
                    buttonAlign: 'left',
                    defaults: {
                        selectOnFocus: true
                    },
                    items: [
                        {
                            xtype: 'fieldcontainer',
                            layout: {
                                type: 'table',
                                columns: 3
                            },
                            defaults: {
                                width: '95%',
                                listeners: {
                                    specialkey: function(field, e) {
                                        if (e.getKey() == e.ENTER) me.doSearch();
                                    }
                                }
                            },
                            items: [
                                {
                                    xtype: 'datefield',
                                    fieldLabel: App.localeRes('Date From'),
                                    allowBlank: false,
                                    name: 'dateFrom',
                                    value: firstDay
                                },
                                {
                                    xtype: 'datefield',
                                    fieldLabel: App.localeRes('Date To'),
                                    allowBlank: false,
                                    name: 'dateTo',
                                    value: lastDay
                                }
                            ]
                        }
                    ],
                    buttons: [
                        {
                            text: App.localeRes('Search'),
                            iconCls: 'silk-magnifier',
                            handler: function(button, event) {
                                me.doSearch();
                            }
                        },
                        {
                            text: App.localeRes('Reset'),
                            handler: function(button, event) {
                                me.down('#searchCriteriaForm')
                                    .getForm()
                                    .reset();
                            }
                        },
                        {
                            text: App.localeRes('Export Excel'),
                            ui: 'soft-purple',
                            handler: function(button, event) {
                                if (App.isGranted(['PAYMENT_REPORT']) == true) {
                                    var data = me
                                        .down('#searchCriteriaForm')
                                        .getForm()
                                        .getValues();
                                    window.open(
                                        App.url('/report/payment/excel') +
                                            '?dateFrom=' +
                                            data.dateFrom +
                                            '&dateTo=' +
                                            data.dateTo
                                    );
                                } else {
                                    App.showError('You Have No Authority To Process This Action!', 'Authority Issue');
                                }
                            }
                        }
                    ]
                },
                {
                    xtype: 'report_PaymentGrid',
                    flex: 1
                }
            ]
        });

        me.callParent(arguments);
        me.doSearch();
    }
});

Ext.define('App.view.Report_PaymentGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.report_PaymentGrid',
    width: '100%',
    height: '100%',
    scrollable: {
        alwaysShow: true
    },

    reloadStore: function() {
        var me = this;
        me.getView().noRefreshHeaders = true;
        me.getStore().reload();
    },

    initComponent: function() {
        var me = this;
        Ext.apply(me, {
            bbar: {
                xtype: 'pagingtoolbar',
                displayInfo: true
            },
            store: Ext.create('App.store.Report_PaymentStore', {}),
            columns: [
                {
                    xtype: 'rownumberer',
                    width: 80
                },
                {
                    xtype: 'datecolumn',
                    dataIndex: 'paymentDate',
                    text: App.localeRes('Payment Date'),
                    flex: 1
                },
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'orderNo',
                    text: App.localeRes('Order No.'),
                    renderer: function(value, metaData, record, row, col, store, grid) {
                        //if(record.get('ignoreC')) return "<font  color='#CCD0D9'><b>"+value+"</b></font>";
                        if(record.get('status')==-2) return "<font  color='#CCD0D9'><b>"+value+"</b></font>";
                        if (record.get('type') == OrderTypeCombo.TYPE_S)
                            return "<font  color='#DB4C77'><b>" + value + '</b></font>';
                        return "<font  color='#10559A'><b>" + value + '</b></font>';
                    }
                },
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'customerName',
                    text: App.localeRes('Customer'),
                    flex: 1,
                    renderer: function(value, metaData, record, row, col, store, grid){
                        if (metaData) metaData.tdAttr = 'data-qtip="' 
                            + App.setCustTooltip(
                                record.get("customerName")
                                , record.get("phone")
                                , record.get("email")
                            )+ '"';
                            
                         return App.setClickableCustOrder(
                            Ext.id(), 
                            record.get('custId'),
                            value,
                            function(){
                                me.reloadStore();
                            });
                    }
                },
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'orderStatus',
                    text: App.localeRes('Status'),
                    renderer: function(value, metaData, record, row, col, store, grid) {
                        return StatusCombo.convertToString(value);
                    }
                },
                {
                    text: App.localeRes('Payment Amount'),
                    dataIndex: 'paymentAmount',
                    xtype: 'gridcolumn',
                    align: 'right',
                    flex: 1,
                    renderer: function(value, metaData, record, row, col, store, grid) {
                        return '$' + Ext.util.Format.number(value, '0,000.00');
                    }
                },
                {
                    text: App.localeRes('Order Amount'),
                    dataIndex: 'orderAmount',
                    xtype: 'gridcolumn',
                    align: 'right',
                    flex: 1,
                    renderer: function(value, metaData, record, row, col, store, grid) {
                        return '$' + Ext.util.Format.number(value, '0,000.00');
                    }
                },
                {
                    text: App.localeRes('Total Payment (Current)'),
                    dataIndex: 'currentPayment',
                    xtype: 'gridcolumn',
                    align: 'right',
                    flex: 1,
                    renderer: function(value, metaData, record, row, col, store, grid) {
                        return '$' + Ext.util.Format.number(value, '0,000.00');
                    }
                }
            ],
            listeners: {}
        });
        me.callParent(arguments);
    }
});
