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


        $filter_akm = "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',
                    'B1',
                    'C1',
                    'D1',
                    'E1',
                    'F1',
                    'G1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                     array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'H1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                ),
            );


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


$header = array(
  'Nilai Huruf'=>'string',
  'Nilai Indeks'=>'string',
  'Bobot Nilai Minimum' => 'string',
  'Bobot Nilai Maksimum' =>'string',
  'Tanggal Mulai Efektif' =>'string',
  'Tanggal Akhir Efektif' =>'string',
  'Kode Prodi' =>'string',
  'Keterangan' =>'string'
);



$data_rec = array();
$temp_data = $db->fetch_custom("select * from skala_nilai $filter_akm order by kode_jurusan asc");
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->nilai_huruf,
                  $key->nilai_indeks,
                  $key->bobot_nilai_min,
                  $key->bobot_nilai_maks,
                  $key->tgl_mulai_efektif,
                  $key->tgl_akhir_efektif,
                  $key->kode_jurusan,
                  $ket
                );          
    }
$filename = "Data_Skala_Nilai_".$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,'Skala Nilai', $header, $style);
$writer->writeToStdOut();
exit(0);
?>