<?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']."'";
            if ($_SESSION['jurusan']=='0') {
              $jurusan = '';
            }
          }
          $nama_jurusan = "All";
      } 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['jenis_keluar'])) {
          
        if ($_POST['jenis_keluar']=='all') {
          $jenis_keluar = '';
          $nama_jenis_keluar = "";
        } else {
          $jenis_keluar = "and id_jenis_keluar='".$_POST['jenis_keluar']."'";
          $nama_jenis_keluar = $_POST['jenis_keluar'];
        }


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

        }


        $filter_akm = "where id is not null $jurusan $jenis_keluar $status";

    $datas_keluar = $db->fetch_custom("select * from jenis_keluar");
    foreach ($datas_keluar as $keluar) {
      $data_keluar[] = $keluar->id_jns_keluar." : ".$keluar->ket_keluar;
    }

 
$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',
                     'H1',
                    'J1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'B1',
                      'F1',
                      'G1',
                      'I1',
                      'K1',
                      'L1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

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

$header = array(
  'NIM'=>'string',
  'Nama'=>'string',
  'Jenis Keluar'=>'string',
  'Tanggal keluar / Tanggal lulus'=>'string',
  'Semester Keluar' => 'string',
  'Nomor SK'=>'string',
  'Tanggal SK' => 'string',
  'IP Kumulatif'=>'string',
  'Ket Kelulusan' => 'string',
  'Kode Prodi'=>'string',
  'Ket Import'=>'string'
);


$data_rec = array(); 


        $order_by = "order by nim ASC";

        $temp_data = $db->fetch_custom("select * from kelulusan $filter_akm $order_by");


     
        foreach ($temp_data 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->nim,
                                      $key->nama,
                                      $key->id_jenis_keluar,
                                      $key->tanggal_keluar,
                                      $key->semester,
                                      $key->sk_yudisium,
                                      $key->tgl_sk_yudisium,
                                      $key->ipk,
                                      $key->keterangan_kelulusan,
                                      $key->kode_jurusan,
                                      $ket
                        );
                       
            }

$comments = array(
'A1' => 'NIM / NIPD Mahasiswa

info : wajib disi',
'B1' => 'Nama Mahasiswa
Info : Boleh kosong',
'C1' => 'Jenis keluar
'.implode("\n", $data_keluar).'

Info : Wajib Diisi',
'D1' => 'format tahun-bulan-tanggal
Info : Wajib Diisi',
'E1' => 'Peride keluar
tahun+semester
1 : ganjil, 2 genap, 
20221 berarti keluar 2022 ganjil
Info : Wajib Diisi',
'H1' => '
Info : Wajib Diisi',
'J1' => '
Info : Wajib Diisi'
);
$writer->setComment($comments);

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