 Ext.ns('activity1');

activity1.entriesGrid = Ext.extend(Ext.grid.GridPanel, {
  autoload:false,
  enableHdMenu:false,
  store: new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
      url:'/process/EMBrowser/site/counter/entries/process.activityentries.php',
      method: 'POST'
    }),
    reader: new Ext.data.JsonReader({
      root: 'results',
      totalProperty: 'totalCount'
    },[
      {name: 'entry_id', type: 'int', mapping: 'entry_id'},
      {name: 'value', type: 'string', mapping: 'value'},
      {name: 'data', type: 'int', mapping: 'data'},
      {name: 'price', type: 'int', mapping: 'price'},
      {name: 'start_date', type: 'string', mapping: 'start_date'},
      {name: 'end_date', type: 'string', mapping: 'end_date'}
    ])
  }),
  initComponent:function() {
    this.actionEdit = new Ext.ux.grid.RowActions({
      header:'&nbsp;'
      ,width: 50
      ,scope:this
      ,autoWidth:false
      ,hideMode:'display'
      ,keepSelection:true
      ,actions:[{
        iconCls:'icon-edit'
        ,tooltip:'Modifier'
      }]
    });
    var CheckColumn = new Ext.grid.CheckColumn({
      header: " ",
      dataIndex: 'checkbox',
      hideable:false,
      width: 35
    });
    var config = {
      plugins:[CheckColumn,this.actionEdit],
      tbar: new Ext.Toolbar({
        items:[new Ext.Toolbar.Button({
          text: 'Suppression',
          iconCls:'remove'
      })]}),
      bbar: new Ext.PagingToolbar({
        pageSize: 10,
        ownerCt: this,
        store: this.store,
        displayInfo: true,
        
        items:['->']
      }),
      cm: new Ext.grid.ColumnModel([
        {
          header: 'entry_id',
          dataIndex:'entry_id'
          ,hidden:true
          ,hideable:false
        },
        CheckColumn,
        {
          header: 'data',
          dataIndex:'data'
          ,hidden:true
          ,hideable:false
        },{
          header: 'Valeur',
          dataIndex: 'value',
          width: 100
        },{
          header: 'Prix',
          dataIndex: 'price',
          width: 100
        },{
          header: 'Début',
          dataIndex: 'start_date',
          width: 65
        },{
          header: 'Fin',
          dataIndex: 'end_date',
          width: 65
        }
        ,this.actionEdit
        ])
    }; 
     // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    // call parent initComponent
    activity1.entriesGrid.superclass.initComponent.call(this);
  } // end of function initComponent
  ,onRender:function() {
    activity1.entriesGrid.superclass.onRender.apply(this, arguments);
  } // eo function onRender
  ,refresh:function(activity_id) {
    this.activity_id = activity_id;
    this.store.load({
       url:this.url
      ,waitMsg:'Loading...'
      ,params:{cmd:'list',activity_id:activity_id,start:0,limit:10}
    });
  }
  ,initPrice:function(activity_id) {
    Ext.Ajax.request({
      ownerCt: this,
      waitMsg: 'Please wait...',
      url:'/process/EMBrowser/site/counter/entries/process.activityentries.php',
      params: {
        cmd: "initprice",
        activity_id:activity_id
      },
      success: function(response,scope){
        var result = Ext.util.JSON.decode(response.responseText);
        switch(result.success){
          case 1:
            scope.ownerCt.colModel.setColumnHeader(4,result.price);
          break;
          default:
            Ext.MessageBox.alert('Attention',result.error);
          break;
        }
      },
      failure: function(response) {
        Ext.MessageBox.alert('error','L application n a pas pu se connecter a la base de données. Veuillez réessayer plus tard');
      }
    });
    
  }
  ,getCheckBoxValues:function() {
    var entries = [];
    for(i = 0; i< this.getStore().getCount(); i++){
      if(this.getStore().getAt(i).get('checkbox')){
        entries.push(this.getStore().getAt(i).get('entry_id'));
      }
      
    }
    return entries;
  }
  ,
  countCheck:function() {
    var check = 0;
    for(i = 0; i< this.getStore().getCount(); i++){
      if(this.getStore().getAt(i).get('checkbox')){
        check++;
      }
    }
    return check;
  }
});

Ext.reg('activity1entriesgrid', activity1.entriesGrid);

