 Ext.ns('energywater');

energywater.mainPanel = Ext.extend(Ext.Panel,{
  title:"Gestion des compteurs Energétiques",
  layout:'table',
  layoutConfig:{columns:2},
  collapsed:true,
  collapsible:true,
  bodyCfg: {cls:'x-panel-body-em' },
  frame:false,
  initComponent:function() {
  // hard coded config - cannot be changed from outside
    var config = {
      items:[{
        xtype:'energywaterleftpanel',
        margins: '5 5 5 5',
        bodyStyle: 'padding:1px',
        height:505,
        width:280
      },{
        xtype:'energywatercenterpanel',
        margins: '5 5 5 5',
        bodyStyle: 'padding:2px',
        height:502,
        width:448
      }]
    };
    // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    energywater.mainPanel.superclass.initComponent.apply(this, arguments);
    this.leftPanel = this.items.itemAt(0);
    this.centerPanel = this.items.itemAt(1);
    this.counterGrid = this.leftPanel.items.itemAt(0);
    this.entriesMainPanel = this.centerPanel.items.itemAt(1);
    //this.counterinfos = this.entriesMainPanel.items.itemAt(0);
    this.entriesForm = this.entriesMainPanel.items.itemAt(0);
    this.entriesGrid = this.entriesMainPanel.items.itemAt(1);
    this.counterGrid.on('rowclick',this.readCounter,this);
    this.gridDeleteButton=this.counterGrid.topToolbar.items.itemAt(1);
    this.gridDeleteButton.on('click',this.deleteCounters,this);
    this.counterGrid.actionEdit.callbacks = {
      'icon-edit':function(grid, record, action, row, col) {
        this.editCounter(grid, row);
      }.createDelegate(this)
    }
//waterCounterInfos:
    //this.waterCounterInfos = this.counterinfos.items.itemAt(0);
    //this.waterCounterInfos.init_tpl(true);
    //this.waterCounterInfos.initModifyAction();
    //this.waterCounterInfos.buttons[0].on('click',this.initEditWaterCounterFormWindow,this);
// energyCounterInfos:
    //this.energyCounterInfos = this.counterinfos.items.itemAt(1);
    //this.energyCounterInfos.initModifyAction();
    //this.energyCounterInfos.init_tpl(true);
    //this.energyCounterInfos.buttons[0].on('click',this.initEditEnergyCounterFormWindow,this);
  }
  ,onRender:function() {
    energywater.mainPanel.superclass.onRender.apply(this, arguments);
  } // eo function onRender
  ,readCounter:function(grid,rowIndex,e){
    this.centerPanel.layout.setActiveItem('entriesmainpanel');
    var record = grid.getStore().getAt(rowIndex);
    var counter_id = record.get('counter_id');
    var type = record.get('type');
    this.entriesForm.init(type,counter_id);
    this.entriesGrid.init(type,counter_id);
  }
  ,deleteCounters: function(grid,rowIndex,e) {
    this.counterGrid.getCheckBoxValues();
  }
  ,editCounter: function(grid, rowIndex){
    var record = grid.getStore().getAt(rowIndex);
    var counter_id = record.get('counter_id');
    var type = record.get('type');
    switch(type) {
      case "water" :
        this.initEditWaterCounterFormWindow(counter_id);
      break;
      case "energy" :
        this.initEditEnergyCounterFormWindow(counter_id);
      break;
      case "elecdouble" :
        this.initEditEnergyCounterFormWindow(counter_id);
      break;
      case "elecsimple" :
        this.initEditEnergyCounterFormWindow(counter_id);
      break;
    }
  }
  ,initEditWaterCounterFormWindow:function (counter_id){
    var site_tn_id = this.counterGrid.tn_id;
    if(this.waterCounterFormWindow == null || !this.waterCounterFormWindow.isVisible()){
      this.waterCounterFormWindow = new water.counterFormWindow();
      this.waterCounterForm = this.waterCounterFormWindow.items.itemAt(0);
      this.waterCounterForm.init(site_tn_id);
      this.waterCounterForm.on("afterlayout",function() {
        this.waterCounterForm.getField("division").store.on("load",function(){
          this.waterCounterForm.initEdit(counter_id);
        },this,{single: true});
      },this,{single: true});
      this.waterCounterFormButton = this.waterCounterForm.buttons[0];
      var scope_watercounter = this;
      this.waterCounterFormButton.on({
        scope:scope_watercounter,
        click:function(scope){
          this.editWaterCounter(counter_id);
        }
      });
      this.waterCounterFormWindow.show();
    }else {
      this.waterCounterFormWindow.toFront();
    }
  }
  ,editWaterCounter: function(counter_id) {
    if(this.waterCounterForm.isValid()){
      Ext.Ajax.request({
        ownerCt: this,
        waitMsg: 'Please wait...',
        url: this.waterCounterForm.url,
        params: {
          cmd:"update",
          counter_id:counter_id,
          countername: this.waterCounterForm.getField('watercountername').getValue(),
          division_id: this.waterCounterForm.getField('division').getValue(),
          usagetype: this.waterCounterForm.getField('usagetype').getValue()
        },
        success: function(response,scope){
          var result = Ext.util.JSON.decode(response.responseText);
          switch(result.success){
            case 1:
              scope.ownerCt.counterGrid.fireEvent("modifycounter",this.counterGrid);
              scope.ownerCt.counterGrid.refresh(scope.ownerCt.counterGrid.tn_id);
              scope.ownerCt.waterCounterFormWindow.close();
            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');
    }
  }
  ,initEditEnergyCounterFormWindow:function (counter_id){
    var site_tn_id = this.counterGrid.tn_id;
    if(this.energyCounterModifyFormWindow == null || !this.energyCounterModifyFormWindow.isVisible()){
      this.energyCounterModifyFormWindow = new energy.counterModifyFormWindow();
      this.energyCounterFormModify = this.energyCounterModifyFormWindow.items.itemAt(0);
      this.energyCounterFormModify.init(site_tn_id);
      this.energyCounterFormModify.on("afterlayout",function() {
        this.energyCounterFormModify.getField("division").store.on("load",function(){
          this.energyCounterFormModify.initEdit(counter_id);
        },this,{single: true});
      },this,{single: true});
      this.energyCounterFormModifyButton = this.energyCounterFormModify.buttons[0];
      var scope_energycounter = this;
      this.energyCounterFormModifyButton.on({
        scope:scope_energycounter,
        click:function(scope){
          this.editEnergyCounter(counter_id);
        }
      });
      this.energyCounterModifyFormWindow.show();
    }else {
      this.energyCounterModifyFormWindow.toFront();
    }
  }
  ,editEnergyCounter: function(counter_id) {
    if(this.energyCounterFormModify.isValid()){
      Ext.Ajax.request({
        ownerCt: this,
        waitMsg: 'Please wait...',
        url: this.energyCounterFormModify.url,
        params: {
          cmd:"update",
          counter_id:counter_id,
          countername: this.energyCounterFormModify.getField('energycountername').getValue(),
          division_id: this.energyCounterFormModify.getField('division').getValue(),
          defaultenergysuppliername: this.energyCounterFormModify.getField('defaultenergysuppliername').getValue(),
          usagetype: this.energyCounterFormModify.getField('energyusagetype').getValue()
        },
        success: function(response,scope){
          var result = Ext.util.JSON.decode(response.responseText);
          switch(result.success){
            case 1:
              scope.ownerCt.counterGrid.fireEvent("modifycounter",scope.ownerCt.counterGrid);
              scope.ownerCt.counterGrid.refresh(scope.ownerCt.counterGrid.tn_id);
              scope.ownerCt.energyCounterModifyFormWindow.close();
            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');
    }
  }
});

Ext.reg('energywatermainpanel', energywater.mainPanel);
