diff --git a/public/brand-list.css b/public/brand-list.css index 71a760e6b268003cced83df223e945b6cd77bcbb..747c3c2afc564b28f355878b64743b850d4c698e 100644 --- a/public/brand-list.css +++ b/public/brand-list.css @@ -238,3 +238,18 @@ #deletePopup .popup-content .cancel-btn:hover { background-color: #5a6268; /* สีเทาเข้มเมื่อ hover */ } +/* ปุ่มย้อนกลับ */ +.back-btn { + display: inline-block; + padding: 10px 20px; + background-color: #007bff; + color: white; + border-radius: 6px; + text-decoration: none; + margin-bottom: 20px; + transition: background-color 0.3s ease; +} + +.back-btn:hover { + background-color: #0056b3; +} diff --git a/routes/index.js b/routes/index.js index 69ba8041c30e91820e814464dc9dc4a9e03bca07..f65cfc5889d9bb8541dcfe3977f753041a516254 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,7 +2,7 @@ const express = require('express'); const router = express.Router(); const authController = require('../controllers/authController'); const productController = require('../controllers/productController'); - +const db = require('../config/db'); // เส้นทางสำหรับหน้า register router.get('/register', (req, res) => { res.render('register'); // แสดงฟอร์มการสมัครสมาชิก @@ -80,8 +80,41 @@ router.get('/supplier-list', supplierController.getSupplierList); router.get('/edit-supplier/:id', supplierController.editSupplierForm); router.post('/edit-supplier/:id', supplierController.updateSupplier); +// เส้นทางการลบผู้จัดจำหน่าย +// routes/index.js +// Route สำหรับลบผู้จัดจำหน่าย +router.get('/delete-supplier/:id', (req, res) => { + const { id } = req.params; + + // คำสั่ง SQL เพื่อทำการลบข้อมูล + const query = 'DELETE FROM suppliers WHERE id = ?'; + + db.execute(query, [id]) + .then(() => { + res.redirect('/supplier-list'); // เปลี่ยนเส้นทางไปที่ supplier-list หลังการลบ + }) + .catch((err) => { + console.error(err); + res.status(500).send('Error deleting supplier'); + }); +}); + -router.get('/delete-supplier/:id', supplierController.deleteSupplier); +// routes/index.js +router.post('/delete-supplier/:id', (req, res) => { + const { id } = req.params; + + // ลบข้อมูลจากฐานข้อมูล + db.query('DELETE FROM suppliers WHERE id = ?', [id], (err, result) => { + if (err) { + console.log(err); + return res.status(500).send('Error deleting supplier'); + } + + // หลังลบ, เปลี่ยนเส้นทางไปที่ supplier-list + res.redirect('/supplier-list'); + }); +}); const brandController = require('../controllers/brandController'); @@ -101,6 +134,22 @@ router.get('/edit-brand/:id', brandController.editBrandForm); router.post('/edit-brand/:id', brandController.updateBrand); // Route สำหรับลบแบรนด์ -router.get('/delete-brand/:id', brandController.deleteBrand); +// Route สำหรับลบแบรนด์ +router.get('/delete-brand/:id', (req, res) => { + const { id } = req.params; + + // คำสั่ง SQL เพื่อทำการลบข้อมูลแบรนด์ + const query = 'DELETE FROM brands WHERE id = ?'; + + db.execute(query, [id]) + .then(() => { + res.redirect('/brand-list'); // หลังจากลบ, เปลี่ยนเส้นทางไปที่ brand-list + }) + .catch((err) => { + console.error(err); + res.status(500).send('Error deleting brand'); + }); +}); + module.exports = router; diff --git a/views/brand-list.ejs b/views/brand-list.ejs index 910262b6e1b44f596bf1d9bdd5c7bb1c152b7ea4..ac044600412968dcfa362b7a18917ccec09cf882 100644 --- a/views/brand-list.ejs +++ b/views/brand-list.ejs @@ -26,6 +26,9 @@ <div class="brand-list-container"> <h1>Brand List</h1> + <!-- ปุ่มย้อนกลับ --> + <a href="/product" class="back-btn">Back to Dashboard</a> + <a href="/create-brand" class="create-brand-btn">Create New Brand</a> <table class="brand-table"> @@ -52,34 +55,29 @@ </div> <script> - function confirmDelete(productId) { - // แสดงป๊อปอัพเมื่อผู้ใช้คลิกลบ + function confirmDelete(brandId) { document.getElementById('deletePopup').style.display = 'flex'; - // เก็บรหัสสินค้าเพื่อใช้ในการลบ - window.productToDelete = productId; + window.brandToDelete = brandId; } - + function cancelDelete() { - // ซ่อนป๊อปอัพเมื่อผู้ใช้เลือกยกเลิก document.getElementById('deletePopup').style.display = 'none'; } - - function deleteProduct() { - // ไปที่ URL การลบสินค้าด้วย ID ที่ถูกเก็บไว้ - window.location.href = `/delete/${window.productToDelete}`; + + function deleteBrand() { + window.location.href = `/delete-brand/${window.brandToDelete}`; } </script> - + <!-- ป๊อปอัพยืนยันการลบ --> <div id="deletePopup"> <div class="popup-content"> - <h3>Are you sure you want to delete this product?</h3> + <h3>Are you sure you want to delete this brand?</h3> <p>This action cannot be undone. Please confirm your decision.</p> - <button onclick="deleteProduct()">Yes, Delete</button> + <button onclick="deleteBrand()">Yes, Delete</button> <button class="cancel-btn" onclick="cancelDelete()">Cancel</button> </div> </div> - </body> </html> diff --git a/views/supplier-list.ejs b/views/supplier-list.ejs index 41361901243519e2d8423295123bda876f1ed834..49395440a3964fd83be352f3510ce23e784f6fc1 100644 --- a/views/supplier-list.ejs +++ b/views/supplier-list.ejs @@ -25,7 +25,7 @@ <h1>Supplier List</h1> <!-- ปุ่มย้อนกลับ --> - <a href="/" class="back-btn">Back to Dashboard</a> + <a href="/product" class="back-btn">Back to Dashboard</a> <a href="/create-supplier" class="create-supplier-btn">Create New Supplier</a> @@ -48,44 +48,52 @@ <td><%= supplier.address %></td> <td><%= supplier.phone %></td> <td><%= supplier.email %></td> - <td class="action-buttons"> + <td> + <!-- ปุ่มแก้ไข --> <a href="/edit-supplier/<%= supplier.id %>" class="edit-btn">Edit</a> + + <!-- ปุ่มลบ --> <a href="javascript:void(0);" class="delete-btn" onclick="confirmDelete('<%= supplier.id %>')">Delete</a> </td> </tr> <% }) %> + </tbody> </table> </div> <script> - function confirmDelete(productId) { - // แสดงป๊อปอัพเมื่อผู้ใช้คลิกลบ - document.getElementById('deletePopup').style.display = 'flex'; - // เก็บรหัสสินค้าเพื่อใช้ในการลบ - window.productToDelete = productId; - } - - function cancelDelete() { - // ซ่อนป๊อปอัพเมื่อผู้ใช้เลือกยกเลิก - document.getElementById('deletePopup').style.display = 'none'; - } - - function deleteProduct() { - // ไปที่ URL การลบสินค้าด้วย ID ที่ถูกเก็บไว้ - window.location.href = `/delete/${window.productToDelete}`; - } + function confirmDelete(supplierId) { + // แสดงป๊อปอัพเมื่อผู้ใช้คลิกลบ + document.getElementById('deletePopup').style.display = 'flex'; + // เก็บรหัสผู้จัดจำหน่ายเพื่อใช้ในการลบ + window.supplierToDelete = supplierId; +} + +function cancelDelete() { + // ซ่อนป๊อปอัพเมื่อผู้ใช้เลือกยกเลิก + document.getElementById('deletePopup').style.display = 'none'; +} + +function deleteSupplier() { + // ไปที่ URL การลบผู้จัดจำหน่ายด้วย ID ที่ถูกเก็บไว้ + window.location.href = `/delete-supplier/${window.supplierToDelete}`; +} + + </script> <!-- ป๊อปอัพยืนยันการลบ --> - <div id="deletePopup"> - <div class="popup-content"> - <h3>Are you sure you want to delete this product?</h3> - <p>This action cannot be undone. Please confirm your decision.</p> - <button onclick="deleteProduct()">Yes, Delete</button> - <button class="cancel-btn" onclick="cancelDelete()">Cancel</button> - </div> + <!-- ป๊อปอัพยืนยันการลบ --> +<div id="deletePopup"> + <div class="popup-content"> + <h3>Are you sure you want to delete this supplier?</h3> + <p>This action cannot be undone. Please confirm your decision.</p> + <button onclick="deleteSupplier()">Yes, Delete</button> <!-- เปลี่ยนเป็น deleteSupplier() --> + <button class="cancel-btn" onclick="cancelDelete()">Cancel</button> </div> +</div> + </body>