2025-06-06 17:14:52 +08:00

170 lines
6.5 KiB
PHP

<link rel="stylesheet" href="../../index.css" type="text/css" />
<script src="../../home.js"></script>
<style>
body{
overflow: auto;
}
</style>
<?php
include "../ConnectDB.php";
// Start the session
session_start();
// Check if the user is already logged in
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
if (isset($_SESSION['user_type']) && $_SESSION['user_type'] === 'admin') {
$sql = "SELECT * FROM inventory";
$result = mysqli_query($conn, $sql);
$row = mysqli_num_rows($result);
if ($row <= 0) {
echo '<h3>Nothing Here</h3>';
}
//buttons
echo '<br><br><br><br><br><br><br><br>';
echo '<div id="grid">';
echo '<table style="text-align: left;">';
echo '<tr>';
echo "<td><button id='addfruit' onclick='addfruit()'>ADD</button></td>";
echo "<td width='20px'></td>";
echo '<form action="update.php" method="POST">';
echo "<td><button id='update' type='submit' style='text-align: center;'>SAVE</button></td>";
echo '</tr>';
echo '</table>';
//modify area
while ($row_data = mysqli_fetch_array($result)) {
echo '<table id="items" style="text-align: center;">';
echo '<tr>';
echo '<th>Image </th>';
echo '<td colspan="2">';
echo '<img src="data:image/jpeg;base64,' . base64_encode($row_data['img']) . '" title="' . $row_data['name'] . '" id="' . $row_data['name'] . '" style="width:30%" onclick="changeimg(this)">';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th width="40%">Name </th>';
echo '<td width="60%" colspan="2">';
echo '<input name="data['.$row_data['name'].'][name]" value="'.$row_data['name'].'" style="width: 90%;">';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Short description </th>';
echo '<td colspan="2">';
echo '<textarea name="data['.$row_data['name'].'][simpledesc]">'.$row_data['simpledesc'].'</textarea>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Full description </th>';
echo '<td style="overflow: auto;" colspan="2">';
echo '<textarea name="data['.$row_data['name'].'][fulldesc]">'.$row_data['fulldesc'].'</textarea>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Inventory </th>';
echo '<td width="max-content">';
echo '<input name="data['.$row_data['name'].'][quantities]" value="'.$row_data['quantities'].'">';
echo '</td>';
echo '<td>kg</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Buyer price </th>';
echo '<td>¥</td>';
echo '<td width="max-content">';
echo '<input name="data['.$row_data['name'].'][buyer_price]" id="buyer_price" value="'.$row_data['buyer_price'].'">';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Seller price </th>';
echo '<td>¥</td>';
echo '<td width="max-content">';
echo '<input name="data['.$row_data['name'].'][seller_price]" id="seller_price" value="'.$row_data['seller_price'].'">';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>buyer availability </th>';
echo '<td><input type="radio" name="data['.$row_data['name'].'][buyer_availability]" value="1" '.($row_data['buyer_availability'] == '1' ? 'checked' : '').'></td>';
echo '<td><input type="radio" name="data['.$row_data['name'].'][buyer_availability]" value="0" '.($row_data['buyer_availability'] == '0' ? 'checked' : '').' ></td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '<td>available</td>';
echo '<td>not available</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Seller availability </th>';
echo '<td><input type="radio" name="data['.$row_data['name'].'][seller_availability]" value="1" '.($row_data['seller_availability'] == '1' ? 'checked' : '').' ></td>';
echo '<td><input type="radio" name="data['.$row_data['name'].'][seller_availability]" value="0" '.($row_data['seller_availability'] == '0' ? 'checked' : '').' ></td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '<td>available</td>';
echo '<td>not available</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="3" id="' . $row_data['name'] . '" onclick="deletefruit(this)" style="cursor: hand; text-align: center; color: white; background-color: red; border-radius: 10px">DELETE</td>';
echo '</tr>';
echo '<input type="hidden" name="data['.$row_data['name'].'][id]" value="'.$row_data['name'].'">';
echo '</table>';
}
echo '</form>';
echo '</div>';
}
else {
echo "<script type='text/javascript'>
alert('Only admin can visit!');
</script>";
}
}
else {
echo "<script type='text/javascript'>
window.location.href = '../../index.html';
</script>";
}
echo '<dialog class="dialog">';
echo'<form method="dialog">';
echo'<p>Are you sure to delete this fruit?</p>';
echo'<br>';
echo'<button class="dialogbutton" type="submit" value="Yes">Yes</button>';
echo'&nbsp&nbsp&nbsp';
echo'<button class="dialogbutton" id="cancel-button">No</button>';
echo'</form>';
echo'<br>';
echo'</dialog>';
mysqli_close($conn);
?>
<script>
function addfruit(){
makeIFrame("./fruit_add.php", "login", "overlay");
}
function deletefruit(tag){
var name = tag.id;
var dialog = document.querySelector('dialog');
dialog.showModal();
dialog.addEventListener('close', function(event) {
var targetButton = event.target.returnValue;
if (targetButton === "Yes") {
window.location.href = "fruit_del.php?name=" + name;
}
});
}
function changeimg(tag) {
var name = tag.id;
makeIFrame("./changeimg.php?name=" + name, "login", "overlay");
}
</script>