Ext.define("App.view.OrderSheetWindow", {
    extend: "Ext.window.Window",
    alias: "widget.orderSheetWindow",
    title: App.localeRes("Window"),
    constrainHeader: true,
    layout: 'fit',
    modal: true,
    width: '60%',
    height: '60%',
    config: {
        readOnly: false,
    },
    loadPanel: function(){
        var me = this;
        Ext.Ajax.request({
            url: App.url("/file/list.json"),
            params: { 
				refId: me.params.id,
				refType: "ms"
            },
            success: function(response, opts) {
                var result = Ext.decode(response.responseText);
				me.down('form').removeAll(true);
                result.records.forEach(function(item, index, array) {
                   me.down('form').add({
                       xtype: 'imagefield',
                       name: item.filename,
                       thumbnailPath: App.url("/file/thumbnail/"+item.id+"/"+item.skey+"/"+item.filename),
                       path: App.url("/file/dl/"+item.id+"/"+item.skey+"/"+item.filename),
                       imageWidth: item.imageWidth,
                       imageHeight: item.imageHeight,
                       fileId: item.id,
                       skey: item.skey,
                   });
                });
            },
            failure: function(response, opts) {
                App.showError('Message : ' + response.status);
            },
            scope: this
        });
    },

    initComponent: function() {
        var me = this;

        Ext.apply(me, {
            items: [
                {
                    xtype: 'form',
                    layout: {
                        type: 'column',
                    },
                    scrollable: true,
                    tbar:[
                    {
                        xtype: 'button',
                        text: App.localeRes('Delete'),
                        hidden: !App.isGranted(['FILE_DELETE']),
                        margin: 4,
                        ui: 'soft-red',
                        handler: function(button, event){
                            if(!me.params.id) return;
                            var records = [];
                            me.down("form").items.items.forEach(function(item, index, array){
                                    if(item.selected){
                                        records.push({
                                            fileId: item.fileId,
                                            refId: me.params.id,
                                            refType: "ms",
                                            skey: item.skey,
                                        });
                                    }
                                });
                    
                            if(records.length<=0) return;
                            Ext.Ajax.request({
                                url: App.url("/file/multi-delete"),
                                params: { 
                                    deleteRecord: Ext.encode(records),
                                },
                                success: function(response, opts) {
                                    App.showMsg('Deleted');
                                    me.loadPanel(me.refId);
                                },
                                failure: function(response, opts) {
                                    App.showError('Message : ' + response.status);
                                },
                                scope: this
                            });
                        }
                    },
                    '->',
                    {
                        xtype: 'button',
                        text: App.localeRes('Select'),
                        margin: 4,
                        ui: 'soft-blue',
                        handler: function(button, event){
                            if(!me.params.id) return;
                            var records = [];
                            me.down("form").items.items.forEach(function(item, index, array){
                                    if(item.selected){
                                        records.push({
                                            fileId: item.fileId,
                                        });
                                    }
                                });
                    
                            if(records.length<=0) return;
                            if(records.length>1){
                                App.showInfo("Please select only ONE item.");
                                return;
                            } 
                            Ext.Ajax.request({
                                url: App.url("/order/save"),
                                params: { 
                                    id: me.params.id,
                                    fileId:records[0].fileId,
                                },
                                success: function(response, opts) {
                                    App.showMsg('Saved');
                                    me.close();
                                },
                                failure: function(response, opts) {
                                    App.showError('Message : ' + response.status);
                                },
                                scope: this
                            });
                        }
                    }],
                    items:[]
                }
            ],
            listeners: {
                show: function () {
                    if(me.params && me.params.id){
                        me.loadPanel();
                    }
                },
                close: function(window, eOpts) {
                    if (me.callbackOnClose) {
                        me.callbackOnClose(me._save);
                    }
                },
            },
        });
        me.callParent(arguments);
    }

});
