<?php
include "../../inc/config.php";
require_once '../../lib/Writer.php';
include "../../lib/vendor/autoload.php";
use \JsonMachine\JsonMachine;
$json_response = 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();
}
$filter_semester = "";
$filter_sms = "";
$filter_jenis = '';
$filter_mbkm = '';
$merge_key_val = array();
    $url = 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) {

        $jumlah['token'] = get_token();
        $token = get_token();
    }
        if ($_POST['semester']!='all') {
          $filter_semester = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_smt='".$_POST['semester']."')";
          
       }

       if ($_POST['jurusan']!='all') {
          $filter_sms = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_sms='".$_POST['jurusan']."')";
      }

        if ($_POST['jenis']!='all') {
          $filter_jenis = "and id_aktivitas in (select id_akt_mhs from akt_mhs where id_jns_akt_mhs='".$_POST['jenis']."')";
       }

       $filter_mbkm = "id_konversi_aktivitas in(select id_konversi_akt_mhs from konversi_akt_mhs where soft_delete='0') $filter_semester $filter_sms $filter_jenis";




        $order_by = "kode_mata_kuliah ASC";
        if ($_POST['total_data']<=35) {
            $limit = $_POST['total_data'];
        } else {
            $limit = 35;
        }

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

        }

      $data_param = [
        'act' => 'GetListKonversiKampusMerdeka',
          'token' => $token,
          'filter' => $filter_mbkm,
          'order' => "",
          'limit' => $limit,
          'offset' => $offset

      ];

      $anggota_data = service_request($data_param);
    /*  dump($data_param);
      dump($anggota_data);*/

      $id_akt_anggota = $anggota_data;
      $id_matkul = array();
      if (!empty($id_akt_anggota->data)) {
        foreach ($id_akt_anggota->data as $dt) {
          $id_aktivitas[$dt->id_aktivitas] = "'".$dt->id_aktivitas."'";
          $id_matkul[] = "'".$dt->id_matkul."'";
        }

        $akt_concat_where = implode(",", $id_aktivitas);
        $id_mk_where = implode(",", $id_matkul);
        $temp_data_akt = [
          'act' => 'GetListAktivitasMahasiswa',
            'token' => $token,
            'filter' => "id_aktivitas in($akt_concat_where)",
            'order' => "",
            'limit' => "",
            'offset' => ""

        ];
        $aktivitas_data = service_request($temp_data_akt);



        foreach ($aktivitas_data->data as $akt_data) {
          $data_aktivitas[$akt_data->id_aktivitas] = $akt_data;
        }
        $temp_data_mk = [
          'act' => 'GetListMataKuliah',
            'token' => $token,
            'filter' => "id_matkul in($id_mk_where)",
            'order' => "",
            'limit' => "",
            'offset' => ""

        ];
        $aktivitas_data_mk = service_request($temp_data_mk);

        

        foreach ($aktivitas_data_mk->data as $akt_data_mk) {
          $data_aktivitas_mk[$akt_data_mk->id_matkul] = array(
            'kode_mk' => $akt_data_mk->kode_mata_kuliah,
            'nama_mk' => $akt_data_mk->nama_mata_kuliah
          );
        }
      }



 
$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',
                    'H1',
                    'I1',
                    'J1',
                    'K1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                      'D1',
                      'F1',
                      'G1',
                      'L1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

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

$header = array(
  'Semester'=>'string',
  'ID AKTIVITAS'=>'string',
  'NIM'=>'string',
  'Nama Mahasiswa'=>'string',
  'Kode MK'=>'string',
  'Nama MK'=>'string',
  'SKS' => 'string',
  'Nilai Huruf'=>'string',
  'Nilai Indeks' => 'string',
  'Nilai Angka' => 'string',
  'Kode Prodi'=>'string',
  'ID Aktivitas NEO '=>'string'
);

if (!empty($anggota_data->data)) {
  foreach ($anggota_data->data as $key) {
        if (in_array($key->id_aktivitas, array_keys($data_aktivitas))) {
              if (in_array($data_aktivitas[$key->id_aktivitas]->id_prodi, array_keys($data_array_jur))) { 
                $kode_jur = $data_array_jur[$data_aktivitas[$key->id_aktivitas]->id_prodi]['kode_jurusan'];
              } else {
                $kode_jur = '';
              }
              $data_rec = array(
                  convert_ascii($data_aktivitas[$key->id_aktivitas]->id_semester),
                  convert_ascii($data_aktivitas[$key->id_aktivitas]->keterangan),
                  convert_ascii($key->nim),
                  convert_ascii($key->nama_mahasiswa),
                  convert_ascii($data_aktivitas_mk[$key->id_matkul]['kode_mk']),
                  convert_ascii($data_aktivitas_mk[$key->id_matkul]['nama_mk']),
                  convert_ascii($key->sks_mata_kuliah),
                  convert_ascii($key->nilai_huruf),
                  convert_ascii($key->nilai_indeks),
                  convert_ascii($key->nilai_angka),
                  $kode_jur,
                  $key->id_aktivitas
              );
              $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') {
$final = array();
$jml_akhir = 0;
if (is_file($json_filename)) {
  $data_file = JsonMachine::fromFile($json_filename);
  
  foreach ($data_file as $key => $value) {
        $final[] = array_values($value);
        $jml_akhir++;
  }

}



$filename = 'Nilai_MBKM.xlsx';
$writer->writeSheet($final,'Data Nilai', $header, $style);
$writer->writeToFile(SITE_ROOT."upload/temp/".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename)));
//$writer->writeToStdOut();
//exit(0);
$modul_name = explode("/", $_SERVER['REQUEST_URI']);

$total_length = count($modul_name);
$index_modul = $total_length-2;

$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">".$jml_akhir."  Nilai MBKM berhasil di generate</font><br />";
        $msg .= "<a class='btn btn-block btn-primary btn-xs' href='".base_url()."upload/temp/".XLSXWriter::sanitize_filename($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($filename);

        $jumlah['json_file'] = $json_filename;

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



array_push($json_response, $jumlah);

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