Ext.define("App.view.CustomerMeetingGrid", {
    extend: "Ext.grid.Panel",
    xtype: "customerMeetingGrid",
    params: {},

    loadStore: function(custId, meetingDate) {
        var store = this.getStore();
        Ext.apply(store.getProxy().extraParams, {
            custId: custId,
            meetingDate: meetingDate,
            isDESC: true,
        });

        store.loadPage(1, {
            params: { start: 0 },
            callback: function(records, operation, success) {}
        });
        this.reconfigure(store);
    },

    initComponent: function() {
        var me = this;

        Ext.apply(me, {
            tbar: {
                defaultButtonUI: "default",
                layout: "hbox",
                padding: 0,
                items: [
                    {
                        xtype: "label",
                        itemId: "meetingDetail",
                        html: "<b>" + App.localeRes("Meeting") + "</b>"
                    },
                    "->",
                    {
                        xtype: "button",
                        itemId: "newMeetingBtn",
                        text: App.localeRes("New Meeting"),
                        margin: "0 0 16 0",
                        hidden: !App.isGranted(['MEETING_MAINTAIN']),
                        handler: function(button, event) {
                            App.loadModuleWin("meetingWindow", App.localeRes("Meeting"), {
                                params: {
                                    custId: me.params.custId
                                },
                                callbackOnClose: function() {
                                    me.loadStore(me.params.custId);
                                }
                            });
                        }
                    }
                ]
            },
            bbar: {
                xtype: "pagingtoolbar",
                displayInfo: true
            },
            store: Ext.create("App.store.MeetingStore", {}),
            columns: [
                {
                    xtype: "actioncolumn",
                    text: "",
                    width: 30,
                    align: "center",
                    hidden: !App.isGranted(['MEETING_DELETE']),
                    items: [
                        {
                            iconCls: "silk-cross",
                            tooltip: App.localeRes("Delete"),
                            handler: function(view, rowIndex, colIndex, item, e, record) {
                                Ext.Ajax.request({
                                    url: App.url("/meeting/delete"),
                                    params: { id: record.get("id") },
                                    success: function(response, opts) {
                                        App.showMsg("Record Deleted");
                                        me.loadStore(me.params.custId);
                                    },
                                    failure: function(response, opts) {
                                        App.showError("Cannot Delete : " + response.status);
                                    },
                                    scope: this
                                });
                            },
                            scope: me
                        }
                    ]
                },
                {
                    xtype: "gridcolumn",
                    dataIndex: "orderNo",
                    text: App.localeRes("Order No.")
                },
                {
                    xtype: "gridcolumn",
                    dataIndex: "type",
                    text: App.localeRes("Type"),
                    renderer: function(value, metaData, record, row, col, store, grid) {
                        return MeetingTypeCombo.convertToString(value);
                    }
                },
                {
                    xtype: "gridcolumn",
                    dataIndex: "date",
                    text: App.localeRes("Date"),
                    width: 150,
                    renderer: function(value, metaData, record, row, col, store, grid){
                        return App.convertTimestampToDateTime(value);
                    }
                },
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'remarks',
                    text: App.localeRes('Description'),
                    flex: 1,
                    cellWrap: true,
                    renderer: function (value, metaData, record, row, col, store, grid) {
                        var str = value?value:"--";
                        str = str + "<br/><b>"+App.localeRes("Staff")+" : "+(record.get("staffName")?record.get("staffName"):"--")+"</b>";
                        return '<div style="padding: 0px 0px 0px 10px;">' + App.nl2br(str) + '</div>';
                    },
                },
                {
                    xtype: "actioncolumn",
                    text: "",
                    width: 30,
                    align: "center",
                    hidden: !App.isGranted(['MEETING_MAINTAIN']),
                    items: [
                        {
                            iconCls: "silk-page-white-edit",
                            tooltip: "Edit",
                            handler: function(view, rowIndex, colIndex, item, e, record) {
                                App.loadModuleWin("meetingWindow", App.localeRes("Meeting"), {
                                    params: {
                                        record: record
                                    },
                                    callbackOnClose: function() {
                                        me.loadStore(me.params.custId);
                                        me.reconfigure(me.getStore());
                                    }
                                });
                            },
                            scope: me
                        }
                    ]
                }
            ]
        });

        me.callParent(arguments);
    }
});
