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

class ViewUsulanHistory extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/pb_usulan_history/template');
      $this->SetTemplateFile('view_usulan_hitory.html');
   }

   function ProcessRequest() {
      $Obj = new UsulanHistory();
		
		// inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);

      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];

		// inisialisasi filter
      $filter = array();
      
      if (isset($_POST['action'])) $filter = $_POST->AsArray();
      elseif (isset($_GET['page']) && is_array($this->Data)) $filter = $this->Data;
      
		$dataGet = $_GET->AsArray();
		if($_POST->AsArray()==''){
			$filter['nomor_usulan'] = '';
			if($dataGet['nomor_usulan']!=''){
				$filter['nomor_usulan'] = $dataGet['nomor_usulan'];
			}
		}		
      $return['filter'] = $filter;
		// Inisialisasi komponen paging
      $totalData = $Obj->getDataCountUsulanHistory($filter);
      $itemViewed = 20;
      if (isset($_GET['page'])) $currPage = $_GET['page']->Integer()->Raw();
      if (!isset($currPage) OR $currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType). '&nomor_usulan=' . Dispatcher::Instance()->Encrypt($filter['nomor_usulan']);

      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['start'] = $startRec+1;
      
		// Generate data
		if ($totalData > 0) $return['data'] = $Obj->getDataUsulanHistory($filter, $startRec, $itemViewed);
      else $return['data'] = array();
		
      $return['start'] = 1;
		return $return;
   }

   function ParseTemplate($data = NULL) {
		// notification
		$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
      $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
      $this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
	
		// here, url search
      $urlCari		= Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
      $urlDetil	= Dispatcher::Instance()->GetUrl('pb_usulan_history', 'popupDetilUsulan', 'view', 'html');
		$urlEdit 	= Dispatcher::Instance()->GetUrl('pb_usulan_history', 'editUsulanHistory', 'view', 'html');
		$urlDelete = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
         "&urlReturn=".Dispatcher::Instance()->Encrypt('pb_usulan_history|usulanHistory|view|html').
         "&label=".Dispatcher::Instance()->Encrypt('History Usulan');
		
		// parse URL
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', $urlCari);
      
		// data search
		$this->mrTemplate->AddVar('content', 'NOMOR_USULAN', $data['filter']['nomor_usulan']);
		
      if (empty($data['data'])) {
            $this->mrTemplate->AddVar('data_grid', 'DATA_EMPTY', 'YES');
         } else {
            $this->mrTemplate->AddVar('data_grid', 'DATA_EMPTY', 'NO');

            $dataList = $data['data'];

			for ($i=0; $i<sizeof($dataList); $i++) {
            $no = $i+$data['start'];
            $dataList[$i]['nomor'] = $no;
				$dataList[$i]['total_usulan'] = number_format($dataList[$i]['total_usulan'], 2, ',', '.');
				
				if ($no % 2 != 0)
               $dataList[$i]['class_name'] = 'table-common-even';
            else
               $dataList[$i]['class_name'] = '';
				
            $dataList[$i]['url_detil'] = $urlDetil.'&id='.$dataList[$i]['hisId'];
            $dataList[$i]['url_edit'] = $urlEdit.'&id='.$dataList[$i]['hisId'];
            $dataList[$i]['url_delete'] = $urlDelete.
					"&urlDelete=".Dispatcher::Instance()->Encrypt('pb_usulan_history|deleteUsulanHistory|do|json').
					"&id=".Dispatcher::Instance()->Encrypt($dataList[$i]['hisId']).
					"&dataName=".Dispatcher::Instance()->Encrypt($dataList[$i]['nomor_usulan']);

				if($dataList[$i]['statusId'] != 1) $dataList[$i]['display'] = 'none';
				else $dataList[$i]['display'] = '';
				
            $this->mrTemplate->AddVars('data_item', $dataList[$i], '');
            $this->mrTemplate->parseTemplate('data_item', 'a');
         }
      }
   }
}
?>