<?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 kode_jurusan_aktivitas='".$_SESSION['jurusan']."'";
          }

          $nama_jurusan = "Semua Jurusan";
      } else {
         
          $kode_jur = "and kode_jurusan='$jurusan'";
           $jurusan = "and kode_jurusan_aktivitas='$jurusan'";

        $nama_jurusan = $db->fetch_custom_single("select nama_jurusan from jurusan where id is not null $kode_jur");
        $nama_jurusan = $nama_jurusan->nama_jurusan;
        $nama_jurusan = str_replace(" ", "_", $nama_jurusan);

      }

  $jurusan_mhs = "";
  if ($_POST['jurusan_mhs']!='all') {
  $jurusan_mhs = "and aktivitas_anggota.kode_jurusan='".$_POST['jurusan_mhs']."'";
  }

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

        }

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



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

//column width
$col_width = array(
  1 => 15,
  2 => 17,
  3 => 15,
  4 => 20,
  5 => 17,
  6 => 27,
  7 => 27,
  8 => 39
  );
$writer->setColWidth($col_width);

$header = array(
  'Semester'=>'string',
  'ID AKTIVITAS'=>'string',
  'NIM'=>'string',
  'NAMA'=>'string',
  'Jenis Peran' => 'string',
  'Kode Prodi Aktivitas' => 'string',
  'Kode Prodi Mahasiswa' => 'string',
  'Keterangan'=>'string'
);

$no=1;
$index = 2;

$data_rec = array();
   
        $order_by = "order by semester desc";
 
        $temp_rec = $db->fetch_custom("select aktivitas_anggota.* from aktivitas_anggota $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->semester,
                                      $key->id_aktivitas,
                                      $key->nim,
                                      $key->nama,
                                      $key->jenis_peran,
                                      $key->kode_jurusan_aktivitas,
                                      $key->kode_jurusan,
                                      $ket
                        );

             $no++;
             $index++;

            }

$comments = array(
      'A1' => 'Semester:
Periode Semester
tahun+semester
ganjil 1, genap 2, perbaikan 3
contoh 20231 berarti 2023 ganjil
Info : Wajib Diisi',
      'B1' => '
Diisi sama dengan id di akvitias mahasiswa, ke aktivitas mana Peserta/Anggota ini akan dimasukan

,bisa gunakan Id dari neo, download Aktivitas Mahasiswa dari Menu Tool -> Data Downloader -> Download Aktivitas Mhs, Di Hasil download kolom terakhir ada ID Neo Berupa Id seperti format 0e9d43ec-9b78-4df1-896e-6ea160d0588c
info : wajib disi',
      'C1' => '
      info : Wajib disi',
      'D1' => '
      info : Boleh Kosong',
      'E1' => 'Jenis Peran Mahasiswa
1 : Ketua
2 : Anggota
3 : Personal
info : Wajib disi',
      'F1' => '
      info : Wajib disi',
      'G1' => '
      info : Wajib disi'
      );
$writer->setComment($comments);
$filename = 'Data_Anggota_Aktivitas_'.$nama_semester.$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 Anggota '.$nama_semester.$nama_jurusan, $header, $style);
$writer->writeToStdOut();
exit(0);
?>