<?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";
      } 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 from nilai 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['filter_kelas']=='all') {
        	$nama_kelas = '';
        	$nama_kelas_judul = "";
        } else {
        	$nama_kelas = "and nama_kelas='".$_POST['filter_kelas']."'";
        	$nama_kelas_judul = $_POST['filter_kelas'];
        }

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

        }


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



 
$writer = new XLSXWriter();
$style =
        array (
                      array(
              'border' => array(
                'style' => 'thin',
                'color' => '000000'
                ),
            'allfilleddata' => true
            ),
            array(
                'fill' => array(
                    'color' => 'ff0000'
                    ),
                'cells' => array(
                    'A1',
                    'C1',
                    'E1',
                    'F1',
                    'G1',
                    'H1',
                    'I1',
                    'J1',
                    'K1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'B1',
                      'D1',
                      '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 => 20,
  6 => 15,
  7 => 15,
  8 => 15,
  9 => 15,
  10 => 20,
  11 => 18,
  12 => 39
  );
$writer->setColWidth($col_width);

$header = array(
  'NIM'=>'string',
  'Nama'=>'string',
  'Kode Matakuliah'=>'string',
  'Nama Matakuliah'=>'string',
  'Semester'=>'string',
  'Kelas' => 'string',
  'Nilai Huruf'=>'string',
  'Nilai Indeks' => 'string',
  'Nilai Angka' => 'string',
  'Kode Prodi Mahasiswa'=>'string',
  'Kode Prodi Kelas'=>'string',
  'Keterangan'=>'string'
);

$data_rec = array(); 

        $order_by = " order by kode_mk ASC";

        $data = $db->fetch_custom("select * from nilai $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->nim,
                                      $key->nama,
                                      $key->kode_mk,
                                      $key->nama_mk,
                                      $key->semester,
                                      $key->nama_kelas,
                                      $key->nilai_huruf,
                                      $key->nilai_indek,
                                      $key->nilai_angka,
                                      $key->kode_jurusan,
                                      $key->kode_jurusan_kelas,
                                      $ket
                        );

                    }

$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 Nilai', $header, $style);
$writer->writeToStdOut();
exit(0);
?>