<?php include "headerLink.php"?>


<!DOCTYPE html>
<html lang="en">
<script type="text/javascript">
function changeStatus(childUnitNo,childUnitID,property_status, locationName) {
	window.open("changeStatus.php?childUnitNo="+childUnitNo+"&childUnitID="+childUnitID+"&property_status="+property_status+"&locationName="+locationName,'_blank');
}
function newPopup(url) {
	popupWindow = window.open(
		//url,'popUpWindow','height=600,width=700,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
		url,'_blank','height=600,width=700,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
//function confirmFunction(childUnitNo,childUnitID,unit_status, locationName, projectLocation){
function confirmFunction(childUnitID,childUnitNo,unit_status,locationName,projectLocation){
        // return true or false, depending on whether you want to allow the `href` property to follow through or not
		//alert(childUnitID + childUnitNo + unit_status + locationName + projectLocation);
		var new_unit_status = "";
		var show_unit_status = "";
		if(unit_status == 'Available'){
			new_unit_status = 'disable';
			show_unit_status = 'Unavailable';
		}
		if(unit_status == 'Unavailable'){
			new_unit_status = 'enable';
			show_unit_status = 'Available';
		}
		var r = confirm("Are you sure want to set this property to " + show_unit_status + " ?  \n Unit No.: " + childUnitNo + "\nLocation Name: " + projectLocation  );
		if (r == true) {
			//alert();
			window.location.href="changeStatus.php?childUnitID="+childUnitID+"&unit_status="+unit_status+"&page=ListProperties.php&projectLocation="+projectLocation;
		} else {
			window.location.href="ListProperties.php?projectLocation="+projectLocation;
		}
    }
function editProperty(childUnitID, parentsUnitId,projectLocation){
	//alert(parentsUnitId);
	window.location.href="edit_propertyrecord.php?childUnitID="+childUnitID+"&parentUnitId="+parentsUnitId+"&projectLocation="+projectLocation;
}
function history(childUnitID){
	//alert(parentsUnitId);
	window.location.href="history_propertyrecord.php?childUnitID="+childUnitID;
}
function viewDetails(parentsUnitId,childUnitID,projectLocation){
	window.location.href="viewPropertyRecord.php?property_id="+parentsUnitId+"&childUnitID="+childUnitID+"&projectLocation="+projectLocation;
}
function changeProject(childUnitID,childUnitNo,locationName,projectLocation){
	window.location.href="changeProject.php?childUnitID="+childUnitID+"&childUnitNo="+childUnitNo+"&locationName="+locationName+"&projectLocation="+projectLocation;
}


</script>

<body class="animsition">
    <div class="page-wrapper">
        <!-- HEADER MOBILE-->
        <?php include "menuSideBar_mobile.php" ;?>
        <!-- END HEADER MOBILE-->
		
        <!-- MENU SIDEBAR-->
        <?php include "menuSideBar.php" ;?>
        <!-- END MENU SIDEBAR-->

        <!-- PAGE CONTAINER-->
        <div class="page-container">
            <!-- HEADER DESKTOP-->
			<?php include "headerPage.php"?>
            <!-- END HEADER DESKTOP-->
			
			
			
			<!-- MAIN CONTENT-->
			
			<?php 
			
			//include "projectLocation_session.php";
			//echo "PROJECT NAME: ".$_SESSION["projectName"];
			 $sqlListProperties = "";
			$sqlOrderBy = "ORDER BY (CASE WHEN properties.unit_no like '[a-z]%' THEN 0 ELSE 1 END),properties.unit_no";
			if($_SESSION["projectName"] == "PPR"){
				//ECHO $_SESSION["projectName"];
				 $sqlListProperties = "select properties.* from properties 
											inner join locations on properties.location_id = locations.id
											inner join projects on locations.project_id = projects.id
											where locations.id =  " . $_SESSION["location_id"];
			}
			ELSE IF($_SESSION["projectName"] != "PRR"){
				$sqlOrderBy = " ORDER BY (CASE WHEN properties_unit.unit_no like '[a-z]%' THEN 0 ELSE 1 END),properties_unit.unit_no ";
				
				$sqlListProperties = "select properties.*, properties_unit.unit_no as childUnitNo,properties_unit.id as childUnitID, properties_unit.unit_status from properties_unit 
									inner join properties on properties_unit.property_id = properties.id
									inner join locations on properties.location_id = locations.location_id
									inner join projects on locations.project_id = projects.id
									where locations.id = ". $_SESSION["location_id"] ." $sqlOrderBy";
			}
			if($sqlListProperties !=  ""){
				$runListProperties = mysqli_query($link, $sqlListProperties);
				if($runListProperties === FALSE){
					die(mysqli_error($link));
				}
			}
			$projectName = $projectName;
			$statusUpdate = $_GET["statusUpdate"];
			$showmsg = "";
			if( $statusUpdate != "Success" && $statusUpdate != "" && $statusUpdate != null){
				$showmsg = $statusUpdate;
			}
			
			?>
            
            <!--main content upper start-->
			<?php include "main-content-upper.php"?>
            <!--main content upper end-->
			<!--main content middle start-->
			<div class="row" >
				
				
				<div class="col-lg-12">
					<div class="table-responsive">
						<table class="table table-bordered table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
							<thead>
								<tr bgcolor="#7cc8fc" align="center" valign="middle">
									<td><b>#</b></td>
									<td><b>Unit No.</b></td>
									<td><b>Full Address</b></td>
									<td><b>Type</b></td>
									<td><b>Notes</b></td>
									<td><b>Vacancy</b></td>
									<td><b>Action</b></td>
								</tr>
							</thead>
							<tbody>
							<?php 
							
							$no = 1;
							$page="ListProperties_process.php";
							
							$countrow = mysqli_num_rows($runListProperties);
							if($countrow > 0){
								while($resultProperties  = mysqli_fetch_array($runListProperties)){
									$parentsUnitNo = $resultProperties["unit_no"];
									$parentsUnitId = $resultProperties["id"];
									$block = $resultProperties["block"];
									$line1 = $resultProperties["line1"];
									$line2 = $resultProperties["line2"];
									$city = $resultProperties["city"];
									$state = $resultProperties["state"];
									$country = $resultProperties["country"];
									$postcode = $resultProperties["postcode"];
									$notes = $resultProperties["catatan"];
									$parentsUnitAddress = "";
									$fullAddress = "";
									if($block != "")
										$parentsUnitAddress = " Blok ".strtoupper($block);
									if($line1 != "" && $block != "")
										$parentsUnitAddress .=  "," . $line1;
									else
										$parentsUnitAddress .=  $line1;
									if($line2 != "")
										$parentsUnitAddress .= "," .$line2;
									if($postcode != "")
										$parentsUnitAddress .= "," . $postcode;
									if($city != "")
										$parentsUnitAddress .= "," . $city;
									if($state != "")
										$parentsUnitAddress .= "," .$state;
									if($country != "")
										$parentsUnitAddress .= "," . $country ;
									
									if($parentsUnitNo != "")
										$fullAddress = $parentsUnitNo . ", " . $parentsUnitAddress;
									
									$childUnitNo = $resultProperties["childUnitNo"];
									$childUnitID = $resultProperties["childUnitID"];
									$type = $resultProperties["property_type"];
									$unit_status = $resultProperties["status"];
									$unit_vacancy = $resultProperties["vacancy"];
									if($unit_status == 'disable')
										$unit_status = "Unavailable";
									else if($unit_status == 'enable')
										$unit_status = "Available";

	?>                          <tr>
									<td><?php echo $no;?></td>
									<td><?php echo $parentsUnitNo;?></td>
									<td><?php echo $fullAddress;?></td>
									<td><?php echo $type;?></td>
									<td><?php echo $notes;?></td>
									<td><?php echo $unit_status;?></td>
									<td>
										<button type="button"  onclick="viewDetails('<?php echo $parentsUnitId?>','<?php echo $childUnitID?>','<?php echo $_SESSION["projectLocation"]?>');" class="btn btn-outline-primary btn-sm">View Details</button>
										<button type="button"  onclick="confirmFunction('<?php echo $childUnitID?>','<?php echo $childUnitNo?>','<?php echo $unit_status?>','<?php echo $locationName?>','<?php echo $_SESSION["projectLocation"]?>');" class="btn btn-outline-warning btn-sm">Change Status</button>
										<button type="button"  onclick="JavaScript:newPopup('changeProject.php?childUnitID=<?php echo $childUnitID?>&childUnitNo=<?php echo $childUnitNo?>&locationName=<?php echo $locationName?>&projectLocation=<?php echo $_SESSION["projectLocation"]?>&username=<?php echo $_SESSION["username"]?>&staff_id=<?php echo $_SESSION["staff_id"]?>')" class="btn btn-outline-warning btn-sm">Change Project</button>
										<button type="button"  onclick="history('<?php echo $childUnitID?>')"  class="btn btn-outline-success btn-sm">History</button>
										<button type="button"  onclick="editProperty('<?php echo $childUnitID?>','<?php echo $parentsUnitId?>','<?php echo $_SESSION["projectLocation"]?>')"  class="btn btn-outline-warning btn-sm">Edit</button>
										<button type="button"  onclick="deleteProperty('<?php echo $name; echo $company?>','<?php echo $unit?>','<?php echo $tenant_id?>')"  class="btn btn-outline-danger btn-sm">Delete</button>
										
										
										<input type="button" value=""  />		
										<input type="button" value=""  />		
									</td>
								</tr>
							<?php 
								$no++;
								} 
							}else{
								?>
								<tr>
									<td colspan = "8" align="center" >~No Record Found!~</td>
								</tr>
								
								<?php 
							}
							?>
							   
							</tbody>
						</table>
					</div>
				</div>
			</div>
			
		   <!--main content middle end-->
			<!--main content bottom start-->
				<?PHP include "main-content-bottom.php";?>
			<!--main content bottom end-->
			
            <!-- END MAIN CONTENT-->
            <!-- END PAGE CONTAINER-->
        </div>
    </div>
    <?PHP include "footerLink.php";?>
</body>
</html>
<!-- end document-->
