updated admin dashboard-rooms functionality

This commit is contained in:
Juthatip McDevitt 2024-06-19 19:56:53 -05:00
parent 106699e720
commit 580ee39acc
2 changed files with 291 additions and 0 deletions

View file

@ -50,4 +50,84 @@
echo 0;
}
}
if(isset($_POST['get_all_rooms'])){
$res = selectAll('rooms');
$i = 0;
$data = "";
while($row = mysqli_fetch_assoc($res)){
if($row['status']==1){
$status = "
<button onclick='toggleStatus($row[id], 0)' class='btn-third'>Active</button>
";
} else{
$status = "
<button onclick='toggleStatus($row[id], 1)' class='btn-cancel'>Inactive</button>
";
}
$data.="
<tr>
<td>$i</td>
<td>$row[name]</td>
<td>$row[area] sq.ft.</td>
<td>
<span style='font-weight:600;'>Adult:</span> $row[adult]</span><br>
<span style='font-weight:600;'>Children:</span> $row[children]
</td>
<td>$$row[price]</td>
<td>$row[quantity]</td>
<td>$status</td>
<td>
<button type='button' onclick='edit_room($row[id])' class='btn-third' data-bs-toggle='modal' data-bs-target='#edit-room'>Edit</button>
</td>
</tr>
";
$i++;
}
echo $data;
}
if(isset($_POST['get_room'])){
$frm_data = filteration($_POST);
$res1 = select("SELECT * FROM `rooms` WHERE `id`=?", [$frm_data['get_room']], 'i');
$res2 = select("SELECT * FROM `room_accommodation` WHERE `room_id`=?", [$frm_data['get_room']], 'i');
$res3 = select("SELECT * FROM `room_additionalAccom` WHERE `room_id`=?", [$frm_data['get_room']], 'i');
$roomdata = mysqli_fetch_assoc($res1);
$accommodation = [];
$additional_accomm = [];
if(mysqli_num_rows($res2)>0){
while($row = mysqli_fetch_assoc($res2)){
array_push($accommodation, $row['accommodation_id']);
}
}
if(mysqli_num_rows($res3)>0){
while($row = mysqli_fetch_assoc($res3)){
array_push($additional_accomm, $row['additionalAccom_id']);
}
}
$data = ["roomdata" => $roomdata, "accommodation" => $accommodation, "additional_accomm" => $additional_accomm];
$data = json_encode($data);
echo $data;
}
if(isset($_POST['edit_room'])){
}
if(isset($_POST['toggleStatus'])){
$frm_data = filteration($_POST);
$q = "UPDATE `rooms` SET `status`=? WHERE `id`=?";
$values = [$frm_data['value'], $frm_data['toggleStatus']];
if(update($q, $values, 'ii')){
echo 1;
} else{
echo 0;
}
}
?>

View file

@ -139,10 +139,102 @@
</div>
</div>
<!--edit room modal-->
<div class="modal fade" id="edit-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="edit_room_form" autocomplete="off">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit 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>
<input type="hidden" name="room_id">
</div>
</div>
<div class="modal-footer">
<button type="reset" class="btn-cancel" data-bs-dismiss="modal">Cancel</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();
@ -188,6 +280,7 @@
if(this.responseText == 1){
alert('success', 'New room is added');
add_room_form.reset();
get_all_rooms();
} else{
alert('error', 'Fail to update new accommodation')
@ -196,7 +289,125 @@
xhr.send(data);
}
function get_all_rooms(){
let xhr = new XMLHttpRequest();
xhr.open("POST", "ajax/room.php", true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function(){
document.getElementById('room_data').innerHTML = this.responseText;
}
xhr.send('get_all_rooms');
}
let edit_room_form = document.getElementById('edit_room_form');
function edit_room(id){
let xhr = new XMLHttpRequest();
xhr.open("POST", "ajax/room.php", true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function(){
let data = JSON.parse(this.responseText);
edit_room_form.elements['name'].value = data.roomdata.name;
edit_room_form.elements['area'].value = data.roomdata.area;
edit_room_form.elements['price'].value = data.roomdata.price;
edit_room_form.elements['quantity'].value = data.roomdata.quantity;
edit_room_form.elements['adult'].value = data.roomdata.adult;
edit_room_form.elements['children'].value = data.roomdata.children;
edit_room_form.elements['detail'].value = data.roomdata.detail;
edit_room_form.elements['room_id'].value = data.roomdata.id;
edit_room_form.elements['accommodation'].forEach(el => {
if(data.accommodation.includes(Number(el.value))){
el.checked = true;
}
});
edit_room_form.elements['additional_accomm'].forEach(el => {
if(data.additional_accomm.includes(Number(el.value))){
el.checked = true;
}
});
}
xhr.send('get_room='+id);
}
edit_room_form.addEventListener('submit', function(e){
e.preventDefault();
submit_edit_room();
});
function submit_edit_room(){
let data = new FormData();
data.append('edit_room', '');
data.append('room_id', edit_room_form.elements['room_id'].value);
data.append('name', edit_room_form.elements['name'].value);
data.append('area', edit_room_form.elements['area'].value);
data.append('price', edit_room_form.elements['price'].value);
data.append('quantity', edit_room_form.elements['quantity'].value);
data.append('adult', edit_room_form.elements['adult'].value);
data.append('children', edit_room_form.elements['children'].value);
data.append('detail', edit_room_form.elements['detail'].value);
let accommodation = [];
edit_room_form.elements['accommodation'].forEach(el => {
if(el.checked){
accommodation.push(el.value);
}
});
let additional_accomm = [];
edit_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('edit-room');
var modal = bootstrap.Modal.getInstance(myModal);
modal.hide()
if(this.responseText == 1){
alert('success', 'Edit');
submit_edit_room.reset();
get_all_rooms();
} else{
alert('error', 'Fail to edit, try again later');
}
}
xhr.send(data);
}
function toggleStatus(id, val){
let xhr = new XMLHttpRequest();
xhr.open("POST", "ajax/room.php", true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function(){
if(this.responseText == 1){
alert('success', 'Active');
get_all_rooms();
} else{
alert('success', 'Inactive');
}
}
xhr.send('toggleStatus='+id+'&value='+val);
}
window.onload = function(){
get_all_rooms();
}
</script>
</body>