<?php
session_start();
include "../../inc/config.php";

require_once '../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_daya_tampung";
      } 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 (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']."'";
        }

        }


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

//column width
$col_width = array(
  1 => 13,
  2 => 15,
  3 => 19,
  4 => 51,
  5 => 19,
  6 => 24,
  7 => 25,
  8 => 15,
  9 => 22,
  10 => 49
  );
$writer->setColWidth($col_width);

$header = array(
  'Semester'=> 'string',
  'Kode Prodi'=> 'string',
  'Id  Jalur Pendaftaran'=> 'string',
  'Nama Jalur Pendaftaran'=> 'string',
  'Jml Target Mhs Baru'=> 'string',
  'Jml Pendaftar Ikut Seleksi'=> 'string',
  'Jml Pendaftar Lulus Seleksi'=> 'string',
  'Jml Daftar Ulang'=> 'string',
  'Jml Mengundurkan Diri'=> 'string',
  'Keterangan'=> 'string'
);
  

$data_rec = array(); 
   
        $order_by = "order by semester,kode_jurusan";

    
        $temp_rec = $db->fetch_custom("select * from daya_tampung $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->kode_jurusan,
                       $key->id_jalur_masuk,
                       $key->nm_jalur_masuk,
                       $key->target_mhs_baru,
                       $key->calon_ikut_seleksi,
                       $key->calon_lulus_seleksi,
                       $key->daftar_sbg_mhs,
                       $key->pst_undur_diri,
                       $ket
                       );
            }

$aray_comment = array(
'A1' => 'Periode Semester
Info : Wajib Diisi',
'B1' => 'Kode Program Studi
Info : Wajib Diisi',
'C1' => '
id nama jalur
9 Program Internasional
11 Program Kerjasama Perusahaan/Institusi/Pemerintah
4 Prestasi
3 Penelusuran Minat dan Kemampuan (PMDK)
12 Seleksi Mandiri
13 Ujian Masuk Bersama Lainnya
14 Seleksi Nasional Berdasarkan Tes (SNBT)
15 Seleksi Nasional Berdasarkan Prestasi (SNBP)

Isikan semua jalur, jika data belum ada. 
Namun, jika anda kebutuhanya untuk update, maka boleh isi jalur pendaftaran yang akan diupdate saja.',
'D1' => '
Info :Wajib Diisi',
'E1' => '
Info :Wajib Diisi',
'F1' => '
Info :Wajib Diisi',
'G1' => '
Info :Wajib Diisi',
'H1' => '
Info :Wajib Diisi',
'I1' => '
Info :Wajib Diisi',
);
$writer->setComment($aray_comment);
$judul_file = $nama_semester;

$judul_jurusan = $nama_jurusan;

$filename = $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,'daya tampung '.$nama_semester.$nama_jurusan, $header, $style);
$writer->writeToStdOut();
exit(0);
?>