<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<table>
					<tr>
						<td width="150px"><h3>Owner list</h3></td>
						<td><a href="index.php?pagecontent=owners&add=y"><button type="button" class="btn btn-dark">Add Owner</button></a></td>
					</tr>
				</table>
			</div>
		</div>
		<div class="clearfix"></div>
		<div class="row">
			<?php if(isset($_REQUEST['update']) || isset($_REQUEST['add'])){ ?>
				<div class="col-md-12 col-sm-12 col-xs-12">
					<div class="x_panel">
						<div class="x_title">
							<h2>Owner/Partner Details</h2>
							<div class="clearfix"></div>
						</div>
						<div class="x_content">

							<form class="form-horizontal form-label-left" method="post">
								<input type="hidden" name="id" value="<?php echo $_GET['update'] ? getDetailByTable('owners',$_GET['update'])['o_id'] : ''; ?>" />

								<div class="item form-group">
									<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Full Name <span class="required">*</span>
									</label>
									<div class="col-md-6 col-sm-6 col-xs-12">
										<input id="name" class="form-control col-md-7 col-xs-12" name="name" placeholder="Farhan Kamarzaman" type="text" value="<?php echo $_GET['update'] ? getDetailByTable('owners',$_GET['update'])['o_name'] : ''; ?>">
									</div>
								</div>
								<div class="item form-group">
									<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Username <span class="required">*</span>
									</label>
									<div class="col-md-6 col-sm-6 col-xs-12">
										<input id="name" class="form-control col-md-7 col-xs-12" name="username" placeholder="farhan" type="text" value="<?php echo $_GET['update'] ? getDetailByTable('owners',$_GET['update'])['o_username'] : ''; ?>">
									</div>
								</div>
								<div class="item form-group">
									<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Password <span class="required">*</span>
									</label>
									<div class="col-md-6 col-sm-6 col-xs-12">
										<input id="name" class="form-control col-md-7 col-xs-12" name="password" placeholder="Farhan Kamarzaman" type="password" value="<?php echo $_GET['update'] ? getDetailByTable('owners',$_GET['update'])['o_password'] : ''; ?>">
									</div>
								</div>
								<div class="item form-group">
									<label class="control-label col-md-3 col-sm-3 col-xs-12" for="contact">Contact
									</label>
									<div class="col-md-6 col-sm-6 col-xs-12">
										<input type="text" id="contact" name="contact" data-validate-length-range="8,20" class="form-control col-md-7 col-xs-12" value="<?php echo $_GET['update'] ? getDetailByTable('owners',$_GET['update'])['o_contact'] : ''; ?>">
									</div>
								</div>
								<div class="ln_solid"></div>
								<div class="form-group">
									<div class="col-md-6 col-md-offset-3">
										<button id="send" type="submit" class="btn btn-success" name="<?php echo $_GET['update'] ? 'updateBtn' : 'submitBtn'; ?>"><?php echo $_GET['update'] ? 'Update' : 'Submit'; ?></button>
										<button type="submit" class="btn btn-primary" onclick="javascript:window.location='index.php?pagecontent=dashboard'">Cancel</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			<?php } ?>

			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table id="datatable" class="table table-striped table-bordered">
							<thead>
								<tr>
									<th>Name</th>
									<th>Contact</th>
									<th>Room</th>
									<th>Property</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$result=mysqli_query($conn, "SELECT * FROM owners");
									while($row = mysqli_fetch_array($result))
									{
								?>
								<tr>
									<td width="200"><?php echo $row['o_name'];?></a></td>
									<td><?php echo $row['o_contact'];?></td>
									<td></td>
									<td></td>
									<td>
										<a href="?pagecontent=owners&update=<?php echo $row['o_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
										<a href="?pagecontent=owners&remove=<?php echo $row['o_id']; ?>" onclick="return confirm('Are you sure you want to remove this owner?');"><button class="btn btn-xs btn-danger">Remove</button></a>
						</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['remove'])){
		
		$id = $_REQUEST['remove'];
		
		mysqli_query($conn,"DELETE FROM owners WHERE o_id=$id") or die (mysql_error());
		
		header('Location: ?pagecontent=owners');  
	}

	if(isset($_POST['submitBtn']))
	{
		$fullname=$_POST['name'];
		$username=$_POST['username'];
		$password=$_POST['password'];
		$contact=$_POST['contact'];

		if(!getDetailByMysqliWhere('owners','o_username',$username)){
			mysqli_query($conn, "INSERT INTO owners (o_name, o_username, o_password, o_contact) 
			VALUES ('$fullname', '$username', '$password', '$contact')");

			header('Location: ?pagecontent=owners&success=New owner added');
		}else{
			header('Location: ?pagecontent=owners&error=Unable to add owner');
		}
	}

	if(isset($_POST['updateBtn']))
	{
		$id=$_POST['id'];
		$fullname=$_POST['name'];
		$username=$_POST['username'];
		$password=$_POST['password'];
		$contact=$_POST['contact'];

		if(mysqli_query($conn, "UPDATE owners SET o_name='$fullname', o_username='$username', o_password='$password', o_contact='$contact' WHERE o_id=$id")){
			header('Location: ?pagecontent=owners&success=Updated');
		}else{
			header('Location: ?pagecontent=owners&error=Unable to update');
		}
	}
?>