<?php
	//find user id
	if(isset($_GET['update'])){
        $getRouterApartmentId = $_GET['update'];
        $getRoomId = getDetailFromTable('router_apartment',$getRouterApartmentId)['r_id'];
    }
?>
<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">Room</label>
								<div class="col-md-4 col-sm-3 col-xs-12">

                                    <select class="form-control" name="room" <?php select($row['r_id'], $roomId); ?>>
                                        <option value="0" <?php echo select(0,$getRoomId); ?>>NO ROOM SELECTED</option>

                                        <?php 
                                            $result=mysqli_query($conn, "SELECT * FROM rooms WHERE r_status=1 ORDER BY p_id ASC, r_no ASC");

                                            while($row = mysqli_fetch_array($result))
                                            {
                                        
                                        ?>
                                        
                                        <option value="<?php echo $row['r_id'];?>" <?php echo select($row['r_id'],$getRoomId); ?>><?php echo $row['r_no'];?> - <?php echo getDetailFromTable('properties',$row['p_id'])[1];?></option>

                                        <?php } ?>
                                    </select>
								</div>
							  </div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Apartment name <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="apartment" type="text" value="<?php if(isset($_GET['update'])){ echo getDetailByTable('router_apartment',$getRouterApartmentId)['ra_title']; } ?>">
								</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"><?php if(isset($_GET['update'])){ echo "Update"; }else{echo "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']))
	{
		$room = $_POST['room'];
		$apartment = $_POST['apartment'];
		
		$adminId = $_SESSION['admin'][0];

        if(isset($_GET['update'])){
            mysqli_query($conn,"UPDATE router_apartment SET ra_title='$apartment', r_id=$room WHERE ra_id=$getRouterApartmentId");
        }else{
            mysqli_query($conn,"INSERT INTO router_apartment (r_id,ra_title,a_id)
            VALUES ($room,'$apartment',$adminId)");
        }

		header('Location: ?pagecontent=router_apartment');
	}
?>