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

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

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


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

        $filter_mk = "where 1=1 $sem_kur_filter $jurusan $sem_kur_filter $sem_filter_matkul $status $status_mkur $jns_mk ";




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

//column width
$col_width = array(
  1 => 15,
  2 => 20,
  3 => 10,
  4 => 15,
  5 => 15,
  6 => 18,
  7 => 13,
  8 => 25,
  9 => 15,
  10 => 15,
  11 => 10,
  12 => 20,
  13 => 11,
  14 => 13,
  15 => 20,
  16 => 15,
  17 => 39
  );
$writer->setColWidth($col_width);

$header = array(
  'Kode MK'=>'string',
  'Nama MK'=>'string',
  'Jenis MK'=>'string',
  'SKS Tatap Muka'=>'string',
  'SKS Praktek' => 'string',
  'SKS Praktek Lapangan' => 'string',
  'SKS Simulasi' => 'string',
  'Metode Pembelajaran' => 'string',
  'Tgl Mulai Efektif' => 'string',
  'Tgl Akhir Efektif' => 'string',
  'Semester' => 'string',
  'Tahun Kurikulum' => 'string',
  'Wajib' => 'string',
  'Kode Prodi' => 'string',
  'Kelompok Matakuliah' => 'string',
  'Id Matkul' => 'string',
  'Keterangan'=>'string'
);

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

    
        $temp_rec = $db->fetch_custom("select * from mat_kurikulum $filter_mk $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->jns_mk,
                                      $key->sks_tm,
                                      $key->sks_prak,
                                      $key->sks_prak_lap,
                                      $key->sks_sim,
                                      $key->metode_pelaksanaan_kuliah,
                                      $key->tgl_mulai_efektif,
                                      $key->tgl_akhir_efektif,
                                      $key->semester,
                                      $key->tahun,
                                      $key->wajib,
                                      $key->kode_jurusan,
                                      $key->kelompok_mk,
                                      $key->id_matkul,
                                      $ket
                        );


            }
//pekerjaan
$data_jenis_mk = $db->fetch_custom("select * from jenis_mk");
foreach ($data_jenis_mk as $hasil) {
  $jenis_mk[] = $hasil->id_jns_mk." : ".$hasil->nm_jns_mk;
}
$data_kelompok_mk = $db->fetch_custom("select * from kelompok_mk");
foreach ($data_kelompok_mk as $hasil) {
  $kelompok_mk[] = $hasil->id_kel_mk." : ".$hasil->nm_kel_mk;
}
$comments = array(
'A1' => 'Kode Mata Kuliah

info : wajib disi',
'B1' => 'Nama Mata Kuliah
info : wajib disi',
'B1' => 'Nama Mata Kuliah
info : wajib disi',
'C1' => 'Jenis Mata Kuliah. Isi sesuai id dibawah
'.implode("\n", $jenis_mk).'

Info : Wajib Diisi
',
'D1' => 'sks_tm : Jumlah Sks Tatap Muka
Isi 0 : jika tidak ada
info : wajib disi',
'E1' => 'Jumlah Sks Praktek.
Isi 0 : jika tidak ada
info : wajib disi',
'F1' => 'Jumlah SKS Prak Lapaangan.
Isi 0 : jika tidak ada
info : wajib disi',
'G1' => 'Jumlah SKS Simulasi.
Isi 0 : jika tidak ada
info : wajib disi',
'H1' => 'Metode Pembelajaran
info : Boleh kosong jika belum ada',
'I1' => 'ket : tanggal Mulai efektif matakuliah
format : tahun-bulan-tgl
contoh : 2017-07-01
info : Boleh kosong jika belum ada',
'J1' => 'ket : tanggal Akhir efektif matakuliah
format : tahun-bulan-tgl
contoh : 2017-07-01
info : Boleh kosong jika belum ada',
'K1' => 'Semester Matakuliah 1 sampai 8
info : Wajib diisi',
'L1' => 'Mulai Berlaku Kurikulum
format : tahunsemester
contoh : 20201',
'M1' => 'Apakah Matkul Wajib
1 : YA
0 : TIDAK',
'N1' => 'Kode Prodi/Jurusan, 
Lihat di menu master data -> jurusan
Wajib diisi',
'O1' => 'Kelompok Matakuliah. Isi sesuai id dibawah
'.implode("\n", $kelompok_mk).'

Info : Wajib Diisi
',
'P1' => 'Hanya untuk update Matakuliah, Bisa didapat dari download matkul di menu tool -> download data
Info : Boleh Kosong');
$writer->setComment($comments);

$filename = "Matakuliah_Kurikulum_".$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 MatKurikulum', $header, $style);
$writer->writeToStdOut();
exit();
?>