<?php
require_once GTFWConfiguration::GetValue('application', 'docroot').'module/'.Dispatcher::Instance()->mModule.'/business/PerbaikanBarang.class.php';
require_once GTFWConfiguration::GetValue('application', 'docroot') . 'main/function/date.php';

class ViewPerbaikanBarang extends HtmlResponse
{
	function TemplateModule ()
	{
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/'.Dispatcher::Instance()->mModule.'/template');
		$this->SetTemplateFile('view_perbaikanbarang.html');
	}
	
	function ProcessRequest ()
   {
		$PerbaikanBarangObj = new PerbaikanBarang;
		
		// inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		$this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
		$_POST= $_POST->AsArray();
	
		if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['nomorBA'])) {
				$nomorBA = $_POST['nomorBA'];
				$idMitra = $_POST['idMitra'];
				$namaMitra = $_POST['namaMitra'];
			} elseif(isset($_GET['nomorBA'])) {
				$nomorBA = Dispatcher::Instance()->Decrypt($_GET['nomorBA']);
				$idMitra = Dispatcher::Instance()->Decrypt($_GET['idMitra']);
				$namaMitra = Dispatcher::Instance()->Decrypt($_GET['namaMitra']);
			} else {
				$nomorBA = '';
				$idMitra = '';
				$namaMitra = '';
			}
		}

		$totalData = $PerbaikanBarangObj->GetCountDataPerbaikanBarang($nomorBA, $idMitra,$namaMitra);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&nomorBA=' . Dispatcher::Instance()->Encrypt($nomorBA). '&namaMitra=' . Dispatcher::Instance()->Encrypt($namaMitra). '&idMitra=' . Dispatcher::Instance()->Encrypt($idMitra).'&cari=' . Dispatcher::Instance()->Encrypt(1));
		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
		
		$dataPerbaikanBarang=$PerbaikanBarangObj->GetDataPerbaikanBarang($startRec, $itemViewed, $nomorBA, $idMitra, $namaMitra);
      
		$return['dataPerbaikanBarang'] = $dataPerbaikanBarang;
		$return['search']['nomorBA'] = $nomorBA;
		$return['search']['idMitra'] = $idMitra;
		$return['search']['namaMitra'] = $namaMitra;
		return $return;
	}
	
	
	function ParseTemplate ($data = NULL)
	{
		
		 // render message box
     if ($this->Pesan) {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
			$this->mrTemplate->AddVar('warning_box', 'NOTEBOX', $this->css);
		}
      // ---------
		
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'NOMORBA', $search['nomorBA']);
		$this->mrTemplate->AddVar('content', 'IDMITRA', $search['idMitra']);
		$this->mrTemplate->AddVar('content', 'NAMAMITRA', $search['namaMitra']);
		
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('perbaikan_barang', 'perbaikanBarang', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('perbaikan_barang', 'inputPerbaikanBarang', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_MITRAKERJA', Dispatcher::Instance()->GetUrl('perbaikan_barang', 'mitraKerja', 'popup', 'html') );
	 
		if (empty($data['dataPerbaikanBarang']))
		{
			$this->mrTemplate->addVar('data_grid', 'IS_DATA_EMPTY', 'YES');
			return;
		}
		else 
		{	
			$this->mrTemplate->addVar('data_grid', 'IS_DATA_EMPTY', 'NO');
			
			//mulai bikin tombol delete
			$label = "Manajemen Perbaikan Barang";
			$urlDelete = Dispatcher::Instance()->GetUrl('perbaikan_barang', 'deletePerbaikanBarang', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('perbaikan_barang', 'perbaikanBarang', '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'));
			//selesai bikin tombol delete
      
			$dataPerbaikanBarang = $data['dataPerbaikanBarang'];
			
			for ($i=0; $i<sizeof($dataPerbaikanBarang); $i++) {
				$no = $i+1;
				$dataPerbaikanBarang[$i]['number'] = $no;
				if ($no % 2 != 0) $dataPerbaikanBarang[$i]['class_name'] = 'table-common-even';
				else $dataPerbaikanBarang[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataPerbaikanBarang)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				
				$idEnc = Dispatcher::Instance()->Encrypt($dataPerbaikanBarang[$i]['id']);
				
				$dataPerbaikanBarang[$i]['total_harga']=number_format($dataPerbaikanBarang[$i]['total_harga'], 2, ',', '.');
				$dataPerbaikanBarang[$i]['tanggal_perbaikan']=IndonesianDate($dataPerbaikanBarang[$i]['tanggal_perbaikan'], 'yyyy-mm-dd');
				$dataPerbaikanBarang[$i]['do_date']=IndonesianDate($dataPerbaikanBarang[$i]['do_date'], 'yyyy-mm-dd');
				$dataPerbaikanBarang[$i]['url_detail']=Dispatcher::Instance()->GetUrl('perbaikan_barang','detilPerbaikanBarang', 'popup', 'html') . '&idPerbaikanBarang=' . $idEnc;
				if($dataPerbaikanBarang[$i]['isDone'] == '0')
                                    $dataPerbaikanBarang[$i]['url_edit']=Dispatcher::Instance()->GetUrl('perbaikan_barang','inputPerbaikanBarang', 'view', 'html') . '&idPerbaikanBarang=' . $idEnc;
                                else{ 
                                    $dataPerbaikanBarang[$i]['edit_display'] = "none";
                                    $dataPerbaikanBarang[$i]['disabled'] = "disabled";
                                }
				$this->mrTemplate->AddVars('data_list', $dataPerbaikanBarang[$i], 'DATA_');
				$this->mrTemplate->parseTemplate('data_list', 'a');	 
			}
		}	
	}
}
?>
