(function(){
  Ext.ns('Sliver.Privilege');
  Sliver.Privilege.PrivilegeManager = Ext.extend(Ext.Panel,{
    initComponent:function(){
        //获得全局center面板引用
        var main = $c(Sliver.main);
        this.gridCtxMenu;

        this.store = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({
                url: 'system/listSystemPrivilege.action'
            }),
            reader: new Ext.data.JsonReader({
                root         : 'list',
                totalProperty: 'total',
                id: 'privilegeID',
                fields: [
                   'privilegeID',
                   'privilegeName',
                   'privilegeGroup',
                   'privilegeIcon',
                   'privilegeUrl',
                   'privilegeRemark'
                ]
            }),
            remoteSort: true
        });
        //this.store.setDefaultSort('privilegeID', 'DESC');
        this.store.load();

        this.cm = new Ext.grid.ColumnModel({
          defaults: {
              sortable: true
          },
          columns:[
            new Ext.grid.RowNumberer(),{
               header:'${text.Module.icon}',
               dataIndex:'privilegeIcon',
               /*renderer:function(v){
                   return '<div class="'+v+'" style="height:32px;width:31px;"></div>';
               },*/
               width:24
            },{
               header:'${text.Privilege.name}',
               dataIndex:'privilegeName',
               width:80
            },{
               header:'${text.All.remark}',
               dataIndex:'privilegeRemark',
               renderer:function(v){
                 return v.length>40?Ext.util.Format.substr(v,0,40)+"...":v;
               }
            },{
               header:'${text.All.edit}',
               align:'right',
               width:40,
               renderer : function(value, cellmeta, record, rowIndex, columnIndex,store) {
                  //var upFn;
                  removePrivilegeFn=function(id,name){
                      Ext.MessageBox.confirm('${text.All.confirm}','${text.All.confirm}${text.All.remove}${text.Privilege}？',function(btn){
                        if(btn == 'yes'){
                          Ext.Ajax.request({
                            url:'system/removePrivilege.action',
                            params:{
                              "privilege.privilegeID":id,
                              "privilege.privilegeName":name
                            },
                            success:function(response){
                              var j=Ext.decode(response.responseText);
                              if(j.success===true){
                                $m({title:'${text.All.deleted}',html:'${text.Privilege}[&nbsp;'+name+'&nbsp;]${text.All.deleted}'});
                              }
                            }
                          });
                        }
                      });
                  }
                  updatePrivilegeFn=function(id,name){
                      var temp = main.get(id);
                      if(temp){
                        main.setActiveTab(temp);
                      }else{
                        Ext.Ajax.request({
                          url:'system/updatePrivilege.js',
                          params:{
                            'privilegeID':id,
                            'privilegeName':name
                          },
                          success:function(response){
                            var result = eval(response.responseText);
                            if(result){
                              main.add(result);
                              main.setActiveTab(result);
                            }
                          }.createDelegate(this)
                        });
                      }
                  }
                  return "<a href='#' onclick=\"updatePrivilegeFn('"+record.data.privilegeID+"','"+record.data.privilegeName+"')\">${text.All.update}</a>" +
                        "　<a href='#' onclick=\"removePrivilegeFn('"+record.data.privilegeID+"','"+record.data.privilegeName+"')\">${text.All.remove}</a>";
               }
            }
          ]
        });

        this.cm.defaultSortable = true;

        this.sm = new Ext.grid.RowSelectionModel({
          singleSelect : true
        });

        this.grid  = new Ext.grid.GridPanel({
          region:'center',
          border:false,
          cls:'left right',
          width:'67%',
          viewConfig: {
            forceFit:true
          },
          store: this.store,
          cm: this.cm,
          sm: this.sm,
          loadMask: true,
          bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: this.store,
            displayInfo:true,
            plugins: [
              new Ext.ux.plugins.PageComboResizer({pageSizes: [20, 30, 50, 100, 200, 500]})
            ]
          }),
          listeners:{
            scope:this,
            'dblclick':function(){
              this.actions.update.createDelegate(this)();
            }.createDelegate(this),
            'rowcontextmenu':function(grid,rowindex,e){
              this.sm.selectRow(rowindex,false);
              e.stopEvent();
              if(!this.gridCtxMenu){
                this.gridCtxMenu = new Ext.menu.Menu({
                  items:[{
                    text:'${text.All.add}',
                    handler:this.actions.insert.createDelegate(this)
                  },{
                    text:'${text.All.update}',
                    handler:this.actions.update.createDelegate(this)
                  },'-',{
                    text:'${text.All.refresh}',
                    handler:this.actions.reload.createDelegate(this)
                  },'-',{
                    text:'${text.All.remove}',
                    handler:this.actions.remove.createDelegate(this)
                  }]
                });
              }
              this.gridCtxMenu.showAt(e.getPoint());
            }.createDelegate(this)
          }
        });

        Ext.apply(this,{
          id:'PrivilegeManager',
          title:'${text.Privilege.manager}',
          closable:true,
          iconCls : 'icon-offline',
          shim : false,
          animCollapse : false,
          resizable:false,
          border: false,
          layout : 'border',
          items : [this.grid]
        });

        this.actions = {

          reload:function(){
            this.store.reload();
          },

          insert:function (){
            Ext.Ajax.request({
              url:'system/insertPrivilege.js',
              success:function(response){
                var result = eval(response.responseText);
                if(result){
                  main.add(result);
                  main.setActiveTab(result);
                }
              }.createDelegate(this)
            });
          },

          remove : function(){
            var record = this.sm.getSelected();
            if(record){
              Ext.MessageBox.confirm('${text.All.confirm}','${text.All.confirm}${text.All.remove}${text.Privilege}？',function(btn){
                if(btn == 'yes'){
                  Ext.Ajax.request({
                    url:'system/removePrivilege.action',
                    params:{
                      "privilege.privilegeID"    :record.data.privilegeID,
                      "privilege.privilegeName"  :record.data.privilegeName
                    },
                    success:function(response){
                      var j=Ext.decode(response.responseText);
                      if(j.success===true){
                        this.store.remove(record);
                      }
                    }.createDelegate(this)
                  });
                }
              }.createDelegate(this));
            }
          },

          update : function(){
            var record = this.sm.getSelected();
            if(record){
              var temp = main.get(record.data.privilegeID);
              if(temp){
                main.setActiveTab(temp);
              }else{
                Ext.Ajax.request({
                  url:'system/updatePrivilege.js',
                  params:{
                    'privilegeID'    :record.data.privilegeID,
                    'privilegeName'  :record.data.privilegeName
                  },
                  success:function(response){
                    var result = eval(response.responseText);
                    if(result){
                      main.add(result);
                      main.setActiveTab(result);
                    }
                  }.createDelegate(this)
                });
              }
            }
          }.createDelegate(this)
        };

        this.onBeforeDestroy = function(){
          if(this.gridCtxMenu)this.gridCtxMenu.destroy();
          this.grid.destroy();
          delete this.grid;
          for(p in this.actions){
            delete p;
          }
          delete this.actions;
        }.createDelegate(this);
        this.on('beforedestroy',this.onBeforeDestroy);

        Sliver.Privilege.PrivilegeManager.superclass.initComponent.call(this, arguments);
    }
  });
  return new Sliver.Privilege.PrivilegeManager();
})();