<?php 
session_start();
require_once "config.php";
/*Please don't change the sequences code below-start*/
$projectLocation = $_POST["projectLocation"];
include "splitLocationName_project.php";
$projectName = $_SESSION["projectName"];
$locationName = $_SESSION["locationName"];
$location_id = $_SESSION["location_id"];
/*Please don't change the sequences code above-end*/

//echo "MENU: ". $_SESSION["menu"];
//echo "sub-MENU: ". $_SESSION["submenu"];

$location_id_selected = $_POST["location_id_selected"];
$property_type = $_POST["property_type"];
$parentUnitNo = $_POST["parentUnitNo"];
$count_childUnitNo = count($_POST["child_unit_no"]);
$block = $_POST["block"];
$line1 = $_POST["line1"];
$line2 = $_POST["line2"];
$city = $_POST["city"];
$postcode = $_POST["postcode"];
$state = $_POST["state"];
$country = $_POST["country"];
$kedudukan_lot = $_POST["kedudukan_lot"];
$jenis_pemilikan = $_POST["jenis_pemilikan"];
$no_hakmilik = $_POST["no_hakmilik"];
$no_lot = $_POST["no_lot"];
$mukim = $_POST["mukim"];
$daerah = $_POST["daerah"];
$pegangan = $_POST["pegangan"];
$tempoh_pajakan = $_POST["tempoh_pajakan"];
$tarikh_luput_pajakan = $_POST["tarikh_luput_pajakan"];
$tarikh_perjanjian_jualbeli = $_POST["tarikh_perjanjian_jualbeli"];
$luas_tanah_kp = $_POST["luas_tanah_kp"];
$luas_lantai_kp = $_POST["luas_lantai_kp"];
$harga_belian = $_POST["harga_belian"];
$status_pindahmilik = $_POST["status_pindahmilik"];
$catatan = $_POST["catatan"];
$catatan_tambahan = $_POST["catatan_tambahan"];


if($parentUnitNo == ""){
	//echo "PARENTS UNIT ZERO";
	 if($count_childUnitNo >= 1){
		  for($y=0;$y<$count_childUnitNo;$y++){
			  $childUnitNo = strtoupper($_POST["child_unit_no"][$y]);
			  $parentUnitNo = $childUnitNo;
			  //insert properties table first - start
			  $insertSQL = "insert into properties 
								(id,
								location_id,
								property_type, 
								unit_no, 
								block, 
								line1, 
								line2, 
								city, 
								state, 
								country, 
								postcode, 
								kedudukan_lot, 
								jenis_pemilikan,
								no_hakmilik, 
								no_lot, 
								mukim, 
								daerah, 
								pegangan, 
								tempoh_pajakan, 
								tarikh_luput_pajakan, 
								tarikh_perjanjian_jualbeli,
								luas_tanah_kp, 
								luas_lantai_kp, 
								harga_belian, 
								status_pindahmilik, 
								catatan,
								catatan_tambahan,
								created_at, 
								updated_at)
								values(
								'',
								'".$location_id."',
								'".$property_type."',
								'".$parentUnitNo."',
								'".$block."',
								'".$line1."',
								'".$line2."',
								'".$city."',
								'".$state."',
								'".$country."',
								'".$postcode."',
								'".$kedudukan_lot."',
								'".$jenis_pemilikan."',
								'".$no_hakmilik."',
								'".$no_lot."',
								'".$mukim."',
								'".$daerah."',
								'".$pegangan."',
								'".$tempoh_pajakan."',
								'".date("Y-m-d", strtotime($tarikh_luput_pajakan))."',
								'".date("Y-m-d", strtotime($tarikh_perjanjian_jualbeli))."',
								'".$luas_tanah_kp."',
								'".$luas_lantai_kp."',
								'".$harga_belian."',
								'".$status_pindahmilik."',
								'".$catatan."',
								'".$catatan_tambahan."',
								'".date("Y-m-d H:i:s")."',
								'".date("Y-m-d H:i:s")."'
								)
				";

				if (mysqli_query($link, $insertSQL)) {
				  $last_id = mysqli_insert_id($link);
				  //echo "New record created successfully. Last inserted ID is: " . $last_id;
				} else {
				  echo "Error: " . $insertSQL . "<br>" . mysqli_error($link);
				}
				//end insert properties table
				 $sqlInsertChildUnitNo = "insert into properties_unit values (NULL,'$childUnitNo', 'enable', $last_id, '".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
				mysqli_query($link, $sqlInsertChildUnitNo);
		  }
	  
	 }
}

else if($parentUnitNo != "" || $parentUnitNo != null){
	//echo "PARENTS UNIT NOT-ZERO";
	$insertSQL = "insert into properties 
					(id,
					location_id,
					property_type, 
					unit_no, 
					block, 
					line1, 
					line2, 
					city, 
					state, 
					country, 
					postcode, 
					kedudukan_lot, 
					jenis_pemilikan,
					no_hakmilik, 
					no_lot, 
					mukim, 
					daerah, 
					pegangan, 
					tempoh_pajakan, 
					tarikh_luput_pajakan, 
					tarikh_perjanjian_jualbeli,
					luas_tanah_kp, 
					luas_lantai_kp, 
					harga_belian, 
					status_pindahmilik, 
					catatan,
					catatan_tambahan,
					created_at, 
					updated_at)
					values(
					'',
					'".$location_id."',
					'".$property_type."',
					'".strtoupper($parentUnitNo)."',
					'".$block."',
					'".$line1."',
					'".$line2."',
					'".$city."',
					'".$state."',
					'".$country."',
					'".$postcode."',
					'".$kedudukan_lot."',
					'".$jenis_pemilikan."',
					'".$no_hakmilik."',
					'".$no_lot."',
					'".$mukim."',
					'".$daerah."',
					'".$pegangan."',
					'".$tempoh_pajakan."',
					'".date("Y-m-d", strtotime($tarikh_luput_pajakan))."',
					'".date("Y-m-d", strtotime($tarikh_perjanjian_jualbeli))."',
					'".$luas_tanah_kp."',
					'".$luas_lantai_kp."',
					'".$harga_belian."',
					'".$status_pindahmilik."',
					'".$catatan."',
					'".$catatan_tambahan."',
					'".date("Y-m-d H:i:s")."',
					'".date("Y-m-d H:i:s")."'
					)
	";

	if (mysqli_query($link, $insertSQL)) {
	  $last_id = mysqli_insert_id($link);
	  //echo "New record created successfully. Last inserted ID is: " . $last_id;
	  
	 
	} else {
	  echo "Error: " . $insertSQL . "<br>" . mysqli_error($link);
	}

	 //insert all child unit no
	 if($count_childUnitNo >= 1){
		  for($y=0;$y<$count_childUnitNo;$y++){
			 $childUnitNo = strtoupper($_POST["child_unit_no"][$y]);
			 $sqlInsertChildUnitNo = "insert into properties_unit values (NULL,'$childUnitNo', 'enable', $last_id, '".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
			mysqli_query($link, $sqlInsertChildUnitNo);
		  }
	  
	 }
}
 header("Location: properties_registerNew_additionalInfo.php?projectLocation=".$projectLocation."&property_id=" . $last_id . "&menu=" . $_SESSION["menu"]. "&submenu=" . $_SESSION["submenu"]);
?>