Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Finish
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldStarPro committed Dec 8, 2023
1 parent a448be5 commit 47216ad
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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");
Expand All @@ -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);
}

}
4 changes: 4 additions & 0 deletions src/main/java/io/hardingadonis/miu/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 41 additions & 25 deletions src/main/webapp/assets/js/admin/order-admin.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]);
//});

7 changes: 4 additions & 3 deletions src/main/webapp/view/admin/dashboard-admin.jsp
Original file line number Diff line number Diff line change
@@ -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"%>
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -84,7 +85,7 @@
</div>
<div class="content">
<div class="text">TOTAL CUSTOMERS</div>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">15</span>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">${Singleton.userDAO.count()}</span>
</div>
</div>
</a>
Expand All @@ -97,7 +98,7 @@
</div>
<div class="content">
<div class="text">TOTAL PRODUCTS</div>
<span class="number count-to" data-from="0" data-to="1000" data-speed="1000" data-fresh-interval="20">50</span>
<span class="number count-to" data-from="0" data-to="1000" data-speed="1000" data-fresh-interval="20">${Singleton.productDAO.count()}</span>
</div>
</div>
</a>
Expand All @@ -110,7 +111,7 @@
</div>
<div class="content">
<div class="text">TOTAL ORDERS</div>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">30</span>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">${Singleton.orderDAO.count()}</span>
</div>
</div>
</a>
Expand Down
18 changes: 10 additions & 8 deletions src/main/webapp/view/admin/order-admin.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<thead>
<tr>
<th>ID</th>
<th>User ID</th>
<th>User Full Name</th>
<th>Total Price</th>
<th>Payment</th>
<th>Status</th>
Expand All @@ -103,14 +103,14 @@
<c:forEach var="c" items="${Singleton.orderDAO.getAll()}">
<tr>
<td>${c.ID}</td>
<td>${c.userID}</td>
<td>${Singleton.userDAO.get(c.userID).getFullName()}</td>
<td>${c.totalPrice}</td>
<td>${c.payment}</td>
<td>${c.status}</td>
<td>
<a href="#" class="btn btn-info btn-tiny" title="Edit">
<button href="#" class="btn btn-info btn-tiny" title="Edit" onclick="openEditOrderStatusModal(${c.ID})">
<i class="fa fa-pencil"></i>
</a>
</button>
</td>
</tr>
</c:forEach>
Expand Down Expand Up @@ -152,10 +152,10 @@
<div class="form-group">
<label for="editStatus">Status:</label>
<select class="form-select" id="editStatus">
<option value="PROCESSING">PROCESSING</option>
<option value="SHIPPING">SHIPPING</option>
<option value="DONE">DONE</option>
<option value="CANCELED">CANCELED</option>
<option value="processing">PROCESSING</option>
<option value="shipping">SHIPPING</option>
<option value="done">DONE</option>
<option value="canceled">CANCELED</option>
</select>
</div>
</form>
Expand Down Expand Up @@ -183,6 +183,8 @@
<script src="<%=request.getContextPath()%>/assets/js/admin/chart-pie-demo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/simple-datatables.min.js"></script>
<script src="<%=request.getContextPath()%>/assets/js/admin/datatables-simple-demo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

</body>
</html>

0 comments on commit 47216ad

Please sign in to comment.