<?php
session_start();
header('Access-Control-Allow-Origin: *');
include "../../inc/config.php";



$url = get_service_url('url_rest');
$token = get_token();

$json_response = array();

$filter_sms = "";
$filter_angkatan = "";
$filter_status = "";
$jumlah = 0;
$array_status = array();
      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 id_periode_masuk >= '".$_POST['angkatan_awal']."1' and id_periode_masuk<='".$_POST['angkatan_akhir']."2'";


        $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;
        }
        $temp_count_mk = [
              'act' => 'GetCountRiwayatPendidikanMahasiswa',
                'token' => $token,
                'filter' => $filter_mahasiswa
            ];
        $temp_count = json_decode($db->run($temp_count_mk,$url));

        if(!empty($temp_count->data)) {
          $jumlah = $temp_count->data;
        }

        
        if ($jumlah>0) {
           $json_response['jumlah'] = $jumlah;
        } else {
          $json_response['jumlah'] = 0;
        }

        echo json_encode($json_response);