<?php
session_start();
include "../../inc/config.php";
$json_response = array();
$jumlah = array();
if ($_POST['total_data']>0) {
	$error_kelas = "";
	$url_rest = get_service_url('rest');
    $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 = "";
    $array_aktivitas = array();
    
    $query_jenis = '';
    $query_jurusan = '';
    $all_id = array();
    $array_substansi = array();
    $data_kls = array();
    $data_ids = array();


		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 substansi_kuliah.*,id_sms,substansi_kuliah.id as id_krs from substansi_kuliah
 inner join jurusan on substansi_kuliah.kode_jurusan=jurusan.kode_jurusan where substansi_kuliah.id is not null $query_jurusan and status_error!=1 $query_not_in limit 3");
	

		foreach ($data as $dt) {
			$id_substansi = "";
			$data_req_substansi = [
				'act' => 'GetListSubstansiKuliah',
			    'token' => get_token(),
		    	'filter' => "nama_substansi='".$dt->nama_substansi."'",
			    'order' => "",
			    'limit' => "1",
			    'offset' => ""
			];
				$check_substansi_exist = json_decode($db->run($data_req_substansi,$url_rest));
				if (!empty($check_substansi_exist->data)) {
					$id_substansi = $check_substansi_exist->data[0]->id_substansi;
				}
				if ($_POST['method']=='up') {
				if ($id_substansi!="") {
					$array_key = array(
						'id_substansi' => $id_substansi
					);
					$array_substansi_kuliah_update = array(
		              'id_prodi' => $dt->id_sms,
		              'nama_substansi' => $dt->nama_substansi,
		              'sks_tatap_muka' => $dt->sks_tatap_muka,
		              'sks_praktek' => $dt->sks_praktek,
		              'sks_praktek_lapangan' => $dt->sks_praktek_lapangan,
		              'sks_simulasi' => $dt->sks_simulasi,
		            );
					$param_update = array(
						'act' => 'UpdateSubstansiKuliah',
						'key' => $array_key,
						'token' => get_token(),
						'record' => $array_substansi_kuliah_update
					);
					$update_substansi = service_request($param_update);
					$value_data = addslashes($update_substansi->error_desc);
					if ($value_data!='') {
						$error_update_data[] = array(
							'keterangan' => $value_data,
							'status_error' => 2
						);
						$data_id_error[] =  $dt->id;
					} else {
						$update_data[] = array(
							'keterangan' => '',
							'status_error' => 1
						);
						$data_id_sukses[] =  $dt->id;
					}
				} else {
					$error_update_data[] = array(
						'keterangan' => 'Nama Substansi Kuliah belum ada di Feeder, Silakan Pilih Metode Insert',
						'status_error' => 2
					);
					$data_id_error[] =  $dt->id;
				}
			} else {
				if ($id_substansi!="") {
					$error_update_data[] = array(
						'keterangan' => 'Nama Substansi Kuliah ini sudah ada di Feeder',
						'status_error' => 2
					);
					$data_id_error[] =  $dt->id;
				} else {
					$array_substansi = array(
		              'id_prodi' => $dt->id_sms,
		              'nama_substansi' => $dt->nama_substansi,
		              'sks_tatap_muka' => $dt->sks_tatap_muka,
		              'sks_praktek' => $dt->sks_praktek,
		              'sks_praktek_lapangan' => $dt->sks_praktek_lapangan,
		              'sks_simulasi' => $dt->sks_simulasi,
		            );
					$param = array(
						'act' => 'InsertSubstansiKuliah',
						'token' => get_token(),
						'record' => $array_substansi
					);
					/*dump($array_substansi);
					dump($param);*/
					$insert_substansi = service_request($param);
					$value_data = addslashes($insert_substansi->error_desc);
					if ($value_data!='') {
						$error_update_data[] = array(
							'keterangan' => $value_data,
							'status_error' => 2
						);
						$data_id_error[] =  $dt->id;
					} else {
						$update_data[] = array(
							'keterangan' => '',
							'status_error' => 1
						);
						$data_id_sukses[] =  $dt->id;
					}
				}
				
			}
			
			$id_substansi = "";
			$all_id[] = $dt->id_krs;

	}


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

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

		$db->fetch_custom("update substansi_kuliah 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 substansi_kuliah where unik_id=".$_POST['random_number']);

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

		$db->fetch_custom("update aktivitas_anggota set unik_id=0 where  unik_id=".$_POST['random_number']);
		
		$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses." data Skala Nilai 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