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

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

class ViewInputRuang extends HtmlResponse {
	function TemplateModule()
   {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/gedung/template');
		$this->SetTemplateFile('view_input_ruang.html');
	}
   
   function ProcessRequest()
   {
      $Obj = new Ruang;
      $gId = $Obj->gId;
		$User = $Obj->GetDataUserByUsername($_SESSION['username']);
      if ($gId === false)
      {
         $urlRedirect = Dispatcher::Instance()->GetUrl('gedung','gedung','view','html');
         if (isset($_GET['ascomponent']) AND $_GET['ascomponent']->Raw())
            $urlRedirect .= '&ascomponent=1';
         $this->RedirectTo($urlRedirect);
         return null;
      }
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi default value
      $data = array
      (
         "kode" => '',
         "nama" => '',
         "jnsRuangId" => 0,
         "kapasitas" => 0,
         "luas" => 0,
         "panjang" =>0,
         "lebar" =>0,
         "tipeRuangId" => 0,
         "fasilitas_ruang" => array(),
         "jmlKursi" => 0,
         "jmlMeja" => 0,
         "jmlKursi" => 0,
         "konstruksi" => array(),
         "unit_id" => '',
         "unit_nama" => '',
         "kondisiRuangId" => 0,
         "picId" => 0,
         "oldFoto" => '',
			"warna_cat" => '',
			"merk_cat" => '',
			"nomor_cat" => '',
			"lantaiId" => $_GET['lantaiId']->integer()->Raw()
      );
		
      if (is_array($this->Data)) {
         $data = array_intersect_key($this->Data, $data) + $data;
         $return['fasilitas_ruang'] = $Obj->GetFasilitasList();
         foreach (array_keys($this->Data['fasjml_']) as $key) if ($this->Data['fasjml_'][$key] > 0)
         {
            $return['fasilitas_ruang'][$key]['id_fas_ruang'] = $return['fasilitas_ruang'][$key]['id_fasilitas'];
            $return['fasilitas_ruang'][$key]['jml_fasilitas'] = $this->Data['fasjml_'][$key];
         }
      }
      elseif (isset($_GET['id']))
      {
         $result = $Obj->GetDataRuangById($_GET['id']->Integer()->Raw());
         // unit pengguna ruang
         $return['unit_pengguna_ruang'] = $Obj->GetUnitPenggunaRuang($_GET['id']->Integer()->Raw());
         if ($result == false) {
            unset($_GET['id']);
            $return['fasilitas_ruang'] = $Obj->GetFasilitasList();
         }
         else
         {
            $return['fasilitas_ruang'] = $Obj->GetFasilitasByRuangId($_GET['id']->Integer()->Raw());
            $result['konstruksi'] = $Obj->GetKonstruksiByRuangId($_GET['id']->Integer()->Raw());
            $data = array_intersect_key($result, $data) + $data;
         }
      }else $return['fasilitas_ruang'] = $Obj->GetFasilitasList();
      
      $return['data'] = $data;
      // ---------
      
      // render combo box
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_ruang', array('jnsRuangId', $Obj->ComboJnsRuang(), $data['jnsRuangId'], false, ''), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tipe_ruang', array('tipeRuangId', $Obj->ComboTipeRuang(), $data['tipeRuangId'], false, ''), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kondisi_ruang', array('kondisiRuangId', $Obj->ComboKondisiRuang(), $data['kondisiRuangId'], false, ''), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'pic_ruangan', array('picId', $Obj->ComboPicRuangan(), $data['picId'], false, ''), Messenger::CurrentRequest);
      // ---------
      
      // inisialisasi link address
      $return['link']['url_action'] = Dispatcher::Instance()->GetUrl('gedung', 'inputRuang', 'do', 'html');
      if (isset($_GET['id'])) $return['link']['url_action'] .= '&id='.$_GET['id']->Integer()->Raw();
      // ---------
      
      // inisialisasi data lain
      #$return['fasilitasList'] = $Obj->GetFasilitasList();
      $return['konstruksiRuangList'] = $Obj->GetKonstruksiRuangList();
      $return['comboJnsKonstruksi'] = $Obj->ComboJnsKonstruksi();
      $return['gedung']['gedungNama'] = $Obj->gedungNama;
      $return['gedung']['gedungKode'] = $Obj->gedungKode;
      // ---------
      
      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();
      $this->mrTemplate->AddVars('content', $data['gedung'], '');
      
      // render message box
      if($this->Pesan)
      {
         $msg = '';
         if (count($this->Pesan) > 1) foreach ($this->Pesan as $value)
            $msg .= "\t$value<br/>\n";
         else $msg .= $this->Pesan[0];
         
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $msg);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}
      // ---------
      
      // render link
      $this->mrTemplate->AddVar('content', 'URL_ACTION', $data['link']['url_action']);
      $this->mrTemplate->AddVar('content', 'URL_POP_UP_UNIT', Dispatcher::Instance()->GetUrl('gedung', 'PopUpUnitkerja', 'view', 'html'));
      $this->mrTemplate->AddVar('content', 'URL_POP_UP_UNIT_PENGGUNA_RUANG', Dispatcher::Instance()->GetUrl('gedung', 'PopUpUnitkerjaPenggunaRuang', 'view', 'html'));
      // ---------
      
      // render form
      $form = $data['data'];
      if (isset($_GET['id']))
         $form['judul'] = 'Ubah';
      else $form['judul'] = 'Tambah';
      unset($form['fasilitas'], $form['konstruksi']);
	  
