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

class ViewInputSewaAset extends HtmlResponse 
{
	function TemplateModule() 
	{
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/sewa_aset/template');
      $this->SetTemplateFile('view_input_sewa_aset.html');
   }
	
   function ProcessRequest() 
	{
      $obj = new SewaAset;
		
		$msg = Messenger::Instance()->Receive(__FILE__);
		$this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
	
      $hour = $obj->GetHour();
      $minute = $obj->GetMinute();
		
		$id = $_GET['id']->Integer()->Raw();
      $result = $obj->GetDataSewaAsetById($id);
      
		$data = array();
		if(is_array($this->Data))
		{
			$data = $this->Data;
			
			$startDate = $data['tgl_awal_year']."-".$data['tgl_awal_mon']."-".$data['tgl_awal_day'];
         $startHour = $data['jam_awal'];
         $startMinute = $data['menit_awal'];
			
         $endDate = $data['tgl_akhir_year']."-".$data['tgl_akhir_mon']."-".$data['tgl_akhir_day'];
         $endHour = $data['jam_akhir'];
         $endMinute = $data['menit_akhir'];
		} 
			elseif(isset($_GET['id']))
		{
         if ($result === false) 
				unset($_GET['id']);
         else 
				$data = $result[0];
				$data['asetSewaList']=$result;
				$explAwal = explode(":", $data['sewa_waktu_awal']);
				$explAkhir = explode(":", $data['sewa_waktu_akhir']);
				
				$startDate = $data['sewa_tanggal_awal'];
				$startHour = $explAwal[0];
				$startMinute = $explAwal[1];
				
				$endDate = $data['sewa_tanggal_akhir'];
				$endHour = $explAkhir[0];
				$endMinute = $explAkhir[1];
      }
			else
		{
         $today = date("Y-m-d");
         $hourNow = date('H');
         $minuteNow = date('i');

         $startDate = $today;
         $startHour = $hourNow;
         $startMinute = $minuteNow;

         $endDate = $today;
         $endHour = $hourNow;
         $endMinute = $minuteNow;
			$data['nomor_sewa'] = "SW".".".date('Y.His');
			$data['jumlah_hari'] = 1;
		}

      Messenger::Instance()->SendToComponent('tanggal', 'Tanggal', 'view', 'html', 'tgl_awal',array($startDate, '2000', date('Y')+1), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('tanggal', 'Tanggal', 'view', 'html', 'tgl_akhir',array($endDate, '2000', date('Y')+1), Messenger::CurrentRequest);

      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jam_awal',array('jam_awal', $hour, $startHour,'','id="jam_awal" onchange="dayDiff(event);"'),Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'menit_awal',array('menit_awal', $minute, $startMinute,'','id="menit_awal" onchange="dayDiff(event);"'),Messenger::CurrentRequest);

      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jam_akhir',array('jam_akhir', $hour, $endHour,'','id="jam_akhir" onchange="dayDiff();"'),Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'menit_akhir',array('menit_akhir', $minute, $endMinute,'','id="menit_akhir" onchange="dayDiff(event);"'),Messenger::CurrentRequest);
		
		$getMitra = $obj->GetComboMitra();		
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'mitra_kerja', array('pengadaanMitraId', $getMitra, $data['pengadaanMitraId'], 'false', ''), Messenger::CurrentRequest);
		
		$getUnit = $obj->GetComboUnit();
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit_kerja', array('pengadaanUnitkerjaId', $getUnit, $data['pengadaanUnitkerjaId'], 'false', ''), Messenger::CurrentRequest);
		
		//print_r($id);
		$return['data'] = $data;
		$return['json']['sewa_aset'] = json_encode($data['asetSewaList']);
      unset ($return['data']['asetSewaList']);
      return $return;
   }

   function ParseTemplate($data = NULL)
	{
		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);
		}
			
		$urlAsetSewaPopup = Dispatcher::Instance()->GetUrl('sewa_aset', 'sewaAset', 'popup', 'html');
		$urlAsetSewaList	= Dispatcher::Instance()->GetUrl('sewa_aset', 'sewaAset', 'view', 'html');
		$urlMitra = Dispatcher::Instance()->GetUrl('sewa_aset', 'mitra', 'popup', 'html');
		$urlUnit = Dispatcher::Instance()->GetUrl('sewa_aset', 'unit', 'popup', 'html');
		$this->mrTemplate->AddVar('content', 'URL_ASET_LIST', $urlAsetSewaList);
		$this->mrTemplate->AddVar('content', 'URL_MITRA_POPUP', $urlMitra);
		$this->mrTemplate->AddVar('content', 'URL_UNIT_POPUP', $urlUnit);
		$this->mrTemplate->AddVar('content', 'URL_ASET_POPUP', $urlAsetSewaPopup);
		$this->mrTemplate->AddVars('content', $data['json'], 'JSON_' );

      if($data['data']['sewa_id']=='')
         $urlAction = Dispatcher::Instance()->GetUrl('sewa_aset', 'inputSewaAset', 'do', 'html');
      else
			$urlAction = Dispatcher::Instance()->GetUrl('sewa_aset', 'inputSewaAset', 'do', 'html').'&id='.Dispatcher::Instance()->Encrypt($data['data']['sewa_id']);
      $this->mrTemplate->AddVar('content', 'ACTION_URL', $urlAction);
		
      $this->mrTemplate->AddVars('content', $data['data'],'');
   }
}
?>