<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/gedung/business/AppGedung.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/gedung/business/AppGedungAkademik.class.php';
//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';
class ViewInputGedung extends HtmlResponse {


   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/gedung/template');
      $this->SetTemplateFile('input_gedung.html');
   }
   
   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 ProcessRequest() {
      $Obj = new AppGedung();
      $Obj2 = new AppGedungAkademik();
      $get = $_GET->AsArray();
		
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $post = $msg[0][0];
		$notebox['msg'] = $msg[0][1];
		$notebox['css'] = $msg[0][2];
      $return['notebox'] = $notebox;
      // ---------
      
      //get data by id
      if (is_array($post))
      {
         $gedung['gd_id'] = $post['gedung_id'];
         $gedung['gd_brg_id'] = $post['aset_id'];
         $gedung['aset_kode'] = $post['kode_aset'];
         $gedung['aset_nama'] = $post['kode'];
         $gedung['gd_kode'] = $post['gedung_kode'];
         $gedung['gd_nama'] = $post['gedung_nama'];
         $gedung['gd_thn_perolehan'] = $post['thn_pemerolehan'];
         $gedung['gd_sbr_dana'] = $post['sbr_dana'];
         $gedung['biaya_pembangunan'] = $post['biaya_pembangunan'];
         $gedung['gd_luas_tanah'] = $post['luas_tanah'];
         $gedung['gd_luas_bangunan'] = $post['luas_bangunan'];
         $gedung['gd_sifat_bangunan'] = $post['sifat_bangunan'];
         $gedung['gd_imb'] = $post['imb'];
         $gedung['gd_peruntukan'] = $post['peruntukan_bangunan'];
         $gedung['gd_fas_listrik'] = $post['listrik'];
         $gedung['gd_fas_air'] = $post['air'];
         $gedung['gd_fas_telp'] = $post['telepon'];
         $gedung['gd_milik_brg_id'] = $post['kepemilikan'];
         $gedung['gd_peng_brg_id'] = $post['penguasaan'];
         $gedung['program_studi'] = $post['program_studi'];
         $gedung['gd_kondisi_id'] = $post['kondisi'];
         $gedung['umur_ekonomis'] = $post['umur_ekonomis'];
         $gedung['umur_teknis'] = $post['umur_teknis'];
         $gedung['nilai_residu'] = $post['nilai_residu'];
         $gedung['nilai_perolehan'] = $post['nilai_perolehan'];
         $gedung['gd_kmps_id'] = $post['lokasi_kampus'];
         $gedung['gd_keterangan'] = $post['keterangan'];
			$gedung['unitkerjaId'] = $post['unitkerjaId'];
         $gedung['unitkerjaNama'] = $post['unitkerjaNama'];
         $gedung['gd_denah'] = '';
         $gedung['gd_foto'] = '';
      }
      else $gedung = $Obj->GetGedungById(Dispatcher::Instance()->Decrypt($get['id_gedung']));
      if(!empty($gedung)){
         $sbr_dana = $gedung['gd_sbr_dana'];
         $kepemilikan = $gedung['gd_milik_brg_id'];
         $lokasi_kampus = $gedung['gd_kmps_id'];
         $kondisi = $gedung['gd_kondisi_id'];
         $peruntukan = $gedung['gd_peruntukan'];
         $penguasaan = $gedung['gd_peng_brg_id'];
			$idProdi = $Obj2->GetIdProdi(Dispatcher::Instance()->Decrypt($get['id_gedung']));
         $prodi = $idProdi['id'];
      }
      //combo
      $cbxsumberdana = $Obj->GetCbxSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sbr_dana', array('sbr_dana', $cbxsumberdana, $sbr_dana, false, ''), Messenger::CurrentRequest);
      
      $cbxkepemilikan = $Obj->GetCbxKepemilikan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepemilikan', array('kepemilikan', $cbxkepemilikan, $kepemilikan, false, ''), Messenger::CurrentRequest);
      
      $cbxlokasi = $Obj->GetCbxLokasiKampus();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'lokasi_kampus', array('lokasi_kampus', $cbxlokasi, $lokasi_kampus, false, ''), Messenger::CurrentRequest);
      
      $cbxkondisi = $Obj->GetCbxKondisi();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kondisi', array('kondisi', $cbxkondisi, $kondisi, false, ''), Messenger::CurrentRequest);
      
      $cbxperuntukan = $Obj->GetCbxPeruntukanBangunan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'peruntukan_bangunan', array('peruntukan_bangunan', $cbxperuntukan, $peruntukan, false, ''), Messenger::CurrentRequest);
      
      $cbxpenguasaan = $Obj->GetCbxPenguasaanBarang();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'penguasaan', array('penguasaan', $cbxpenguasaan, $penguasaan, false, ''), Messenger::CurrentRequest);
      
		$cbxprodi = $Obj2->ComboboxProdi();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'program_studi', array('program_studi', $cbxprodi, $prodi, false, ''), Messenger::CurrentRequest);
      
      $biaya_gedung = $Obj->GetBiayaGedung(Dispatcher::Instance()->Decrypt($get['id_gedung']));
      $biaya = $Obj->GetBiaya();
      
      if(!empty($get['id_gedung']))
         $fasilitas_gedung = $Obj->GetFasilitasGedungList(Dispatcher::Instance()->Decrypt($get['id_gedung']));
      else
         $fasilitas_gedung = $Obj->GetFasilitasGedungRef();
      
      #print_r($fasilitas_gedung);
      $return['detil_gedung'] = $gedung;
      $return['biaya_gedung'] = $biaya_gedung;
      $return['biaya'] = $biaya;
      $return['penanggung_jawab'] = $Obj->GetPenanggungjawabGedung();
      $return['fasilitas_gedung'] = $fasilitas_gedung;
      return $return;
   }
   
   function ParseTemplate($data = NULL) {
      #print_r($data['fasilitas_ref']);
      #print_r($data['fasilitas_gedung']);
      //set Label
      $this->TemplateLabeling();
      
      // render message box
      if($data['notebox']['msg'])
      {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['notebox']['msg']);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $data['notebox']['css']);
		}
      // ---------
		
      if(GTFWConfiguration::GetValue( 'application', 'sia')===false){
			$this->mrTemplate->AddVar('content', 'PROD', 'none');
		}
		
      if(!empty($_GET['id_gedung'])) {
         $label = 'Edit';
         $url_action = Dispatcher::Instance()->GetUrl('gedung', 'UpdateGedung' , 'do', 'html');
      }
      else {
         $label = 'Tambah';
         $url_action = Dispatcher::Instance()->GetUrl('gedung', 'AddGedung' , 'do', 'html');
      }
      $this->mrTemplate->AddVar('content', 'LABEL_ACTION', $label);
      $this->mrTemplate->AddVar('content', 'URL_ACTION', $url_action);
      $this->mrTemplate->AddVar('content', 'URL_POP_UP_ASET', Dispatcher::Instance()->GetUrl('gedung', 'PopUpAset' , 'view', 'html'));
	  $this->mrTemplate->AddVar('content', 'URL_POP_UP_UNIT_PJ', Dispatcher::Instance()->GetUrl('gedung', 'PopUpUnitPj' , 'view', 'html'));
	  $this->mrTemplate->AddVar('content', 'MAX_SIZE', GTFWConfiguration::GetValue('application','max_upload_size'));
      
      #biaya gedung
      if(!empty($data['biaya'])) {
         $this->mrTemplate->AddVar('biaya', 'BIAYA_EMPTY', 'NO');
         for($b=0; $b<sizeof($data['biaya']); $b++) {
            for($bg=0; $bg<sizeof($data['biaya_gedung']); $bg++) {
               if($data['biaya_gedung'][$bg]['id_biaya'] == $data['biaya'][$b]['id']) {
                  $this->mrTemplate->AddVar('list_biaya', 'NILAI_BIAYA', $data['biaya_gedung'][$bg]['nilai_biaya']);
               }/*else{
                  $this->mrTemplate->AddVar('list_biaya', 'NILAI_BIAYA', $data['biaya_gedung'][$bg]['nilai_biaya']);
               }*/
               $this->mrTemplate->AddVar('list_biaya', 'ID_GEDUNG_BIAYA', $data['biaya_gedung'][$b]['id_gedung_biaya']);
            }
            $this->mrTemplate->AddVars('list_biaya', $data['biaya'][$b], '');
            $this->mrTemplate->parseTemplate('list_biaya', 'a');
         }
      }
      else $this->mrTemplate->AddVar('biaya', 'BIAYA_EMPTY', 'YES');
      
      #fasilitas gedung
      $id_fas = array();
      if(!empty($data['fasilitas_gedung'])) {
         $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'NO');
         for($f=0; $f<sizeof($data['fasilitas_gedung']); $f++) {
            if(!empty($data['fasilitas_gedung'][$f]['id_fas_gdg'])) {
               $this->mrTemplate->AddVar('list_fasilitas_ref', 'FAS_CHECKED', 'checked="checked"');
               $this->mrTemplate->AddVar('list_fasilitas_ref', 'ID_GEDUNG_FASILITAS', $data['fasilitas_gedung'][$f]['id_fas_gdg']);
            }else{
               $this->mrTemplate->AddVar('list_fasilitas_ref', 'FAS_CHECKED', '');
               $this->mrTemplate->AddVar('list_fasilitas_ref', 'ID_GEDUNG_FASILITAS', '');
            }
            $this->mrTemplate->AddVar('list_fasilitas_ref', 'FAS_ID', $data['fasilitas_gedung'][$f]['id_fasilitas']);
            if(empty($data['fasilitas_gedung'][$f]['jml_fasilitas']))
               $id_fas[] = $data['fasilitas_gedung'][$f]['id_fasilitas'];
            
            $this->mrTemplate->AddVar('list_fasilitas_ref', 'FAS_NAMA', $data['fasilitas_gedung'][$f]['nama_fas']);
            $this->mrTemplate->AddVar('list_fasilitas_ref', 'FAS_KET', $data['fasilitas_gedung'][$f]['keterangan']);
            $this->mrTemplate->AddVar('list_fasilitas_ref', 'FAS_JML', $data['fasilitas_gedung'][$f]['jml_fasilitas']);
            $this->mrTemplate->parseTemplate('list_fasilitas_ref', 'a');
         }
      }
      else $this->mrTemplate->AddVar('fasilitas', 'FASILITAS_EMPTY', 'YES');
      
      $hide_komponen = implode('|',$id_fas);
      $this->mrTemplate->AddVar('content', 'HIDE_KOMPONEN', $hide_komponen);
      
      //$this->mrTemplate->AddVar('content', 'UNITKERJANAMA', $data['penanggung_jawab']);
      
      if(!empty($data['detil_gedung'])) {
         $this->mrTemplate->AddVar('content', 'ASET_KODE', $data['detil_gedung']['aset_kode']);
         $this->mrTemplate->AddVar('content', 'ASET_NAMA', $data['detil_gedung']['aset_nama']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_KODE', $data['detil_gedung']['gd_kode']);
         $this->mrTemplate->AddVar('content', 'ASET_ID', $data['detil_gedung']['gd_brg_id']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_ID', $data['detil_gedung']['gd_id']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_NAMA', $data['detil_gedung']['gd_nama']);
         $this->mrTemplate->AddVar('content', 'THN_PEMEROLEHAN', $data['detil_gedung']['gd_thn_perolehan']);
         $this->mrTemplate->AddVar('content', 'LUAS_TANAH', $data['detil_gedung']['gd_luas_tanah']);
         $this->mrTemplate->AddVar('content', 'LUAS_BANGUNAN', $data['detil_gedung']['gd_luas_bangunan']);
         $this->mrTemplate->AddVar('content', 'BIAYA_PEMBANGUNAN', $data['detil_gedung']['biaya_pembangunan']);
         $this->mrTemplate->AddVar('content', 'UMUR_EKONOMIS', $data['detil_gedung']['umur_ekonomis']);
         $this->mrTemplate->AddVar('content', 'UMUR_TEKNIS', $data['detil_gedung']['umur_teknis']);
         $this->mrTemplate->AddVar('content', 'NILAI_RESIDU', $data['detil_gedung']['nilai_residu']);
         $this->mrTemplate->AddVar('content', 'NILAI_PEROLEHAN', $data['detil_gedung']['nilai_perolehan']);
			$this->mrTemplate->AddVar('content', 'UNITKERJAID', $data['detil_gedung']['unitkerjaId']);
         $this->mrTemplate->AddVar('content', 'UNITKERJANAMA', $data['detil_gedung']['unitkerjaNama']);
			
         if($data['detil_gedung']['gd_sifat_bangunan'] == 'Semi permanen')
            $this->mrTemplate->AddVar('content', 'SP_SELECT', 'selected="selected"');
         elseif($data['detil_gedung']['gd_sifat_bangunan'] == 'Permanen')
            $this->mrTemplate->AddVar('content', 'P_SELECT', 'selected="selected"');
         elseif($data['detil_gedung']['gd_sifat_bangunan'] == 'Sederhana')
            $this->mrTemplate->AddVar('content', 'S_SELECT', 'selected="selected"');
         if($data['detil_gedung']['gd_imb'] == 'Ya')
            $this->mrTemplate->AddVar('content', 'IMB_CHECKED', 'checked');
         $this->mrTemplate->AddVar('content', 'KETERANGAN', $data['detil_gedung']['gd_keterangan']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_DENAH', $data['detil_gedung']['gd_denah']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_FOTO', $data['detil_gedung']['gd_foto']);
      }
   }
}
?>