<?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 = "All Dosen Ajar";
      } 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['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 from kelas_kuliah 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['status']=='all') {
          $status = '';
        } else {
          $status = "and status_error='".$_POST['status']."'";
        }

        }


        $filter_dosen = "where id is not null $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',
                    'D1',
                    'E1',
                    'F1',
                    'H1',
                    'J1',
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'C1',
                      'G1',
                        'I1',
                       'K1',
                       'L1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

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

$header = array(
  'Semester'=>'string',
  'NIDN/NUPTK/ID Dosen NEO'=>'string',
  'Nama Dosen'=>'string',
  'Kode Matakuliah'=>'string',
  'Nama Kelas' => 'string',
  'Rencana Tatap Muka' => 'string',
  'Tatap Muka Real' => 'string',
  'Kode Prodi'=>'string',
  'SKS Ajar' => 'string',
  'Jenis Evaluasi' => 'string',
  'ID Substansi Kuliah' => 'string',
  'Keterangan'=>'string'
);

$no=1;
$index = 2;

$data_rec = array();
    $order_by = "order by kode_mk,nama_kelas ASC";

    
        $temp_rec = $db->fetch_custom("select * from ajar_dosen $filter_dosen $order_by");


                    foreach ($temp_rec as $key) {

                                  if ($key->status_error==0) {
                                    $ket = "Belum diproses";
                                  } elseif ($key->status_error==1) {
                                    $ket = "Sukses";
                                  } else {
                                    $ket = strip_tags($key->keterangan);
                                  }

                                    $data_rec[] = array(
                                      $key->semester,
                                      $key->nidn,
                                      $key->nama_dosen,
                                      $key->kode_mk,
                                      $key->nama_kelas,
                                      $key->rencana_tatap_muka,
                                      $key->tatap_muka_real,
                                      $key->kode_jurusan,
                                      $key->sks_ajar,
                                      $key->id_jenis_evaluasi,
                                      $key->id_substansi,
                                      $ket
                        );


                                   $index++;
                              }


$comments = array(
'A1' => 'Semester Pelaporan
format : tahun+semester (1 : Ganjil, 2 : Genap, 3 : Pendek)
contoh : 20231 (berarti 2023 ganjil)
info : Wajib disi',
'B1' => 'NIDN / NUPTK/ ID DOSEN NEO
Gunakan NIDN atau NUPTK atau gunakan ID Dosen NEO,
ID Dosen Neo Bisa di download di Menu Tool -> Data Downloader -> Download Data Dosen
info : Wajib disi',
'C1' => 'Nama Dosen
info : Boleh di Kosongkan',
'D1' => 'Kode Matakuliah
Info : Wajib Diisi',
'E1' => 'Nama Kelas Kuliah
Ket : Nama kelas tidak boleh melebihi 5 karakter
Info : Wajib Diisi',
'F1' => 'Tatap Muka:
Jumlah Total Tatap Muka Satu Semester

Info : Wajib Diisi',
'G1' => '
Jumlah Tatap Muka Realisasi.
Jika dikosongkan, Sistem akan menyamakan realisasi dengan jumlah tatap muka

info : Boleh di Kosongkan',
'H1' => 'Lihat di menu master data -> jurusan

Info : Wajib Diisi',
'I1' => 'SKS AJAR:
Jika kosong, sks akan diambil dari matakuliah.

info : Boleh di Kosongkan',
'J1' => 'Jenis Evaluasi
ID Nama Jenis
1: Evaluasi Akademik
2: Aktivitas Partisipatif
3: Hasil Proyek
4: Kognitif/ Pengetahuan

Info : Wajib Diisi',
'K1' => '
ID Substansi Kuliah Neo Bisa di download di Menu Tool -> Data Downloader -> Download Substansi Kuliah
info : Boleh Kosong'
);

$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 ajar '.$nama_semester.$nama_jurusan.$nama_mk, $header, $style);
$writer->writeToStdOut();
exit(0);
?>