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

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

class ViewFasilitas extends HtmlResponse {
   #var $pesan;
   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/fasilitas/template');
      $this->SetTemplateFile('view_fasilitas.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 Fasilitas();
      
      $post = $_POST->AsArray();
      if(!empty($post['key']))
         $key = $post['key'];
      else
         $key = '';
         
      if(isset($_GET['cari'])) {
         $get_data = $_GET->AsArray();
         $key = $get_data['key'];
      }
      
      if(!isset($this->data['tambah']['action']))
         $this->data['tambah']['action'] ='add';
      
      if(!empty($_GET['id_fasilitas'])) {
         $get = $_GET->AsArray();
         //update
         $id_fasilitas = Dispatcher::Instance()->Decrypt($get['id_fasilitas']);
         $data_fasilitas_byId = $Obj->GetDataById($id_fasilitas);
         $this->data['tambah']['action'] ='edit';
         $key = $get['key'];
      }
      
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();  
         $startRec =($currPage-1) * $itemViewed;
      }
      
      $totalData = $Obj->GetCount($key);
      $data_fasilitas = $Obj->GetData($startRec,$itemViewed,$key);
      #print_r($data_fasilitas); 
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . 
      '&key=' . Dispatcher::Instance()->Encrypt($key) . 
      '&cari=' . Dispatcher::Instance()->Encrypt(1));
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);

      #$msg = Messenger::Instance()->Receive(__FILE__);
      #$this->Pesan = $msg[0][1];
      
      $return['fasilitas'] = $data_fasilitas;
      $return['fasilitas_byId'] = $data_fasilitas_byId;
      $return['start'] = $startRec+1;
      $return['key'] = $key;
      $return['msg'] = Dispatcher::Instance()->Decrypt($_GET['err']);
      return $return;
   }
   
   function ParseTemplate($data = NULL) {
      //set Label
      $this->TemplateLabeling();
      
      #print_r($data); exit;      
      if($this->data['tambah']['action']=='add') {
         $label_action = 'Tambah';
         $url_action = Dispatcher::Instance()->GetUrl('fasilitas', 'AddFasilitas' , 'do', 'html');
      } else {
         $label_action = 'Edit';
         $url_action = Dispatcher::Instance()->GetUrl('fasilitas', 'UpdateFasilitas' , 'do', 'html');
      }
      
      if(isset($data['msg'])) {
         if($data['msg'] == 'add') {
            $isiPesan = 'Proses Penambahan Data Berhasil';
            $class = 'notebox-done';
         }
         elseif($data['msg'] == 'noadd') {
            $isiPesan = 'Proses Penambahan Data Gagal';
            $class = 'notebox-warning';
         }elseif($data['msg'] == 'update') {
            $isiPesan = 'Proses Perubahan Data Berhasil';
            $class = 'notebox-done';
         }
         elseif($data['msg'] == 'noupdate') {
            $isiPesan = 'Proses Perubahan Data Gagal';
            $class = 'notebox-warning';
         }elseif($data['msg'] == 'del') {
            $isiPesan = 'Data Berhasil Dihapus';
            $class = 'notebox-done';
         }
         elseif($data['msg'] == 'nodel') {
            $isiPesan = 'Data Gagal Dihapus';
            $class = 'notebox-warning';
         }elseif($data['msg'] == 'null') {
            $isiPesan = 'Nama Fasilitas Harap Diisi. ';
            $class = 'notebox-warning';
         }
         
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $isiPesan);
         $this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $class);
      }
      
      $this->mrTemplate->AddVar('content', 'KEY', $data['key']);
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('fasilitas', 'Fasilitas', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_ACTION', $url_action);
      $this->mrTemplate->AddVar('content', 'SEARCH_NAMA', $this->data['search']['nama']);
      $this->mrTemplate->AddVar('content', 'TAMBAH_ID', $this->data['tambah']['id']);
      $this->mrTemplate->AddVar('content', 'TAMBAH_NAMA', $this->data['tambah']['nama']);
      $this->mrTemplate->AddVar('content', 'TAMBAH_ACTION', $this->data['tambah']['action']);
     
      $this->mrTemplate->AddVar('content', 'LABEL_ACTION', $label_action); 
      
      if (empty($data['fasilitas'])) {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
      } else {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
         
         //untuk confirm delete
         $label = 'Fasilitas Ruang';
         $urlDelete = 'fasilitas|DeleteFasilitas|do|html';
         $urlReturn = 'fasilitas|Fasilitas|view|html';
         $URLDELETE = Dispatcher::Instance()->GetUrl('fasilitas', 'DeleteFasilitas', 'do', 'html');
         
         for($i=0; $i<count($data['fasilitas']); $i++) {
            $no = $i+$data['start'];
            if($no % 2 != 0)
               $this->mrTemplate->AddVar("data_item", "CLASS_NAME", 'table-common-even');
            else
               $this->mrTemplate->AddVar("data_item", "CLASS_NAME", '');
            $this->mrTemplate->AddVar("data_item", "NOMOR", $no);
            $this->mrTemplate->AddVar("data_item", "NAMA", $data['fasilitas'][$i]['fasilitas_nama']);
            
            $this->mrTemplate->AddVar("data_item", "URL_EDIT", Dispatcher::Instance()->GetUrl('fasilitas', 'Fasilitas', 'view', 'html') .
            '&key=' . Dispatcher::Instance()->Encrypt($data['key']) . 
            '&id_fasilitas=' . Dispatcher::Instance()->Encrypt($data['fasilitas'][$i]['fasilitas_id']));
            #'&key='.Dispatcher::Instance()->Encrypt($data['key']);
            
            $this->mrTemplate->AddVar("data_item", "IDDELETE", $data['fasilitas'][$i]['fasilitas_id']);
            $this->mrTemplate->AddVar("data_item", "URLDELETE", $URLDELETE);
            $this->mrTemplate->AddVar("data_item", "URL_DELETE", Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html') .
            '&urlDelete='. $urlDelete.'&urlReturn='.$urlReturn.
            '&id='.Dispatcher::Instance()->Encrypt($data['fasilitas'][$i]['fasilitas_id']).
            '&label='.$label.'&dataName='.$data['fasilitas'][$i]['fasilitas_nama']);
            
            $this->mrTemplate->parseTemplate("data_item", "a");
         }
      }
      
      //update
      if(!empty($data['fasilitas_byId'])) {
         $this->mrTemplate->AddVar('content', 'FASILITAS_ID', $data['fasilitas_byId']['fasilitas_id']);
         $this->mrTemplate->AddVar('content', 'FASILITAS_NAMA', $data['fasilitas_byId']['fasilitas_nama']);
      }
   }
}
?>
