<?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;
		}
        $sukses_count = 0;

        $error_count = 0;
        $error_count_in = 0;

        $error_msg = array();

        $filter_kur_where = array();
        $filter_kur_where_id = array();
        $filter_mk_where = array();

        $update_data = array();
        $update_data_kur = array();
        $update_data_status = array();

        $data_mk_kur = array();
        $data_mk = array();
        $data_mk_detail = array();
        $data_mk_up = array();
        $data_id_kur = array();

        $update_data_er =array();
        $update_data_er_status =array();
        $error_mk = "";
        $array_id_anggota = array();
        $array_exist_konversi = array();
        $array_id_mk = array();
        $array_id_mk_cross = array();
        $data_id_sukses = array();
        $data_id_sukses_kur = array();
        $data_id_error= array();
        $data_id_error_kur= array();
        $data_id_kurikulum = array();

        $all_errors = array();
        $all_error = array();
        $query_not_in = "";
        $id_mk = "";
        $error_mk = "";

        $msg = "";
        $array_konversi = array();
        $filter_mk_where_cross = array();
        $array_id_mk_cross = array();
        
        $query_jenis = '';
        $query_jurusan = '';
        $all_id = array();
        $id_konversi = array();

        $sem = "";
        $data_ids = array();
        $data_ids_up = array();
        $data_id_mk = array();

        $data_akt = array();
        $aktivitas_id = array();
        $data_id_aktivitas = array();
        $nim_data = array();
        $check_konversi_exist = array();

        $jurusan = "";
        $query_matkul = "";
        $error_update_data = array();
        $error_update_data_kur = array();
        $data_update_kur = array();
        $data_update_kur_id = array();
        $array_update_mats = array();
        
	
    	  $semester = "";
		  if ($_POST['semester']!='all') {
		    $semester = "and semester='".$_POST['semester']."'";
		  }

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

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

		$data = $db->fetch_custom("select *,mat_kurikulum.id as id_krs from mat_kurikulum inner join jurusan on mat_kurikulum.kode_jurusan=jurusan.kode_jurusan where (status_error!=1 or status_error_kur!=1) $jurusan $kurikulum $semester $query_not_in limit 11");



		foreach ($data as $dt_id) {
			$data_loop[] = $dt_id;
			$filter_kur_where[$dt_id->id_sms.convert_ascii($dt_id->tahun)] = "('".$dt_id->id_sms."','".convert_ascii($dt_id->tahun)."')";
			$filter_mk_where[$dt_id->id_sms.convert_ascii($dt_id->kode_mk)] = "('".$dt_id->id_sms."','".convert_ascii($dt_id->kode_mk)."')";
			$filter_mk_where_cross[convert_ascii($dt_id->kode_mk)] = "('".convert_ascii($dt_id->kode_mk)."')";
			$filter_kur_where_id[$dt_id->id_matkul] = "('".$dt_id->id_matkul."','".convert_ascii($dt_id->tahun)."')";
		}

		if (!empty($filter_kur_where)) {
			$kur_concat_where = implode(",", $filter_kur_where);
			$data_req_kur = [
				'act' => 'GetListKurikulum',
			    'token' => $token,
			    'filter' => "(id_prodi,id_semester) IN ($kur_concat_where)",
			    'order' => "",
			    'limit' => "",
			    'offset' => ""

			];
			$check_kur_exist_json = json_decode($db->run($data_req_kur,$url_rest));
			if (!empty($check_kur_exist_json->data)) {
				foreach ($check_kur_exist_json->data as $kur_data) {
					$data_id_kurikulum[convert_ascii($kur_data->id_prodi).convert_ascii($kur_data->id_semester)] = $kur_data->id_kurikulum;
				}
			}
		}

		if ($_POST['method']!='up_id') {
			if (!empty($filter_mk_where)) {
				$mk_concat_where = implode(",", $filter_mk_where);
				$data_req_mk = [
					'act' => 'GetListMataKuliah',
				    'token' => $token,
				    'filter' => "(id_prodi,kode_mata_kuliah) IN ($mk_concat_where)",
				    'order' => "",
				    'limit' => "",
				    'offset' => ""

				];
				$check_mk_exist_json = json_decode($db->run($data_req_mk,$url_rest));
				if (!empty($check_mk_exist_json->data)) {
					foreach ($check_mk_exist_json->data as $mk_data) {
						$data_id_mk[convert_ascii($mk_data->id_prodi).convert_ascii($mk_data->kode_mata_kuliah)] = $mk_data->id_matkul;
					}
				}
			}
			if (!empty($filter_mk_where_cross)) {
				$mk_concat_where_cross = implode(",", $filter_mk_where_cross);
				$data_req_mk = [
					'act' => 'GetListMataKuliah',
				    'token' => $token,
				    'filter' => "(kode_mata_kuliah) IN ($mk_concat_where_cross)",
				    'order' => "",
				    'limit' => "",
				    'offset' => ""

				];
				$check_mk_exist_cross_json = json_decode($db->run($data_req_mk,$url_rest));
				if (!empty($check_mk_exist_cross_json->data)) {
					foreach ($check_mk_exist_cross_json->data as $mk_data_cross) {
						$array_id_mk_cross[convert_ascii($mk_data_cross->kode_mata_kuliah)] = $mk_data_cross->id_matkul;
					}
				}
			}
		}


		foreach ($data_loop as $value) {
			$jmlsks_mk=$value->sks_tm+$value->sks_prak+$value->sks_prak_lap+$value->sks_sim;
			if ($_POST['method']=='in') {
				if (!in_array($value->id_sms.convert_ascii($value->kode_mk), array_keys($data_id_mk)) && !in_array(convert_ascii($value->kode_mk), array_keys($array_id_mk_cross))) {
				$data_mk[] = array(
				    'id_sms' => $value->id_sms,
				    'kode_mk' => $value->kode_mk,
				    'nm_mk' => $value->nama_mk,
				    'jns_mk' => $value->jns_mk,
				    'metode_pelaksanaan_kuliah' => $value->metode_pelaksanaan_kuliah,
				    'sks_mk' => $jmlsks_mk,
				    'sks_tm' => $value->sks_tm,
				    'sks_prak' => $value->sks_prak,
				    'sks_prak_lap' => $value->sks_prak_lap,
				    'sks_sim' => $value->sks_sim,
				    'kelompok_mk' => $value->kelompok_mk,
				    'tgl_mulai_efektif' => $value->tgl_mulai_efektif,
				    'tgl_akhir_efektif' => $value->tgl_akhir_efektif,
					'tahun' => $value->tahun,
					'smt' => $value->semester,
					'a_wajib' => $value->wajib,
				);
				$data_mk_detail[] = array(
					'tahun' => $value->tahun,
					'smt' => $value->semester,
					'a_wajib' => $value->wajib,
				    'id_sms' => $value->id_sms,
				    'kode_mk' => $value->kode_mk,
				    'nm_mk' => $value->nama_mk,
				    'jns_mk' => $value->jns_mk,
				    'metode_pelaksanaan_kuliah' => $value->metode_pelaksanaan_kuliah,
				    'sks_mk' => $jmlsks_mk,
				    'sks_tm' => $value->sks_tm,
				    'sks_prak' => $value->sks_prak,
				    'sks_prak_lap' => $value->sks_prak_lap,
				    'kelompok_mk' => $value->kelompok_mk,
				    'sks_sim' => $value->sks_sim,
				    'tgl_mulai_efektif' => $value->tgl_mulai_efektif,
				    'tgl_akhir_efektif' => $value->tgl_akhir_efektif
				);
				$data_ids[] = $value->id_krs;
			} else {
				$error_update_data[] = array(
					'keterangan' => "Matakuliah ini sudah ada di Feeder",
					'status_error' => 2
				);
				$data_id_error[] = $value->id_krs;
				if (in_array($value->id_sms.convert_ascii($value->tahun), array_keys($data_id_kurikulum))) {
					if (in_array($value->id_sms.convert_ascii($value->kode_mk), array_keys($data_id_mk))) {
						$data_mk_kur[] = array(
							'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
							'id_matkul' => $data_id_mk[$value->id_sms.convert_ascii($value->kode_mk)],
							'semester' => $value->semester,
							'sks_mata_kuliah' => $jmlsks_mk,
						    'sks_tatap_muka' => $value->sks_tm,
						    'sks_praktek' => $value->sks_prak,
						    'sks_praktek_lapangan' => $value->sks_prak_lap,
						    'sks_simulasi' => $value->sks_sim,
							'apakah_wajib' => $value->wajib,
						);

						$data_id_kur[] = $value->id_krs;
					} elseif (in_array(convert_ascii($value->kode_mk), array_keys($array_id_mk_cross))) {
						$data_mk_kur[] = array(
							'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
							'id_matkul' => $array_id_mk_cross[convert_ascii($value->kode_mk)],
							'semester' => $value->semester,
							'sks_mata_kuliah' => $jmlsks_mk,
						    'sks_tatap_muka' => $value->sks_tm,
						    'sks_praktek' => $value->sks_prak,
						    'sks_praktek_lapangan' => $value->sks_prak_lap,
						    'sks_simulasi' => $value->sks_sim,
							'apakah_wajib' => $value->wajib,
						);
						$data_id_kur[] = $value->id_krs;
					}
				} else {
						$error_update_data_kur[] = array(
							'keterangan_kur' => "kurikulum tidak ditemukan di Feeder",
							'status_error_kur' => 2
						);
						$data_id_error_kur[] = $value->id_krs;
				}

			}
			} elseif ($_POST['method']=='up_id') {
				//check if it exist
					$array_update_mats[] = array(
						'id_matkul' => $value->id_matkul,
					    'kode_mata_kuliah' => $value->kode_mk,
					    'nama_mata_kuliah' => $value->nama_mk,
					    'id_prodi' => $value->id_sms,
					    'id_jenis_mata_kuliah' => $value->jns_mk,
					    'metode_pelaksanaan_kuliah' => $value->metode_pelaksanaan_kuliah,
					    'sks_mata_kuliah' => $jmlsks_mk,
					    'sks_tatap_muka' => $value->sks_tm,
					    'sks_praktek' => $value->sks_prak,
					    'sks_praktek_lapangan' => $value->sks_prak_lap,
					    'id_kelompok_mata_kuliah' => $value->kelompok_mk,
					    'sks_simulasi' => $value->sks_sim,
					    'metode_kuliah' => $value->metode_pelaksanaan_kuliah,
					    'tanggal_mulai_efektif' => $value->tgl_mulai_efektif,
					);
					$data_ids_up[] = $value->id_krs;
/*					$array_keys = array(
				    	'id_mk' => $value->id_matkul
				    );
					$array_update_mats[] = array('key' => $array_keys, 'data' => $data_mk_up);*/
				//update matkur
				if (in_array($value->id_sms.convert_ascii($value->tahun), array_keys($data_id_kurikulum))) {
					$data_update_kur[] = array(
						'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
						'id_matkul' => $value->id_matkul,
						'semester' => $value->semester,
						'sks_mata_kuliah' => $jmlsks_mk,
					    'sks_tatap_muka' => $value->sks_tm,
					    'sks_praktek' => $value->sks_prak,
					    'sks_praktek_lapangan' => $value->sks_prak_lap,
					    'sks_simulasi' => $value->sks_sim,
						'apakah_wajib' => $value->wajib,
					);
					$data_update_kur_id[] = $value->id_krs;
/*					$mat_kur_key = array(
						'id_kurikulum_sp' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
						'id_mk' => $value->id_matkul
					);
					$data_update_kur[] = array('key'=>$mat_kur_key,'data'=>$data_up_kur);*/
				} else {
						$error_update_data_kur[] = array(
							'keterangan_kur' => "kurikulum tidak ditemukan di Feeder",
							'status_error_kur' => 2
						);
						$data_id_error_kur[] = $value->id_krs;
				}
			} else {
				//check if it exist
				if (in_array($value->id_sms.convert_ascii($value->kode_mk), array_keys($data_id_mk))) {
					$array_update_mats[] = array(
						'id_matkul' => $data_id_mk[$value->id_sms.convert_ascii($value->kode_mk)],
					    'kode_mata_kuliah' => $value->kode_mk,
					    'nama_mata_kuliah' => $value->nama_mk,
					    'id_prodi' => $value->id_sms,
					    'id_jenis_mata_kuliah' => $value->jns_mk,
					    'metode_pelaksanaan_kuliah' => $value->metode_pelaksanaan_kuliah,
					    'sks_mata_kuliah' => $jmlsks_mk,
					    'sks_tatap_muka' => $value->sks_tm,
					    'sks_praktek' => $value->sks_prak,
					    'sks_praktek_lapangan' => $value->sks_prak_lap,
					    'id_kelompok_mata_kuliah' => $value->kelompok_mk,
					    'sks_simulasi' => $value->sks_sim,
					    'metode_kuliah' => $value->metode_pelaksanaan_kuliah,
					    'tanggal_mulai_efektif' => $value->tgl_mulai_efektif,
					    //'tanggal_selesai_efektif' => $value->tgl_akhir_efektif
					);
					$data_ids_up[] = $value->id_krs;
					/*$array_keys = array(
				    	'id_matkul' => $data_id_mk[$value->id_sms.convert_ascii($value->kode_mk)]
				    );
					$array_update_mats[] = array('key' => $array_keys, 'data' => $data_mk_up);*/
				} else {
					$error_update_data[] = array(
						'keterangan' => "Matakuliah ini tidak ada di Feeder",
						'status_error' => 2
					);
					$data_id_error[] = $value->id_krs;
				}
				//update matkur
				if (in_array($value->id_sms.convert_ascii($value->tahun), array_keys($data_id_kurikulum))) {
					if (in_array($value->id_sms.convert_ascii($value->kode_mk), array_keys($data_id_mk))) {

						$data_id_mk[convert_ascii($mk_data->id_prodi).convert_ascii($mk_data->kode_mata_kuliah)] = $mk_data->id_matkul;
						$data_update_kur[] = array(
							'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
							'id_matkul' => $data_id_mk[$value->id_sms.convert_ascii($value->kode_mk)],
							'semester' => $value->semester,
							'sks_mata_kuliah' => $jmlsks_mk,
						    'sks_tatap_muka' => $value->sks_tm,
						    'sks_praktek' => $value->sks_prak,
						    'sks_praktek_lapangan' => $value->sks_prak_lap,
						    'sks_simulasi' => $value->sks_sim,
							'apakah_wajib' => $value->wajib,
						);
						$data_update_kur_id[] = $value->id_krs;
/*						$mat_kur_key = array(
							'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
							'id_matkul' => $data_id_mk[$value->id_sms.convert_ascii($value->kode_mk)]
						);
						$data_update_kur[] = array('key'=>$mat_kur_key,'data'=>$data_up_kur);*/

					} elseif (in_array(convert_ascii($value->kode_mk), array_keys($array_id_mk_cross))) {
						//$data_id_mk[convert_ascii($mk_data->id_prodi).convert_ascii($mk_data->kode_mata_kuliah)] = $mk_data->id_matkul;
						$data_update_kur[] = array(
							'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
							'id_matkul' => $array_id_mk_cross[convert_ascii($value->kode_mk)],
							'semester' => $value->semester,
							'sks_mata_kuliah' => $jmlsks_mk,
						    'sks_tatap_muka' => $value->sks_tm,
						    'sks_praktek' => $value->sks_prak,
						    'sks_praktek_lapangan' => $value->sks_prak_lap,
						    'sks_simulasi' => $value->sks_sim,
							'apakah_wajib' => $value->wajib,
						);
						$data_update_kur_id[] = $value->id_krs;
/*						$mat_kur_key = array(
							'id_kurikulum' => $data_id_kurikulum[$value->id_sms.convert_ascii($value->tahun)],
							'id_matkul' => $array_id_mk_cross[convert_ascii($value->kode_mk)]
						);
						$data_update_kur[] = array('key'=>$mat_kur_key,'data'=>$data_up_kur);*/
					} else {
						$error_update_data_kur[] = array(
							'keterangan_kur' => "Matakuliah kurikulum tidak ditemukan di Feeder",
							'status_error_kur' => 2
						);
						$data_id_error_kur[] = $value->id_krs;
					}
				} else {
						$error_update_data_kur[] = array(
							'keterangan_kur' => "kurikulum tidak ditemukan di Feeder",
							'status_error_kur' => 2
						);
						$data_id_error_kur[] = $value->id_krs;
				}

			}
			
			$all_id[] = $value->id_krs;
		}


