<?php
session_start();
include "../../inc/config.php";

require_once '../Writer.php';


      $jurusan = trim($_POST['jur_filter']);
      if ($jurusan=='all') {
          $jurusan = "";
             if ($_SESSION['level']!=1) {
              $jurusan = "and trim(kode_jurusan)='".$_SESSION['jurusan']."'";
          }
          $nama_jurusan = "Semua Nilai Transfer";
      } else {
          $jurusan = "and trim(kode_jurusan)='$jurusan'";

        $nama_jurusan = $db->fetch_custom_single("select nama_jurusan from jurusan where id is not null $jurusan");
        $nama_jurusan = $nama_jurusan->nama_jurusan;
        $nama_jurusan = str_replace(" ", "_", $nama_jurusan);

      }

       if (isset($_POST['filter_matkul'])) {
          

        if ($_POST['filter_matkul']=='all') {
        	$kode_mk = '';
        	$nama_mk = "";
        } else {
        	$kode_mk = "and kode_mk='".$_POST['filter_matkul']."'";
        	
        $nama_mk = $db->fetch_custom_single("select nama_mk,kode_mk from nilai_transfer where id is not null $jurusan $kode_mk");
          if ($nama_mk->nama_mk!='') {
            $nama_mk = str_replace(" ", "_", $nama_mk->nama_mk);
          } else {
            $nama_mk = $nama_mk->kode_mk;
          }
        }

        if ($_POST['sem_filter']=='all') {
          $semester = '';
        } else {
          $semester = "and nilai_transfer.semester='".$_POST['sem_filter']."'";
        }

           if ($_POST['status']=='all') {
        	$status = '';
        } else {
        	$status = "and status_error='".$_POST['status']."'";
        }

        }

        $filter_nilai = "where id is not null $jurusan $kode_mk $status $semester";



$writer = new XLSXWriter();
$style =
        array (
                      array(
              'border' => array(
                'style' => 'thin',
                'color' => '000000'
                ),
            'allfilleddata' => true
            ),
            array(
                'fill' => array(
                    'color' => 'ff0000'
                    ),
                'cells' => array(
                       'A1',
                    'C1',
                     'D1',
                    'E1',
                    'F1',
                    'G1',
                    'H1',
                    'I1',
                    'J1',
                    'K1',
                    'L1',
                    
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                        'B1',
                        'M1',
                      'N1',
                      'O1'

                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

//column width
$col_width = array(
  1 => 15,
  2 => 23,
  3 => 15,
  4 => 23,
  5 => 10,
  6 => 15,
  7 => 22,
  8 => 25,
  9 => 19,
  10 => 19,
  11 => 19,
  12 => 20,
  13 => 25,
  14 => 27,
  15 => 39
  );
$writer->setColWidth($col_width);


$header = array(
  'NIM'=>'string',
  'NAMA'=>'string',
  'Kode Mk Asal'=>'string',
  'Nama Mk Asal'=>'string',
  'SKS Asal'=>'string',
  'Nilai Huruf Asal'=>'string',
  'Kode Matakuliah Diakui'=>'string',
  'Nama Matakuliah Diakui'=>'string',
  'Nilai Huruf Diakui' => 'string',
  'Nilai Indeks Diakui' => 'string',
  'Kode Prodi' => 'string',
  'Semester' => 'string',
  'Kode Asal Perguruan Tinggi' => 'string',
  'ID Aktivitas' => 'string',
  'Keterangan'=>'string',
);

$no=1;
$index = 2;

$data_rec = array();

        $order_by = " order by kode_mk ASC";

        $data = $db->fetch_custom("select * from nilai_transfer $filter_nilai $order_by");


    
                    foreach ($data as $key) {
                     
               			if ($key->status_error==0) {
               				$ket = "Belum diproses";
               			} elseif ($key->status_error==1) {
               				$ket = "Sukses";
               			} else {
               				$ket = $key->keterangan;
               			}
                     $data_rec[] = array(
                                      $key->nim,
                                      $key->nama,
                                      $key->kode_mk_asal,
                                      $key->nm_mk_asal,
                                      $key->sks_asal,
                                      $key->nilai_huruf_asal,
                                      $key->kode_mk,
                                      $key->nama_mk,
                                      $key->nilai_huruf_diakui,
                                      $key->nilai_angka_diakui,
                                      $key->kode_jurusan,
                                      $key->semester,
                                      $key->kode_pt,
                                      $key->id_aktivitas,
                                      $ket
                        );

                         $no++;
                         $index++;
                    }


$comments = array(
'A1' => 'NIM / NIPD Mahasiswa
info : wajib disi',
'C1' => '
info : wajib disi',
'D1' => '
info : wajib disi',
'E1' => '
info : wajib disi',
'F1' => '
info : wajib disi',
'G1' => '
info : wajib disi',
'H1' => '
info : wajib disi',
'I1' => '
info : wajib disi',
'J1' => '
info : wajib disi',
'K1' => '
info : wajib disi',
'L1' => 'Peride Masuk Kuliah/Angkatan
tahun+semester
1 : ganjil, 2 genap, 
20221 berarti angakatan 2022 ganjil
Info : Wajib Diisi',
'M1' => 'info :
Kode Asal Perguruan Tinggi , Bisa lihat referensi kode pt di menu Master Data, Data Prodi Kampus. 

Boleh kosog, baiknya Diisi',
'N1' => 'Id Aktivitas,
anda bisa download dulu data aktivitas di menu tool, download aktivitas, 
pilih jenis yang pertukaran pelajar, 
anda bisa menggunakan isian Id Aktivitas atau ID Aktivitas NEO'
);
$writer->setComment($comments);
$filename = 'nilai_transfer'.$nama_jurusan.$nama_mk.'.xlsx';
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$writer->writeSheet($data_rec,'Data Nilai Transfer', $header, $style);
$writer->writeToStdOut();
exit(0);
?>