<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/laporan_mahasiswa/business/AppLaporanMahasiswa.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/laporan_mahasiswa/business/AppLaporanMahasiswaSia.class.php';

class ViewLaporanMahasiswa extends HtmlResponse{
   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/laporan_mahasiswa/template');
      $this->SetTemplateFile('view_laporan_mahasiswa.html');
   }
   function ProcessRequest() {
      $lapObj = new AppLaporanMahasiswa();
      $siaObj = new AppLaporanMahasiswaSia();
      
      $msg = Messenger::Instance()->Receive(__FILE__);
      $return['Pesan'] = $msg[0][1];
      $return['css'] = $msg[0][2];
      
      $dataPeriodeAktif = $lapObj->GetPeriodeAktif();
      $dataPeriode = $lapObj->GetDataPeriode();
      $dataProdi = $lapObj->GetDataProdi();
      $dataJenjangAkademik = $lapObj->GetJenjangAkademik();
      $dataJalurMasuk = $lapObj->GetJalurMasuk();
      $dataStatusReg = $lapObj->GetStatusRegistrasi();
      
      if($_POST || isset($_GET['cari'])) {
         if((isset($_POST['periode']))or(isset($_POST['jenjang']))or(isset($_POST['jalur']))or(isset($_POST['prodi']))or(isset($_POST['status_reg']))){
            $periode = $_POST['periode']->Raw();
            $jenjang = $_POST['jenjang']->Raw();
            $jalur = $_POST['jalur']->Raw();
            $prodi = $_POST['prodi']->Raw();
            $status_reg = $_POST['status_reg']->Raw();
            #print_r($periode);print("a");
         } elseif(isset($_GET['periode'])){
            $periode = Dispatcher::Instance()->Decrypt($_GET['periode']->Raw());
            $jenjang = Dispatcher::Instance()->Decrypt($_GET['jenjang']->Raw());
            $jalur = Dispatcher::Instance()->Decrypt($_GET['jalur']->Raw());
            $prodi = Dispatcher::Instance()->Decrypt($_GET['prodi']->Raw());
            $status_reg = Dispatcher::Instance()->Decrypt($_GET['status_reg']->Raw());
         } else {
            $periode = $dataPeriodeAktif['0']['periode_id'];
            $jenjang = '';
            $jalur = '';
            $prodi = 'all';
            $status_reg = 'all';
            #print_r($periode);print("b");
         }

         $_SESSION['periode'] = $periode;
         $_SESSION['jenjang'] = $jenjang;
         $_SESSION['jalur'] = $jalur;
         $_SESSION['prodi'] = $prodi;
         $_SESSION['status_reg'] = $status_reg;
         //print($_SESSION['periode'] = $periode);
      }
      
      if($periode=='all'){
         $periode='';
      }
           
      if($jenjang=='all'){
         $jenjang='';
      }
      
      if($jalur=='all'){
         $jalur='';
      }
       
      /*if($prodi=='all'){
         $prodi='';
      }
      
      if($status_reg=='all'){
         $status_reg='';
      }*/
      
      $totalData = $lapObj->GetCountDataMahasiswa($periode,$jenjang,$jalur,$prodi,$status_reg);
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
         $startRec =($currPage-1) * $itemViewed;
      }
      $dataLaporanMahasiswa = $lapObj->GetDataMahasiswa($periode,$jenjang,$jalur,$prodi,$status_reg,$startRec,$itemViewed);
      #print_r($dataLaporanMahasiswa);
      for ($j=0; $j<sizeof($dataLaporanMahasiswa); $j++){
         $nim[$j] = $dataLaporanMahasiswa[$j]['nim'];
      }
      if ($nim!='')
         $nim = implode("','",$nim);
      
      $dataLaporanMahasiswaSia = $siaObj->GetDataMahasiswaSia($nim);
      
      for ($j=0; $j<sizeof($dataLaporanMahasiswa); $j++){
         for($k=0; $k<sizeof($dataLaporanMahasiswaSia); $k++){
            if($dataLaporanMahasiswaSia[$k]['nim']==$dataLaporanMahasiswa[$j]['nim'])
               $dataLaporanMahasiswa[$j]['status_akd'] = $dataLaporanMahasiswaSia[$k]['status_all'];
         }
      }
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
            Dispatcher::Instance()->mSubModule, 
            Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . 
            '&periode=' . Dispatcher::Instance()->Encrypt($periode) . 
            '&jenjang=' . Dispatcher::Instance()->Encrypt($jenjang) . 
            '&jalur=' . Dispatcher::Instance()->Encrypt($jalur) . 
            '&prodi=' . Dispatcher::Instance()->Encrypt($prodi) .
            '&status_reg=' . Dispatcher::Instance()->Encrypt($status_reg) .
            '&cari=' . Dispatcher::Instance()->Encrypt(1));
      
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      $msg = Messenger::Instance()->Receive(__file__);
      
      if (isset($periode))
         $periode_selected = $periode;
      else
         $periode_selected = $return['Data']['0']['id'];
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'periode',
         array('periode',$dataPeriode,$periode_selected,'true',''), Messenger::CurrentRequest);
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenjang',
         array('jenjang',$dataJenjangAkademik,$jenjang,'true',''), Messenger::CurrentRequest);

      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jalur',
         array('jalur',$dataJalurMasuk,$jalur,'true',''), Messenger::CurrentRequest);

      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'prodi',
         array('prodi',$dataProdi,$prodi,'true',''), Messenger::CurrentRequest);
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status_reg',
         array('status_reg',$dataStatusReg,$status_reg,'true',''), Messenger::CurrentRequest);
      
      $return['pesan'] = $msg[0][1];
      $return['css'] = $msg[0][2];
      $return['start'] = $startRec+1;
      
      $return['periode'] = $periode;
      $return['jenjang']=$jenjang;
      $return['jalur']=$jalur;
      $return['prodi'] = $prodi;
      $return['status_reg'] = $status_reg;
      
      $return['search']['prodiKode'] = $dataProdiById[0]['prodi_id'];
      $return['search']['prodiNama'] = $dataProdiById[0]['prodi'];
      
      $return['dataLaporanMahasiswa'] = $dataLaporanMahasiswa;
      $return['dataPeriodeAktif'] = $dataPeriodeAktif;
      $return['dataNamaPeriode'] = $dataNamaPeriode;
      $return['dataPeriode'] = $dataPeriode;
      $return['dataJenjangAkademik'] = $dataJenjangAkademik;
      $return['dataJalurMasuk'] = $dataJalurMasuk;
      return $return;
   }

   function ParseTemplate($data = NULL) {
      $search = $data['search'];
      $periode = $data['periode'];
      $jenjang = $data['jenjang'];
      $jalur = $data['jalur'];
      $prodi = $data['prodi'];
      
      $dataLaporanMahasiswa = $data['dataLaporanMahasiswa'];
      $dataPeriodeAktif = $data['dataPeriodeAktif'];
      $dataNamaPeriode = $data['dataNamaPeriode'];
      $dataPeriode = $data['dataPeriode'];
      $dataJenjangAkademik = $data['$dataJenjangAkademik'];
      $dataJalurMasuk = $data['dataJalurMasuk'];
            
      #var_dump($periode);
            
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', 
         Dispatcher::Instance()->GetUrl('laporan_mahasiswa', 'laporanMahasiswa', 'view', 'html'));
      $this->mrTemplate->AddVar('content', 'URL_EXCEL', 
         Dispatcher::Instance()->GetUrl('laporan_mahasiswa', 'ExcelLaporanMahasiswa', 'view', 'xls')
         .'&periode='.$periode.'&jenjang='.$jenjang.'&jalur='.$jalur.'&prodi='.$prodi.'&status_reg='.$data['status_reg']);
      $this->mrTemplate->AddVar('content', 'URL_PRINT', 
         Dispatcher::Instance()->GetUrl('laporan_mahasiswa', 'daftarLaporanMahasiswa', 'print', 'html'));

      if($data['pesan']) {
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['pesan']);
         $this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $data['css']);
      }
      if(!empty($data['dataPeriodeAktif'])){
         $this->mrTemplate->SetAttribute('periode', 'visibility', 'visible');
         $this->mrTemplate->AddVars('periode', $data['dataPeriodeAktif'][0], '');
      }
      if(!empty($data['dataNamaPeriode'])){
         $this->mrTemplate->SetAttribute('periode', 'visibility', 'visible');
         $this->mrTemplate->AddVars('periode', $data['dataNamaPeriode'][0], '');   
      }

      if (empty($data['dataLaporanMahasiswa'])) {
         $this->mrTemplate->AddVar('data_laporan_mahasiswa', 'LAPORAN_EMPTY', 'YES');
      } else {
         $decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
         $encPage = Dispatcher::Instance()->Encrypt($decPage);
         $this->mrTemplate->AddVar('data_laporan_mahasiswa', 'LAPORAN_EMPTY', 'NO');
         $dataLaporanMahasiswa = $data['dataLaporanMahasiswa'];

         for ($i=0; $i<sizeof($dataLaporanMahasiswa); $i++) {
            $no = $i+$data['start'];
            $dataLaporanMahasiswa[$i]['number'] = $no;
            if ($no % 2 != 0) $dataLaporanMahasiswa[$i]['class_name'] = 'table-common-even';
            else $dataLaporanMahasiswa[$i]['class_name'] = '';
            
            $idEnc = Dispatcher::Instance()->Encrypt($dataLaporanMahasiswa[$i]['id']);
            
            if($dataLaporanMahasiswa[$i]['jenis_kelamin'] == 'L'){
               $dataLaporanMahasiswa[$i]['JK_TITLE'] = 'Laki-laki';
            }else{
               $dataLaporanMahasiswa[$i]['JK_TITLE'] = 'Perempuan';
            }
            
            if($dataLaporanMahasiswa[$i]['status_reg'] == 'S'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_REG'] = 'Sudah Registrasi';
            }else{
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_REG'] = 'Belum Registrasi';
            }
            
            if($dataLaporanMahasiswa[$i]['status_mhs'] == 'CM'){
               $dataLaporanMahasiswa[$i]['status_akd'] = 'N';
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_MHS'] = 'Calon Mahasiswa';
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Non Aktif';
            }else{
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_MHS'] = 'Mahasiswa';
            }
            
            if($dataLaporanMahasiswa[$i]['status_akd'] == 'A'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Aktif';
            }elseif($dataLaporanMahasiswa[$i]['status_akd'] == 'N'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Non Aktif';
            }elseif($dataLaporanMahasiswa[$i]['status_akd'] == 'C'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Cuti';
            }elseif($dataLaporanMahasiswa[$i]['status_akd'] == 'K'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Keluar';
            }elseif($dataLaporanMahasiswa[$i]['status_akd'] == 'L'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Lulus';
            }elseif($dataLaporanMahasiswa[$i]['status_akd'] == 'B'){
               $dataLaporanMahasiswa[$i]['STATUS_TITLE_AKD'] = 'Blokir';
            }
            
            $dataLaporanMahasiswa[$i]['total_bayar'] = number_format($dataLaporanMahasiswa[$i]['total_bayar'], 2,',','.');
            if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);           
            if($i == sizeof($dataLaporanMahasiswa)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
            $this->mrTemplate->AddVars('data_laporan_mahasiswa_item', $dataLaporanMahasiswa[$i], 'LAPORAN_');
            $this->mrTemplate->parseTemplate('data_laporan_mahasiswa_item', 'a');    
         }
      }
   }
}
?>