	  //untuk ratio
	  if ($form['kapasitas']!=0 or $form['luas']!=0)
	  {$form['ratio']=$form['kapasitas']/$form['luas'];}
	  else $form['ratio']=0;
      // end utk ratio----
	  
	  $this->mrTemplate->AddVars('content', $form, '');
      // ---------
      
      // render list fasilitas
      #print_r($data['fasilitas_ruang']);
      if(!empty($data['fasilitas_ruang'])) {
         $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'NO');
         for($f=0; $f<sizeof($data['fasilitas_ruang']); $f++) {
            if(!empty($data['fasilitas_ruang'][$f]['id_fas_ruang'])) {
               $this->mrTemplate->AddVar('fasilitas_item', 'FAS_CHECKED', 'checked="checked"');
               $this->mrTemplate->AddVar('fasilitas_item', 'ID_RUANG_FASILITAS', $data['fasilitas_ruang'][$f]['id_fas_ruang']);
            }else{
               $this->mrTemplate->AddVar('fasilitas_item', 'FAS_CHECKED', '');
               $this->mrTemplate->AddVar('fasilitas_item', 'ID_RUANG_FASILITAS', '');
            }
            $this->mrTemplate->AddVar('fasilitas_item', 'FAS_ID', $data['fasilitas_ruang'][$f]['id_fasilitas']);
            if(empty($data['fasilitas_ruang'][$f]['jml_fasilitas']))
               $id_fas[] = $data['fasilitas_ruang'][$f]['id_fasilitas'];
               
            $this->mrTemplate->AddVar('fasilitas_item', 'FAS_NAMA', $data['fasilitas_ruang'][$f]['nama_fas']);
            $this->mrTemplate->AddVar('fasilitas_item', 'FAS_JML', $data['fasilitas_ruang'][$f]['jml_fasilitas']);
            $this->mrTemplate->parseTemplate('fasilitas_item', 'a');
         }
         $hide_komponen = implode('|',$id_fas);
         $this->mrTemplate->AddVar('content', 'HIDE_KOMPONEN', $hide_komponen);
      }
      else $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'YES');
      /*if (count($data['fasilitasList']) > 0)
      {
         $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'NO');
         
         $form = array();
         foreach ($data['fasilitasList'] as $value)
         {
            $form['fasilitasNama'] = $value['name'];
            $form['fasilitasId'] = $value['id'];
            $form['checked'] = (in_array($value['id'], $data['data']['fasilitas'])) ? 'checked' : '';
            
            $this->mrTemplate->AddVars('fasilitas_item', $form, '');
            $this->mrTemplate->parseTemplate('fasilitas_item', 'a');
         }
      }
      else $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'YES');*/
      // ---------
      
      // render list kontruksi ruang
      if (count($data['konstruksiRuangList']) > 0)
      {
         $this->mrTemplate->AddVar('konstruksi', 'KONSTRUKSI_EMPTY', 'NO');
         
         $form = array();
         foreach ($data['konstruksiRuangList'] as $value)
         {
            $form['konstruksiRuangNama'] = $value['name'];
            $form['konstruksiRuangId'] = $value['id'];
            
            $this->mrTemplate->clearTemplate('cbjnskonstruksi');
            foreach ($data['comboJnsKonstruksi'] as $value)
            {
               $option['jenisKonstruksiNama'] = $value['name'];
               $option['jenisKonstruksiId'] = $value['id'];
               $option['selected'] = (isset($data['data']['konstruksi'][$form['konstruksiRuangId']]) AND $data['data']['konstruksi'][$form['konstruksiRuangId']] == $value['id']) ? 'selected' : '';
            
               $this->mrTemplate->AddVars('cbjnskonstruksi', $option, '');
               $this->mrTemplate->parseTemplate('cbjnskonstruksi', 'a');
            }
            
            $this->mrTemplate->AddVars('konstruksi_item', $form, '');
            $this->mrTemplate->parseTemplate('konstruksi_item', 'a');
         }
      }
      else $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'YES');
      
      //unit pengguna ruang
      if(empty($data['unit_pengguna_ruang'])) {
         $this->mrTemplate->AddVar('tpl_pengguna_ruang_list', 'PENGGUNA_RUANG_LIST_EMPTY', 'YES');
      } else {
         $this->mrTemplate->AddVar('tpl_pengguna_ruang_list', 'PENGGUNA_RUANG_LIST_EMPTY', 'NO');
         $no_pengguna_ruang_list = array();
         $no_pengguna_ruang_list = $data['unit_pengguna_ruang'];
         $number=1;
         //print_r($datadbInvoice);
         for($i=0;$i<sizeof($data['unit_pengguna_ruang']);$i++) {
            $data['unit_pengguna_ruang'][$i]['number'] = ($i+1);
            $data['unit_pengguna_ruang'][$i]['id_delete'] = $data['unit_pengguna_ruang'][$i]['ruang_id'];
            $this->mrTemplate->AddVars('tpl_pengguna_ruang_item', $data['unit_pengguna_ruang'][$i], 'DATA_');
            $this->mrTemplate->parseTemplate('tpl_pengguna_ruang_item', 'a');
         }
      }
      // ---------
      
      $this->mrTemplate->AddVar('content', 'MAX_SIZE', GTFWConfiguration::GetValue('application','max_upload_size'));
      // foto ruangan
/*
      $foto = (string) $data['data']['oldFoto'];
      if ($foto !== '' AND file_exists(GTFW_APP_DIR."/images/data/$foto"))
      {
			$this->mrTemplate->SetAttribute('foto', 'visibility', 'visible');
			$this->mrTemplate->AddVar('foto', 'oldFoto', $foto);
      }
*/
      // ---------
   }
}
?>