<?php
require_once GTFWConfiguration::GetValue('application', 'docroot').
'module/mitra_kerja/business/MitraKerja.class.php';

//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';

class ViewInputMitraKerja extends HtmlResponse {
   function TemplateModule()
   {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/mitra_kerja/template');
		$this->SetTemplateFile('view_input_mitra.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new MitraKerja;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		@$this->Data = $msg[0][0];
		@$this->Pesan = $msg[0][1];
		@$this->css = $msg[0][2];
      
      if (is_array($this->Data))
         $data = $this->Data;
      elseif (isset($_GET['mkId']))
      {
         $result = $Obj->GetMitraById($_GET['mkId']->Integer()->Raw());
         if ($result == false) unset($_GET['mkId']);
         else $data = $result;
      }
	  
      @$return['data'] = $data;
      
      // combo_box jenis bidang usaha
      $bidUsahaList = $Obj->GetBidangUsahaList();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kode_bidusaha', array('', $bidUsahaList, '', false, 'id="bu_list"'), Messenger::CurrentRequest);
      foreach ($bidUsahaList as $value)
         $JSON_BIDUSAHA_LIST[$value['id']] = $value['name'];
      $return['JSON_BIDUSAHA_LIST'] = $JSON_BIDUSAHA_LIST;
	  
	  //combo_box bank
	  $getBank = $Obj->GetBankList();
	  Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'bank_id', array('', $getBank, '', false, 'id="bank_list"'), Messenger::CurrentRequest); 
	  if(!empty($getBank)){
	  foreach ($getBank as $value)
         $JSON_BANK_LIST[$value['id']] = $value['name'];
      $return['JSON_BANK_LIST'] = $JSON_BANK_LIST;
      }
	  
	  //untuk contact person
	  $return['JSON_CONTACT_PERSON'] = $Obj->GetMkCpById($_GET['mkId']->Integer()->Raw());
	  
	   // inisialisasi link address
      $return['link']['url_action'] = Dispatcher::Instance()->GetUrl('mitra_kerja', 'input', 'do', 'html');
      if (isset($_GET['mkId'])) $return['link']['url_action'] .= '&mkId='.$_GET['mkId']->Integer()->Raw();
	  
      return $return;
   }
   
   
   function TemplateLabeling(){
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      
      //patTemplate name yang memiliki label2 dinamic
      $arrContent=array("content");
      
      for($i=0;$i<count($arrContent);$i++){
         for($j=0;$j<count($arrLabel);$j++){
            $this->mrTemplate->AddVar($arrContent[$i],$arrLabel[$j]['labelCode'],$arrLabel[$j]['labelName']);
         }
      }
   }
   
   function ParseTemplate($data = NULL)
   {
      //set Label
      $this->TemplateLabeling();
    
      // render message box
      if($this->Pesan)
      {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
	  }
      
      // render form
      $this->mrTemplate->AddVar('content', 'URL_ACTION', $data['link']['url_action']);
      $this->mrTemplate->AddVar('content', 'JSON_BIDUSAHA_LIST', json_encode($data['JSON_BIDUSAHA_LIST']));
	  $this->mrTemplate->AddVar('content', 'JSON_BANK_LIST', json_encode($data['JSON_BANK_LIST']));
	  
	  if(!empty($data['data']['cp'])){
	  $dataCp = $data['data']['cp'];}else{
	  $dataCp = $data['JSON_CONTACT_PERSON'];}
	  
      $this->mrTemplate->AddVar('content', 'JSON_CONTACT_PERSON', json_encode($dataCp));
      // ---------
      
      $form = $data['data'];
      $mkBidUsaha = $form['mkBidUsaha'];
	  $mkBank = $form['mkBank'];
      unset($form['mkBidUsaha']);
	  unset($form['mkBank']);
      unset($form['cp']);
      if (isset($form['mkId']))
         $form['judul'] = 'Ubah';
      else $form['judul'] = 'Tambah';
      $this->mrTemplate->AddVars('content', $form, 'VALUE_');
      
      if(count($mkBidUsaha) > 0)
      {
			$this->mrTemplate->SetAttribute('json_add_bidang_usaha', 'visibility', 'visible');
         foreach ($mkBidUsaha as $value)
         {
            $this->mrTemplate->AddVar('json_add_bidang_usaha', 'JSON_ADD_BU_ID', $value);
            $this->mrTemplate->parseTemplate('json_add_bidang_usaha', 'a');
         }
	  }
		
	  if(count($mkBank) > 0)
      {
			$this->mrTemplate->SetAttribute('json_add_bank', 'visibility', 'visible');
         foreach ($mkBank as $value)
         {
            $this->mrTemplate->AddVar('json_add_bank', 'JSON_ADD_BANK_ID', $value);
            $this->mrTemplate->parseTemplate('json_add_bank', 'a');
         }
	  }
      
   }
}
?>