shubraVeil/admin/products.php

238 lines
11 KiB
PHP
Raw Normal View History

2024-12-25 13:05:50 +02:00
<?php
session_start();
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
require_once "config/database.php";
// Handle product deletion
if(isset($_GET['delete'])) {
$id = mysqli_real_escape_string($conn, $_GET['delete']);
mysqli_query($conn, "DELETE FROM products WHERE id = $id");
header("location: products.php");
}
// Handle product addition/editing
if($_SERVER["REQUEST_METHOD"] == "POST") {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
$price = mysqli_real_escape_string($conn, $_POST['price']);
$category = mysqli_real_escape_string($conn, $_POST['category']);
$product_type = mysqli_real_escape_string($conn, $_POST['product_type']);
if(isset($_POST['id'])) {
// Update existing product
$id = mysqli_real_escape_string($conn, $_POST['id']);
$sql = "UPDATE products SET name='$name', description='$description',
price='$price', category='$category', product_type='$product_type' WHERE id=$id";
} else {
// Add new product
if(isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
$target_dir = "../images/products/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$target_file = $target_dir . basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
$image_path = "images/products/" . basename($_FILES["image"]["name"]);
} else {
$image_path = "";
}
$sql = "INSERT INTO products (name, description, price, category, product_type, image)
VALUES ('$name', '$description', '$price', '$category', '$product_type', '$image_path')";
}
mysqli_query($conn, $sql);
header("location: products.php");
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<title>إدارة المنتجات - ShubraVeil</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;700&display=swap" rel="stylesheet">
<style>
body { font-family: 'Tajawal', sans-serif; }
.sidebar {
min-height: 100vh;
background-color: #343a40;
padding-top: 20px;
}
.sidebar a {
color: #fff;
padding: 10px 15px;
display: block;
text-decoration: none;
}
.sidebar a:hover {
background-color: #495057;
}
.main-content {
padding: 20px;
}
.product-image {
max-width: 100px;
max-height: 100px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<!-- Sidebar -->
<nav class="col-md-2 d-none d-md-block sidebar">
<div class="text-center mb-4">
<img src="../images/logo.jpg" alt="Logo" style="max-width: 120px;">
</div>
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link" href="index.php">
<i class="fas fa-home ml-2"></i>
الرئيسية
</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="products.php">
<i class="fas fa-box ml-2"></i>
المنتجات
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="orders.php">
<i class="fas fa-shopping-cart ml-2"></i>
الطلبات
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="settings.php">
<i class="fas fa-cog ml-2"></i>
الإعدادات
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">
<i class="fas fa-sign-out-alt ml-2"></i>
تسجيل الخروج
</a>
</li>
</ul>
</div>
</nav>
<!-- Main content -->
<main role="main" class="col-md-10 mr-auto ml-auto col-lg-10 px-4 main-content">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3">
<h1 class="h2">إدارة المنتجات</h1>
<button class="btn btn-primary" data-toggle="modal" data-target="#addProductModal">
<i class="fas fa-plus ml-2"></i>إضافة منتج جديد
</button>
</div>
<!-- Products Table -->
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>الصورة</th>
<th>اسم المنتج</th>
<th>السعر</th>
<th>الفئة</th>
<th>نوع المنتج</th>
<th>الإجراءات</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($conn, "SELECT * FROM products ORDER BY id DESC");
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><img src='../" . $row['image'] . "' class='product-image' alt='" . $row['name'] . "'></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['product_type'] . "</td>";
echo "<td>
<button class='btn btn-sm btn-info edit-product' data-id='" . $row['id'] . "'>
<i class='fas fa-edit'></i>
</button>
<a href='?delete=" . $row['id'] . "' class='btn btn-sm btn-danger' onclick='return confirm(\"هل أنت متأكد من حذف هذا المنتج؟\")'>
<i class='fas fa-trash'></i>
</a>
</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
<!-- Add Product Modal -->
<div class="modal fade" id="addProductModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">إضافة منتج جديد</h5>
<button type="button" class="close ml-0" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="" method="post" enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group">
<label>اسم المنتج</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="form-group">
<label>الوصف</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
<div class="form-group">
<label>السعر</label>
<input type="number" name="price" class="form-control" step="0.01" required>
</div>
<div class="form-group">
<label>الفئة</label>
<input type="text" name="category" class="form-control">
</div>
<div class="form-group">
<label>نوع المنتج</label>
<select name="product_type" class="form-control" required>
<option value="">اختر نوع المنتج</option>
<option value="essential_oils">الزيوت الأساسية</option>
<option value="fixed_oils">الزيوت الثابتة</option>
<option value="hydrosols">الهيدروسولات العطرية</option>
<option value="natural_cosmetics">مستحضرات تجميل طبيعية</option>
</select>
</div>
<div class="form-group">
<label>صورة المنتج</label>
<input type="file" name="image" class="form-control-file">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">إلغاء</button>
<button type="submit" class="btn btn-primary">حفظ</button>
</div>
</form>
</div>
</div>
</div>
</main>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>