


Ext.ns('ES');

ES.ElectricityMainPanel = Ext.extend(Ext.Panel,{
  width: 700,
  layout:'table',
  layoutConfig:{columns:2},
  defaults: {
    bodyStyle:'padding:1px'
  },
  bodyStyle: 'padding:2px',
  frame:false,
  height:810,
  initComponent:function() {
// hard coded config - cannot be changed from outside
    var config = {
      items:[{
        xtype:'ESelectricityleftpanel',
        margins: '5 5 5 5',
        height:804,
        width:250
      },{
        xtype:'ESelectricitycenterpanel',
        margins: '5 5 5 5',
        bodyStyle: 'padding:5px',
        height:804,
        width:444
      }]
    };
    // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    ES.ElectricityMainPanel.superclass.initComponent.apply(this, arguments);
    this.electricityCounterGrid = this.items.itemAt(0).items.itemAt(0);
    this.electricityCenterPanel = this.items.itemAt(1);
    this.electricityCounterInfo = this.electricityCenterPanel.items.itemAt(0);
    this.electricityCounterEntriesGrid = this.electricityCenterPanel.items.itemAt(1);
    this.electricityCounterGrid.on('rowclick',this.refreshElectricityCounterInfos,this);
    this.electricityCounterGrid.topToolbar.items.itemAt(1).on('click',this.deleteCounters,this);
//    this.electricityCounterInfo.buttons[0].on('click',this.initUpdateElectricityCounterFormWindow,this);
    this.electricityCounterGrid.actionDetails.callbacks = {
      'icon-management':function(grid, record, action, row, col) {
        this.refreshElectricityCounterInfos(grid, row, action);
      }.createDelegate(this)
    }
    this.electricityCounterGrid.action.callbacks = {
      'icon-edit':function(grid, record, action, row, col) {
        this.initUpdateElectricityCounterFormWindow(grid, row);
      }.createDelegate(this)
    }
  }
  ,onRender:function() {
    ES.ElectricityMainPanel.superclass.onRender.apply(this, arguments);
    this.isElectricityDemo();
  } // eo function onRender
  ,refreshElectricityCounterInfos:function(grid,rowIndex,e){
    cm = grid.colModel;
    readindex = cm.findColumnIndex('counter_id');
    var record = grid.getStore().getAt(rowIndex);
    this.counter_id = record.get('counter_id');
    this.electricityCounterInfo.refresh(this.counter_id);
    this.electricityCounterEntriesGrid.refresh(this.counter_id);
  }
  
  ,initUpdateElectricityCounterFormWindow:function (grid, rowIndex){
    var cm = grid.colModel;
    readindex = cm.findColumnIndex('counter_id');
    var record = grid.getStore().getAt(rowIndex);
    this.counter_id = record.get('counter_id');
    if(this.electricityCounterFormWindow == null || !this.electricityCounterFormWindow.isVisible()){
      this.electricityCounterFormWindow = new ES.ElectricityCounter_FormWindow();
      this.electricityCounterFormWindow.title = "Modification du compteur electricité";
      this.electricityCounterForm = this.electricityCounterFormWindow.items.itemAt(0);
      var scope_app_electricitycounter_layout = this;
      this.electricityCounterForm.buttons[0].on({
        scope:scope_app_electricitycounter_layout,
        click:function(scope){
          this.updateCounter();
        }
      });
      this.electricityCounterFormWindow.show();
    }else {
      this.electricityCounterFormWindow.toFront();
    }
    this.electricityCounterForm.hideFairType();
    this.electricityCounterForm.setField('electricitycountername',record.get('countername'));
  }
  ,updateCounter: function() {
    if(this.electricityCounterForm.isValid()){
      Ext.Ajax.request({
        ownerCt: this,
        waitMsg: 'Please wait...',
        url: '/process/extjs/site/consumption/electricity/electricitycounter.php',
        params: {
          cmd: 'update',
          counter_id:this.counter_id,
          countername: this.electricityCounterForm.getField('electricitycountername').getValue()
        },
        success: function(response,scope){
          var result = Ext.util.JSON.decode(response.responseText);
          switch(result.success){
            case 1:
              Ext.MessageBox.alert(
                'Modification du compteur electricité'
                ,'Le nom du compteur a été modifié.'
                ,function(){
                  scope.ownerCt.electricityCounterFormWindow.close();
                  scope.ownerCt.electricityCounterInfo.refresh(scope.ownerCt.counter_id);
                  scope.ownerCt.electricityCounterGrid.refresh();
                }
              ,scope);
            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');
        }
      });
    }else {
      Ext.MessageBox.alert('Attention','Le formulaire n est pas valide');
    }
  },deleteCounters: function() {
    if(this.electricityCounterGrid.countCheck() > 0) { 
      if(this.electricityCounterGrid.isCheck(this.counter_id)){
        this.electricityCounterInfo.hide();
        this.electricityCounterEntriesGrid.hide();
      }
      Ext.Ajax.request({
        ownerCt: this.electricityCounterGrid,
        waitMsg: 'Please wait...',
        url: '/process/extjs/site/consumption/electricity/electricitycounter.php',
        params: {
          tn_ids:Ext.util.JSON.encode(this.electricityCounterGrid.getCheckBoxValues()) 
          ,cmd:'delete'
        },
        success: function(response,scope){
          scope.ownerCt.refresh(0);
        },
        failure: function() {
        }
      });
    } else {
      Ext.MessageBox.show({
        title: 'Aucun élément sélectionné',
        msg: 'Pour supprimer, veuillez sélectionner les éléments à supprimer avec les checkbox',
        width: 300
      });
    }
  }
  ,isElectricityDemo: function(){
    Ext.Ajax.request({
      ownerCt: this,
      waitMsg: 'Please wait...',
      url: '/process/extjs/site/consumption/electricity/electricitycounter.php',
      params: {cmd: 'demo'}
      ,
      success: function(response,scope){
        var result = Ext.util.JSON.decode(response.responseText);
        switch(result.success){
          case 1:
            scope.ownerCt.electricityCounterGrid.demoModality();
            scope.ownerCt.electricityCounterEntriesGrid.demoModality();
          break;
        }
      }
    });
  }
});

Ext.reg('ESelectricityMainPanel', ES.ElectricityMainPanel);
