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/order-admin.js b/src/main/webapp/assets/js/admin/order-admin.js index d0049b9c..668e46a1 100644 --- a/src/main/webapp/assets/js/admin/order-admin.js +++ b/src/main/webapp/assets/js/admin/order-admin.js @@ -1,9 +1,7 @@ -var selectedRowIndex; +var orderId; -function openEditModal() { +function openEditStatusModal() { $('#editOrderModal').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 @@ -13,36 +11,54 @@ function populateEditModalFields(row) { document.getElementById('editStatus').value = orderStatus; } -function saveChangesEditOrder() { - // Lấy giá trị từ modal - var editedStatus = document.getElementById('editStatus').value; +function openEditOrderStatusModal(id) { + orderId = id; + + openEditStatusModal(); +} - // Kiểm tra giá trị có đọc được không - console.log("Edited Order Status:", editedStatus); +function saveChangesEditOrder() { + let editedStatus = document.getElementById('editStatus').value; - // Cập nhật hàng trong bảng - var rowToUpdate = $('#datatablesSimple tbody tr').eq(selectedRowIndex); - console.log("Row to update:", rowToUpdate); + // Get the order ID from the row or another source - // Kiểm tra có lấy được hàng cần update không - if (rowToUpdate.length > 0) { - rowToUpdate.find('td:eq(4)').text(editedStatus); + const url = contextPath + '/admin/order?id=' + orderId + '&status=' + editedStatus; - // Đóng modal - closeEditModal(); - } else { - console.error("Row to update not found!"); - } + $.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]); -}); +//// 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/dashboard-admin.jsp b/src/main/webapp/view/admin/dashboard-admin.jsp index a8f93daa..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"%> @@ -84,7 +85,7 @@
TOTAL CUSTOMERS
- 15 + ${Singleton.userDAO.count()}
@@ -97,7 +98,7 @@
TOTAL PRODUCTS
- 50 + ${Singleton.productDAO.count()}
@@ -110,7 +111,7 @@
TOTAL ORDERS
- 30 + ${Singleton.orderDAO.count()}
diff --git a/src/main/webapp/view/admin/order-admin.jsp b/src/main/webapp/view/admin/order-admin.jsp index d38b80cd..f6808cae 100644 --- a/src/main/webapp/view/admin/order-admin.jsp +++ b/src/main/webapp/view/admin/order-admin.jsp @@ -92,7 +92,7 @@ ID - User ID + User Full Name Total Price Payment Status @@ -103,14 +103,14 @@ ${c.ID} - ${c.userID} + ${Singleton.userDAO.get(c.userID).getFullName()} ${c.totalPrice} ${c.payment} ${c.status} - + @@ -152,10 +152,10 @@
@@ -183,6 +183,8 @@ + +