<?php 
require_once("header.php");
require_once("session_check.php");

//CODE FOR CHANGE STATUS TO INACTIVE
if(isset($_GET['action']) && $_GET['action'] == "inactive")
{
	$sql_update = "UPDATE `pd_property` SET `property_status` = 'Inactive' WHERE `property_id` = '".$_GET['property_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['change_succ_msg'] = 'Property de-activated successfully.';
	header("location: property_list.php");
	exit;
}

//CODE FOR CHANGE STATUS TO ACTIVE
if(isset($_GET['action']) && $_GET['action'] == "active")
{
	$sql_update = "UPDATE `pd_property` SET `property_status` = 'Active' WHERE `property_id` = '".$_GET['property_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['change_succ_msg'] = 'Property activated successfully.';
	header("location: property_list.php");
	exit;
}

//CODE FOR DELETE PROPERTY
if(isset($_GET['action']) && $_GET['action'] == "delete")
{
	$sql_banner_image1 = "SELECT * FROM `pd_property_photos` WHERE `property_id` = '".$_GET['property_id']."'";
	$exe_banner_image1 = mysqli_query($connection,$sql_banner_image1);
	while($arr_banner_image1 = mysqli_fetch_array($exe_banner_image1))
	{
		unlink("../uploads/property/".$arr_banner_image1['photo']);
	}
			
	mysqli_query($connection,"DELETE FROM `pd_property_photos` WHERE `property_id` = '".$_GET['property_id']."'");
	
	$sql_banner_image = "SELECT * FROM `pd_property` WHERE `property_id` = '".$_GET['property_id']."'";
	$exe_banner_image = mysqli_query($connection,$sql_banner_image);
	$arr_banner_image = mysqli_fetch_array($exe_banner_image);
	unlink("../uploads/property/".$arr_banner_image['cover_photo']);
	
	mysqli_query($connection,"DELETE FROM `pd_property` WHERE `property_id` = '".$_GET['property_id']."'");

	$_SESSION['del_succ_msg'] = 'Property deleted successfully.';
	header("location: property_list.php");
	exit;
}
?>

<?php require_once("left_panel.php");?>
<div class="content">
	<div class="workplace">
	    <?php if(isset($_SESSION['change_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['change_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['change_succ_msg']);
		}
		?>
		<?php if(isset($_SESSION['del_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['del_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['del_succ_msg']);
		}
		?>
		<div class="row-fluid">
			<div class="span12">                    
				<div class="head clearfix">
					<div class="isw-grid"></div>
					<h1>Properties List</h1>                               
				</div>
				<div class="block-fluid table-sorting clearfix">
					<table cellpadding="0" cellspacing="0" width="100%" class="table" id="tSortable">
						<thead>
							<tr>
								<th width="10%">Serial No.</th>
								<th width="35%">Broker Details</th>
								<th width="20%">Property</th>
								<th width="10%">Status</th>
								<th width="25%">Action</th>
							</tr>
						</thead>
						<tbody>
						<?php
						$user_counter = 1;
						$sql_record = "SELECT * FROM `pd_property` ORDER BY `property_id` DESC";
						$exe_record = mysqli_query($connection,$sql_record);
						$num_record = mysqli_num_rows($exe_record);
						if($num_record>0)
						{
							while($fetch_record = mysqli_fetch_array($exe_record))
							{
								$cover_photo = stripslashes($fetch_record['cover_photo']);
								$property_status = $fetch_record['property_status'];
								
								$get_type = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pd_property_type` WHERE `type_id` = '".$fetch_record['type_id']."'"));
    						    $get_subtype = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pd_property_subtype` WHERE `subtype_id` = '".$fetch_record['subtype_id']."'"));
								
								$get_broker = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pd_brokers` WHERE `user_id` = '".$fetch_record['broker_id']."'"));
								
								$name = stripslashes($get_broker['fname'])." ".stripslashes($get_broker['lname']);
								$phone = stripslashes($get_broker['phone']);
								$email = stripslashes($get_broker['email']);

							?>
							<tr>
								<td><?php echo $user_counter;?></td>
								<td><b>Name : </b><?php echo $name;?><br><b>Phone No : </b><?php echo $phone;?><br><b>Email Address : </b><?php echo $email;?></td>
								<td><img src="../uploads/property/<?php echo $cover_photo;?>" alt="" height="100" width="100"><br><strong><?php echo stripslashes($get_type['type']);?> - <?php echo stripslashes($get_subtype['subtype']);?></strong></td>
								<td><?php if($property_status == 'Active') { ?><a href="property_list.php?property_id=<?php echo $fetch_record['property_id']?>&action=inactive" title="Click to inactive" onclick="return confirm('Are you sure you want to inactive this property?');"><ul class="the-icons clearfix"><li><i class="isb-unlock"></i></li></ul></a><?php } else { ?><a href="property_list.php?property_id=<?php echo $fetch_record['property_id']?>&action=active" title="Click to active" onclick="return confirm('Are you sure you want to active this property?');"><ul class="the-icons clearfix"><li><i class="isb-lock"></i></li></ul></a><?php } ?></td>
								<td><button class="btn" type="button" name="back" onclick="window.location.href='property_details.php?property_id=<?php echo $fetch_record['property_id']?>'">View Details</button>&nbsp;&nbsp;<button class="btn" type="button" name="back" onclick="window.location.href='property_enquiry_list.php?property_id=<?php echo $fetch_record['property_id']?>'">Enquiry List</button>&nbsp;&nbsp;<a href="property_list.php?property_id=<?php echo $fetch_record['property_id']?>&action=delete" title="Delete" onclick="return confirm('Are you sure you want to delete this property?');" style="float:left; display:block; margin:0 5px 0 0;"><ul class="the-icons clearfix"><i class="isb-delete"></i></ul></a></td>
							</tr>
							<?php
							$user_counter++;
							}
						}
						else
						{
						?>
						<tr>
							<td><font color="#FF0000">No property found</font></td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>
						<?php
						}
						?>
						</tbody>
					</table>
				</div>
			</div>                                
		</div>
	</div>
</div>

<?php require_once("footer.php"); ?>