<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/smta/business/AppSmta.class.php';
class ViewSmta extends HtmlResponse {

	function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/smta/template');
		$this->SetTemplateFile('view_smta.html');
	}
	
	function ProcessRequest() {
		$smtaObj = new AppSmta();		
      if($_POST || isset($_GET['cari'])) {		    
         if((isset($_POST['nama_smta']))|| (isset($_POST['kode_smta']))||(isset($_POST['kota']))){
            $namaSmta=$_POST['nama_smta'];          
            $kodeSmta=$_POST['kode_smta'];          
            $kota=$_POST['kota'];        
         }elseif( (isset($_GET['nama_smta']))or(isset($_GET['kode_smta'])) or (isset($_GET['kota'])))
         { 
            $kodeSmta = Dispatcher::Instance()->Decrypt($_GET['kode_smta']);	
            $namaSmta = Dispatcher::Instance()->Decrypt($_GET['nama_smta']);			
            $kota = Dispatcher::Instance()->Decrypt($_GET['kota']);			
         }else{
            $namaSmta='';
            $kodeSmta='';
            $kota='';
         }
      }
     	$totalData = $smtaObj->GetCountDataSmta($kodeSmta,$namaSmta,$kota);
		$itemViewed = 20;
		$startRec = 0 ;
		
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		
		$dataSmta = $smtaObj->GetDataSmta($startRec, $itemViewed,$kodeSmta,$namaSmta,$kota);

		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType .'&kode_smta='. Dispatcher::Instance()->Encrypt($kodeSmta).'&nama_smta='.Dispatcher::Instance()->Encrypt($namaSmta).'&kota='.Dispatcher::Instance()->Encrypt($kota).'&cari=' . Dispatcher::Instance()->Encrypt(1));
		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
	
      $dataKota=$smtaObj->GetDataKota();
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kota',
            array('kota',$dataKota,$kota,'false','onChange="searchByKota()";'), Messenger::CurrentRequest);

		$msg = Messenger::Instance()->Receive(__FILE__);
      
      $return['pesan']=$msg[0][1];
      $return['css']=$msg[0][2];

		$return['dataSmta'] = $dataSmta;
		$return['start'] = $startRec+1;
		$return['search']['nama_smta'] = $namaSmta;
		$return['search']['kode_smta'] = $kodeSmta;
		$return['search']['kotaKode'] = $dataKotaById[0]['kota_kode'];
      $return['search']['kotaNama'] = $dataKotaById[0]['kota_nama'];
		$return['dataKota'] = $dataKota;
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
	
		$search = $data['search'];
		$dataKota=$data['dataKota'];



    
      $this->mrTemplate->AddVar('content', 'NAMA_SMTA', $search['nama_smta']);
      $this->mrTemplate->AddVar('content', 'KODE_SMTA', $search['kode_smta']);
		$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['pesan']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('smta', 'smta', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('smta', 'inputSmta', 'view', 'html'));
      

      
      if($data['pesan']) {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['pesan']);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $data['css']);
		}
			
	   if (empty($data['dataSmta'])) {
			$this->mrTemplate->AddVar('data_smta', 'SMTA_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_smta', 'SMTA_EMPTY', 'NO');
			$dataSmta = $data['dataSmta'];
		
//mulai bikin tombol delete
			$label = "Data Smta";
			$urlDelete = Dispatcher::Instance()->GetUrl('smta', 'deleteSmta', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('smta', 'smta', 'view', 'html');
			Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlRetrn),Messenger::NextRequest);
			$this->mrTemplate->AddVar('content', 'URL_DELETE', Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html'));


			for ($i=0; $i<sizeof($dataSmta); $i++) {
				$no = $i+$data['start'];
				$dataSmta[$i]['number'] = $no;
				//print_r($dataSmta);
				if ($no % 2 != 0) $dataSmta[$i]['class_name'] = 'table-common-even';
				else $dataSmta[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataSmta)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataSmta[$i]['smta_kode']);
            $urlAccept = 'smta|deleteSmta|do|html-cari-'.$cari;
            $urlReturn = 'smta|smta|view|html-cari-'.$cari;
				$dataSmta[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('smta', 'inputSmta', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataSmta[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataSmta[$i]['smta_kode'].'&label='.$label.'&dataName='.$dataSmta[$i]['smta_nama'];       
				$this->mrTemplate->AddVars('data_smta_item', $dataSmta[$i], 'SMTA_');
				$this->mrTemplate->parseTemplate('data_smta_item', 'a');	 
			}
		}
	}
}
?>