diff --git a/src/main/java/io/hardingadonis/miu/controller/admin/OrderAdmin.java b/src/main/java/io/hardingadonis/miu/controller/admin/OrderAdmin.java index 009d1197..6af0f74e 100644 --- a/src/main/java/io/hardingadonis/miu/controller/admin/OrderAdmin.java +++ b/src/main/java/io/hardingadonis/miu/controller/admin/OrderAdmin.java @@ -1,10 +1,13 @@ package io.hardingadonis.miu.controller.admin; import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.OrderStatus; +import io.hardingadonis.miu.services.Singleton; import java.io.IOException; import javax.servlet.*; import javax.servlet.annotation.*; import javax.servlet.http.*; +import org.json.simple.JSONObject; @WebServlet(name = "OrderAdmin", urlPatterns = {"/admin/order"}) public class OrderAdmin extends HttpServlet { @@ -14,7 +17,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); - + HttpSession session = request.getSession(); Admin admin = (Admin) session.getAttribute("admin"); @@ -30,6 +33,35 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("UTF-8"); + response.setContentType("text/html; charset=UTF-8"); + + OrderStatus orderStatus = OrderStatus.create(request.getParameter("status")); + + try { + int id = Integer.parseInt(request.getParameter("id")); + + Order order = Singleton.orderDAO.get(id); + + if (order != null) { + order.setStatus(orderStatus); + + Singleton.orderDAO.update(order); + } + } catch (NumberFormatException ex) { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + + return; + } + + JSONObject jsonResponse = new JSONObject(); + jsonResponse.put("status", "success"); + jsonResponse.put("message", "Status updated successfully"); + + response.setContentType("application/json"); + response.getWriter().write(jsonResponse.toString()); + + response.setStatus(HttpServletResponse.SC_OK); } } diff --git a/src/main/java/io/hardingadonis/miu/model/Order.java b/src/main/java/io/hardingadonis/miu/model/Order.java index f53897cb..213064c7 100644 --- a/src/main/java/io/hardingadonis/miu/model/Order.java +++ b/src/main/java/io/hardingadonis/miu/model/Order.java @@ -17,6 +17,10 @@ public class Order { public Order() { } + public Order(OrderStatus status) { + this.status = status; + } + public Order(int userID, String address, long totalPrice, Payment payment, OrderStatus status) { this.userID = userID; this.address = address; diff --git a/src/main/webapp/assets/js/admin/edit-category.js b/src/main/webapp/assets/js/admin/edit-category.js index 1d37e4e0..46e4370b 100644 --- a/src/main/webapp/assets/js/admin/edit-category.js +++ b/src/main/webapp/assets/js/admin/edit-category.js @@ -1,8 +1,9 @@ - -var categoryId; +var selectedRowIndex; function openEditModal() { $('#editCategoryModal').modal('show'); + // Lưu index của hàng được chọn + selectedRowIndex = $(this).closest('tr').index(); } // Function to populate fields in the edit modal with existing data @@ -12,53 +13,35 @@ function populateEditModalFields(row) { document.getElementById('editCategoryName').value = categoryName; } -function openEditModel(id) { - categoryId = id; - - openEditModal(); -} +function saveChangesEditCategory() { + // Lấy giá trị từ modal + var editedCategoryName = document.getElementById('editCategoryName').value; -function saveCategoryChanges() { - let editedCategoryName = document.getElementById('editCategoryName').value; + // Kiểm tra giá trị có đọc được không + console.log("Edited Category Name:", editedCategoryName); - // Get the category ID from the row or another source + // Cập nhật hàng trong bảng + var rowToUpdate = $('#datatablesSimple tbody tr').eq(selectedRowIndex); + console.log("Row to update:", rowToUpdate); - const url = contextPath + '/admin/category?id=' + categoryId + '&name=' + editedCategoryName; + // Kiểm tra có lấy được hàng cần update không + if (rowToUpdate.length > 0) { + rowToUpdate.find('td:eq(1)').text(editedCategoryName); - $.ajax({ - url: url, - type: "POST", - dataType: "json", - success: function (data) { - if (data.status === "success") { - Swal.fire({ - title: "Success!", - text: "Category information updated successfully!", - icon: "success" - }).then((result) => { - if (result.isConfirmed) { - window.location.reload(); - } - }); - } else { - Swal.fire({ - title: "Oops...", - text: "Something went wrong!", - icon: "error" - }); - } - }, - }); + // Đóng modal + closeEditCategoryModal(); + } else { + console.error("Row to update not found!"); + } } -function closeEditModal() { +// Function to close the edit modal +function closeEditCategoryModal() { $('#editCategoryModal').modal('hide'); } -$(document).on('click', '#cancelEditBtn', function () { - closeEditModal(); -}); - -$(document).on('click', '#saveCategoryChangesBtn', function () { - saveCategoryChanges(); +// Event delegation to handle dynamically added elements +$(document).on('click', '.btn-tiny', function () { + openEditModal.call(this); // Phải gọi hàm openEditModal với this là element được click + populateEditModalFields($(this).closest('tr')[0]); }); diff --git a/src/main/webapp/assets/js/admin/order-admin.js b/src/main/webapp/assets/js/admin/order-admin.js index e69de29b..668e46a1 100644 --- a/src/main/webapp/assets/js/admin/order-admin.js +++ b/src/main/webapp/assets/js/admin/order-admin.js @@ -0,0 +1,64 @@ +var orderId; + +function openEditStatusModal() { + $('#editOrderModal').modal('show'); +} + +// Function to populate fields in the edit modal with existing data +function populateEditModalFields(row) { + var orderStatus = row.cells[4].textContent.trim(); + + document.getElementById('editStatus').value = orderStatus; +} + +function openEditOrderStatusModal(id) { + orderId = id; + + openEditStatusModal(); +} + +function saveChangesEditOrder() { + let editedStatus = document.getElementById('editStatus').value; + + // Get the order ID from the row or another source + + const url = contextPath + '/admin/order?id=' + orderId + '&status=' + editedStatus; + + $.ajax({ + url: url, + type: "POST", + dataType: "json", + success: function (data) { + if (data.status === "success") { + Swal.fire({ + title: "Success!", + text: "Order Status information updated successfully!", + icon: "success" + }).then((result) => { + if (result.isConfirmed) { + window.location.reload(); + } + }); + } else { + Swal.fire({ + title: "Oops...", + text: "Something went wrong!", + icon: "error" + }); + } + }, + }); +} + + +// Function to close the edit modal +function closeEditModal() { + $('#editOrderModal').modal('hide'); +} + +//// Event delegation to handle dynamically added elements +//$(document).on('click', '.btn-tiny', function () { +// openEditModal.call(this); // Phải gọi hàm openEditModal với this là element được click +// populateEditModalFields($(this).closest('tr')[0]); +//}); + diff --git a/src/main/webapp/view/admin/category-admin.jsp b/src/main/webapp/view/admin/category-admin.jsp index 2e988cba..a20bf39e 100644 --- a/src/main/webapp/view/admin/category-admin.jsp +++ b/src/main/webapp/view/admin/category-admin.jsp @@ -231,4 +231,4 @@ - + \ No newline at end of file diff --git a/src/main/webapp/view/admin/dashboard-admin.jsp b/src/main/webapp/view/admin/dashboard-admin.jsp index 36a6b682..0df42e07 100644 --- a/src/main/webapp/view/admin/dashboard-admin.jsp +++ b/src/main/webapp/view/admin/dashboard-admin.jsp @@ -1,5 +1,6 @@ <%@ page contentType="text/html" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@page import="io.hardingadonis.miu.services.Singleton"%> @@ -83,8 +84,8 @@ face
-
TỔNG KHÁCH HÀNG
- 15 +
TOTAL CUSTOMERS
+ ${Singleton.userDAO.count()}
@@ -96,8 +97,8 @@ bookmark
-
TỔNG SẢN PHẨM
- 50 +
TOTAL PRODUCTS
+ ${Singleton.productDAO.count()}
@@ -109,8 +110,8 @@ shopping_cart
-
TỔNG ĐƠN HÀNG
- 30 +
TOTAL ORDERS
+ ${Singleton.orderDAO.count()}
diff --git a/src/main/webapp/view/admin/order-admin.jsp b/src/main/webapp/view/admin/order-admin.jsp index 9f1a50db..f6808cae 100644 --- a/src/main/webapp/view/admin/order-admin.jsp +++ b/src/main/webapp/view/admin/order-admin.jsp @@ -88,29 +88,29 @@ Order Table
- +
- + - + - + @@ -152,10 +152,10 @@
@@ -163,7 +163,7 @@ @@ -183,6 +183,8 @@ + + diff --git a/src/main/webapp/view/admin/user-admin.jsp b/src/main/webapp/view/admin/user-admin.jsp index 3e03b96c..808cbd3a 100644 --- a/src/main/webapp/view/admin/user-admin.jsp +++ b/src/main/webapp/view/admin/user-admin.jsp @@ -97,7 +97,7 @@ - +
IDUser IDUser Full Name Total Price Payment StatusActionsAction
${c.ID}${c.userID}${Singleton.userDAO.get(c.userID).getFullName()} ${c.totalPrice} ${c.payment} ${c.status} - +
Email Address StatusActionsAction