<?php
	//find user id
	$getRoomId = $_GET['room'];
	if(isset($_GET['update'])){$getRoomCardId = $_GET['update'];}
?>
<div class="right_col" role="main">
    <div class="">
        <div class="row">
			
            <?php if(isset($_REQUEST['update'])){ ?>
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Update Access Card details</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">Type</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
								  <select class="form-control" name="type">
									  
									<option value="1" <?php echo select(getDetailByTable('room_cards',$getRoomCardId)['rc_id'],1); ?>>DOOR</option>
									<option value="2" <?php echo select(getDetailByTable('room_cards',$getRoomCardId)['rc_id'],2); ?>>PARKING</option>
									<option value="3" <?php echo select(getDetailByTable('room_cards',$getRoomCardId)['rc_id'],3); ?>>DOOR & PARKING</option>
									  
								  </select>
								</div>
                            </div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Serial No</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
									<input id="roomno" class="form-control col-12" name="serialNo" type="text" value="<?php echo getDetailFromTable('room_cards',$getRoomCardId)['rc_no']; ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
									<textarea id="message" class="form-control" cols="100%" rows="5" name="note"><?php echo urldecode(getDetailFromTable('room_cards',$getRoomCardId)['rc_note']); ?></textarea>
								</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="updateBtn" class="btn btn-success">Update</button>
								</div>
							</div>
						</form>
					</div>
                </div>
            </div>

            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Issue Date</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">

						<form class="form-horizontal form-label-left" method="post">
                            
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Date</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
								  <?php $date = date("Y-m-d"); if($_GET['date']){ $date = $_GET['date'];} ?>
									<input id="roomno" class="form-control col-12" name="date" type="date" value="<?php echo getDetailFromTable('room_cards',$getRoomCardId)['rc_issued_at']; ?>">
							  </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="issueBtn" class="btn btn-success">Update</button>
                                    <a href="?pagecontent=room_cards&room=<?php echo $getRoomId; ?>&update=<?php echo $getRoomCardId; ?>&clearIssue=y"><button class="btn btn-danger">Clear</button></a>
								</div>
							</div>
						</form>
					</div>
                </div>
            </div>

            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>Terminate Date</h2>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">

                        <form class="form-horizontal form-label-left" method="post">
                            
                            
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Date</label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <?php $date = date("Y-m-d"); if($_GET['date']){ $date = $_GET['date'];} ?>
                                    <input id="roomno" class="form-control col-12" name="date" type="date" value="<?php echo getDetailFromTable('room_cards',$getRoomCardId)['rc_terminated_at']; ?>">
                            </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="terminateBtn" class="btn btn-success">Update</button>
                                    <a href="?pagecontent=room_cards&room=<?php echo $getRoomId; ?>&update=<?php echo $getRoomCardId; ?>&clearTerminate=y"><button class="btn btn-danger">Clear</button></a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="x_panel">
                    <div class="x_content">
                        <img src="uploads/cards/<?php echo getDetailByTable('room_cards',$getRoomCardId)['rc_file']; ?>" width="100%">
                    </div>

                    <div class="x_content">

                        <form class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
                                <div class="item form-group">
                                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Upload 
                                    </label>
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <input id="night" class="form-control col-md-3 col-xs-12" name="fileToUpload" type="file" >
                                    </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="uploadBtn">Upload</button>
                                    </div>
                                </div>
                            </form>
                        
                        <?php
                                if(isset($_POST['uploadBtn'])){

                                    if(getDetailByTable("room_cards",$getRoomCardId)['rc_file']){
                                        $roomCardFile = getDetailByTable("room_cards",$getRoomCardId)['rc_file'];
                                        unlink("uploads/cards/".$roomCardFile);
                                    }
                                    
                                    //upload receipt
                                    $uploadedFile = $_FILES['fileToUpload']['tmp_name']; 
                                    $sourceProperties = getimagesize($uploadedFile);
                                    $newFileName = $getRoomCardId."_".time();
                                    $dirPath = "uploads/cards/";
                                    $ext = pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION);
                                    $imageType = $sourceProperties[2];

                                    $filename = $newFileName.".".$ext;
                                    $filePath = $dirPath.$filename;
                                    
                                    switch ($imageType) {
        
        
                                        case IMAGETYPE_PNG:
                                            $imageSrc = imagecreatefrompng($uploadedFile); 
                                            $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                                            imagepng($tmp,$dirPath. $newFileName.".".$ext);
                                            break;           
        
                                        case IMAGETYPE_JPEG:
                                            $imageSrc = imagecreatefromjpeg($uploadedFile); 
                                            $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                                            imagejpeg($tmp,$dirPath. $newFileName.".".$ext);
                                            break;
        
                                        case IMAGETYPE_GIF:
                                            $imageSrc = imagecreatefromgif($uploadedFile); 
                                            $tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                                            imagegif($tmp,$dirPath. $newFileName.".".$ext);
                                            break;
        
                                        default:
                                            echo "Invalid Image type.";
                                            exit;
                                            break;
                                    }
                                    //end


                                    mysqli_query($conn, "UPDATE room_cards SET rc_file='$filename' WHERE rc_id=$getRoomCardId");

                                    $adminName = $_SESSION['admin'][1];
                                    $serialNo = getDetailByTable("room_cards",$getRoomCardId)['rc_no'];
                                    $roomNo = getDetailByTable("rooms",getDetailByTable("room_cards",$getRoomCardId)['r_id'])['r_no'];

                                    sendMessage2(-664044603,$adminName." upload card photo for
Room : ".$roomNo."
Serial No : ".$serialNo."
");
                                    sendPhoto(-664044603,"https://admin.homestay.dzvisual.my/production/".$filePath);

                                    //RECORD LOG
                                    $adminId = $_SESSION['admin'][0];
                                    $logTitle = ucfirst($_SESSION['admin'][1])." upload refund file";
                                    // mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$getDepositId,'$ipAddress',$adminId)");

                                    header("Location: ?pagecontent=room_cards&room=$getRoomId");
                                }
                        ?>
                    </div>
                </div>
            </div>

            <?php }else{ ?>
            
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Add Access Card detail</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">

						<form class="form-horizontal form-label-left" method="post">
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Serial No</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
									<input id="roomno" class="form-control col-12" name="serialNo" type="text">
							  </div>
							</div>

							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Type</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
								  <select class="form-control" name="type">
									  
									<option value="1" <?php echo select(getDetailByTable('room_cards',$getRoomCardId)['rc_id'],1); ?>>DOOR</option>
									<option value="2" <?php echo select(getDetailByTable('room_cards',$getRoomCardId)['rc_id'],2); ?>>PARKING</option>
									<option value="3" <?php echo select(getDetailByTable('room_cards',$getRoomCardId)['rc_id'],3); ?>>DOOR & PARKING</option>
									  
								  </select>
								</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="addBtn" class="btn btn-success">Add</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-6 col-sm-6 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Info</h2>
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">

			<table class="table table-striped">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Description</th>
				  <th>Amount</th>
				</tr>
			  </thead>
			  <tbody>
				<tr>
				  <th scope="row">1</th>
				  <td>Total spent on inventories</td>
				  <td>RM <?php echo totalInventoryAmount($getId); ?></td>
				</tr>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>
			
              <div class="clearfix"></div>

              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Access Card List</h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>

                  <div class="x_content">

                    <div class="table-responsive">
                      <table class="table table-striped jambo_table bulk_action">
                        <thead>
                          <tr class="headings">
                            <th class="column-title">Bil </th>
                            <th class="column-title">Serial No </th>
                            <th class="column-title">Type </th>
                            <th class="column-title">Status </th>
                            <th class="column-title">File</th>
                            <th class="column-title">Issue</th>
                            <th class="column-title">Terminate</th>
                            <th class="column-title">Note</th>
                            <th class="column-title">Admin</th>
                            <th class="column-title no-link last"><span class="nobr">Action</span>
                            </th>
                            <th class="bulk-actions" colspan="7">
                              <a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
                            </th>
                          </tr>
                        </thead>

                        <tbody>
							<?php
								$result=mysqli_query($conn, "SELECT * FROM room_cards WHERE r_id=$getRoomId");
								while($row = mysqli_fetch_array($result)){
							?>
                          <tr class="even pointer">
							  
                            <td class=" "><?php echo ++$no; ?></td>
                            <td class=" "><?php echo $row['rc_no']; ?></td>
                            <td class=" "><?php echo accessCardType($row['rc_type']); ?></td>
                            <td class=" "><?php echo cardStatus($row['rc_activated_at']); ?></td>
                            <td class=" "><a href="?pagecontent=room_cards&room=<?php echo $getRoomId; ?>&update=<?php echo $row['rc_id']; ?>"><?php echo $row['rc_file']; ?></a></td>
                            <td class=" "><?php echo $row['rc_issued_at']; ?></td>
                            <td class=" "><?php echo $row['rc_terminated_at']; ?></td>
                            <td class=" "><?php echo urldecode($row['rc_note']); ?></td>
                            <td class=" "><?php echo getDetailByTable('admin',$row['a_id'])['a_username']; ?></td>
                            <td class=" last">
								<a href="?pagecontent=room_cards&room=<?php echo $getRoomId; ?>&update=<?php echo $row['rc_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
								<a href="?pagecontent=room_cards&room=<?php echo $getRoomId; ?>&remove=<?php echo $row['rc_id']; ?>"><button class="btn btn-xs btn-danger">Remove</button></a>
                            </td>
                          </tr>
							<?php } ?>
                        </tbody>
                      </table>
                    </div>
							
						
                  </div>
                </div>
              </div>
        </div>
    </div>
</div>

<?php
	function status($input){
		switch($input){
			case '0': return "NEW REPORT"; break;
			case '1': return "REVIEW"; break;
			case '2': return "IN PROCESS"; break;
			case '3': return "COMPLETED"; break;
		}
	}

	if(isset($_REQUEST['remove'])){
		
		$getRoomCardId = $_REQUEST['remove'];

		$roomCardFile = getDetailByTable('room_cards',$getRoomCardId)['rc_file'];

		mysqli_query($conn,"DELETE FROM room_cards WHERE rc_id=$getRoomCardId");
        unlink("uploads/cards/".$roomCardFile);
		
		header('Location: index.php?pagecontent=room_cards&room='.$getRoomId);  
	}

	if(isset($_POST['addBtn']))
	{
		$postSeralNo = $_POST['serialNo'];
		$postType = $_POST['type'];
		// $postDescription = urlencode($_POST['description']);
		
		$sessionAdminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "INSERT INTO room_cards (r_id, rc_no, rc_type, a_id)
		VALUES ($getRoomId, '$postSeralNo', '$postType', $sessionAdminId)");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_POST['updateBtn']))
	{
		$postSeralNo = $_POST['serialNo'];
		$postType = $_POST['type'];
		$postDescription = urlencode($_POST['note']);
		
		//$sessionAdminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "UPDATE room_cards SET rc_no='$postSeralNo', rc_type=$postType, rc_note='$postDescription' WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_POST['issueBtn']))
	{
		$postDate = $_POST['date'];
		
		mysqli_query($conn, "UPDATE room_cards SET rc_issued_at='$postDate' WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_REQUEST['clearIssue']))
	{
        $getRoomCardId = $_GET['update'];

		mysqli_query($conn, "UPDATE room_cards SET rc_issued_at = NULL WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_POST['terminateBtn']))
	{
		$postDate = $_POST['date'];
		
		mysqli_query($conn, "UPDATE room_cards SET rc_terminated_at='$postDate' WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_REQUEST['clearTerminate']))
	{
		mysqli_query($conn, "UPDATE room_cards SET rc_terminated_at=NULL WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

    function cardStatus($input){
        if($input){return "Activated";}else{return "Unknown";}
    }

    function imageResize($imageSrc,$imageWidth,$imageHeight) {

        $newImageWidth = 800;
        //$newImageHeight = 800;
        $newImageHeight = ($newImageWidth*$imageHeight)/$imageWidth;

        $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
        imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);

        return $newImageLayer;
    }
?>