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


require_once '../../lib/Writer.php';
include "../../lib/vendor/autoload.php";
use \JsonMachine\JsonMachine;

$json_response = array();
$final = array();
$data_rec = array();
$merge_key_val = 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_rest = get_service_url('rest');

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

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

$filter_mahasiswa = $_POST['filter'];
$jumlah['filter'] = $filter_mahasiswa;

if ($offset==0) {
    $jumlah['token'] = get_token();
    $token = $jumlah['token'];



$filter_sms = "";
$filter_angkatan = "";
$filter_status = "";


$angkatan = "";

    $data_jurusan = $db->fetch_all('jurusan');
    $data_array_jur = array();
    foreach ($data_jurusan as $dt) {
        $data_array_jur[$dt->id_sms] = $dt->kode_jurusan;    
    }

      if ($_POST['jurusan']!="all") {
            $filter_sms = "and id_prodi='".$_POST['jurusan']."'";
        } else {
            if ($_SESSION['level']!=1) {
                $filter_sms = "and id_prodi='".$_SESSION['id_sms']."'";
            }
        }


        if ($_POST['status_mhs']!='') {
              $aktif = 0;
               foreach ($_POST['status_mhs'] as $val) {
                   if ($val!='AKTIF') {
                       $array_status[] = "'$val'";
                   } else {
                      $aktif=1;
                   }
               }
               $implode_status = implode(",", $array_status);
               if (!empty($array_status)) {
                 if ($aktif==1) {
                    $filter_status = "and (id_registrasi_mahasiswa in(select id_reg_pd from reg_pd where tgl_keluar is null) or id_jenis_keluar IN(".$implode_status.") )";
                 } else {
                    $filter_status = " and id_jenis_keluar IN(".$implode_status.")";
                 }
              } else {
                    $filter_status = " and id_registrasi_mahasiswa in(select id_reg_pd from reg_pd where tgl_keluar is null)";
              }
        }

        $filter_angkatan = "and (left(id_periode_masuk,4) BETWEEN '".$_POST['angkatan_awal']."' and '".$_POST['angkatan_akhir']."')";


        $data_rec = array();
        $filter_mahasiswa = "id_registrasi_mahasiswa in(select id_reg_pd from nilai_transkrip) $filter_sms $filter_angkatan $filter_status";

        if ($_POST['nim']!='all') {
            $nim_label = $_POST['value_nim'];
            $filter_mahasiswa  = "nim like '%".$_POST['value_nim']."%' $filter_sms";
        } else {
            $filter_mahasiswa = $filter_mahasiswa;
        }

        $jumlah['filter'] = $filter_mahasiswa;
}

$order_by = "nim ASC";

$limit = 31;
$temp_rec = array();
$data_dic_riwayat = [
      'act' => 'GetListRiwayatPendidikanMahasiswa',
        'token' => $token,
        'filter' => $filter_mahasiswa,
        'order' => $order_by,
        'limit' => $limit,
        'offset' => $offset
    ];
$temp_riwayat = json_decode($db->run($data_dic_riwayat,$url_rest));
if(!empty($temp_riwayat->data)) {
  foreach ($temp_riwayat->data as $keys) {
      $key = $db->convert_obj_to_array($keys);
      //dump($key);
      $data_reg_pd[$key['id_registrasi_mahasiswa']] = array(
      'nipd' => $key['nim'],
      'id_jalur_masuk' => $key['id_jalur_daftar'],
      'nm_pd' => $key['nama_mahasiswa'],
      'id_jns_daftar' => $key['id_jenis_daftar'],
      'tgl_masuk_sp' => $key['tanggal_daftar'],
      'mulai_smt' => $key['id_periode_masuk'],
      'id_pembiayaan' => $key['id_pembiayaan'],
      'id_sms' => $key['id_prodi'],
      'id_mahasiswa' => $key['id_mahasiswa'],
      'keterangan_keluar' => $key['keterangan_keluar']
    );

    $id_mahasiswa[] = "'".$key['id_mahasiswa']."'";
    $id_reg_pd[] = "'".$key['id_registrasi_mahasiswa']."'";
  }
}


$id_reg_pd_concat_where = implode(",", $id_reg_pd);

            $data_req_transkrip = [
              'act' => 'GetTranskripMahasiswa',
                'token' => $token,
                'filter' => "id_registrasi_mahasiswa in($id_reg_pd_concat_where) and nilai_huruf  like '%".$_POST['value_nilai']."%' ",
                'order' => "id_registrasi_mahasiswa asc",
                'limit' => "",
                'offset' => ""

            ];
            
            $check_transkrip_exist = json_decode($db->run($data_req_transkrip,$url_rest));
 $jumlah_data = 0;
  $jml = 0;
  $jml_mhs = array();
  $jumlah_mhs = 0;

//  print_r($check_transkrip_exist);
if (!empty($check_transkrip_exist->data)) {
  foreach ($check_transkrip_exist->data as $key) {
    $jml_mhs[$key->id_registrasi_mahasiswa] = $key->id_registrasi_mahasiswa;
    $data_hapus_transkrip = [
        'act' => 'DeleteTranskripMahasiswa',
        'token' => $token,
        'key' => array(
            "id_registrasi_mahasiswa" => $key->id_registrasi_mahasiswa,
            "id_matkul" => $key->id_matkul
        )

    ];
    $hapus_data_transkrip = json_decode($db->run($data_hapus_transkrip,$url_rest));
   $jml++;
  }
}
$jumlah_data =  $_POST['jumlah_data'] + $jml;
$jumlah_mhs = $_POST['jumlah_mhs'] + count($jml_mhs);
$jumlah['jumlah_data'] = $jumlah_data;
$jumlah['jumlah_mhs'] = $jumlah_mhs;

if ($_POST['last']=='yes') {
    $final = $jumlah_data;

  if ($final<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();
  } else {
    $msg =  "<div class=\"alert alert-warning \" role=\"alert\">
            <font color=\"#3c763d\">".$jumlah_mhs." data Mahasiswa dan</font><br />
            <font color=\"#3c763d\">".$final." data Transkrip bernilai huruf ".$_POST['value_nilai']." berhasil di Uncheck/dihilangkan</font><br />";
            $msg .= "</div></div>";

            $jumlah['data_uri'] = '';

            $jumlah['last_notif'] = $msg;
            //unlink($json_filename);
    }

}
array_push($json_response, $jumlah);

echo json_encode($json_response);
exit();

?>