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

class ViewWargaNegara extends HtmlResponse {

	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/warga_negara/template');
		$this->SetTemplateFile('view_warga_negara.html');
	}
	
	function ProcessRequest() {
		$wargaNegaraObj = new AppWargaNegara();
		
    if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['warga_negara'])) {
				$warga_negara = $_POST['warga_negara'];
			} elseif(isset($_GET['warga_negara'])) {
				$warga_negara = Dispatcher::Instance()->Decrypt($_GET['warga_negara']);
			} else {
				$warga_negara = '';
			}
		}
		
	//view
		$totalData = $wargaNegaraObj->GetCountDataWargaNegara($warga_negara);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$dataWargaNegara = $wargaNegaraObj->getDataWargaNegara($startRec, $itemViewed, $warga_negara);
  	#print_r($dataWargaNegara);
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&warga_negara=' . Dispatcher::Instance()->Encrypt($warga_negara) . '&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__);
		$return['Pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		
		$return['siaPesan'] = $msg[1][1];
		$return['siaCss'] = $msg[1][2];

		$return['dataWargaNegara'] = $dataWargaNegara;
		$return['start'] = $startRec+1;

		$return['search']['warga_negara'] = $warga_negara;
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'WARGA_NEGARA', $search['warga_negara']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('warga_negara', 'wargaNegara', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('warga_negara', 'inputWargaNegara', '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($data['siaPesan']) {
			$this->mrTemplate->SetAttribute('warning_connection', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_connection', 'ISI_PESAN', $data['siaPesan']);
			$this->mrTemplate->AddVar('warning_connection', 'CLASS_PESAN', $data['siaCss']);
		}

		if (empty($data['dataWargaNegara'])) {
			$this->mrTemplate->AddVar('data_warga_negara', 'WARGA_NEGARA_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_warga_negara', 'WARGA_NEGARA_EMPTY', 'NO');
			$dataWargaNegara = $data['dataWargaNegara'];
		
//mulai bikin tombol delete
			$label = "Data Warga Negara";
			$urlDelete = Dispatcher::Instance()->GetUrl('warga_negara', 'deleteWargaNegara', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('warga_negara', 'wargaNegara', 'view', 'html');
			Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::NextRequest);
			$this->mrTemplate->AddVar('content', 'URL_DELETE', Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html'));

			for ($i=0; $i<sizeof($dataWargaNegara); $i++) {
				$no = $i+$data['start'];
				$dataWargaNegara[$i]['number'] = $no;
				if ($no % 2 != 0) $dataWargaNegara[$i]['class_name'] = 'table-common-even';
				else $dataWargaNegara[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataWargaNegara)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataWargaNegara[$i]['warga_negara_id']);

            $urlAccept = 'warga_negara|deleteWargaNegara|do|html-cari-'.$cari;
            $urlReturn = 'warga_negara|wargaNegara|view|html-cari-'.$cari;
            $dataName = $dataWargaNegara[$i]['warga_negara_nama'];
				$dataWargaNegara[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('warga_negara', 'inputWargaNegara', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataWargaNegara[$i]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$idEnc.'&label='.$label.'&dataName='.$dataName;
            
				$this->mrTemplate->AddVars('data_warga_negara_item', $dataWargaNegara[$i], 'WARGA_NEGARA_');
				$this->mrTemplate->parseTemplate('data_warga_negara_item', 'a');	 
			}
		}
	}
}
?>
