<?php
	//find user id
	$ownerId = $_GET['owner'] ? $_GET['owner'] : NULL;
?>
<div class="right_col" role="main">
    <div class="">
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Room Detail</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">

						<form class="form-horizontal form-label-left" method="post">

							
							  <div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Owner</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
								  <select class="form-control" name="ownerId">
									  <?php
									  	$result = mysqli_query($conn, "SELECT * FROM owners ORDER BY o_name ASC");
									  	while($row = mysqli_fetch_assoc($result)){
									  ?>
									<option value="<?php echo $row['o_id']; ?>" <?php select($row['o_id'], $ownerId); ?>><?php echo $row['o_name']; ?></option>
									  <?php } ?>
								  </select>
								</div>
								 <a href="?pagecontent=owner_add"><button type="button" class="btn btn-primary">New Owner</button></a>
							  </div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomtype">Room Type <span class="required">*</span></label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select class="form-control" name="roomtype">
										<option value="1">Studio Bedroom</option>
										<option value="2">One Bedroom</option>
										<option value="3">Two Bedroom</option>
										<option value="4">Three Bedroom</option>
										<option value="5">Duplex Bedroom</option>
									</select>
								</div>
							</div>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Room No <span class="required">*</span></label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="roomno" type="text">
								</div>
							</div>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="nopax">No pax <span class="required">*</span></label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="nopax" class="form-control col-md-7 col-xs-12" name="nopax" type="number">
								</div>
							</div>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomplace">Property <span class="required">*</span>
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select class="form-control" name="propertyId">
										<option>Choose option</option>
										<?php $result = mysqli_query($conn,"SELECT * FROM properties");
										while($row = mysqli_fetch_assoc($result)){
                                            // if(filterPropertyAdmin($row['pty_id'],$_SESSION['admin']['a_id'])==true){continue;};
                                            
                                            ?>
										<option value="<?php echo $row['pty_id']; ?>"><?php echo $row['pty_title']; ?></option>
										<?php } ?>
									</select>
								</div>
							</div>
						
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="nopax">Rental <span class="required">*</span></label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="nopax" class="form-control col-md-7 col-xs-12" name="rental" type="number" value="0">
								</div>
							</div>
						
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Profit Sharing<span class="required">*</span></label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="profitSharingPercent" type="text" value="0">
								</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" name="submit" class="btn btn-success">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>
        </div>
    </div>
</div>

<?php

	if(isset($_POST['submit']))
	{
		$ownerId = $_POST['ownerId'];
		$roomtype = $_POST['roomtype'];
		$roomno = $_POST['roomno'];
		$nopax = $_POST['nopax'];
		$propertyId = $_POST['propertyId'];
		$rental = $_POST['rental'];
		$profitSharingPercent = $_POST['profitSharingPercent'];
		
		$adminId = $_SESSION['admin']['a_id'];
		
		mysqli_query($conn, "INSERT INTO rooms (o_id, r_type, r_no, r_pax, p_id, r_rental, r_profit_sharing, a_id)
		VALUES ($ownerId, $roomtype, '$roomno', $nopax, $propertyId, $rental, $profitSharingPercent, $adminId)") or die (mysql_error($conn));
		
		header('Location: index.php?pagecontent=rooms');
		
		//TELEGRAM NOTIFICATION
		$adminUsername = $_SESSION['admin']['a_username'];
		
$messaggio = "NEW ROOM ADDED BY ".$adminUsername."

Owner : ".$ownerId."
Type : ".getDetailFromTable('room_type',$roomtype)[1]."
Room No : ".$roomno."
Pax : ".$nopax."
Property : ".getDetailFromTable('properties',$propertyId)[1]."
Rental : ".$rental."
Profit Sharing : ".($profitSharingPercent*100)."%

".$ipAddress;

sendMessage($chatID, $messaggio, $token);
	}
?>