<?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();
		$id_kelas_feeder = array();
	
		$update_data_er =array();
		$update_data_er_status =array();
		$error_mk = "";

		$data_id_sukses = array();
		$data_id_error= array();
		$check_akt_kelas = array();
		$check_akt_kelas_merdeka = 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_kelas = array();
		$data_kls = array();
		$data_ids = array();
		$filter_mk_where = array();
		$filter_mk_where_cross = array();
		$filter_mk_kur = array();

		$array_id_mk = array();
		$array_id_mk_cross = array();
		$data_kelas_kuliah = array();
		$error_data = "";
		$error_update_data = array();
		$data_id_error = array();

		//merdeka
		$data_kelas_merdeka = array();
		$data_ids_merdeka = array();
		$check_ids = array();
		$data_kelas_id_exist = array();




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

		$data = $db->fetch_custom("select kelas_kuliah_update.id as id_kelas,kelas_kuliah_update.* from kelas_kuliah_update where kelas_kuliah_update.id is not null and status_error!=1 $query_not_in limit 10");


		foreach ($data as $dt_id) {
			$id_kelas_where[$dt_id->id_neo_feeder] = "('".$dt_id->id_neo_feeder."')";
			$data_kelas_kuliah[] = $dt_id;
		}

		if (!empty($data_kelas_kuliah)) {


			$matkul_id_kelas = array();
			$implode_id_kelas = implode(",", $id_kelas_where);
			$kelas_filter = "(id_kelas_kuliah) IN ($implode_id_kelas)";

			$data_req_kelas = [
				'act' => 'GetDetailKelasKuliah',
			    'token' => $token,
			    'filter' => $kelas_filter,
			    'order' => "",
			    'limit' => "",
			    'offset' => ""

			];
			$check_kelas_exist = json_decode($db->run($data_req_kelas,$url_rest));
			if (!empty($check_kelas_exist->data)) {
				foreach ($check_kelas_exist->data as $dt_id_kelas) {
				$id_kelas_feeder[$dt_id_kelas->id_kelas_kuliah] = $dt_id_kelas;
				}
			}


			foreach ($data_kelas_kuliah as $dt) {
				if (strlen($dt->nama_kelas)>5) {
						$error_update_data[] = array(
							'keterangan' => 'Nama Kelas tidak boleh lebih dari 5 Karakter',
							'status_error' => 2
						);
						$data_id_error[] = $dt->id_kelas;
				}
				if (in_array($dt->id_neo_feeder, array_keys($id_kelas_feeder))) {
						$data_kelas_update[] = array(
							'id_prodi' => $id_kelas_feeder[$dt->id_neo_feeder]->id_prodi,
							'id_semester' => $id_kelas_feeder[$dt->id_neo_feeder]->id_semester,
							'id_matkul' => $id_kelas_feeder[$dt->id_neo_feeder]->id_matkul,
							'nama_kelas_kuliah' => $dt->nama_kelas
							
						);
						$id_kls_neo_update[] = $dt->id_neo_feeder;
						$data_ids[] = $dt->id_kelas;

				} else {
					$error_update_data[] = array(
						'keterangan' => "Tidak bisa update karena kelas belum ada di NEO",
						'status_error' => 2
					);
					$data_id_error[] = $dt->id_kelas;
				}

					$all_id[] = $dt->id_kelas;

		}
	}

		//update error
		if (!empty($error_update_data)) {
			updateMulti('kelas_kuliah_update',$error_update_data,'id',$data_id_error);
		}
		$error_update_data = array();
		$data_id_error = array();


		if (!empty($data_kelas_update)) {	
			foreach ($data_kelas_update as $key => $kls) {

						  $key_update['id_kelas_kuliah'] = $id_kls_neo_update[$key];
						  $data_dic = [
						      'act' => 'UpdateKelasKuliah',
						      'key' => $key_update,
						        'token' => $token,
						        'record' => $kls,
						    ];

						   // exit();
							$temp_recs = json_decode($db->run($data_dic,$url_rest));


							
							//print_r($temp_recs);
									$value_data = addslashes($temp_recs->error_desc);
									$data_key = $data_ids[$key];

									if ($value_data!='') {
										$error_update_data[] = array(
											'keterangan' => $value_data,
											'status_error' => 2
										);
										$data_id_error[] = $data_key;
									} else {
										$update_data[] = array(
											'keterangan' => '',
											'status_error' => 1
										);
										$data_id_sukses[] = $data_key;
									}	
				}
			
				
		}

		//update error
		if (!empty($error_update_data)) {
			updateMulti('kelas_kuliah_update',$error_update_data,'id',$data_id_error);
		}

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


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

		$implode_id = implode(",", $all_id);
		
		$jumlah['offset'] = $implode_id;

		$db->fetch_custom("update kelas_kuliah_update set unik_id=".$_POST['random_number']." where id in (".$implode_id.")");

	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 kelas_kuliah_update where unik_id=".$_POST['random_number']);

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

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

	
		$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses." data Kelas berhasil Di Update 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