Ext.ns('site');

site.mainPanel = Ext.extend(Ext.Panel,{
   layout: 'column'
   ,renderTo: 'EMBrowsersite'
   ,border: false
   ,defaults: {
        columnWidth: 1,
        style: {
          padding: '5px'
        }
    }
   ,initComponent:function() {
    // hard coded config - cannot be changed from outside
    var config = {
      items:[{
        xtype:'siteinfos'
      },{
        xtype:'energywatermainpanel',
        width: '730px',
        height:510
      },{
        xtype:'sitekeyingtabpanel',
        width: '730px',
        height:600
      }]
    };
    // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    // call parent initComponent
    site.mainPanel.superclass.initComponent.call(this);
    this.siteinfos = this.items.itemAt(0);
    this.energywatermainpanel = this.items.itemAt(1);
    this.energywaterleftpanel = this.energywatermainpanel.items.itemAt(0);
    this.energywatercountergrid = this.energywaterleftpanel.items.itemAt(0);
    this.sitekeyingtabpanel = this.items.itemAt(2);
    this.energywatercountergrid.on("modifycounter",this.refreshsitekeyingleftpanel,this);
    this.siteinfos.buttons[0].on("click",this.initEditSiteInfoWindow,this);
  } // end of function initComponent
  ,onRender:function() { 
    // call parent
    site.mainPanel.superclass.onRender.apply(this, arguments);
    // after parent code, e.g. install event handlers on rendered components
  } 
  ,refresh:function() {
    this.siteinfos.refresh(this.tn_id);
    this.energywatercountergrid.refresh(this.tn_id);
    this.sitekeyingtabpanel.refresh(this.tn_id);
  }
  ,refreshsitekeyingleftpanel:function() {
    this.sitekeyingtabpanel.refresh(this.tn_id);
  }
  ,initEditSiteInfoWindow: function(){
    if(this.siteInfoFormWindow == null || !this.siteInfoFormWindow.isVisible()){
      this.siteInfoFormWindow = new energy.infosWindow();
      this.siteInfoForm = this.siteInfoFormWindow.items.itemAt(0);
      this.siteInfoForm.initEdit(this.tn_id);
      this.siteInfoFormButton = this.siteInfoForm.buttons[0];
      var scope_siteinfo = this;
      this.siteInfoFormButton.on({
        scope:scope_siteinfo,
        click:function(scope){
          this.editSiteInfo(this.tn_id);
        }
      });
      this.siteInfoFormWindow.show();
    } else {
      this.siteInfoFormWindow.toFront();
    }
  }
  ,editSiteInfo : function(tn_id){
   if(this.siteInfoForm.isValid()){
      Ext.Ajax.request({
        ownerCt: this,
        method:'POST',
        waitMsg: 'Please wait...',
        url: '/process/EMBrowser/site/load_infos.php',
        params: {
          cmd:"update",
          tn_id: tn_id,
          sitename:this.siteInfoForm.getField('sitename').getValue(),
          activitysector_name: "",
          contact: this.siteInfoForm.getField('contact').getValue(),
          address: this.siteInfoForm.getField('address').getValue(),
          zip: this.siteInfoForm.getField('zip_code').getValue(),
          city: this.siteInfoForm.getField('city').getValue(),
          country: this.siteInfoForm.getField('country').getValue(),
          phone: this.siteInfoForm.getField('phone').getValue(),
          fax: this.siteInfoForm.getField('fax').getValue(),
          country_id: this.siteInfoForm.getField('country_id').getValue()
        },
        success: function(response,scope){
          var result = Ext.util.JSON.decode(response.responseText);
          switch(result.success){
            case 1:
              scope.ownerCt.siteinfos.refresh(scope.ownerCt.tn_id);
              scope.ownerCt.siteInfoFormWindow.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('sitemainpanel', site.mainPanel);



