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

require_once '../../lib/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_Konversi";
      } else {
          $jurusan = "and trim(kode_jurusan)='$jurusan'";

        $nama_jurusan = $db->fetch_custom_single("select nama_jurusan from jurusan where id!='' $jurusan");
        $nama_jurusan = $nama_jurusan->nama_jurusan;
        $nama_jurusan = str_replace(" ", "_", $nama_jurusan);

      }



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

        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 nilai_kampus_merdeka where id!='' $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['status']=='all') {
          $status = '';
        } else {
          $status = "and status_error='".$_POST['status']."'";
        }

        }


        $filter_nilai = "where id!='' $jurusan $semester $kode_mk $status";



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

//column width
$col_width = array(
  1 => 12,
  2 => 16,
  3 => 15,
  4 => 15,
  5 => 13,
  6 => 13,
  7 => 13,
  8 => 13,
  9 => 13,
  10 => 13,
  11 => 13,
  12 => 39
  );
$writer->setColWidth($col_width);

$header = array(
  'Semester'=>'string',
  'ID AKTIVITAS'=>'string',
  'NIM'=>'string',
  'Nama Mahasiswa'=>'string',
  'Kode MK'=>'string',
  'Nama MK'=>'string',
  'SKS' => 'string',
  'Nilai Huruf'=>'string',
  'Nilai Indeks' => 'string',
  'Nilai Angka' => 'string',
  'Kode Prodi'=>'string',
  'Keterangan'=>'string'
);

$data_rec = array(); 

        $order_by = " order by kode_mk ASC";

        $data = $db->fetch_custom("select * from nilai_kampus_merdeka $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->semester,
                                      convert_ascii($key->id_aktivitas),
                                      convert_ascii($key->nim),
                                      convert_ascii($key->nama),
                                      convert_ascii($key->kode_mk),
                                      convert_ascii($key->nama_mk),
                                      convert_ascii($key->sks),
                                      convert_ascii($key->nilai_huruf),
                                      convert_ascii($key->nilai_indeks),
                                      convert_ascii($key->nilai_angka),
                                      convert_ascii($key->kode_jurusan),
                                      convert_ascii($ket)
                        );

                    }
 $comments = array(
      'A1' => 'Semester:
Periode Semester
tahun+semester
ganjil 1, genap 2, perbaikan 3
contoh 20231 berarti 2023 ganjil
Info : Wajib Diisi',
      'B1' => '
Diisi sama dengan id di akvitias mahasiswa, ke aktivitas mana Nilai Konversi ini akan dimasukan

jika kampus merdeka, gunakan Id dari neo, download Aktivitas Mahasiswa dari Menu Tool -> Data Downloader -> Download Aktivitas Mhs, Di Hasil download kolom terakhir ada ID Neo Berupa Id seperti format 0e9d43ec-9b78-4df1-896e-6ea160d0588c
info : wajib disi',
      'C1' => '
      info : Wajib disi',
      'D1' => '
      info : Boleh Kosong',
      'E1' => '
      info : Wajib disi',
      'F1' => '
      info : Boleh Kosong',
      'G1' => '
      Jika dikosongkan, maka akan diambil dari data sks matakuliah feeder',
      'H1' => '
      info : Wajib disi',
      'I1' => '
      info : Wajib disi',
      'J1' => '
      info : Wajib disi',
      'K1' => '
      info : Wajib disi'
      );
      $writer->setComment($comments);
$filename = $nama_semester.$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 Konversi', $header, $style);
$writer->writeToStdOut();
exit(0);
?>