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

        }

        if ($_POST['filter_peran']=='all') {
          $peran = '';
        } else {
          $peran = "and jenis='".$_POST['filter_peran']."'";
        }



        $filter_kelas = "where id is not null $jurusan $semester $peran $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',
                    'F1',
                    'G1',
                    'H1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'D1',
                      'I1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

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

$header = array(
  'Semester'=>'string',
  'ID AKTIVITAS'=>'string',
  'NIDN/NUPTK/ID Dosen NEO'=>'string',
  'NAMA'=>'string',
  'Jenis Peran' => 'string',
  'Urutan Bimbing/Uji' => 'string',
  'Kategori Kegiatan' => 'string',
  'Kode Prodi' => 'string',
  'Keterangan'=>'string'
);

$no=1;
$index = 2;

$data_rec = array();
   
        $order_by = "order by semester desc";
 
        $temp_rec = $db->fetch_custom("select aktivitas_bimbing_uji.* from aktivitas_bimbing_uji $filter_kelas $order_by");
                    foreach ($temp_rec 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,
                                      $key->id_aktivitas,
                                      $key->nidn_dosen,
                                      $key->nama_dosen,
                                      $key->jenis,
                                      $key->urutan,
                                      $key->kategori_kegiatan,
                                      $key->kode_jurusan,
                                      $ket
                        );

             $no++;
             $index++;

            }

$filename = "Pembimbing_Penguji_".$nama_semester.$nama_jurusan.'.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 PembimbingPenguji', $header, $style);
$writer->writeToStdOut();
exit(0);
?>