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

        $filter_krs = "where id is not null $jurusan $status";




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

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

$header = array(
  'NIM'=>'string',
  'Nama'=>'string',
  'Jenis Prestasi'=>'string',
  'Tingkat Prestasi' => 'string',
  'Nama Prestasi' => 'string',
  'Tahun' => 'string',
  'Penyelenggara'=>'string',
  'Peringkat'=>'string',
  'Kode Prodi' => 'string',
  'ID Aktivitas' => 'string',
  'ID Prestasi Neo' => 'string',
  'Keterangan'=>'string'
);
  

$data_rec = array(); 


   
        $order_by = "order by nipd ASC";

    
        $temp_rec = $db->fetch_custom("select * from prestasi_mahasiswa $filter_krs $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->nipd,
                                      $key->nama,
                                      $key->id_jns_prestasi,
                                      $key->id_tkt_prestasi,
                                      $key->nm_prestasi,
                                      $key->thn_prestasi,
                                      $key->penyelenggara,
                                      $key->peringkat,
                                      $key->kode_jurusan,
                                      $key->id_aktivitas,
                                      $key->id_prestasi,
                                      $ket
                        );

                    }
       
$filename = 'Prestasi_'.$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 Prestasi', $header, $style);
$writer->writeToStdOut();
exit(0);
?>