/*echo "<pre>";
		print_r($data_id_kurikulum);
		print_r($data_update_kur_id);
		exit();*/
		//update error
		if (!empty($error_update_data)) {
			updateMulti('mat_kurikulum',$error_update_data,'id',$data_id_error);
		}

		$error_update_data = array();
		$data_id_error = array();

		//update error kur
		if (!empty($error_update_data_kur)) {
			updateMulti('mat_kurikulum',$error_update_data_kur,'id',$data_id_error_kur);
		}
		//print_r($error_update_data_kur);
		$error_update_data_kur = array();
		$data_id_error_kur = array();


		if (!empty($array_update_mats)) {

			foreach ($array_update_mats as $key => $mat_update) {
				$key_input['id_matkul'] = $mat_update['id_matkul'];
				unset($mat_update['id_matkul']);
				  $data_dic = [
				      'act' => 'UpdateMataKuliah',
				      'key' => $key_input,
				        'token' => $token,
				        'record' => $mat_update,

				    ];


				$temp_recs = json_decode($db->run($data_dic,$url_rest));
				$data_key = $data_ids_up[$key];


				 
				$value_data = addslashes($temp_recs->error_desc);

				if ($temp_recs->error_desc!='') {
					$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;
				}
			}
/*
			$result = $proxy->UpdateRecordset($token, 'mata_kuliah',json_encode($array_update_mats));

					foreach ($result['result'] as $key => $value) {
						$value_data = addslashes($value['error_desc']);
						$data_key = $data_ids_up[$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('mat_kurikulum',$error_update_data,'id',$data_id_error);
		}
		$error_update_data = array();
		$data_id_error = array();

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


if (!empty($data_mk_kur)) {

	if ($_POST['method']=='in') {
		foreach ($data_mk_kur as $key => $mk_kur) {
			$data_dic = [
		      'act' => 'InsertMatkulKurikulum',
		        'token' => $token,
		        'record' => $mk_kur,
		    ]; 
		$temp_recs = json_decode($db->run($data_dic,$url_rest));
/*		dump($data_dic);
		dump($temp_recs);*/
		$value_data = addslashes($temp_recs->error_desc);
		$data_key = $data_id_kur[$key];
			if ($value_data=='Data mata kuliah kurikulum ini sudah ada') {
					$error_update_data_kur[] = array(
						'keterangan_kur' => $value_data,
						'status_error_kur' => 2
					);
					$data_id_error_kur[] = $data_key;
			} else {
				if ($value_data!='') {
					$key_input['id_kurikulum'] = $mk_kur['id_kurikulum'];
					$key_input['id_matkul'] = $mk_kur['id_matkul'];
					unset($mk_kur['id_kurikulum']);
					unset($mk_kur['id_matkul']);
					  $data_dic = [
					      'act' => 'UpdateMatkulKurikulum',
					      'key' => $key_input,
					        'token' => $token,
					        'record' => $mk_kur,

					    ];
					$temp_recs = json_decode($db->run($data_dic,$url_rest));
/*					dump($data_dic);
					dump($temp_recs);*/
					$value_datas = addslashes($temp_recs->error_desc);
					if ($value_datas!='') {
						$error_update_data_kur[] = array(
							'keterangan_kur' => $value_data,
							'status_error_kur' => 2
						);
						$data_id_error_kur[] = $data_key;
					} else {
						$update_data_kur[] = array(
							'keterangan_kur' => '',
							'status_error_kur' => 1
						);
						$data_id_sukses_kur[] = $data_key;
					}

				} else {
					$update_data_kur[] = array(
						'keterangan_kur' => '',
						'status_error_kur' => 1
					);
					$data_id_sukses_kur[] = $data_key;
				}
			}
		}
	}	
}

