<?php
include "../../inc/config.php";
require_once '../../lib/Writer.php';
include "../../lib/vendor/autoload.php";
use \JsonMachine\JsonMachine;
$json_response = array();
$jumlah = array();
if ($_POST['total_data']<1) {
    $msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">Tidak ada data berhasil di generate</font><br />
        </div>";

    $jumlah['last_notif'] = $msg;
    array_push($json_response, $jumlah);

    echo json_encode($json_response);
    exit();
}

$url = $db->get_service_url('rest');
$data_rec = array();
$offset = $_POST['offset'];
$token = $_POST['token'];

$jumlah['offset'] = $offset;
$jumlah['token'] = $_POST['token'];

$no = $offset;

if ($offset==0) {
$token = $db->get_token();
    $jumlah['token'] = $token;
}


        $id_sms = '';
        $status_mhs = "";
        $angkat = "";
        $smt_yudisium = "";
         $filter_status = "";
$periode_count = "";
        $array_status = array();
      if ($_POST['jurusan']!="all") {
            $filter_sms = "id_registrasi_mahasiswa is not null and id_prodi='".$_POST['jurusan']."'";
        } else {
            $filter_sms = "id_registrasi_mahasiswa is not null";
        }
        
        $filter_angkatan = "and (angkatan BETWEEN '".$_POST['angkatan_awal']."' and '".$_POST['angkatan_akhir']."')";

        if ($_POST['jns_keluar']!='') {
               foreach ($_POST['jns_keluar'] as $val) {
                       $array_status[] = "'$val'";
               }
            if (!empty($array_status)) {
                $implode_status = implode(",", $array_status);
                    $filter_status = " and id_jenis_keluar IN(".$implode_status.")";
            }
        }


  if ($_POST['id_periode_keluar']!='all') {   
     $periode_count = "and id_periode_keluar='".$_POST['id_periode_keluar']."'"; 
  }

        $filter_mahasiswa = "$filter_sms $filter_angkatan $filter_status $periode_count";


        if ($_POST['total_data']<=50) {
            $limit = $_POST['total_data'];
        } else {
            $limit = 50;
        }


        //$filter_kelulusan = "id_registrasi_mahasiswa is not null $id_periode_keluar $id_sms $status_mhs $angkat";

        


        /*$order_by = "angkatan DESC";
        $temp_rec = [
          'act' => 'GetListMahasiswaLulusDO',
            'token' => $token,
            'filter' => trim($filter_kelulusan),
            'order' => '',
            'limit' => $limit,
            'offset' => $offset

        ];

        $temp_rec_nim = json_decode($db->run($temp_rec,$url_rest));



        if (!empty($temp_rec_nim->data)) {
            foreach ($temp_rec_nim->data as $dt) {

            
              $nim_data[convert_ascii($dt->nim).$dt->id_prodi] = "('".convert_ascii($dt->nim)."','".$dt->id_prodi."')";
              $smt_keluar[$dt->id_registrasi_mahasiswa] = $dt->id_periode_keluar;
            }

            $implode_nipd = implode(",", $nim_data);
        }

        $filter_mhs = "(trim(p.nipd),p.id_sms) in ($implode_nipd)";*/

$header = array(
  'NIM'=>'string',
  'Nama'=>'string',
  'Jenis Keluar'=>'string',
  'Tanggal Keluar'=>'string',
  'Semester Keluar' => 'string',
  'SK Yudisium'=>'string',
  'Tanggal SK Yudisium' => 'string',
  'IP Kumulatif'=>'string',
  'No Ijazah / No sertifikat profesi' => 'string',
  'Ket' => 'string',
  'Kode Prodi'=>'string',
  'Nama Jenis Keluar' => 'string',
);
$file_name = $_POST['random_number'];
//write temp file
$json_filename = SITE_ROOT."upload/temp/$file_name.json";

/*        print_r($temp_data);

        if ($temp_data['result']) {
            foreach ($temp_data['result'] as $krs) {

            }
        }*/


$data_dic_riwayat = [
      'act' => 'GetListMahasiswaLulusDO',
        'token' => $token,
        'filter' => $filter_mahasiswa,
        'order' => 'id_registrasi_mahasiswa desc',
        'limit' => $limit,
        'offset' => $offset
    ];
$temp_riwayat = json_decode($db->run($data_dic_riwayat,$url));
if(!empty($temp_riwayat->data)) {
  foreach ($temp_riwayat->data as $key) {
    $tgl_sk_yudisium = $key->tgl_sk_yudisium;
    if ($tgl_sk_yudisium!="") {
      $tgl_sk_yudisium = tgl_english($key->tgl_sk_yudisium);
    }
    if (strlen(trim($key->no_seri_ijazah))>5) {
        $no_seri = $key->no_seri_ijazah;
    } else {
        $no_seri = $key->no_sertifikat_profesi;
    }
      //dump($key);
      $data_rec = 
      array(
            convert_ascii($key->nim),
            convert_ascii($key->nama_mahasiswa),
            convert_ascii($key->id_jns_keluar),
            tgl_english(convert_ascii($key->tanggal_keluar)),
            convert_ascii($key->id_periode_keluar),
            convert_ascii($key->sk_yudisium),
            $tgl_sk_yudisium,
            convert_ascii(round($key->ipk,2)),
            convert_ascii($no_seri),
            $key->keterangan,
            $key->id_jur,
            $key->nama_jenis_keluar
      );
      $merge_key_val[] = array_combine(array_keys($header), $data_rec);
  }
}


$jumlah['data_rec'] = json_encode($data_rec);

$file_name = $_POST['random_number'];
//write temp file
$json_filename = SITE_ROOT."upload/temp/$file_name.json";

write_data($json_filename,$merge_key_val);


if ($_POST['last']=='yes') {
$data_file = JsonMachine::fromFile($json_filename);
foreach ($data_file as $key => $value) {
      $final[] = array_values($value);
      
}


$writer = new XLSXWriter();
$style =
        array (
                      array(
              'border' => array(
                'style' => 'thin',
                'color' => '000000'
                ),
            'allfilleddata' => true
            ),
            array(
                'fill' => array(
                    'color' => 'ff0000'
                    ),
                'cells' => array(
                    'A1',
                    'C1',
                    'D1',
                    'E1',
                    'K1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'B1',
                      'D1',
                      'F1',
                      'G1',
                      'H1',
                      'I1',
                      'J1',
                      'L1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

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



$filename = 'Data_Kelulusan.xlsx';

$writer->writeSheet($final,'Data Prestasi', $header, $style);
$writer->writeToFile(SITE_ROOT."upload/temp/".$_POST['random_number']."_".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename)));

ob_end_clean();



$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">".$_POST['total_data']." data berhasil di generate</font><br />";
        $msg .= "<a class='btn btn-block btn-primary btn-xs' href='".base_url()."upload/temp/".$_POST['random_number']."_".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename))."' target='_blank'><i class='fa  fa-cloud-download'></i> Download File</a></div>
        </div>";

        $jumlah['data_uri'] = SITE_ROOT."upload/temp/".$_POST['random_number']."_".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename));

        $jumlah['json_file'] = $json_filename; 

        $jumlah['last_notif'] = $msg;
}



array_push($json_response, $jumlah);

echo json_encode($json_response);
exit();
?>