<?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;
}


$jurusan = "";

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


$filter_dosen = "id_substansi is not null $jurusan";

$filter_dosen = trim($filter_dosen);


if ($_POST["total_data"] <= 100) {
    $limit = $_POST["total_data"];
} else {
    $limit = 100;
}

        $jurusan = $db->fetch_custom("select id_sms,nama_jurusan,kode_jurusan from jurusan");
        foreach ($jurusan as $jur) {
          $data_array_jur[$jur->id_sms] = $jur->kode_jurusan;

        }
$header = array(
  'Nama Substansi'=>'string',
  'SKS Matakuliah'=>'string',
  'SKS Tatap Muka'=>'string',
  'SKS Praktek' => 'string',
  'SKS Praktek Lapangan' =>'string',
  'SKS Simulai' =>'string',
  'Kode Prodi' =>'string',
  'Nama Program Studi' => 'string',
  'ID Substansi Kuliah' =>'string'
);
              $data_dic = array(
                'act' => 'GetListSubstansiKuliah',
                'token' => $token,
                'filter' => "$filter_dosen",
                'order' => 'nama_substansi asc',
                'limit' => $limit,
                'offset' => $offset
                //'fungsi' => 'InsertPeriodePerkuliahan'
              );
              $temp_data = json_decode($db->run($data_dic,$url));
              if (!empty($temp_data->data)) {
                foreach ($temp_data->data as $dosen) {

                   $data_rec = array(
                      convert_ascii($dosen->nama_substansi),
                      convert_ascii($dosen->sks_mata_kuliah),
                      convert_ascii($dosen->sks_praktek),
                      convert_ascii($dosen->sks_praktek_lapangan),
                      convert_ascii($dosen->sks_simulasi),
                      convert_ascii($dosen->sks_simulasi),
                      $data_array_jur[$dosen->id_prodi],
                      convert_ascii($dosen->nama_program_studi),
                      $dosen->id_substansi
                  );

                   $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 =
        array (
                      array(
              'border' => array(
                'style' => 'thin',
                'color' => '000000'
                ),
            'allfilleddata' => true
            ),
                     array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                    'A1',
                    'B1',
                    'C1',
                    'D1',
                    'E1',
                    'F1',
                    'G1',
                    'H1',
                    'I1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                ),
            );


//column width
$col_width = array(
  1 => 40,
  2 => 20,
  3 => 20,
  4 => 20,
  5 => 20,
  6 => 20,
  7 => 20,
  8 => 30,
  9 => 40
  );
$writer->setColWidth($col_width);



    $writer->setColWidth($col_width);



    $filename = "Data_Substansi_Kuliah.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 Substansi", $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();
?>
