	<?php
	function getValueFromForm ($data, $idEvent) {
		global $conn;
		//echo "<br>data: ".$data[3];
		$arrDetect = array();
		$sql1 = 
		"select designID, labelType, labelName, designName, labelNameTitle ".
		"from form_label, form_design ".
		"where labelType='".$idEvent."' ".
		"and labelID = labelParentID ". 
		"order by designORDER asc ";
		echo $sql1;
		$formValue = array();
		if ($hasil1 = mysqli_query($conn, $sql1)) {
			if (mysqli_num_rows($hasil1) > 0) {
				while ($data1 = mysqli_fetch_array($hasil1)) {
					$labelNameTitle = $data1["labelNameTitle"];
					$labelName 		= $data1["labelName"];
					$designName 	= $data1["designName"];
					
					if (isset($_POST[$designName])) {
						$$designName = $_POST[$designName];
					} else {
						if (isset($data[$designName]))
						$$designName = $data[$designName];						
					}
					$formValue[$designName] = $$designName;
					echo "<br>$designName : ".$$designName;

					//if (isset($_POST[$designName]))
					//echo "<br>".$designName." = ".$formValue[$designName];
					
					
					$sql2 = "
					SELECT designID, designName, labelParentID FROM form_design_other 
					WHERE labelParentID = '".$data1["designID"]."'";
					if ($hasil2 = mysqli_query($conn, $sql2)) {
						if (mysqli_num_rows($hasil2) > 0) {
							while ($data2 = mysqli_fetch_array($hasil2)) {
								$designName2 = $data2["designName"];
								if (isset($_POST[$designName2])) {
									$$designName2 = $_POST[$designName2];
									
									//echo "<br>$designName2: ".$$designName2;
									//echo "<br>".strtolower(str_replace(' ', '', $$designName2));

									
									$sql3 =
									"SELECT elementSubName, elementSubDescription " .
									"FROM form_element_sub " .
									"WHERE REPLACE(LTRIM(RTRIM(LOWER(elementSubDescription))), ' ', '') = '".strtolower(str_replace(' ', '', $$designName2))."' ";
									
									if ($hasil3 = mysqli_query($conn, $sql3)) {
										while ($data3 = mysqli_fetch_array($hasil3)) {
											//echo "<br>".$sql3;
											$arrDetect ["Product Category"] = $data3["elementSubDescription"];
										}
									}
									
									$sql3 =
									"SELECT valueName " .
									"FROM form_element_value " .
									"WHERE REPLACE(LTRIM(RTRIM(LOWER(valueName))), ' ', '') = '".strtolower(str_replace(' ', '', $$designName2))."' ";
									if ($hasil3 = mysqli_query($conn, $sql3)) {
										while ($data3 = mysqli_fetch_array($hasil3)) {
											//echo "<br>".$sql3;
											$arrDetect ["Product Sub Category"] = $data3["valueName"];
										}
									}
								}
							}
						}
					}
					//var_dump($arrDetect);
					//echo $formValue["buyer_name"];
					//
					//echo "<br>".$designName.": ".$$designName;
				}
			}
		}
		if ($arrDetect) {
			foreach($arrDetect as $key => $value) {
				echo '<br><br>'.$key.' '.$value. ' sudah ada, transaksi Gagal Diproses';
			}
			die();
		} else {
			if (isset($product_category_other)) {
				$sql3 = 
				"INSERT INTO form_element_sub (
				elementSubID, elementSubName, elementSubStat, elementSubDescription) VALUES (
				null, '".str_replace(' ', '_', $product_category_other)."' , 'single', '".$product_category_other."' ) ";
				if ($hasil3 = mysqli_query($conn, $sql3)) {
					//echo "<br>".$sql3;
					$formValue["product_category"] = mysqli_insert_id();
					$sql4 =
					"INSERT INTO form_element_link (
					elementLinkID, linkDesignID, linkElementSubID) VALUES (
					null, '41', '".$formValue["product_category"]."' )";
					if (mysqli_query($conn, $sql4)) {}
				}
			}
			if (isset($product_sub_category_other)) {
				$sql3 = 
				"INSERT INTO form_element_value (
				valueID, elementSubParentID, valueName) VALUES (
				null, '".$formValue["product_category"]."', '".$product_sub_category_other."' ) ";
				if ($hasil3 = mysqli_query($conn, $sql3)) {
					//<strong></strong>
					$formValue["product_sub_category"] = mysqli_insert_id();
					$sql4 =
					"INSERT INTO form_element_value (
					valueID, elementSubParentID, valueName) VALUES (
					null, '".$formValue["product_category"]."', 'Others, please specify (Lainnya, masukkan disini)' ) ";
					if (mysqli_query($conn, $sql4)) {}
				}
			}
		}
		
		return array (
			"labelNameTitle" => $labelNameTitle,
			"labelName" 	 => $labelName,
			"formValue" 	 => $formValue
		);
	}

	function kontak_dagang_header($exhibitorID, $idEvent) {
		global $conn;
		$sql = 
		" SELECT * FROM kontak_dagang_header, exhibitor_list ".
		" WHERE exhibitorID = to_exhibitor_id ". 
		" AND to_exhibitor_id = '".$exhibitorID."' ". 
		" AND tgl_transaksi = '".$_COOKIE["tglNAMA"]."'";
		//echo "<br>".$sql; 
		if ($hasil = mysqli_query($conn, $sql)) {
			if (mysqli_num_rows($hasil) > 0) {
				while ($data = mysqli_fetch_array($hasil)) {
					$tgl_transaksi = $data["tgl_transaksi"];
					$companyName = $data["companyName"];
					$header_id = $data["header_id"];
					$hallName = array();
					$standNumber = array();
					$sql2 = 
					"SELECT * FROM exhibitor_list_hallstand ".
					"WHERE to_exhibitor_id = '".$exhibitorID."' ".
					"ORDER BY to_hall_id ";
					//echo $sql2;
					if (mysqli_num_rows($hasil2 = mysqli_query($conn, $sql2)) > 0) {
						while ($data2 = mysqli_fetch_array($hasil2)) {
							$dataRecord = FindDataSingle("dt_hall", "id_hall", $data2["to_hall_id"], "nama_hall");
							//FindData($data2["to_hall_id"], "dt_hall", $dataTitle, $dataRecord);
							//echo "<br>$dataRecord : ".$$dataRecord;
							$hallName[] = $dataRecord;
							$standNumber[] = $data2["standNumber"];  
							$ValueFromForm = getValueFromForm($data, $idEvent);
							$sql_update = '';
							foreach ($ValueFromForm["formValue"] as $key => $value) {
								if (!$sql_update) $sql_update = 'UPDATE kontak_dagang_header SET ';
								else $sql_update .= ', ';
								$sql_update .= $key.' = "'.$value.'" ';
							}
						}
						$sql_update .= 
						',date_updated = "'.date("Y-m-d H:m:s").'", updated_by = "'.$_SESSION['username'].'" '.
						'WHERE header_id = "'.$header_id.'"';
					}
				}
			} else {
				$sql_insert = 
				'INSERT INTO kontak_dagang_header ( '.
				'header_id, to_exhibitor_id, tgl_transaksi, 
				product_fav_hari_ini, saran, ttd_exhibitor, 
				ttd_organizer, date_updated, updated_by) '.'VALUES 
				(null, "'.$exhibitorID.'", "'.$_COOKIE["tglNAMA"].'", 
				"", "", "", "CityNeon", "'.date("Y-m-d H:i:s").'", 
				"'.$_SESSION['username'].'" )';
			}			
		}		
		//echo "<br>".$sql_update;
		//echo "<br>".$sql_insert;
		return array (
			"companyName" 	=> (isset($companyName) ? $companyName : ''),
			"hallName" 		=> (isset($hallName) ? $hallName : ''),
			"standNumber" 	=> (isset($standNumber) ? $standNumber : ''),
			"tgl_transaksi" => (isset($tgl_transaksi) ? $tgl_transaksi : ''),
			"header_id" 	=> (isset($header_id) ? $header_id : ''),
			"ValueFromForm" => (isset($ValueFromForm) ? $ValueFromForm : ''),
			"sql_insert" 	=> (isset($sql_insert) ? $sql_insert : ''),
			"sql_update" 	=> (isset($sql_update) ? $sql_update : '')
		); 
	}
	
	function kontak_dagang_transaksi($exhibitorID, $header_id, $transaksiID, $idEvent) {
		global $conn;
		$sql = 
		"SELECT * FROM kontak_dagang_header as a, exhibitor_list as b, exhibitor_list_hallstand as c " .
		"WHERE a.to_exhibitor_id = '".$exhibitorID."' " .
		"AND c.to_exhibitor_id = '".$exhibitorID."' " .
		"AND exhibitorID = '".$exhibitorID."' " .
		"AND header_id = '".$header_id."' " .
		"AND tgl_transaksi = '".$_COOKIE["tglNAMA"]."'";
		//echo "<br>".$sql."<br><br>";
		if ($hasil = mysqli_query($conn, $sql)) {
			if (mysqli_num_rows($hasil) > 0) {
				$hallName = array();
				$standNumber = array();
				while ($data = mysqli_fetch_array($hasil)) {
					$tgl_transaksi = $data["tgl_transaksi"];
					$companyName = $data["companyName"];
					$header_id = $data["header_id"];
					FindData($data["to_hall_id"], "form_element_value", $dataTitle, $dataRecord);
					$hallName[] = $dataRecord;
					$standNumber[] = $data["standNumber"];  
				}
			}
		}
		
		$sql = 
		"SELECT * FROM kontak_dagang_transaksi ".
		"WHERE transaksiID = '".$transaksiID."' ";
		//echo "<br>".$sql."<br>"; 
		$sql_update = '';
		if ($hasil = mysqli_query($conn, $sql)) {
			if (mysqli_num_rows($hasil) > 0) {
				$data = mysqli_fetch_array($hasil);
			}
		}
		
		//var_dump($data);
		$ValueFromForm = getValueFromForm((isset($data) ? $data : null), $idEvent);
		//var_dump($ValueFromForm);
		
		$os  = array(1220, 1214, 1219);	
		$jn_trans = $ValueFromForm["formValue"]["jn_trans"];				
		//echo "<br>product_category: ".$ValueFromForm["formValue"]["jn_trans"]."<br>";
		if (in_array($jn_trans, $os)) {
			if ($ValueFromForm["formValue"]["product_category"] == null)
				$ValueFromForm["formValue"]["product_category"] = 0;	
			if ($ValueFromForm["formValue"]["product_sub_category"] == null) 	   
				$ValueFromForm["formValue"]["product_sub_category"] = 0; 
		}
		
		$sql_insert = 
		'INSERT INTO kontak_dagang_transaksi ( '.
		'transaksiID, to_kontak_dagang_header, jn_trans, nm_perusahaan, to_negara, '.
		'product_category, product_sub_category, product_detail, estimasi_kontrak, '.
		'date_updated, updated_by, isDeleted, remark, pic) VALUES ( 
		 null, "'.$header_id.'"  ';

		foreach ($ValueFromForm["formValue"] as $key => $value) {
			if (!$sql_update) $sql_update = 'UPDATE kontak_dagang_transaksi SET ';
			else $sql_update .= ', ';
			$sql_update .= $key.' = "'.$value.'" ';
			$sql_insert .= ', "'.$value.'" ';
		}
		
		if ($_SESSION['username'] == 'super') {
			if (isset($_POST["remark"])) {
				$remark = $_POST["remark"];
			} else $remark = 0;
		
			if (isset($_POST["pic"])) {
				$pic = $_POST["pic"];
			} else $pic = 0;
		
		} else {
			$remark = 0;
			$pic = 0;
		}
		
		$sql_update .= 
		', date_updated = "'.date("Y-m-d h:m:s").'", 
		updated_by = "'.$_SESSION['username'].'", remark = "'.$remark.'",  
		pic = "'.$pic.'" WHERE transaksiID = "'.$transaksiID.'" ';  
		
		$sql_insert .= ', "'.date("Y-m-d h:m:s").'", "'.$_SESSION['username'].'", "0", "'.$remark.'", "'.$pic.'")';
		
		//echo "<br>sql_update: ".$sql_update.'<br>';
		//fc "<br>sql_insert: ".$sql_insert.'<br>';
		
		return array (
			"companyName" => (isset($companyName) ? $companyName : null),
			"hallName" => (isset($hallName) ? $hallName  : null),
			"standNumber" => (isset($standNumber) ? $standNumber : null),
			"tgl_transaksi" => (isset($tgl_transaksi) ? $tgl_transaksi : null),
			"header_id" => (isset($header_id) ? $header_id : null),
			"ValueFromForm" => (isset($ValueFromForm) ? $ValueFromForm : null),
			"sql_insert" => (isset($sql_insert) ? $sql_insert : null),
			"sql_update" => (isset($sql_update) ? $sql_update : null)
		); 
	}
	?>
