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

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

class ViewPengadaanInput extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/pengadaan/template');
      $this->SetTemplateFile('view_pengadaan_input.html');
   }

   function ProcessRequest()
   {
      if (!isset($_GET['id'])) $_SESSION['mod=pengadaan']['id'] = 0;
      else $_SESSION['mod=pengadaan']['id'] = $_GET['id']->Integer()->Raw();
      $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 default value
      $data = array();
      if (is_array($this->Data))
         $data = $this->Data;
      elseif (isset($_GET['id']))
      {
         $result = $Obj->GetPengadaanMstById($_GET['id']->Integer()->Raw());
         if ($result === false) unset($_GET['id']);
         else $data = $result;
      }

      if (!isset($data['spkTanggal']))
      {
         $spkTime = (isset($data['pengadaanTglSPK'])) ? strtotime($data['pengadaanTglSPK']) : time();
         list($data['spkTanggal'],$data['spkBulan'],$data['spkTahun']) = explode('-', date('d-m-Y', $spkTime));
      }

      if (!isset($data['pengadaanUnitkerjaId']))
      {
         $tmp = $Obj->GetUnitKerjaByUserName($_SESSION['username']);
         $data['pengadaanUnitkerjaId'] = $tmp['id'];
         $data['unitKerjaNama'] = $tmp['name'];
      }
		
		if (!isset($data['pengadaanNoSPK']))
      {
         $date = date('Ymd.His');
         $data['pengadaanNoSPK'] = 'SPK.'.$date;
      }
      
      if (isset($data['panitia']))
         $data['panitia'] = json_encode($data['panitia']);
      else $data['panitia'] = json_encode(array());

      if (isset($data['rekapUsulan']))
         $data['rekapUsulan'] = json_encode($data['rekapUsulan']);
      else $data['rekapUsulan'] = json_encode(array());

      $return['data'] = $data;
      // ---------

      // render Combo Box
      $comboJnsPengadaan = $Obj->GetComboJnsPengadaan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_pengadaan', array('pengadaanJnsPengBrgId', $comboJnsPengadaan, $data['pengadaanJnsPengBrgId'], false, ''), Messenger::CurrentRequest);

      $comboJlrPengadaan = $Obj->GetComboJlrPengadaan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jalur_pengadaan', array('pengadaanJlrPengBrgId', $comboJlrPengadaan, $data['pengadaanJlrPengBrgId'], false, ''), Messenger::CurrentRequest);

      $comboTanggal= $Obj->GetComboTahun(1, 31, true);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'spk_tanggal', array('spkTanggal', $comboTanggal, $data['spkTanggal'], false, ''), Messenger::CurrentRequest);

      $comboBulan= $Obj->GetComboTahun(1, 12, true);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'spk_bulan', array('spkBulan', $comboBulan, $data['spkBulan'], false, ''), Messenger::CurrentRequest);

      $comboTahun = $Obj->GetComboTahun(date('Y') - 4, date('Y'), false);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'spk_tahun', array('spkTahun', $comboTahun, $data['spkTahun'], false, ''), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun_anggaran', array('pengadaanTahun', $comboTahun, $data['pengadaanTahun'], false, ''), Messenger::CurrentRequest);

      $comboSumberDana = $Obj->GetComboSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sumber_dana', array('pengadaanSumberdanaId', $comboSumberDana, $data['pengadaanSumberdanaId'], false, ''), Messenger::CurrentRequest);

      $comboMitraKerja = $Obj->GetComboMitraKerja();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'mitra_kerja', array('pengadaanMitraId', $comboMitraKerja, $data['pengadaanMitraId'], false, ''), Messenger::CurrentRequest);

      $comboUnitKerja = $Obj->GetComboUnit();
      $unitDefault = $Obj->GetSetting(1);
      foreach ($comboUnitKerja as $value) if ($value['id'] == $unitDefault) {$comboUnit = array($value); break;}
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit', array('pengadaanUnitkerjaId', $comboUnitKerja, $data['pengadaanUnitkerjaId'], false, ''), Messenger::CurrentRequest);

      $comboJabatan = $Obj->GetComboJabatan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jabatan_ref', array('', $comboJabatan, '', false, 'id="JABATAN_REF"'), Messenger::CurrentRequest);
      // ---------

      // inisialisasi url
      $return['url']['pop_up_mak'] = Dispatcher::Instance()->GetUrl('pengadaan', 'popUpMak', 'view', 'html');
      $return['url']['pop_up_rekap'] = Dispatcher::Instance()->GetUrl('pengadaan', 'rekap', 'popup', 'html');
      $return['url']['action'] = Dispatcher::Instance()->GetUrl('pengadaan', 'pengadaanInput', 'do', 'html');
      if (isset($_GET['id'])) $return['url']['action'] .= "&id=".$_GET['id']->Integer()->Raw();
      // ---------

      $Obj = new MAK;
      $arrKegiatan = $Obj->ComboKegiatan();
      if(!empty($arrKegiatan))
         foreach ( $arrKegiatan as $value) $kegiatanList[$value['id']] = $value['name'];
      $return['data']['pengadaanMakNama'] = $kegiatanList[$data['pengadaanMAK']];

      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 form
      $data['data']['mode'] = (isset($_GET['id'])) ? 'Ubah' : 'Tambah';
      $this->mrTemplate->AddVars('content', $data['data'], '');
      // ---------
   }
}
?>