created admin dashboard-rooms

This commit is contained in:
Juthatip McDevitt 2024-06-18 23:30:42 -05:00
parent 03c2db44cb
commit 9d0b741ed6
2 changed files with 227 additions and 0 deletions

View file

@ -0,0 +1,34 @@
<?php
require('../components/utils.php');
require('../components/db_config.php');
adminLogin();
if(isset($_POST['add_room'])){
$accommodation = filteration(json_decode($_POST['accommodation']));
$additional_accomm = filteration(json_decode($_POST['additional_accomm']));
$frm_data = filteration($_POST);
$flag = 0;
$q1 = "INSERT INTO `rooms`(`name`, `area`, `price`, `quantity`, `adult`, `children`, `detail`) VALUES (?, ?, ?, ?, ?, ?, ?)";
$values = [$frm_data['name'], $frm_data['area'], $frm_data['price'], $frm_data['quantity'], $frm_data['adult'], $frm_data['children'], $frm_data['detail']];
if(insert($q1, $values, 'siiiiis')){
$flag = 1;
}
$room_id = mysqli_insert_id($con);
$q2 = "INSERT INTO `room_accommodation`(`room_id`, `accommodation_id`) VALUES (?, ?)";
if($stmt = mysqli_prepare($con, $q2)){
foreach($accommodation as $a){
mysqli_stmt_bind_param($stmt, 'ii', $room_id, $a);
mysqli_stmt_execute($stmt);
}
mysqli_stmt_close($stmt);
} else{
$flag = 0;
die('Query cannot be prepared - insert');
}
//----->Next: create table for room additional accommodation and write function (same as accommodation)
}
?>

View file

@ -0,0 +1,193 @@
<?php
require('components/utils.php');
require('components/db_config.php');
adminLogin();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard-rooms | Midtown Hotel</title>
<?php require('components/link.php') ?>
</head>
<body>
<?php require('components/sidebar.php')?>
<div class="container-fluid" id="dashboard-body">
<div class="row">
<div class="col-lg-10 ms-auto p-4 overflow-hidden">
<h4 class="mb-4">Rooms</h4>
<!--add room-->
<div class="card mb-4">
<div class="card-body">
<div class="mb-4 text-end">
<button type="button" class="btn-third" data-bs-toggle="modal" data-bs-target="#add-room">Add</button>
</div>
<div class="table-responsive-lg" style="height: 400px; overflow-y: scroll;">
<table class="table table-hover border">
<thead>
<tr style="background-color: #D3D3D3;">
<th scope="col">#</th>
<th scope="col">Room</th>
<th scope="col">Area</th>
<th scope="col">Guest</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Status</th>
<th scope="col">Modify</th>
</tr>
</thead>
<tbody id="room_data">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!--add room modal-->
<div class="modal fade" id="add-room" data-bs-backdrop="static" data-bs-keyboard="true" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" style="height: 650px; overflow-y: scroll;">
<form id="add_room_form" autocomplete="off">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add Room</h5>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label" style="font-weight: 600;">Room Type</label>
<input type="text" name="name" class="form-control shadow-none" required/>
</div>
<div class="col-md-6 mb-3">
<label class="form-label" style="font-weight: 600;">Area</label>
<input type="number" min="1" name="area" class="form-control shadow-none" required/>
</div>
<div class="col-md-6 mb-3">
<label class="form-label" style="font-weight: 600;">Price</label>
<input type="number" min="1" name="price" class="form-control shadow-none" required/>
</div>
<div class="col-md-6 mb-3">
<label class="form-label" style="font-weight: 600;">Quantity</label>
<input type="number" min="1" name="quantity" class="form-control shadow-none" required/>
</div>
<div class="col-md-6 mb-3">
<label class="form-label" style="font-weight: 600;">Adult(maximum)</label>
<input type="number" min="1" name="adult" class="form-control shadow-none" required/>
</div>
<div class="col-md-6 mb-3">
<label class="form-label" style="font-weight: 600;">Children(Maximum)</label>
<input type="number" min="1" name="children" class="form-control shadow-none" required/>
</div>
<div class="col-12 mb-3">
<label class="form-label" style="font-weight: 600;">Accommodation</label>
<div class="row">
<?php
$res = selectAll('accommodation');
while($opt = mysqli_fetch_assoc($res)){
echo"
<div class='col-md-12 mb-1'>
<label>
<input type='checkbox' name='accommodation' value='$opt[id]' class='form-check-input'>
$opt[name]
</label>
</div>
";
}
?>
</div>
</div>
<div class="col-12 mb-3">
<label class="form-label" style="font-weight: 600;">Additional Accommodation</label>
<div class="row">
<?php
$res = selectAll('additional_accomm');
while($opt = mysqli_fetch_assoc($res)){
echo"
<div class='col-md-6 mb-1'>
<label>
<input type='checkbox' name='additional_accomm' value='$opt[id]' class='form-check-input'>
$opt[name]
</label>
</div>
";
}
?>
</div>
</div>
<div class="col-12 mb-3">
<label class="form-label" style="font-weight: 600;">Detail</label>
<textarea class="form-control" name="detail" rows="3" style="resize: none;" required></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="reset" class="btn-cancle" data-bs-dismiss="modal">Cancle</button>
<button type="submit" class="btn-third">Submit</button>
</div>
</div>
</form>
</div>
</div>
<?php require('components/script.php') ?>
<script>
let add_room_form = document.getElementById('add_room_form');
add_room_form.addEventListener('submit', function(e){
e.preventDefault();
add_room();
});
function add_room(){
let data = new FormData();
data.append('add_room', '');
data.append('name', add_room_form.elements['name'].value);
data.append('area', add_room_form.elements['area'].value);
data.append('price', add_room_form.elements['price'].value);
data.append('quantity', add_room_form.elements['quantity'].value);
data.append('adult', add_room_form.elements['adult'].value);
data.append('children', add_room_form.elements['children'].value);
data.append('detail', add_room_form.elements['detail'].value);
let accommodation = [];
add_room_form.elements['accommodation'].forEach(el => {
if(el.checked){
accommodation.push(el.value);
}
});
let additional_accomm = [];
add_room_form.elements['additional_accomm'].forEach(el => {
if(el.checked){
additional_accomm.push(el.value);
}
});
data.append('accommodation', JSON.stringify(accommodation));
data.append('additional_accomm', JSON.stringify(additional_accomm));
let xhr = new XMLHttpRequest();
xhr.open("POST", "ajax/room.php", true);
xhr.onload = function(){
console.log(this.responseText);
var myModal = document.getElementById('add-room');
var modal = bootstrap.Modal.getInstance(myModal);
modal.hide()
if(this.responseText == 1){
alert('success', 'New room is added');
add_room_form.reset();
} else{
alert('error', 'Fail to update new accommodation')
}
}
xhr.send(data);
}
</script>
</body>
</html>