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


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

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

$header = array(
  'Kode Matakuliah'=>'string',
  'Nama Matakuliah'=>'string',
  'Basis Evaluasi'=>'string',
  'Komponen Evaluasi'=>'string',
  'Bobot (%)'=>'string',
  'Deskripsi' => 'string',
  'Deskripsi (Inggris)' =>'string',
  'Kode Prodi'=>'string',
  'Keterangan'=>'string'
);

$data_rec = array();
   
        $order_by = "order by kode_mk ASC";

      $no=1;
        $temp_rec = $db->fetch_custom("select * from mat_rencana_evaluasi $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->kode_mk,
                                      $key->nama_mk,
                                      $key->id_basis_evaluasi,
                                      $key->komponen_evaluasi,
                                      $key->bobot_evaluasi,
                                      $key->deskripsi_indonesia,
                                      $key->deskripsi_inggris,
                                      $key->kode_jurusan,
                                      $ket
                        );

             $no++;

            }

$comments = array(
'A1' => 'Kode Mata Kuliah
info : wajib disi',
'B1' => 'Nama Mata Kuliah
info : Boleh kosong',
'C1' => 'Id Basis Evaluasi
2 : Aktivitas Partisipatif
3 : Hasil Proyek
4 : Kognitif/ Pengetahuan

info : WAJIB DIISI',
'D1' => 'Jika Basis Evaluasi 2 atau 3 maka komponen harus dikosongkan

TGS : Tugas
QIZ : Kuis
UTS : Ujian Tengah Semester
UAS : Ujian Akhir Semester',
'E1' => 'Total Bobot harus 100 persen.
Wajib diisi dengan angka,
dan boleh diisi dengan angka 0 jika bobot tidak ada.

info : WAJIB DIISI',
'F1' => 'Deskripi dalam bahasa Indonesia,
info : WAJIB DIISI',
'G1' => 'Deskripi dalam bahasa inggris,
info : Boleh Kosong',
'H1' => 'Kode Prodi/Jurusan, 
Lihat di menu master data -> jurusan
Wajib diisi'
);



        $writer->setComment($comments);
$filename = $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 Evaluasi '.$nama_jurusan.$nama_mk, $header, $style);
$writer->writeToStdOut();
exit(0);
?>