/* 搜尋區 */
.new-order-list-search-wrap {
margin-bottom: 16px;
}
.new-order-list-search-input {
width: 100%;
padding: 8px 12px;
font-size: 1rem;
border-radius: 4px;
border: 1px solid #ccc;
box-sizing: border-box;
}
.new-order-list-search-results {
position: relative;
max-height: 200px;
overflow-y: auto;
border: 1px solid #ddd;
background: white;
margin-top: 4px;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
z-index: 50;
}
.new-order-list-search-item {
padding: 6px 10px;
font-size: 0.9rem;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.new-order-list-search-item:hover {
background-color: #f0f8ff;
}
.new-order-list-search-type {
background: #eee;
color: #666;
font-size: 0.75rem;
padding: 2px 6px;
border-radius: 20px;
margin-left: 8px;
flex-shrink: 0;
}
/* 日期分組 */
.new-order-list-date-group {
margin-bottom: 32px;
}
.new-order-list-date-title {
font-weight: 700;
font-size: 1.0rem;
color: #003366;
margin-bottom: 12px;
}
/* 賣家分組 */
.new-order-list-seller-subgroup {
margin-bottom: 20px;
border-bottom: 1px solid #ddd;
padding-bottom: 16px;
}
.new-order-list-seller-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
}
.new-order-list-seller-icon {
width: 40px;
height: 40px;
border-radius: 50%;
object-fit: cover;
flex-shrink: 0;
}
.new-order-list-seller-name {
font-weight: 700;
font-size: 1.1rem;
color: #003366;
}
/* 商品列表 /
.new-order-list-product-list {
display: flex;
flex-direction: column;
gap: 6px;
}
.new-order-list-product-item {
display: grid;
grid-template-columns: 60px auto 160px;
grid-template-rows: auto auto auto;
column-gap: 10px;
background: #f9fafb;
border-radius: 5px;
padding: 8px 10px;
font-size: 0.85rem;
min-height: 72px;
user-select:none;
position: relative;
}
.new-order-list-product-image {
grid-row: span 3;
width: 60px;
height: 60px;
border-radius: 6px;
object-fit: contain;
box-shadow: 0 0 4px rgb(0 0 0 / 0.1);
}
/ 商品名稱與規格 /
.new-order-list-product-name-qty {
grid-column: 2 /span 2;
grid-row: 1;
display: flex;
flex-direction: column;
justify-content: center;
color: #111;
word-break: break-word;
font-weight: 600;
}
.new-order-list-product-spec {
font-weight: normal;
font-size: 0.8rem;
color: #555;
margin-top: 2px;
}
/ 商品數量及總額 /
.new-order-list-product-qty-price {
grid-column: 2 /span 2;
grid-row: 2;
display: flex;
align-items: center;
gap: 12px;
font-weight: 600;
color: #004085;
font-size: 0.9rem;
user-select:none;
}
.new-order-list-product-qty-price > span:first-child {
font-weight: 500;
color: #333;
}
/ 商品狀態與預計送達時間—位於商品格右上角 /
.new-order-list-product-status-wrapper {
position: absolute;
top: 8px;
right: 8px;
font-weight: 700;
font-size: 0.85rem;
user-select: none;
color: #003366;
display: flex;
flex-direction: column;
align-items: end;
}
.new-order-list-product-status-label {
margin-bottom: 4px;
}
/ 狀態圓圈置中 */
.new-order-list-product-status-label > .status-circle {
display: block;
width: 20px;
height: 20px;
margin: 0 auto 4px;
border-radius: 50%;
border: 2px solid transparent;
}
.status-circle--past {
background-color: #28a745;
border-color: #28a745;
}
.status-circle--current {
background-color: #003366;
border-color: #003366;
}
.status-circle--future {
background-color: #bababa;
border-color: #bababa;
}
.new-order-list-product-eta {
font-weight: normal;
font-size: 0.75rem;
font-style: italic;
color: #555;
white-space: nowrap;
margin-top: 0;
grid-row: 4;
grid-column: 1 / span 4;
text-align: end;
}
/* 按鈕欄位 */
.new-order-list-product-buttons-wrap {
grid-column: 2 / span 3;
grid-row: 5;
display: flex;
justify-content: flex-end;
align-items: center;
gap: 8px;
float: right;
padding-top: 5px;
}
.new-order-list-btn {
padding: 6px 12px;
font-size: 0.85rem;
font-weight: 600;
border-radius: 6px;
border: none;
cursor: pointer;
user-select:none;
transition: background-color 0.4s ease;
min-width: 80px;
white-space: nowrap;
}
.new-order-list-btn:active {
transform: scale(0.95);
}
.new-order-list-btn-confirm {
background-color: #007bff;
color: white;
}
.new-order-list-btn-confirm:hover {
background-color: #0056b3;
}
.new-order-list-btn-review {
background-color: #28a745;
color: white;
}
.new-order-list-btn-review:hover {
background-color: #1e7e34;
}
.new-order-list-btn-detail {
background-color: #6c757d;
color: white;
}
.new-order-list-btn-detail:hover {
background-color: #495057;
}
/* Popup 主要容器 */
.new-order-list-popup-backdrop {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.4);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
user-select:none;
}
.new-order-list-popup-container {
background: white;
border-radius: 8px;
max-width: 960px;
max-height: 90vh;
width: 90vw;
display: flex;
flex-direction: column;
box-shadow: 0 3px 30px rgb(0 0 0 / 0.3);
}
/* 頂部訂單資訊 */
.new-order-list-popup-top-info {
padding: 16px 20px 8px;
border-bottom: 1px solid #ccc;
display: flex;
flex-direction: column;
gap: 4px;
font-size: 0.85rem;
}
.new-order-list-popup-top-info > div {
display: flex;
font-weight: 600;
}
.new-order-list-popup-top-info label {
color: #003366;
width: 140px;
min-width: 140px;
}
.new-order-list-popup-top-info .value {
flex: 1;
color: #222;
word-break: break-word;
}
.new-order-list-popup-btn-pay {
margin-top: 12px;
align-self: flex-start;
font-weight: 600;
font-size: 0.9rem;
background-color: #dc3545;
color: white;
border-radius: 6px;
padding: 6px 12px;
cursor: pointer;
}
.new-order-list-popup-btn-pay:hover {
background-color: #b02a37;
}
/* 狀態路線圖容器 /
.new-order-list-popup-status-col {
flex-shrink: 0;
padding: 14px 20px 0;
overflow-x: auto;
user-select:none;
border-top: 1px solid #ccc;
position: relative;
}
.new-order-list-popup-status-list {
display: flex;
justify-content: center; / 置中 */
align-items: center;
gap: 0px;
position: relative;
padding-left: 12px;
padding-right: 12px;
}
.new-order-list-popup-status-list::before {
display: none;
}
/* 狀態點和文字 /
.new-order-list-popup-status-node {
display: flex;
flex-direction: column;
align-items: center;
position: relative;
z-index: 1;
min-width: 56px;
user-select:none;
}
.new-order-list-popup-status-label {
font-size: 0.85rem;
font-weight: 600;
user-select:none;
}
.new-order-list-popup-status-circle {
width: 20px;
height: 20px;
border-radius: 50%;
border: 2px solid transparent;
margin-bottom: 4px; / 縮短距離,讓圖示跟文字更接近 */
display: block;
}
.new-order-list-popup-status-circle--past {
background-color: #28a745;
border-color: #28a745;
}
.new-order-list-popup-status-circle--current {
background-color: #003366;
border-color: #003366;
}
.new-order-list-popup-status-circle--future {
background-color: #bababa;
border-color: #bababa;
}
.new-order-list-popup-status-text-black {
color: black;
}
.new-order-list-popup-status-text-past {
color: #28a745;
}
.new-order-list-popup-status-text-current {
color: #003366;
}
/* 狀態詳情折合區 */
.new-order-list-status-details-toggle {
cursor: pointer;
user-select: none;
font-size: 0.9rem;
font-weight: 600;
color: #003366;
margin: 10px 20px 0;
text-align: center;
background: none;
border: none;
width: 100%;
}
.new-order-list-status-details-toggle:hover {
color: #0056b3;
}
.new-order-list-status-details-wrap {
padding: 0px 20px 20px;
max-height: 0;
overflow: hidden;
transition: max-height 0.4s ease;
font-size: 0.85rem;
color: #555;
user-select:none;
}
.new-order-list-status-details-wrap.open {
max-height: 500px;
}
.new-order-list-status-detail-item {
display: flex;
justify-content: space-between;
padding: 2px 0;
border-bottom: 1px solid #ddd;
}
.new-order-list-status-detail-name {
font-weight: 600;
color: #003366;
}
.new-order-list-status-detail-time {
font-style: italic;
}
/* 商品詳情:商店Logo與名稱改置於商品圖片上方 */
.new-order-list-popup-detail-seller {
margin-top: 8px;
margin-bottom: 8px;
display: flex;
align-items: center;
gap: 8px;
}
.new-order-list-popup-detail-seller img {
width: 36px;
height: 36px;
border-radius: 50%;
object-fit: cover;
border: 1.5px solid #003366;
}
.new-order-list-popup-detail-seller-name {
font-weight: 700;
font-size: 1rem;
color: #003366;
}
/* 商品圖片與右方詳情排版 */
.new-order-list-popup-detail-product {
display: flex;
gap: 16px;
align-items: flex-start;
user-select: none;
}
.new-order-list-popup-detail-product img {
width: 120px;
height: 80px;
border-radius: 8px;
object-fit: contain;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.1);
}
.new-order-list-popup-detail-info {
flex: 1;
display: flex;
flex-direction: column;
}
.new-order-list-popup-detail-name {
font-weight: 700;
font-size: 1rem;
color: #111;
}
.new-order-list-popup-detail-spec {
font-size: 0.85rem;
color: #555;
margin-top: 6px;
}
.new-order-list-popup-detail-qtyprice {
margin-top: 10px;
font-weight: 600;
font-size: 1rem;
color: #004085;
user-select:none;
}
/* 其他商品區 */
.new-order-list-other-products {
border-top: 1px solid #ccc;
padding-top: 8px;
}
.new-order-list-other-products-toggle {
font-weight: 600;
color: #004085;
background: none;
border: none;
cursor: pointer;
font-size: 0.95rem;
user-select:none;
padding: 4px 0;
}
.new-order-list-other-products-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.35s ease;
}
.new-order-list-other-products-content.open {
max-height: 500px;
margin-top: 8px;
}
.new-order-list-other-product-item {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 6px;
background: #f9fafb;
border-radius: 5px;
padding: 6px 8px;
font-size: 0.85rem;
user-select:none;
}
.new-order-list-other-product-image {
width: 40px;
height: 40px;
object-fit: contain;
border-radius: 4px;
}
.new-order-list-other-product-name {
font-weight: 600;
color: #111;
flex: 1 1 auto;
word-break: break-word;
}
.new-order-list-other-product-qty {
color: #666;
}
.new-order-list-other-product-total {
font-weight: 600;
color: #004085;
}
/* 關閉按鈕 */
.new-order-list-popup-close-btn {
margin-top: auto;
align-self: flex-end;
padding: 6px 12px;
font-size: 0.9rem;
font-weight: 700;
border-radius: 6px;
border: none;
background-color: #dc3545;
color: white;
cursor: pointer;
user-select:none;
flex-shrink: 0;
transition: background-color 0.3s ease;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
margin-top: 10px;
float: right;
}
.new-order-list-popup-close-btn:hover {
background-color: #b02a37;
}
.new-order-list-popup-details {
padding: 0px 20px 20px 20px;
}
/* 響應式 */
@media screen and (max-width: 768px) {
.new-order-list-popup-container {
flex-direction: column;
width: 95vw;
max-height: 95vh;
}
.new-order-list-popup-status-col {
border-top: 1px solid #ccc;
flex-shrink: 0;
padding: 10px 10px 16px;
overflow-x: auto;
}
.new-order-list-popup-details {
max-height: 50vh;
overflow-y: auto;
}
}
@media screen and (max-width: 400px) {
.new-order-list-product-item {
grid-template-columns: 50px auto 108px;
min-height: 68px;
}
.new-order-list-product-image {
width: 50px;
height: 50px;
grid-row: span 3;
}
.new-order-list-product-name-qty {
font-size: 0.75rem;
}
.new-order-list-product-spec {
font-size: 0.75rem;
}
.new-order-list-product-qty-price {
font-size: 0.75rem;
}
.new-order-list-product-eta {
font-size: 0.75rem;
}
.new-order-list-btn {
font-size: 0.75rem;
padding: 5px 10px;
min-width: 72px;
}
.new-order-list-popup-status-label{
font-size: 0.75rem;
}
.new-order-list-popup-status-node {
min-width: 46px;
}
}
// 範例資料
let orderData = [
{
seller_id: "seller_001",
seller_name: "雙子商店",
seller_icon: "https://i.imgur.com/2KXw2Mz.png ",
orders: [
{
purchase_date: "2025-06-19 10:00",
order_number: "ORD20250619001",
payment_method: "信用卡",
payment_voucher: "VOUCHER0001",
payment_date: "2025-06-19 10:30",
products: [
{
id: "p_101",
name: "雙子A物品",
spec: "紅色 / L碼",
total_price: 350.5,
quantity: 1,
image: "https://i.imgur.com/1iDh3v3.jpg ",
status: 4,
eta: null,
confirmed: false,
status_times: {
0: "2025-06-18 08:57",
1: "2025-06-18 09:00",
2: "2025-06-18 11:00",
3: "2025-06-20 13:00",
4: "2025-06-21 15:00",
5: null,
},
},
{
id: "p_103",
name: "雙子物品",
spec: "白色 / M碼",
total_price: 521.5,
quantity: 1,
image: "https://i.imgur.com/P0N3DBN.jpg ",
status: 4,
eta: null,
confirmed: true,
status_times: {
0: "2025-06-18 08:57",
1: "2025-06-18 09:00",
2: "2025-06-18 11:00",
3: "2025-06-20 13:00",
4: "2025-06-21 15:00",
5: null,
},
},
],
},
{
purchase_date: "2025-06-17 09:00",
order_number: "ORD20250617001",
payment_method: "ATM轉帳",
payment_voucher: "VOUCHER0002",
payment_date: "2025-06-17 09:30",
products: [
{
id: "p_102",
name: "雙子B物品",
spec: "藍色 / XL碼",
total_price: 999.9,
quantity: 2,
image: "https://i.imgur.com/P0N3DBN.jpg ",
status: 2,
eta: "2025-06-22 14:00",
confirmed: false,
status_times: {
0: "2025-06-17 09:00",
1: "2025-06-18 11:00",
2: "2025-06-19 08:30",
3: null,
4: null,
},
},
],
},
],
},
{
seller_id: "seller_002",
seller_name: "射手商店",
seller_icon: "https://i.imgur.com/4I5yrvG.png ",
orders: [
{
purchase_date: "2025-06-17 08:00",
order_number: "ORD20250617002",
payment_method: "信用卡",
payment_voucher: "VOUCHER0003",
payment_date: "2025-06-17 08:15",
products: [
{
id: "p_201",
name: "射手B物品",
spec: "綠色 / M碼",
total_price: 150.5,
quantity: 3,
image: "https://i.imgur.com/1nxKY4R.jpg ",
status: 3,
eta: "2025-06-20 16:00",
confirmed: false,
status_times: {
0: "2025-06-17 08:00",
1: "2025-06-17 09:00",
2: "2025-06-18 10:00",
3: "2025-06-19 16:00",
4: null,
5: null,
},
},
],
},
],
},
{
seller_id: "seller_003",
seller_name: "網上商店",
seller_icon: "https://i.imgur.com/4I5yrvG.png ",
orders: [
{
purchase_date: "2025-06-17 08:00",
order_number: "ORD20250617002",
payment_method: "信用卡",
payment_voucher: "VOUCHER0003",
payment_date: "2025-06-17 08:15",
products: [
{
id: "p_201",
name: "網上商店C物品",
spec: "綠色 / M碼",
total_price: 150.5,
quantity: 3,
image: " ",
status: 3,
eta: "2025-06-20 16:00",
confirmed: false,
status_times: {
0: "2025-06-17 08:00",
1: "2025-06-18 09:00",
2: "2025-06-19 10:00",
3: "2025-06-21 16:00",
4: null,
5: null,
},
},
],
},
{
purchase_date: "2025-06-17 08:00",
order_number: "ORD20250617003",
payment_method: null,
payment_voucher: null,
payment_date: null,
products: [
{
id: "p_204",
name: "網上商店物品",
spec: "綠色 / M碼",
total_price: 150.5,
quantity: 3,
image: " ",
status: 0,
eta: null,
confirmed: false,
status_times: {
0: "2025-06-17 08:00",
1: null,
2: null,
3: null,
4: null,
5: null,
},
},
],
},
],
},
];
// DOM Elements
const container = document.getElementById("new-order-list-content");
const searchInput = document.getElementById("new-order-list-search-input");
const searchResultsDiv = document.getElementById("new-order-list-search-results");
// 格式化日期 (僅年月日)
function formatDateOnly(dateStr) {
if (!dateStr) return "";
const d = new Date(dateStr.replace(/-/g, "/"));
if (isNaN(d)) return dateStr;
return ${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日;
}
// 格式化完整日期時間(年月日 HH:mm)
function formatDateTime(dtStr) {
if (!dtStr) return "";
const d = new Date(dtStr.replace(/-/g, "/"));
if (isNaN(d)) return dtStr;
return (
${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日 +
${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}
);
}
// 格式化價格HKD最多1位小數
function formatPriceHKD(value) {
return "HK$" + (Math.round(value * 10) / 10).toFixed(1);
}
// 建立商品項目
function createProductItem(product, sellerName, orderInfo) {
const item = document.createElement("div");
item.className = "new-order-list-product-item";
item.setAttribute(
"aria-label",
${product.name},${product.spec || "無規格"},數量${product.quantity},目前狀態:${STATUS_ARR[product.status]}
);
code Copy code
// 商品圖片
const img = document.createElement("img");
img.className = "new-order-list-product-image";
img.src = product.image;
img.alt = product.name;
img.loading = "lazy";
item.appendChild(img);
// 名稱與規格
const nameQtyDiv = document.createElement("div");
nameQtyDiv.className = "new-order-list-product-name-qty";
const nameSpan = document.createElement("span");
nameSpan.textContent = product.name;
nameQtyDiv.appendChild(nameSpan);
if (product.spec) {
const specSpan = document.createElement("span");
specSpan.className = "new-order-list-product-spec";
specSpan.textContent = product.spec;
nameQtyDiv.appendChild(specSpan);
}
item.appendChild(nameQtyDiv);
// 數量與價格
const qtyPriceDiv = document.createElement("div");
qtyPriceDiv.className = "new-order-list-product-qty-price";
const qtySpan = document.createElement("span");
qtySpan.textContent = `${product.quantity} 件`;
qtyPriceDiv.appendChild(qtySpan);
const priceSpan = document.createElement("span");
priceSpan.textContent = `共${formatPriceHKD(product.total_price)}`;
qtyPriceDiv.appendChild(priceSpan);
item.appendChild(qtyPriceDiv);
// 狀態與預計送達時間(右上方)
const statusWrapper = document.createElement("div");
statusWrapper.className = "new-order-list-product-status-wrapper";
const statusLabel = document.createElement("div");
statusLabel.className = "new-order-list-product-status-label";
statusLabel.textContent = STATUS_ARR[product.status];
statusWrapper.appendChild(statusLabel);
item.appendChild(statusWrapper);
// 按鈕區
const btnsDiv = document.createElement("div");
btnsDiv.className = "new-order-list-product-buttons-wrap";
if (product.status === 4) {
const confirmBtn = document.createElement("button");
confirmBtn.type = "button";
confirmBtn.className = "new-order-list-btn new-order-list-btn-confirm";
confirmBtn.textContent = "確定收貨";
confirmBtn.setAttribute("aria-label", `確定收貨:${product.name}`);
confirmBtn.addEventListener("click", () => {
alert(`您已確定收貨:${product.name}`);
product.status = 4;
product.confirmed = true;
renderCurrentList();
});
btnsDiv.appendChild(confirmBtn);
}
if (product.status === 5) {
const reviewBtn = document.createElement("button");
reviewBtn.type = "button";
reviewBtn.className = "new-order-list-btn new-order-list-btn-review";
reviewBtn.textContent = "寫評價";
reviewBtn.setAttribute("aria-label", `寫評價:${product.name}`);
reviewBtn.addEventListener("click", () => {
alert(`跳轉寫評價頁面:${product.name}`);
});
btnsDiv.appendChild(reviewBtn);
}
// 查看詳情
const detailBtn = document.createElement("button");
detailBtn.type = "button";
detailBtn.className = "new-order-list-btn new-order-list-btn-detail";
detailBtn.textContent = "查看詳情";
detailBtn.setAttribute("aria-label", `查看詳情:${product.name}`);
detailBtn.addEventListener("click", () => openPopup(product, sellerName, orderInfo));
btnsDiv.appendChild(detailBtn);
if (product.status === 3 && product.eta) {
const etaSpan = document.createElement("div");
etaSpan.className = "new-order-list-product-eta";
etaSpan.textContent = `預計於${product.eta}送達`;
item.appendChild(etaSpan);
}
item.appendChild(btnsDiv);
return item;
}
// 篩選訂單資料以便搜尋
function filterOrdersByKeyword(keyword) {
if (!keyword) return orderData;
const k = keyword.trim().toLowerCase();
if (!k) return orderData;
code Copy code
const grouped = {};
orderData.forEach(({ seller_id, seller_name, seller_icon, orders }) => {
orders.forEach((order) => {
const dateKey = order.purchase_date.split(" ")[0];
const filteredProds = order.products.filter(
(prod) =>
prod.name.toLowerCase().includes(k) ||
seller_name.toLowerCase().includes(k)
);
if (filteredProds.length > 0) {
if (!grouped[dateKey]) grouped[dateKey] = {};
if (!grouped[dateKey][seller_id]) {
grouped[dateKey][seller_id] = {
seller_name,
seller_icon,
products: [],
purchase_date: order.purchase_date,
order_number: order.order_number,
payment_method: order.payment_method,
payment_voucher: order.payment_voucher,
payment_date: order.payment_date,
};
}
grouped[dateKey][seller_id].products.push(...filteredProds);
}
});
});
return grouped;
}
// 渲染依日期→賣家→商品
function renderOrderListByDateSeller(groupedData) {
container.innerHTML = "";
const dateKeys = Object.keys(groupedData).sort((a, b) => new Date(b) - new Date(a));
dateKeys.forEach((dateKey) => {
const dateGroup = document.createElement("div");
dateGroup.className = "new-order-list-date-group";
code Copy code
const dateTitle = document.createElement("div");
dateTitle.className = "new-order-list-date-title";
dateTitle.textContent = formatDateOnly(dateKey);
dateGroup.appendChild(dateTitle);
const sellers = Object.entries(groupedData[dateKey]).sort((a, b) =>
a[1].seller_name.localeCompare(b[1].seller_name)
);
sellers.forEach(([sellerId, sellerData]) => {
const sellerGroup = document.createElement("div");
sellerGroup.className = "new-order-list-seller-subgroup";
const sellerHeader = document.createElement("div");
sellerHeader.className = "new-order-list-seller-header";
const sellerIcon = document.createElement("img");
sellerIcon.className = "new-order-list-seller-icon";
sellerIcon.src = sellerData.seller_icon;
sellerIcon.alt = `${sellerData.seller_name} 商店圖示`;
sellerHeader.appendChild(sellerIcon);
const sellerNameH3 = document.createElement("h3");
sellerNameH3.className = "new-order-list-seller-name";
sellerNameH3.textContent = sellerData.seller_name;
sellerHeader.appendChild(sellerNameH3);
sellerGroup.appendChild(sellerHeader);
const productList = document.createElement("div");
productList.className = "new-order-list-product-list";
sellerData.products.forEach((prod) => {
productList.appendChild(createProductItem(prod, sellerData.seller_name, sellerData));
});
sellerGroup.appendChild(productList);
dateGroup.appendChild(sellerGroup);
});
container.appendChild(dateGroup);
});
}
// 初始渲染(不篩選)
let currentFilteredData = null;
function renderCurrentList() {
const grouped = {};
// 在放入產品元素時,確保orderInfo是該訂單資料
orderData.forEach(({seller_id, seller_name, seller_icon, orders}) => {
orders.forEach(order => {
// 以日期為key分組時
const dateKey = order.purchase_date.split(" ")[0];
if(!grouped[dateKey]) grouped[dateKey] = {};
if(!grouped[dateKey][seller_id]){
grouped[dateKey][seller_id] = {
seller_name,
seller_icon,
products: [],
purchase_date: order.purchase_date,
order_number: order.order_number,
payment_method: order.payment_method,
payment_voucher: order.payment_voucher,
payment_date: order.payment_date,
};
}
grouped[dateKey][seller_id].products.push(...order.products);
});
});
currentFilteredData = grouped;
renderOrderListByDateSeller(grouped);
}
// 搜尋相關
function flattenAllSearchItems(data) {
const items = [];
for (const seller of data) {
items.push({
type: "seller",
id: seller.seller_id,
name: seller.seller_name,
ref: seller,
});
for (const order of seller.orders) {
for (const product of order.products) {
items.push({
type: "product",
id: product.id,
name: product.name,
ref: product,
sellerName: seller.seller_name,
});
}
}
}
return items;
}
const allSearchItems = flattenAllSearchItems(orderData);
function searchItems(keyword, offset = 0, limit = 20) {
if (!keyword) return [];
keyword = keyword.trim().toLowerCase();
if (!keyword) return [];
const results = [];
for (let i = 0; i < allSearchItems.length && results.length < limit; i++) {
const item = allSearchItems[i];
const t1 = item.name.toLowerCase();
const t2 = item.sellerName ? item.sellerName.toLowerCase() : "";
if (t1.includes(keyword) || t2.includes(keyword)) results.push(item);
}
return results;
}
function createSearchResultItem(item) {
const div = document.createElement("div");
div.className = "new-order-list-search-item";
div.tabIndex = 0;
div.setAttribute("role", "button");
div.setAttribute("aria-label", ${item.type === "seller" ? "賣家" : "商品"}:${item.name});
code Copy code
const nameSpan = document.createElement("span");
nameSpan.textContent = item.name;
div.appendChild(nameSpan);
const typeBadge = document.createElement("span");
typeBadge.className = "new-order-list-search-type";
typeBadge.textContent = item.type === "seller" ? "賣家" : "商品";
div.appendChild(typeBadge);
div.addEventListener("click", () => {
onSelectSearchItem(item);
});
return div;
}
function clearSearchResults() {
searchResultsDiv.innerHTML = "";
}
function renderSearchResults(items, append = false) {
if (!append) clearSearchResults();
items.forEach((item) => {
searchResultsDiv.appendChild(createSearchResultItem(item));
});
}
function onSelectSearchItem(item) {
clearSearchResults();
currentFilteredData = filterOrdersByKeyword(item.name);
renderOrderListByDateSeller(currentFilteredData);
searchInput.value = "";
}
searchInput.addEventListener("input", (e) => {
const val = e.target.value;
if (!val.trim()) {
clearSearchResults();
currentFilteredData = null;
renderCurrentList();
return;
}
const results = searchItems(val, 0, 20);
renderSearchResults(results);
});
document.addEventListener("click", (e) => {
if (!searchResultsDiv.contains(e.target) && e.target !== searchInput) {
clearSearchResults();
}
});
// openPopup函式
function openPopup(product, sellerName, orderInfo) {
const backdrop = document.createElement("div");
backdrop.className = "new-order-list-popup-backdrop";
backdrop.setAttribute("role", "dialog");
backdrop.setAttribute("aria-modal", "true");
backdrop.setAttribute("aria-label", 商品${product.name} 詳細資訊);
backdrop.tabIndex = -1;
}
// 初始化渲染
renderCurrentList();
})();