<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/verifikasi_pengadaan/business/Pengadaan.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/verifikasi_pengadaan/business/MAK.class.php';

//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
   'module/label/response/Label.proc.class.php';

class ViewPengadaanList extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/verifikasi_pengadaan/template');
      $this->SetTemplateFile('view_verifikasi_pengadaan.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Pengadaan;
      
      // 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
      (
         'pengadaanTahun' => 'all',
         'pengadaanMAK' => '',
         'pengadaanMakNama' => '',
         'pengadaanSumberDanaId' => 'all',
         'pengadaanDetLabel' => '',
         'pengadaanNoSPK' => ''
      );
      
      if (isset($_POST['btncari']))
         $filter = array_intersect_key($_POST->AsArray(), $filter);
      elseif (isset($_GET['page']) && is_array($this->Data))
         $filter = array_intersect_key($this->Data, $filter);
      
      Messenger::Instance()->Send('verifikasi_pengadaan', 'pengadaanList', 'view', 'html', array($filter), Messenger::NextRequest);
      $return['filter'] = $filter;
      // ---------
      
      // render Combo Box
      $comboTahun = $Obj->GetComboTahun(date('Y')-10, date('Y'), false);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun_pengadaan', array('pengadaanTahun', $comboTahun, $filter['pengadaanTahun'], true, ''), Messenger::CurrentRequest);
      
      $comboSumberDana = $Obj->GetComboSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sumber_dana', array('pengadaanSumberDanaId', $comboSumberDana, $filter['pengadaanSumberDanaId'], true, ''), Messenger::CurrentRequest);
      // ---------
      
      // Inisialisasi komponen paging
      $totalData = $Obj->GetListPengadaanCount($filter);
      $itemViewed = 10;
      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);

      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['start'] = $startRec+1;
      // ---------
      
      // Generate data structure
      if ($totalData > 0)
      {
         $return['dataGrid'] = $Obj->GetListPengadaan($filter, $startRec, $itemViewed);
         
         $Obj = new MAK;
         $kegList = $Obj->ComboKegiatan();
         if(!empty($kegList))
            foreach ($kegList as $value) $return['kegList'][$value['id']] = $value['name'];
      }
      else $return['dataGrid'] = array();
      foreach (array_keys($return['dataGrid']) as $key)
      {
         $return['dataGrid'][$key]['pengadaanNominalPengadaan'] = number_format($return['dataGrid'][$key]['pengadaanNominalPengadaan'], 2, ',', '.');
      }
      // ---------
      
      // inisialisasi url
      $return['url']['search'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
      $return['url']['pop_up_mak'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'popUpMak', 'view', 'html');
      $return['url']['verifikasi'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'doVerifikasi', 'view', 'html');
      // ---------
      
      return $return;
   }
   
   function TemplateLabeling(){
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      
      //patTemplate name yang memiliki label2 dinamic
      $arrContent=array("content");
      
      for($i=0;$i<count($arrContent);$i++){
         for($j=0;$j<count($arrLabel);$j++){
            $this->mrTemplate->AddVar($arrContent[$i],$arrLabel[$j]['labelCode'],$arrLabel[$j]['labelName']);
         }
      }
   }
   
   function ParseTemplate($data = NULL)
   {
      //set Label
      $this->TemplateLabeling();
      
      // 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', 'CLASS_PESAN', $this->css);
		}
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // Render Filter Box
      $this->mrTemplate->AddVars('content', $data['filter'], '');
      // ---------
      
      // Render data grid
		if (count($data['dataGrid']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
      $status[0] = 'Tidak Ada Item';
      $status[1] = 'Belum Selesai';
      $status[2] = 'Selesai dengan Masalah';
      $status[3] = 'Selesai tanpa Masalah';
      foreach ($data['dataGrid'] as $value)
      {
			$value['nomor'] = $i;
			$value['class_name'] = ($i % 2) ? 'table-common-even' : '';
         $value['pengadaanMAK'] = '['.$value['pengadaanMAK'].'] '.$data['kegList'][$value['pengadaanMAK']];
         $value['pengadaanDetVerifikasi'] = $status[$value['status']];
         $value['url_verifikasi'] = $data['url']['verifikasi'] . "&id=". $value['pengadaanId'];
            
         $this->mrTemplate->AddVars('data_item', $value, '');
			$this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>
