Devco1
Home
Console
Upload
New File
New Folder
Tools
Info
About
/
home
/
wifiyecu
/
www
/
admin
/
Filename :
add_product.php
back
Copy
<?php session_start(); include '../includes/config.php'; // التحقق من صلاحيات المدير if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; } // معالجة إضافة المنتج if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; $description = $_POST['description'] ?? ''; $price = floatval($_POST['price'] ?? 0); $old_price = !empty($_POST['old_price']) ? floatval($_POST['old_price']) : null; $brand = $_POST['brand'] ?? ''; $stock_quantity = intval($_POST['stock_quantity'] ?? 0); try { $pdo->beginTransaction(); // إضافة المنتج $stmt = $pdo->prepare(" INSERT INTO products (name, description, price, old_price, brand, stock_quantity) VALUES (?, ?, ?, ?, ?, ?) "); $stmt->execute([$name, $description, $price, $old_price, $brand, $stock_quantity]); $product_id = $pdo->lastInsertId(); // معالجة تحميل الصور if (!empty($_FILES['images']['name'][0])) { $upload_dir = '../uploads/products/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) { if ($_FILES['images']['error'][$key] === UPLOAD_ERR_OK) { $file_name = time() . '_' . uniqid() . '_' . basename($_FILES['images']['name'][$key]); $file_path = $upload_dir . $file_name; if (move_uploaded_file($tmp_name, $file_path)) { $is_main = ($key === 0) ? 1 : 0; // أول صورة تكون رئيسية $image_url = 'uploads/products/' . $file_name; $stmt = $pdo->prepare(" INSERT INTO product_images (product_id, image_url, is_main) VALUES (?, ?, ?) "); $stmt->execute([$product_id, $image_url, $is_main]); } } } } // معالجة الألوان if (!empty($_POST['colors'])) { foreach ($_POST['colors'] as $color) { if (!empty($color['name']) && !empty($color['code'])) { $stmt = $pdo->prepare(" INSERT INTO colors (product_id, color_name, color_code) VALUES (?, ?, ?) "); $stmt->execute([$product_id, $color['name'], $color['code']]); } } } $pdo->commit(); $_SESSION['message'] = [ 'text' => 'تم إضافة المنتج بنجاح', 'type' => 'success' ]; header('Location: products.php'); exit; } catch (PDOException $e) { $pdo->rollBack(); $error = "خطأ في إضافة المنتج: " . $e->getMessage(); } } ?> <!DOCTYPE html> <html lang="ar" dir="rtl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>إضافة منتج جديد - لوحة التحكم</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"> <style> .color-preview { width: 30px; height: 30px; border-radius: 4px; display: inline-block; margin-left: 10px; border: 1px solid #ddd; } .image-preview { max-width: 150px; max-height: 150px; margin: 10px; border-radius: 8px; } </style> </head> <body> <!-- شريط التنقل --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="index.php"> <i class="fas fa-cog"></i> لوحة التحكم </a> <div class="navbar-nav ms-auto"> <a class="nav-link" href="products.php"> <i class="fas fa-arrow-right me-1"></i> رجوع </a> </div> </div> </nav> <div class="container mt-4"> <div class="row justify-content-center"> <div class="col-lg-10"> <div class="card"> <div class="card-header"> <h4 class="card-title mb-0"> <i class="fas fa-plus me-2"></i> إضافة منتج جديد </h4> </div> <div class="card-body"> <?php if (isset($error)): ?> <div class="alert alert-danger"><?php echo $error; ?></div> <?php endif; ?> <form method="POST" enctype="multipart/form-data"> <!-- المعلومات الأساسية --> <div class="row mb-4"> <div class="col-12"> <h5 class="border-bottom pb-2"> <i class="fas fa-info-circle me-2"></i> المعلومات الأساسية </h5> </div> <div class="col-md-6 mb-3"> <label for="name" class="form-label">اسم المنتج *</label> <input type="text" class="form-control" id="name" name="name" required> </div> <div class="col-md-6 mb-3"> <label for="brand" class="form-label">الماركة</label> <input type="text" class="form-control" id="brand" name="brand"> </div> <div class="col-12 mb-3"> <label for="description" class="form-label">الوصف</label> <textarea class="form-control" id="description" name="description" rows="4"></textarea> </div> </div> <!-- الأسعار والمخزون --> <div class="row mb-4"> <div class="col-12"> <h5 class="border-bottom pb-2"> <i class="fas fa-tag me-2"></i> الأسعار والمخزون </h5> </div> <div class="col-md-4 mb-3"> <label for="price" class="form-label">السعر الحالي *</label> <div class="input-group"> <span class="input-group-text">$</span> <input type="number" class="form-control" id="price" name="price" step="0.01" min="0" required> </div> </div> <div class="col-md-4 mb-3"> <label for="old_price" class="form-label">السعر القديم</label> <div class="input-group"> <span class="input-group-text">$</span> <input type="number" class="form-control" id="old_price" name="old_price" step="0.01" min="0"> </div> </div> <div class="col-md-4 mb-3"> <label for="stock_quantity" class="form-label">الكمية في المخزون *</label> <input type="number" class="form-control" id="stock_quantity" name="stock_quantity" min="0" required> </div> </div> <!-- الصور --> <div class="row mb-4"> <div class="col-12"> <h5 class="border-bottom pb-2"> <i class="fas fa-images me-2"></i> صور المنتج </h5> <div class="mb-3"> <label for="images" class="form-label">اختر الصور</label> <input type="file" class="form-control" id="images" name="images[]" multiple accept="image/*"> <div class="form-text">يمكنك اختيار أكثر من صورة. أول صورة ستكون الصورة الرئيسية.</div> </div> <div id="imagePreview" class="d-flex flex-wrap"></div> </div> </div> <!-- الألوان --> <div class="row mb-4"> <div class="col-12"> <h5 class="border-bottom pb-2"> <i class="fas fa-palette me-2"></i> الألوان المتاحة </h5> <div id="colorsContainer"> <div class="color-item row g-2 mb-2"> <div class="col-md-5"> <input type="text" class="form-control" name="colors[0][name]" placeholder="اسم اللون"> </div> <div class="col-md-5"> <input type="color" class="form-control" name="colors[0][code]" value="#007bff"> </div> <div class="col-md-2"> <button type="button" class="btn btn-danger w-100 remove-color" disabled> <i class="fas fa-times"></i> </button> </div> </div> </div> <button type="button" class="btn btn-outline-primary btn-sm" id="addColor"> <i class="fas fa-plus me-1"></i> إضافة لون </button> </div> </div> <!-- أزرار --> <div class="row"> <div class="col-12"> <div class="d-flex gap-2"> <button type="submit" class="btn btn-primary"> <i class="fas fa-save me-2"></i> حفظ المنتج </button> <a href="products.php" class="btn btn-secondary"> <i class="fas fa-times me-2"></i> إلغاء </a> </div> </div> </div> </form> </div> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> <script> // معاينة الصور document.getElementById('images').addEventListener('change', function(e) { const preview = document.getElementById('imagePreview'); preview.innerHTML = ''; for (let file of e.target.files) { if (file.type.startsWith('image/')) { const reader = new FileReader(); reader.onload = function(e) { const img = document.createElement('img'); img.src = e.target.result; img.className = 'image-preview'; preview.appendChild(img); } reader.readAsDataURL(file); } } }); // إدارة الألوان let colorIndex = 1; document.getElementById('addColor').addEventListener('click', function() { const container = document.getElementById('colorsContainer'); const colorItem = document.createElement('div'); colorItem.className = 'color-item row g-2 mb-2'; colorItem.innerHTML = ` <div class="col-md-5"> <input type="text" class="form-control" name="colors[${colorIndex}][name]" placeholder="اسم اللون"> </div> <div class="col-md-5"> <input type="color" class="form-control" name="colors[${colorIndex}][code]" value="#007bff"> </div> <div class="col-md-2"> <button type="button" class="btn btn-danger w-100 remove-color"> <i class="fas fa-times"></i> </button> </div> `; container.appendChild(colorItem); colorIndex++; // تفعيل أزرار الحذف للألوان القديمة document.querySelectorAll('.remove-color').forEach(btn => { btn.disabled = false; }); }); // حذف الألوان document.addEventListener('click', function(e) { if (e.target.classList.contains('remove-color') || e.target.closest('.remove-color')) { const btn = e.target.classList.contains('remove-color') ? e.target : e.target.closest('.remove-color'); btn.closest('.color-item').remove(); } }); // حساب الخصم تلقائياً document.getElementById('price').addEventListener('input', calculateDiscount); document.getElementById('old_price').addEventListener('input', calculateDiscount); function calculateDiscount() { const price = parseFloat(document.getElementById('price').value) || 0; const oldPrice = parseFloat(document.getElementById('old_price').value) || 0; if (oldPrice > price && oldPrice > 0) { const discount = Math.round(((oldPrice - price) / oldPrice) * 100); document.getElementById('discountPreview').textContent = `خصم ${discount}%`; } else { document.getElementById('discountPreview').textContent = ''; } } </script> </body> </html>