$data_id_kur = array();
$data_mk_kur = array();
		//update error
		if (!empty($error_update_data_kur)) {
			updateMulti('mat_kurikulum',$error_update_data_kur,'id',$data_id_error_kur);
		}
		$error_update_data_kur = array();
		$data_id_error_kur = array();
		//update sukses
		if (!empty($update_data_kur)) {
			updateMulti('mat_kurikulum',$update_data_kur,'id',$data_id_sukses_kur);
		}
		$update_data_kur = array();
		$data_id_sukses_kur = array();


		if (!empty($data_update_kur)) {
			foreach ($data_update_kur as $key => $mk_kur) {
					$key_input['id_kurikulum'] = $mk_kur['id_kurikulum'];
					$key_input['id_matkul'] = $mk_kur['id_matkul'];
					unset($mk_kur['id_kurikulum']);
					unset($mk_kur['id_matkul']);
					  $data_dic = [
					      'act' => 'UpdateMatkulKurikulum',
					      'key' => $key_input,
					      'token' => $token,
					      'record' => $mk_kur,

					    ];
					$temp_recs = json_decode($db->run($data_dic,$url_rest));
/*					dump($data_dic);
					dump($temp_recs);*/
					$value_data = addslashes($temp_recs->error_desc);
					$data_key = $data_update_kur_id[$key];
					//dump($data_update_kur_id);
					
						if ($value_data!='') {
							$error_update_data_kur[] = array(
								'keterangan_kur' => $value_data,
								'status_error_kur' => 2
							);
							$data_id_error_kur[] = $data_key;
						} else {
							$update_data_kur[] = array(
								'keterangan_kur' => '',
								'status_error_kur' => 1
							);
							$data_id_sukses_kur[] = $data_key;
						}
			}

/*			$result = $proxy->UpdateRecordset($token, 'mata_kuliah_kurikulum',json_encode($data_update_kur));
			array_pop($result['result']);

					foreach ($result['result'] as $key => $value) {

						$value_data = addslashes($value['error_desc']);
						$data_key = $data_update_kur_id[$key];

						if ($value_data!='') {
							$error_update_data_kur[] = array(
								'keterangan_kur' => $value_data,
								'status_error_kur' => 2
							);
							$data_id_error_kur[] = $data_key;
						} else {
							$update_data_kur[] = array(
								'keterangan_kur' => '',
								'status_error_kur' => 1
							);
							$data_id_sukses_kur[] = $data_key;
						}
					}*/

		}

		if (!empty($data_mk)) {

		if ($_POST['method']=='in') {

/*			$result = $proxy->InsertRecordset($token, 'mata_kuliah',json_encode($data_mk));

			foreach ($result['result'] as $key => $value) {

				$value_data = addslashes($value['error_desc']);
				$data_key = $data_ids[$key];*/


				
			foreach ($data_mk as $key => $mk) {
				//$data_mk_detail = $mk;
/*				unset($mk['tahun']);
				unset($mk['semester']);
				unset($mk['apakah_wajib']);*/
				$data_dic = [
			      'act' => 'InsertMataKuliah',
			        'token' => $token,
			        'record' => array(
						'kode_mata_kuliah' => $mk['kode_mk'],
						'nama_mata_kuliah' => $mk['nm_mk'],
						'id_prodi' => $mk['id_sms'],
						'id_jenis_mata_kuliah' => $mk['jns_mk'],
						'id_kelompok_mata_kuliah' => $mk['nm_mk'],
						'sks_mata_kuliah' => $mk['sks_mk'],
					    'sks_tatap_muka' => $mk['sks_tm'],
					    'sks_praktek' => $mk['sks_prak'],
					    'sks_praktek_lapangan' => $mk['sks_prak_lap'],
					    'sks_simulasi' => $mk['sks_sim'],
					    'id_kelompok_mata_kuliah' => $mk['kelompok_mk'],
						'metode_kuliah' => $mk['metode_pelaksanaan_kuliah'],
						'tanggal_mulai_efektif' => $mk['tgl_mulai_efektif'],
						'tanggal_akhir_efektif' => $mk['tgl_akhir_efektif'],
					)
			    ]; 

			  // dump($data_dic);
			$insert_response = json_decode($db->run($data_dic,$url_rest));
			//dump($insert_response);
			$value_data = addslashes($insert_response->error_desc);
			$data_key = $data_ids[$key];


				if ($value_data!='') {
					if (trim($value_data)=='Mata kuliah dengan nama dan kode_mata_kuliah ini sudah ada') {
						/*echo "<pre>";
						print_r($temp_recs);*/
						$data_req_mk = [
							'act' => 'GetListMataKuliah',
						    'token' => $token,
						    'filter' => "kode_mata_kuliah='".$mk['kode_mk']."'",
						    'order' => "",
						    'limit' => "",
						    'offset' => ""

						];

						$check_mk_exist_json = json_decode($db->run($data_req_mk,$url_rest));
						//dump($check_mk_exist_json);
						if (!empty($check_mk_exist_json->data)) {
							foreach ($check_mk_exist_json->data as $mk_data) {
								if (in_array($mk['id_sms'].convert_ascii($mk['tahun']), array_keys($data_id_kurikulum))) {
									$data_mk_kur[] = array(
										'id_kurikulum' => $data_id_kurikulum[$mk['id_sms'].convert_ascii($mk['tahun'])],
										'id_matkul' => $mk_data->id_matkul,
										'semester' => $mk['smt'],
										'sks_mata_kuliah' => $mk['sks_mk'],
									    'sks_tatap_muka' => $mk['sks_tm'],
									    'sks_praktek' => $mk['sks_prak'],
									    'sks_praktek_lapangan' => $mk['sks_prak_lap'],
									    'sks_simulasi' => $mk['sks_sim'],
										'apakah_wajib' => $mk['a_wajib'],
									);

									$data_id_kur[] = $data_key;
								} else {
									$error_update_data_kur[] = array(
										'keterangan_kur' => "kurikulum tidak ditemukan di Feeder",
										'status_error_kur' => 2
									);
									$data_id_error_kur[] = $data_key;
								}
							}
							//dump($data_mk_kur);
						}
						$error_update_data[] = array(
							'keterangan' => 'Matakuliah sudah ada di Feeder',
							'status_error' => 2
						);
						$data_id_error[] = $data_key;

					} else {
						$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;

						//echo "fuck";

						if (in_array($mk['id_sms'].convert_ascii($mk['tahun']), array_keys($data_id_kurikulum))) {
							//echo "fuck2";

							//dump($mk);
						$data_mk_kur[] = array(
							'id_kurikulum' => $data_id_kurikulum[$mk['id_sms'].convert_ascii($mk['tahun'])],
							'id_matkul' => $insert_response->data->id_matkul,
							'semester' => $mk['smt'],
							'sks_mata_kuliah' => $mk['sks_mk'],
						    'sks_tatap_muka' => $mk['sks_tm'],
						    'sks_praktek' => $mk['sks_prak'],
						    'sks_praktek_lapangan' => $mk['sks_prak_lap'],
						    'sks_simulasi' => $mk['sks_sim'],
							'apakah_wajib' => $mk['a_wajib'],
						);

						

						//exit();

						

						$data_id_kur[] = $data_key;


					
						} else {
							$error_update_data_kur[] = array(
								'keterangan_kur' => "kurikulum tidak ditemukan di Feeder",
								'status_error_kur' => 2
							);
							$data_id_error_kur[] = $data_key;
						}
					}
				}
			}	
		}

		//dump($data_mk_kur);

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

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

		//update error
		if (!empty($error_update_data_kur)) {
			updateMulti('mat_kurikulum',$error_update_data_kur,'id',$data_id_error_kur);
		}
		$error_update_data_kur = array();
		$data_id_error_kur = array();
		//update sukses
		if (!empty($update_data_kur)) {
			updateMulti('mat_kurikulum',$update_data_kur,'id',$data_id_sukses_kur);
		}
		$update_data_kur = array();
		$data_id_sukses_kur = array();
/*echo "<pre>";
print_r($data_mk_kur);
exit();*/
if (!empty($data_mk_kur)) {

	if ($_POST['method']=='in') {
/*		$result = $proxy->InsertRecordset($token, 'mata_kuliah_kurikulum',json_encode($data_mk_kur));

			array_pop($result['result']);
			array_pop($result['result']);
			array_pop($result['result']);
			array_pop($result['result']);
			array_pop($result['result']);
		foreach ($result['result'] as $key => $value) {

			$value_data = addslashes($value['error_desc']);
			$data_key = $data_id_kur[$key];*/

		foreach ($data_mk_kur as $key => $mk_kur) {
			$data_dic = [
		      'act' => 'InsertMatkulKurikulum',
		        'token' => $token,
		        'record' => $mk_kur,
		    ]; 
		$temp_recs = json_decode($db->run($data_dic,$url_rest));
		/*dump($data_dic);
		dump($temp_recs);*/
		$value_data = addslashes($temp_recs->error_desc);
		$data_key = $data_id_kur[$key];
			if ($value_data!='') {
				$error_update_data_kur[] = array(
					'keterangan_kur' => $value_data,
					'status_error_kur' => 2
				);
				$data_id_error_kur[] = $data_key;
			} else {
				$update_data_kur[] = array(
					'keterangan_kur' => '',
					'status_error_kur' => 1
				);
				$data_id_sukses_kur[] = $data_key;
			}
		}
	}	
}

		//update error
		if (!empty($error_update_data_kur)) {
			updateMulti('mat_kurikulum',$error_update_data_kur,'id',$data_id_error_kur);
		}
		$error_update_data_kur = array();
		$data_id_error_kur = array();
		//update sukses
		if (!empty($update_data_kur)) {
			updateMulti('mat_kurikulum',$update_data_kur,'id',$data_id_sukses_kur);
		}
		$update_data_kur = array();
		$data_id_sukses_kur = array();

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

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

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

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

		$jumlah_sukses_kur = $jml_error_sukses_kur->jml_sukses;
		$jumlah_error_kur = $jml_error_sukses_kur->jml_error;

		$db->fetch_custom("update mat_kurikulum set unik_id=0 where  unik_id=".$_POST['random_number']);
		
		$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses." data Matakuliah 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=\"#collapseExample1\" 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=\"collapseExample1\">";
				$i=1;
				foreach ($all_errors as $pesan) {
						$msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
					$i++;
					}
		$msg .= "</div>
		</div>";
		$msg .=  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses_kur." data Matakuliah berhasil dimasukan ke Kurikulum</font><br />
		<font color=\"#ce4844\" >".$jumlah_error_kur." data tidak bisa Di Import </font>";
		
		if (!$jumlah_error_kur==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