Ext.ns('elecdouble');

elecdouble.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.energyentries.php',
      method: 'POST'
    }),
    baseParams:{cmd: "list"},
    reader: new Ext.data.JsonReader({
      root: 'results',
      totalProperty: 'totalCount'
    },[
      {name: 'entry_id', type: 'int', mapping: 'uid'},
      {name: 'high_value', type: 'int', mapping: 'high_value'},
      {name: 'low_value', type: 'int', mapping: 'low_value'},
      {name: 'price', type: 'int', mapping: 'price'},
      {name: 'power', type: 'int', mapping: 'power_value'},
      {name: 'start_date', type: 'string', mapping: 'start_date'},
      {name: 'end_date', type: 'string', mapping: 'end_date'},
      {name: 'period', type: 'string', mapping: 'period'},
      {name: 'energy_supplier_name', type: 'string', mapping: 'energy_supplier_name'}
    ])
  }),
  initComponent:function() {
    this.actionEdit = new Ext.ux.grid.RowActions({
      header:'&nbsp;'
      ,width: 30
      ,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: 25
    });
    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: 'HP(kWh)',
          dataIndex: 'high_value',
          width: 50
        },{
          header: 'HC(kWh)',
          dataIndex: 'low_value',
          width: 50
        },{
          header: 'Prix(CHF)',
          dataIndex: 'price',
          width: 55
        },{
          header: 'Puiss.(kW)',
          dataIndex: 'power',
          width: 57
        },{
          header: 'Période',
          dataIndex: 'period',
          width: 65
        },{
          header: 'Début',
          dataIndex: 'start_date',
          width: 65
          ,hidden:true
          ,hideable:false
        },{
          header: 'Fin',
          dataIndex: 'end_date',
          width: 65
          ,hidden:true
          ,hideable:false
        },{
          header: 'Fournisseur',
          dataIndex: 'energy_supplier_name',
          width: 72
        }
        ,this.actionEdit
        ])
    };
     // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    // call parent initComponent
    elecdouble.entriesGrid.superclass.initComponent.call(this);
  } // end of function initComponent
  ,onRender:function() {
    elecdouble.entriesGrid.superclass.onRender.apply(this, arguments);
  } // eo function onRender
  ,refresh:function(counter_id) {
    this.counter_id = counter_id;
    this.store.load({
       url:this.url
      ,waitMsg:'Loading...'
      ,params:{cmd:'list',counter_id:counter_id,start:0,limit:10}
    });
  }
  ,getCheckBoxValues:function() {
    var wentries = [];
    for(i = 0; i< this.getStore().getCount(); i++){
      if(this.getStore().getAt(i).get('checkbox')){
        wentries.push(this.getStore().getAt(i).get('entry_id'));
      }
    }
    return wentries;
  }
  ,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('elecdoubleentriesgrid', elecdouble.entriesGrid);

