Ext.ns('ES');

ES.HeatingMainPanel = 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:'ESheatingleftpanel',
        height:804,
        width:250
      },{
        xtype:'ESheatingcenterpanel',
        margins: '5 5 5 5',
        bodyStyle: 'padding:5px',
        height:804,
        width:444
      }]
    };
    // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    ES.HeatingMainPanel.superclass.initComponent.apply(this, arguments);
    this.heatingCounterGrid = this.items.itemAt(0).items.itemAt(0);
    this.heatingCenterPanel = this.items.itemAt(1);
    this.heatingCounterInfo = this.heatingCenterPanel.items.itemAt(0);
    this.heatingCounterEntriesGrid = this.heatingCenterPanel.items.itemAt(1);
    this.heatingCounterGrid.on('rowclick',this.refreshHeatingCounterInfos,this);
    this.heatingCounterGrid.topToolbar.items.itemAt(1).on('click',this.deleteCounters,this);
    this.heatingCounterGrid.actionDetails.callbacks = {
      'icon-management':function(grid, record, action, row, col) {
        this.refreshHeatingCounterInfos(grid, row, action);
      }.createDelegate(this)
    }
    this.heatingCounterGrid.action.callbacks = {
      'icon-edit':function(grid, record, action, row, col) {
        this.initEditHeatingCounterFormWindow(grid, row);
      }.createDelegate(this)
    }
  }
  ,onRender:function() {
    ES.HeatingMainPanel.superclass.onRender.apply(this, arguments);
    this.isHeatingDemo();
  } // eo function onRender
  ,refreshHeatingCounterInfos:function(grid,rowIndex,e){
    var cm = grid.colModel;
    var readindex = cm.findColumnIndex('counter_id');
    var record = grid.getStore().getAt(rowIndex);
    this.counter_id = record.get('counter_id');
    this.heatingCounterInfo.refresh(this.counter_id);
    this.heatingCounterEntriesGrid.refresh(this.counter_id);
    this.heatingCounterInfo.on("gotUnit",this.setUnitLabel,this);
  }
  ,setUnitLabel:function() {
   this.heatingCounterEntriesGrid.setUnitLabel(this.heatingCounterInfo.unitArray);
  }
  ,deleteCounters: function() {
    if(this.heatingCounterGrid.countCheck() > 0) {
      if(this.heatingCounterGrid.isCheck(this.counter_id)){
        this.heatingCounterInfo.hide();
        this.heatingCounterEntriesGrid.hide();
      }
      Ext.Ajax.request({
        ownerCt: this.heatingCounterGrid,
        waitMsg: 'Please wait...',
        url: '/process/extjs/site/consumption/heating/heatingcounter.php',
        params: {
          tn_ids:Ext.util.JSON.encode(this.heatingCounterGrid.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
      });
    }
  }
  ,initEditHeatingCounterFormWindow:function (grid, rowIndex){
    cm = grid.colModel;
    readindex = cm.findColumnIndex('counter_id');
    var record = grid.getStore().getAt(rowIndex);
    this.counter_id = record.get('counter_id');
    if(this.heatingCounterFormWindow == null || !this.heatingCounterFormWindow.isVisible()){
      this.heatingCounterFormWindow = new ES.HeatingEditCounterFormWindow();                                    
      this.heatingCounterFormWindow.title = "Modification du compteur";
      this.heatingCounterForm = this.heatingCounterFormWindow.items.itemAt(0);
      this.heatingCounterFormButton = this.heatingCounterForm.buttons[0];
      var scope_heatingcounter = this;
      this.heatingCounterFormButton.on({
        scope:scope_heatingcounter,
        click:function(scope){
          this.editHeatingCounter();
        }
      });
      this.heatingCounterFormWindow.show();
    }else {
      this.heatingCounterFormWindow.toFront();
    }
    this.heatingCounterForm.setField('heatingcountername',record.get('countername'));
  }
  ,editHeatingCounter: function() {
    if(this.heatingCounterForm.isValid()){
      Ext.Ajax.request({
        ownerCt: this,
        waitMsg: 'Please wait...',
        url: '/process/extjs/site/consumption/heating/heatingcounter.php',
        params: {
          cmd:"update",
          counter_id:this.counter_id,
          countername: this.heatingCounterForm.getField('heatingcountername').getValue()
        },
        success: function(response,scope){
          var result = Ext.util.JSON.decode(response.responseText);
          switch(result.success){
          case 1:
            Ext.MessageBox.alert(
              'Modification du compteur'
              ,'Le compteur chauffage a été modifié.'
              ,function(){
                scope.ownerCt.heatingCounterGrid.refresh(scope.ownerCt.counter_id);
                scope.ownerCt.heatingCounterInfo.refresh(scope.ownerCt.counter_id);
                scope.ownerCt.heatingCounterFormWindow.close();
              }
            ,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');
    }
  }
  ,isHeatingDemo: function(){
    Ext.Ajax.request({
      ownerCt: this,
      waitMsg: 'Please wait...',
      url: '/process/extjs/site/consumption/heating/heatingcounter.php',
      params: {cmd: 'heatingDemo'},
      success: function(response,scope){
        var result = Ext.util.JSON.decode(response.responseText);
        switch(result.success){
          case 1:
            scope.ownerCt.heatingCounterGrid.demoModality();
            scope.ownerCt.heatingCounterEntriesGrid.demoModality();
          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');
      }
    });
  }
});

Ext.reg('ESheatingMainPanel', ES.HeatingMainPanel);
