<?php
session_start();
include "../../inc/config.php";
require_once "../../lib/Writer.php";
include "../../lib/vendor/autoload.php";
use \JsonMachine\JsonMachine;
$json_response = [];

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 = get_service_url();

$offset = $_POST["offset"];
$token = $_POST["token"];

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

$no = $offset;

if ($offset == 0) {
    $token = get_token();

    $jumlah["token"] = $token;
}

$periode = "";
$jenis_kelamin = "";
$jurusan = "";

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

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

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

$filter_dosen = "id_registrasi_dosen is not null $jurusan $periode $jenis_kelamin";

$filter_dosen = trim($filter_dosen);

$order_by = "nidn ASC";

if ($_POST["total_data"] <= 100) {
    $limit = $_POST["total_data"];
} else {
    $limit = 100;
}
    $header = [
        "Nama" => "string",
        "NIDN" => "string",
        "NUPTK" => "string",
        "NIP" => "string",
        "JK" => "string",
        "Agama" => "string",
        "Tanggal Lahir" => "string",
        "Status" => "string",
        "Program Studi" => "string",
        "ID Dosen NEO" => "string"
    ];
              $data_dic = array(
                'act' => 'GetListPenugasanDosen',
                'token' => $token,
                'filter' => "$filter_dosen",
                'order' => 'nidn,id_prodi asc',
                'limit' => $limit,
                'offset' => $offset
                //'fungsi' => 'InsertPeriodePerkuliahan'
              );
              $temp_data = json_decode($db->run($data_dic,$url));
              $rec_data = $temp_data;
              if (!empty($temp_data->data)) {
                foreach ($temp_data->data as $dos) {
                    $id_dosen[] = "'".$dos->id_dosen."'";
                }

                $implode_id = implode(',', $id_dosen);

                $data_dic_dos = array(
                'act' => 'GetListDosen',
                'token' => $token,
                'filter' => "id_dosen in($implode_id)"
              );
              $temp_data_dos = json_decode($db->run($data_dic_dos,$url));
              $data_dosen = array();
              foreach ($temp_data_dos->data as $dt_dos) {
                  $data_dosen[$dt_dos->id_dosen] = $dt_dos;
              }

                foreach ($rec_data->data as $dosen) {

                   $data_rec = array(
                      convert_ascii($dosen->nama_dosen),
                      convert_ascii($dosen->nidn),
                      convert_ascii($dosen->nuptk),
                      convert_ascii($data_dosen[$dosen->id_dosen]->nip),
                      convert_ascii($data_dosen[$dosen->id_dosen]->jenis_kelamin),
                      convert_ascii($data_dosen[$dosen->id_dosen]->nama_agama),
                      convert_ascii($data_dosen[$dosen->id_dosen]->tanggal_lahir),
                      convert_ascii($data_dosen[$dosen->id_dosen]->nama_status_aktif),
                      convert_ascii($dosen->nama_program_studi),
                      $dosen->id_dosen
                  );

                   $merge_key_val[] = array_combine(array_keys($header), $data_rec);

                }     

      
              }

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

if (!empty($merge_key_val)) {
    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 = [
        [
            "border" => [
                "style" => "thin",
                "color" => "000000",
            ],
            "allfilleddata" => true,
        ],
        [
            "fill" => [
                "color" => "00ff00",
            ],
            "cells" => ["A1", "B1", "D1", "H1", "C1", "E1", "F1","G1","H1","I1","J1"],
            "border" => [
                "style" => "thin",
                "color" => "000000",
            ],
            "verticalAlign" => "center",
            "horizontalAlign" => "center",
        ],
    ];

    //column width
    $col_width = [
        1 => 30,
        2 => 23,
        3 => 23,
        4 => 23,
        5 => 8,
        6 => 23,
        7 => 23,
        8 => 10,
        9 => 40,
        10 => 40
    ];

    $writer->setColWidth($col_width);



    $filename = "Data_Dosen.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->writeToFile('xlsx-simple.xlsx');*/
    $writer->writeSheet($final, "Data Dosen", $header, $style);
    $writer->writeToFile(SITE_ROOT."upload/temp/".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename)));

    $msg ="<div class=\"alert alert-warning \" role=\"alert\"><font color=\"#3c763d\">" .$_POST['total_data'] ." data Kelas berhasil di generate</font><br />";
      $msg .= "<a class='btn btn-block btn-primary btn-xs' href='".base_url()."upload/temp/".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/".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();
?>
