<?php
session_start();
include "../../inc/config.php";
$json_response = array();
$jumlah = array();
if ($_POST['total_data']>0) {
		$url_rest = get_service_url('rest');
		$offset = $_POST['offset'];
		$jumlah['offset'] = $offset;
		$token = $_POST['token'];
		$jumlah['token'] = $_POST['token'];

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

		$error_kelas = "";
		$sukses_count = 0;
		$error_count = 0;
		$error_count_in = 0;
		$error_msg = array();
		$update_data = array();
		$update_data_status = array();
	
		$update_data_er =array();
		$update_data_er_status =array();
		$error_mk = "";

		$data_id_sukses = array();
		$data_id_error= array();
	
		$all_errors = array();
		$all_error = array();
		$query_not_in = "";
		$id_mk = "";
		$error_mk = "";
		$msg = "";
		$query_matkul = '';
		$query_kelas = '';
		$query_jurusan = '';
		$all_id = array();
		$data_rencana = array();
		$data_kls = array();
		$filter_mk_where = array();

		$array_id_mk = array();
		$array_id_mk_cross = array();
		$data_daya_tampung = array();
		$error_data = "";
		$error_update_data = array();
		$data_id_error = array();
		$array_exist_evaluasi = array();
		$kode_mk = array();

		//merdeka
		$data_rencana = array();


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

		if ($_POST['jurusan']!='all') {
			$query_jurusan = "and trim(jurusan.kode_jurusan)='".trim($_POST['jurusan'])."'";
		} else {
			if ($_SESSION['level']!=1) {
				$query_jurusan = "and trim(jurusan.kode_jurusan)='".$_SESSION['jurusan']."'";
			} else {
				$query_jurusan = "";
			}
		}

 		$query_not_in = "and unik_id not in(".$_POST['random_number'].")";

$data = $db->fetch_custom("select daya_tampung.*,id_sms,daya_tampung.id from daya_tampung
 inner join jurusan on daya_tampung.kode_jurusan=jurusan.kode_jurusan where daya_tampung.id is not null $sem $query_jurusan and status_error!=1 $query_not_in limit 8");
		foreach ($data as $value) {
				if ($_POST['metode']=='insert') {
					$temp_data_insert = array(
						'id_prodi' => $value->id_sms,
						'id_jalur_daftar' => $value->id_jalur_masuk,
						'id_semester' => $value->semester,
						'jumlah_target_mahasiswa_baru' => $value->target_mhs_baru,
						'jumlah_pendaftar_ikut_seleksi' => $value->calon_ikut_seleksi,
						'jumlah_pendaftar_lulus_seleksi' => $value->calon_lulus_seleksi,
						'jumlah_daftar_ulang' => $value->daftar_sbg_mhs,
						'jumlah_mengundurkan_diri' => $value->pst_undur_diri
					);
					//dump($temp_data_insert);
				  	$data_dic = [
				      'act' => 'InsertPeriodeJalurPendaftarPerkuliahan',
				        'token' => $token,
				        'record' => $temp_data_insert,
				    ];
				    $temp_recs = service_request($data_dic);
				  /*  dump($data_dic);
				    dump($temp_recs);*/

					$value_data = addslashes($temp_recs->error_desc);
					if ($value_data!='') {
						$error_update_data[] = array(
							'keterangan' => $value_data,
							'status_error' => 2
						);
						$data_id_error[] = $value->id;
					} else {
						$update_data[] = array(
							'keterangan' => '',
							'status_error' => 1
						);
						$data_id_sukses[] = $value->id;
					}
				} else {
					$record_update = array(
						'jumlah_target_mahasiswa_baru' => $value->target_mhs_baru,
						'jumlah_pendaftar_ikut_seleksi' => $value->calon_ikut_seleksi,
						'jumlah_pendaftar_lulus_seleksi' => $value->calon_lulus_seleksi,
						'jumlah_daftar_ulang' => $value->daftar_sbg_mhs,
						'jumlah_mengundurkan_diri' => $value->pst_undur_diri
					);
					$key_update['id_semester'] = $value->semester;
					$key_update['id_prodi'] = $value->id_sms;
					$key_update['id_jalur_daftar'] = $value->id_jalur_masuk;
					$data_dic = [
				      'act' => 'UpdatePeriodeJalurPendaftarPerkuliahan',
				      'key' => $key_update,
				        'token' => $token,
				        'record' => $record_update,
				    ];
				$update_evaluasi = service_request($data_dic);
				$value_data = addslashes($update_evaluasi->error_desc);
				if ($value_data!='') {
					$error_update_data[] = array(
						'keterangan' => $value_data,
						'status_error' => 2
					);
					$data_id_error[] = $value->id;
				} else {
					$update_data[] = array(
						'keterangan' => '',
						'status_error' => 1
					);
					$data_id_sukses[] = $value->id;
				}
				}
			$all_id[] = $value->id;
		}

	/*	dump($error_update_data);
		exit();*/

		//update error
		if (!empty($error_update_data)) {
			updateMulti('daya_tampung',$error_update_data,'id',$data_id_error);
		}
		//update sukses
		if (!empty($update_data)) {
			updateMulti('daya_tampung',$update_data,'id',$data_id_sukses);
		}


		$update_data = array();
		$data_id_sukses = array();

		$implode_id = implode(",", $all_id);


		$db->fetch_custom("update daya_tampung set unik_id=".$_POST['random_number']." where id in (".$implode_id.")");
	//$jumlah['not_in'] = $query_not_in;
	$jumlah['all_id'] = $all_id;
	$jumlah['error_msg'] = $error_msg;
	$jumlah['token'] = $token;

	if (isset($_POST['id_data'])) {

		$jml_error_sukses = $db->fetch_custom_single("SELECT COUNT(IF(status_error='1',1,NULL)) AS jml_sukses,COUNT(IF(status_error='2',1,NULL)) AS jml_error FROM daya_tampung where unik_id=".$_POST['random_number']);

		$jumlah_sukses = $jml_error_sukses->jml_sukses;
		$jumlah_error = $jml_error_sukses->jml_error;

		$db->fetch_custom("update daya_tampung set unik_id=0 where unik_id=".$_POST['random_number']);

	
		$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses." data daya tampung berhasil Di Import Ke Feeder DIKTI</font><br />
		<font color=\"#ce4844\" >".$jumlah_error." data tidak bisa Di Import </font>";
		
		if (!$jumlah_error==0) {
			$msg .= "<a data-toggle=\"collapse\" href=\"#collapseExample\" aria-expanded=\"false\" aria-controls=\"collapseExample\">Detail error</a>";
			//pesan error 
			$all_errors = array('Silakan lihat detail error di list data');
			
		}
		//echo "<br />Total: ".$i." baris data";
		$msg .= "<div class=\"collapse\" id=\"collapseExample\">";
				$i=1;
				foreach ($all_errors as $pesan) {
						$msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
					$i++;
					}
		$msg .= "</div>
		</div>";


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


} else {
	$msg =  "Tidak ada data yang bisa diproses<p>";
		$jumlah['last_notif'] = $msg;
}

array_push($json_response, $jumlah);
echo json_encode($json_response);
//echo convert(memory_get_usage(true)); // 123 kb