pharmacy-pos-qr-system/backend/templates/admin_products.html
thug0bin a0cbb984e5 perf: 제품 검색 최적화 - 사용약품만 옵션 추가
문제: 전체 CD_GOODS 검색 시 178,232건 스캔 + OUTER APPLY → 6-14초 소요

해결:
- '사용약품만' 체크박스 추가 (기본 활성화)
- IM_total INNER JOIN으로 재고 있는 2,810건만 검색
- OUTER APPLY 제거로 쿼리 단순화

성능: 6.5초 → 1.4초 (4.6배 향상)
2026-03-04 13:57:33 +09:00

1071 lines
38 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>제품 검색 - 청춘약국</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
background: #f8fafc;
-webkit-font-smoothing: antialiased;
color: #1e293b;
}
/* ── 헤더 ── */
.header {
background: linear-gradient(135deg, #7c3aed 0%, #8b5cf6 50%, #a78bfa 100%);
padding: 28px 32px 24px;
color: #fff;
}
.header-nav {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
}
.header-nav a {
color: rgba(255,255,255,0.8);
text-decoration: none;
font-size: 14px;
font-weight: 500;
}
.header-nav a:hover { color: #fff; }
.header h1 {
font-size: 24px;
font-weight: 700;
letter-spacing: -0.5px;
margin-bottom: 6px;
}
.header p {
font-size: 14px;
opacity: 0.85;
}
/* ── 컨텐츠 ── */
.content {
max-width: 1100px;
margin: 0 auto;
padding: 24px 20px 60px;
}
/* ── 플로팅 챗봇 ── */
.chatbot-panel {
position: fixed;
right: 24px;
bottom: 90px;
width: 370px;
background: #fff;
border-radius: 20px;
border: 1px solid #e2e8f0;
box-shadow: 0 8px 40px rgba(0,0,0,0.15);
display: none;
flex-direction: column;
height: 520px;
max-height: calc(100vh - 120px);
z-index: 998;
animation: chatSlideUp 0.3s ease;
}
.chatbot-panel.open {
display: flex;
}
@keyframes chatSlideUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.chatbot-header {
padding: 16px 20px;
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
border-radius: 16px 16px 0 0;
color: #fff;
}
.chatbot-header h2 {
font-size: 16px;
font-weight: 700;
display: flex;
align-items: center;
gap: 8px;
}
.chatbot-header p {
font-size: 12px;
opacity: 0.85;
margin-top: 4px;
}
.chatbot-messages {
flex: 1;
overflow-y: auto;
padding: 16px;
display: flex;
flex-direction: column;
gap: 12px;
}
.chat-message {
max-width: 85%;
padding: 12px 16px;
border-radius: 16px;
font-size: 14px;
line-height: 1.5;
word-break: break-word;
}
.chat-message.user {
background: #8b5cf6;
color: #fff;
align-self: flex-end;
border-bottom-right-radius: 4px;
}
.chat-message.assistant {
background: #f1f5f9;
color: #334155;
align-self: flex-start;
border-bottom-left-radius: 4px;
}
.chat-message.system {
background: #fef3c7;
color: #92400e;
align-self: center;
font-size: 12px;
padding: 8px 16px;
}
.chat-message .products-mentioned {
margin-top: 10px;
padding-top: 10px;
border-top: 1px dashed #cbd5e1;
}
.chat-message .product-chip {
display: inline-block;
background: #10b981;
color: #fff;
padding: 4px 10px;
border-radius: 20px;
font-size: 12px;
margin: 2px 4px 2px 0;
cursor: pointer;
}
.chat-message .product-chip:hover {
background: #059669;
}
.chatbot-input {
padding: 16px;
border-top: 1px solid #e2e8f0;
display: flex;
gap: 10px;
}
.chatbot-input input {
flex: 1;
padding: 12px 16px;
border: 2px solid #e2e8f0;
border-radius: 24px;
font-size: 14px;
font-family: inherit;
transition: all 0.2s;
}
.chatbot-input input:focus {
outline: none;
border-color: #10b981;
box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.1);
}
.chatbot-input button {
width: 44px;
height: 44px;
border-radius: 50%;
background: #10b981;
color: #fff;
border: none;
cursor: pointer;
font-size: 18px;
transition: all 0.2s;
display: flex;
align-items: center;
justify-content: center;
}
.chatbot-input button:hover { background: #059669; }
.chatbot-input button:disabled { background: #94a3b8; cursor: not-allowed; }
.chatbot-suggestions {
padding: 12px 16px;
background: #f8fafc;
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.suggestion-chip {
background: #e0e7ff;
color: #4338ca;
padding: 6px 12px;
border-radius: 16px;
font-size: 12px;
cursor: pointer;
border: none;
transition: all 0.2s;
}
.suggestion-chip:hover {
background: #c7d2fe;
}
.typing-indicator {
display: flex;
gap: 4px;
padding: 12px 16px;
align-self: flex-start;
}
.typing-indicator span {
width: 8px;
height: 8px;
background: #94a3b8;
border-radius: 50%;
animation: typing 1.4s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
30% { transform: translateY(-6px); opacity: 1; }
}
/* ── 챗봇 토글 버튼 (항상 표시) ── */
.chatbot-toggle {
position: fixed;
bottom: 24px;
right: 24px;
width: 60px;
height: 60px;
border-radius: 50%;
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
color: #fff;
border: none;
font-size: 28px;
cursor: pointer;
box-shadow: 0 4px 20px rgba(16, 185, 129, 0.4);
z-index: 999;
display: flex;
align-items: center;
justify-content: center;
transition: transform 0.2s, box-shadow 0.2s;
}
.chatbot-toggle:hover {
transform: scale(1.1);
box-shadow: 0 6px 28px rgba(16, 185, 129, 0.5);
}
.chatbot-toggle.active {
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
box-shadow: 0 4px 20px rgba(239, 68, 68, 0.4);
}
/* 모바일 */
@media (max-width: 640px) {
.chatbot-panel {
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 75vh;
border-radius: 20px 20px 0 0;
}
.chatbot-toggle { bottom: 16px; right: 16px; }
}
/* ── 검색 영역 ── */
.search-section {
background: #fff;
border-radius: 14px;
padding: 24px;
margin-bottom: 20px;
border: 1px solid #e2e8f0;
}
.search-box {
display: flex;
gap: 12px;
}
.search-input {
flex: 1;
padding: 14px 18px;
border: 2px solid #e2e8f0;
border-radius: 12px;
font-size: 16px;
font-family: inherit;
transition: all 0.2s;
}
.search-input:focus {
outline: none;
border-color: #8b5cf6;
box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.1);
}
.search-input::placeholder {
color: #94a3b8;
}
.search-btn {
background: #8b5cf6;
color: #fff;
border: none;
padding: 14px 32px;
border-radius: 12px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.search-btn:hover { background: #7c3aed; }
.search-btn:active { transform: scale(0.98); }
.search-hint {
margin-top: 12px;
font-size: 13px;
color: #94a3b8;
}
.search-hint span {
background: #f1f5f9;
padding: 2px 8px;
border-radius: 4px;
margin-right: 8px;
}
/* ── 결과 카운트 ── */
.result-count {
margin-bottom: 16px;
font-size: 14px;
color: #64748b;
}
.result-count strong {
color: #8b5cf6;
font-weight: 700;
}
/* ── 테이블 ── */
.table-wrap {
background: #fff;
border-radius: 14px;
border: 1px solid #e2e8f0;
overflow: hidden;
}
table {
width: 100%;
border-collapse: collapse;
}
thead th {
background: #f8fafc;
padding: 14px 16px;
font-size: 12px;
font-weight: 600;
color: #64748b;
text-align: left;
border-bottom: 1px solid #e2e8f0;
white-space: nowrap;
}
tbody td {
padding: 16px;
font-size: 14px;
color: #334155;
border-bottom: 1px solid #f1f5f9;
vertical-align: middle;
}
tbody tr:hover { background: #faf5ff; }
tbody tr:last-child td { border-bottom: none; }
/* ── 상품 정보 ── */
.product-name {
font-weight: 600;
color: #1e293b;
margin-bottom: 2px;
}
.product-supplier {
font-size: 12px;
color: #94a3b8;
}
.product-supplier.set {
color: #8b5cf6;
font-weight: 500;
}
/* ── 코드/바코드 ── */
.code {
font-family: 'JetBrains Mono', monospace;
font-size: 12px;
padding: 4px 8px;
border-radius: 6px;
display: inline-block;
}
.code-drug {
background: #ede9fe;
color: #6d28d9;
}
.code-barcode {
background: #d1fae5;
color: #065f46;
}
.code-na {
background: #f1f5f9;
color: #94a3b8;
}
/* ── 가격 ── */
.price {
font-weight: 600;
color: #1e293b;
white-space: nowrap;
}
/* ── 재고 ── */
.stock {
font-weight: 600;
white-space: nowrap;
text-align: center;
}
.stock.in-stock { color: #10b981; }
.stock.out-stock { color: #ef4444; }
/* ── QR 버튼 ── */
.btn-qr {
background: #8b5cf6;
color: #fff;
border: none;
padding: 8px 14px;
border-radius: 8px;
font-size: 12px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
white-space: nowrap;
}
.btn-qr:hover { background: #7c3aed; }
.btn-qr:active { transform: scale(0.95); }
/* ── 빈 상태 ── */
.empty-state {
text-align: center;
padding: 60px 20px;
color: #94a3b8;
}
.empty-state .icon {
font-size: 48px;
margin-bottom: 16px;
}
.empty-state p {
font-size: 15px;
}
/* ── 모달 ── */
.modal-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.5);
z-index: 1000;
justify-content: center;
align-items: center;
}
.modal-overlay.active { display: flex; }
.modal-box {
background: #fff;
border-radius: 16px;
padding: 24px;
max-width: 400px;
width: 90%;
text-align: center;
}
.modal-title {
font-size: 18px;
font-weight: 700;
margin-bottom: 16px;
}
.modal-preview {
margin: 16px 0;
}
.modal-preview img {
max-width: 200px;
border: 1px solid #e2e8f0;
border-radius: 8px;
}
/* ── 수량 선택기 ── */
.qty-selector {
display: flex;
align-items: center;
justify-content: center;
gap: 0;
margin: 20px 0;
}
.qty-btn {
width: 44px;
height: 44px;
border: none;
background: #f1f5f9;
font-size: 24px;
font-weight: 600;
color: #64748b;
cursor: pointer;
transition: all 0.15s;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
}
.qty-btn:first-child { border-radius: 12px 0 0 12px; }
.qty-btn:last-child { border-radius: 0 12px 12px 0; }
.qty-btn:hover { background: #e2e8f0; color: #334155; }
.qty-btn:active { transform: scale(0.95); background: #cbd5e1; }
.qty-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.qty-value {
width: 64px;
height: 44px;
background: #fff;
border: 2px solid #e2e8f0;
border-left: none;
border-right: none;
font-size: 20px;
font-weight: 700;
color: #1e293b;
display: flex;
align-items: center;
justify-content: center;
}
.qty-label {
font-size: 13px;
color: #64748b;
margin-bottom: 8px;
}
.modal-btns {
display: flex;
gap: 12px;
justify-content: center;
margin-top: 20px;
}
.modal-btn {
padding: 10px 24px;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
border: none;
transition: all 0.15s;
}
.modal-btn.cancel { background: #f1f5f9; color: #64748b; }
.modal-btn.cancel:hover { background: #e2e8f0; }
.modal-btn.confirm { background: #8b5cf6; color: #fff; }
.modal-btn.confirm:hover { background: #7c3aed; }
/* ── 반응형 ── */
@media (max-width: 768px) {
.search-box { flex-direction: column; }
.table-wrap { overflow-x: auto; }
table { min-width: 700px; }
}
</style>
</head>
<body>
<div class="header">
<div class="header-nav">
<a href="/admin">← 관리자 홈</a>
<div>
<a href="/admin/sales-detail" style="margin-right: 16px;">판매 조회</a>
<a href="/admin/sales">판매 내역</a>
</div>
</div>
<h1>🔍 제품 검색</h1>
<p>전체 제품 검색 · QR 라벨 인쇄 · 🐾 동물약 AI 상담</p>
</div>
<div class="content">
<!-- 검색 -->
<div class="search-section">
<div class="search-box">
<input type="text" class="search-input" id="searchInput"
placeholder="상품명, 바코드, 상품코드로 검색..."
onkeypress="if(event.key==='Enter')searchProducts()">
<button class="search-btn" onclick="searchProducts()">🔍 검색</button>
</div>
<div style="display: flex; justify-content: space-between; align-items: center; margin-top: 12px;">
<div class="search-hint">
<span>예시</span> 타이레놀, 벤포파워, 8806418067510, LB000001423
</div>
<div style="display: flex; gap: 20px;">
<label style="display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; color: #475569;">
<input type="checkbox" id="inStockOnly" checked style="width: 18px; height: 18px; accent-color: #8b5cf6; cursor: pointer;">
<span style="display: flex; align-items: center; gap: 4px;">
📦 <strong style="color: #8b5cf6;">사용약품만</strong>
</span>
</label>
<label style="display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; color: #475569;">
<input type="checkbox" id="animalOnly" style="width: 18px; height: 18px; accent-color: #10b981; cursor: pointer;">
<span style="display: flex; align-items: center; gap: 4px;">
🐾 <strong style="color: #10b981;">동물약만</strong>
</span>
</label>
</div>
</div>
</div>
<!-- 결과 -->
<div class="result-count" id="resultCount" style="display:none;">
검색 결과: <strong id="resultNum">0</strong>
</div>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>상품명</th>
<th>상품코드</th>
<th>바코드</th>
<th>재고</th>
<th>판매가</th>
<th>QR</th>
</tr>
</thead>
<tbody id="productsTableBody">
<tr>
<td colspan="6" class="empty-state">
<div class="icon">🔍</div>
<p>상품명, 바코드, 상품코드로 검색하세요</p>
</td>
</tr>
</tbody>
</table>
</div>
</div><!-- /.content -->
<!-- 동물약 챗봇 패널 -->
<div class="chatbot-panel" id="chatbotPanel">
<div class="chatbot-header">
<h2>🐾 동물약 AI 상담</h2>
<p>심장사상충, 외부기생충, 구충제 등 무엇이든 물어보세요</p>
</div>
<div class="chatbot-suggestions">
<button class="suggestion-chip" onclick="sendSuggestion('강아지 심장사상충 약 추천해줘')">🐕 심장사상충약</button>
<button class="suggestion-chip" onclick="sendSuggestion('넥스가드랑 브라벡토 차이점')">넥스가드 vs 브라벡토</button>
<button class="suggestion-chip" onclick="sendSuggestion('고양이 벼룩 약 뭐가 좋아?')">🐱 고양이 벼룩약</button>
<button class="suggestion-chip" onclick="sendSuggestion('강아지 구충제 추천')">🪱 구충제</button>
</div>
<div class="chatbot-messages" id="chatMessages">
<div class="chat-message assistant">
안녕하세요! 🐾 동물약 상담 AI입니다.<br><br>
반려동물의 <strong>심장사상충 예방</strong>, <strong>벼룩/진드기 예방</strong>, <strong>구충제</strong> 등에 대해 무엇이든 물어보세요!
</div>
</div>
<div class="chatbot-input">
<input type="text" id="chatInput" placeholder="예: 5kg 강아지 심장사상충 약 추천해줘"
onkeypress="if(event.key==='Enter')sendChat()">
<button onclick="sendChat()" id="chatSendBtn"></button>
</div>
</div>
<!-- 챗봇 토글 버튼 -->
<button class="chatbot-toggle" id="chatbotToggle" onclick="toggleChatbot()">🐾</button>
<!-- QR 인쇄 모달 -->
<div class="modal-overlay" id="qrModal" onclick="if(event.target===this)closeQRModal()">
<div class="modal-box">
<div class="modal-title">🏷️ QR 라벨 인쇄</div>
<div id="qrInfo" style="margin-bottom:12px;"></div>
<div class="modal-preview" id="qrPreview">
<p style="color:#64748b;">미리보기 로딩 중...</p>
</div>
<div class="qty-label">인쇄 매수</div>
<div class="qty-selector">
<button class="qty-btn" onclick="adjustQty(-1)" id="qtyMinus"></button>
<div class="qty-value" id="qtyValue">1</div>
<button class="qty-btn" onclick="adjustQty(1)" id="qtyPlus">+</button>
</div>
<div class="modal-btns">
<button class="modal-btn cancel" onclick="closeQRModal()">취소</button>
<button class="modal-btn confirm" onclick="confirmPrintQR()" id="printBtn">인쇄</button>
</div>
</div>
</div>
<script>
let productsData = [];
let selectedItem = null;
let printQty = 1;
const MAX_QTY = 10;
const MIN_QTY = 1;
function formatPrice(num) {
if (!num) return '-';
return new Intl.NumberFormat('ko-KR').format(num) + '원';
}
function escapeHtml(str) {
if (!str) return '';
return str.replace(/[&<>"']/g, m => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));
}
function searchProducts() {
const search = document.getElementById('searchInput').value.trim();
if (!search) {
alert('검색어를 입력하세요');
return;
}
if (search.length < 2) {
alert('2글자 이상 입력하세요');
return;
}
const tbody = document.getElementById('productsTableBody');
tbody.innerHTML = '<tr><td colspan="6" class="empty-state"><p>검색 중...</p></td></tr>';
const animalOnly = document.getElementById('animalOnly').checked;
const inStockOnly = document.getElementById('inStockOnly').checked;
let url = `/api/products?search=${encodeURIComponent(search)}`;
if (animalOnly) url += '&animal_only=1';
if (inStockOnly) url += '&in_stock_only=1';
fetch(url)
.then(res => res.json())
.then(data => {
if (data.success) {
productsData = data.items;
document.getElementById('resultCount').style.display = 'block';
document.getElementById('resultNum').textContent = productsData.length;
renderTable();
} else {
tbody.innerHTML = `<tr><td colspan="6" class="empty-state"><p>오류: ${data.error}</p></td></tr>`;
}
})
.catch(err => {
tbody.innerHTML = '<tr><td colspan="6" class="empty-state"><p>검색 실패</p></td></tr>';
});
}
function renderTable() {
const tbody = document.getElementById('productsTableBody');
if (productsData.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" class="empty-state"><div class="icon">📭</div><p>검색 결과가 없습니다</p></td></tr>';
return;
}
tbody.innerHTML = productsData.map((item, idx) => {
// 분류 뱃지 (동물약만)
const categoryBadge = item.category
? `<span style="display:inline-block;background:#8b5cf6;color:#fff;font-size:10px;padding:2px 5px;border-radius:3px;margin-left:4px;">${escapeHtml(item.category)}</span>`
: '';
// 도매상 재고 표시 (동물약만)
const wsStock = (item.wholesaler_stock && item.wholesaler_stock > 0)
? `<span style="color:#3b82f6;font-size:11px;margin-left:4px;">(도매 ${item.wholesaler_stock})</span>`
: '';
return `
<tr>
<td>
<div class="product-name">
${escapeHtml(item.product_name)}
${item.is_animal_drug ? '<span style="display:inline-block;background:#10b981;color:#fff;font-size:11px;padding:2px 6px;border-radius:4px;margin-left:6px;">🐾 동물약</span>' : ''}
${categoryBadge}
</div>
<div class="product-supplier ${item.is_set ? 'set' : ''}">${escapeHtml(item.supplier) || ''}</div>
</td>
<td><span class="code code-drug">${item.drug_code}</span></td>
<td>${item.barcode
? `<span class="code code-barcode">${item.barcode}</span>`
: `<span class="code code-na">없음</span>`}</td>
<td class="stock ${(item.stock || 0) > 0 ? 'in-stock' : 'out-stock'}">${item.stock || 0}${wsStock}</td>
<td class="price">${formatPrice(item.sale_price)}</td>
<td>
<button class="btn-qr" onclick="printQR(${idx})">🏷️ QR</button>
</td>
</tr>
`}).join('');
}
// ── QR 인쇄 관련 ──
function adjustQty(delta) {
printQty = Math.max(MIN_QTY, Math.min(MAX_QTY, printQty + delta));
updateQtyUI();
}
function updateQtyUI() {
document.getElementById('qtyValue').textContent = printQty;
document.getElementById('qtyMinus').disabled = printQty <= MIN_QTY;
document.getElementById('qtyPlus').disabled = printQty >= MAX_QTY;
document.getElementById('printBtn').textContent = printQty > 1 ? `${printQty}장 인쇄` : '인쇄';
}
function printQR(idx) {
selectedItem = productsData[idx];
printQty = 1;
const modal = document.getElementById('qrModal');
const preview = document.getElementById('qrPreview');
const info = document.getElementById('qrInfo');
preview.innerHTML = '<p style="color:#64748b;">미리보기 로딩 중...</p>';
info.innerHTML = `
<strong>${escapeHtml(selectedItem.product_name)}</strong><br>
<span style="color:#64748b;font-size:13px;">
바코드: ${selectedItem.barcode || selectedItem.drug_code || 'N/A'}<br>
가격: ${formatPrice(selectedItem.sale_price)}
</span>
`;
updateQtyUI();
modal.classList.add('active');
fetch('/api/qr-preview', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
drug_name: selectedItem.product_name,
barcode: selectedItem.barcode || '',
drug_code: selectedItem.drug_code || '',
sale_price: selectedItem.sale_price || 0
})
})
.then(res => res.json())
.then(data => {
if (data.success && data.image) {
preview.innerHTML = `<img src="${data.image}" alt="QR 미리보기">`;
} else {
preview.innerHTML = '<p style="color:#ef4444;">미리보기 실패</p>';
}
})
.catch(() => {
preview.innerHTML = '<p style="color:#ef4444;">미리보기 오류</p>';
});
}
function closeQRModal() {
document.getElementById('qrModal').classList.remove('active');
selectedItem = null;
printQty = 1;
}
async function confirmPrintQR() {
if (!selectedItem) return;
const btn = document.getElementById('printBtn');
const totalQty = printQty;
btn.disabled = true;
let successCount = 0;
let errorMsg = '';
for (let i = 0; i < totalQty; i++) {
btn.textContent = `인쇄 중... (${i + 1}/${totalQty})`;
try {
const res = await fetch('/api/qr-print', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
drug_name: selectedItem.product_name,
barcode: selectedItem.barcode || '',
drug_code: selectedItem.drug_code || '',
sale_price: selectedItem.sale_price || 0
})
});
const data = await res.json();
if (data.success) {
successCount++;
} else {
errorMsg = data.error || '알 수 없는 오류';
break;
}
if (i < totalQty - 1) {
await new Promise(r => setTimeout(r, 500));
}
} catch (err) {
errorMsg = err.message;
break;
}
}
btn.disabled = false;
updateQtyUI();
if (successCount === totalQty) {
alert(`✅ QR 라벨 ${totalQty}장 인쇄 완료!`);
closeQRModal();
} else if (successCount > 0) {
alert(`⚠️ ${successCount}/${totalQty}장 인쇄 완료\n오류: ${errorMsg}`);
} else {
alert(`❌ 인쇄 실패: ${errorMsg}`);
}
}
// 페이지 로드 시 검색창 포커스
document.getElementById('searchInput').focus();
// ══════════════════════════════════════════════════════════════════
// 동물약 챗봇
// ══════════════════════════════════════════════════════════════════
let chatHistory = [];
let isChatLoading = false;
function toggleChatbot() {
const panel = document.getElementById('chatbotPanel');
const btn = document.getElementById('chatbotToggle');
const isOpen = panel.classList.toggle('open');
btn.classList.toggle('active', isOpen);
btn.innerHTML = isOpen ? '✕' : '🐾';
if (isOpen) {
document.getElementById('chatInput').focus();
}
}
function sendSuggestion(text) {
document.getElementById('chatInput').value = text;
sendChat();
}
async function sendChat() {
const input = document.getElementById('chatInput');
const message = input.value.trim();
if (!message || isChatLoading) return;
// 사용자 메시지 표시
addChatMessage('user', message);
input.value = '';
// 히스토리에 추가
chatHistory.push({ role: 'user', content: message });
// 로딩 표시
isChatLoading = true;
document.getElementById('chatSendBtn').disabled = true;
showTypingIndicator();
try {
const response = await fetch('/api/animal-chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages: chatHistory })
});
const data = await response.json();
hideTypingIndicator();
if (data.success) {
// AI 응답 표시
addChatMessage('assistant', data.message, data.products);
// 히스토리에 추가
chatHistory.push({ role: 'assistant', content: data.message });
// 히스토리 길이 제한 (최근 20개)
if (chatHistory.length > 20) {
chatHistory = chatHistory.slice(-20);
}
} else {
addChatMessage('system', '⚠️ ' + (data.message || '오류가 발생했습니다'));
}
} catch (error) {
hideTypingIndicator();
addChatMessage('system', '⚠️ 네트워크 오류가 발생했습니다');
}
isChatLoading = false;
document.getElementById('chatSendBtn').disabled = false;
}
function addChatMessage(role, content, products) {
const container = document.getElementById('chatMessages');
const msgDiv = document.createElement('div');
msgDiv.className = `chat-message ${role}`;
// 줄바꿈 처리
let htmlContent = escapeHtml(content).replace(/\n/g, '<br>');
// 마크다운 굵게 처리
htmlContent = htmlContent.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
msgDiv.innerHTML = htmlContent;
// 언급된 제품 표시 (이미지 포함)
if (products && products.length > 0) {
const productsDiv = document.createElement('div');
productsDiv.className = 'products-mentioned';
productsDiv.innerHTML = '<small style="color:#64748b;">📦 관련 제품:</small>';
const productsGrid = document.createElement('div');
productsGrid.style.cssText = 'display:flex;flex-wrap:wrap;gap:8px;margin-top:8px;';
products.forEach(p => {
const card = document.createElement('div');
card.className = 'product-card-mini';
card.style.cssText = 'display:flex;align-items:center;gap:8px;padding:8px;background:#f8fafc;border-radius:8px;cursor:pointer;border:1px solid #e2e8f0;';
card.onclick = () => searchProductFromChat(p.name);
// 이미지 컨테이너
const imgContainer = document.createElement('div');
imgContainer.style.cssText = 'width:40px;height:40px;flex-shrink:0;';
if (p.image_url) {
const img = document.createElement('img');
img.style.cssText = 'width:40px;height:40px;object-fit:cover;border-radius:4px;background:#e2e8f0;';
img.src = p.image_url;
img.alt = p.name;
img.onerror = function() {
// 이미지 로드 실패 시 아이콘으로 대체
imgContainer.innerHTML = '<div style="width:40px;height:40px;background:#f1f5f9;border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:20px;">💊</div>';
};
imgContainer.appendChild(img);
} else {
// 이미지 없으면 아이콘
imgContainer.innerHTML = '<div style="width:40px;height:40px;background:#f1f5f9;border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:20px;">💊</div>';
}
// 텍스트 (카테고리 뱃지 + 약국/도매 재고)
const textDiv = document.createElement('div');
const pharmacyStock = p.stock || 0;
const wholesalerStock = p.wholesaler_stock || 0;
const stockColor = (pharmacyStock > 0) ? '#10b981' : '#ef4444';
const pharmacyText = (pharmacyStock > 0) ? `약국 ${pharmacyStock}` : '품절';
const wholesalerText = (wholesalerStock > 0) ? `도매 ${wholesalerStock}` : '';
const stockDisplay = wholesalerText ? `${pharmacyText} / ${wholesalerText}` : pharmacyText;
// 카테고리 뱃지
const categoryBadge = p.category
? `<span style="display:inline-block;background:#8b5cf6;color:#fff;font-size:10px;padding:2px 5px;border-radius:3px;margin-left:4px;">${p.category}</span>`
: '';
textDiv.innerHTML = `<div style="font-size:13px;font-weight:500;color:#334155;">${p.name}${categoryBadge}</div><div style="font-size:12px;"><span style="color:#10b981;">${formatPrice(p.price)}</span> <span style="color:${stockColor};margin-left:6px;">${stockDisplay}</span></div>`;
card.appendChild(imgContainer);
card.appendChild(textDiv);
productsGrid.appendChild(card);
});
productsDiv.appendChild(productsGrid);
msgDiv.appendChild(productsDiv);
}
container.appendChild(msgDiv);
container.scrollTop = container.scrollHeight;
}
function showTypingIndicator() {
const container = document.getElementById('chatMessages');
const indicator = document.createElement('div');
indicator.className = 'typing-indicator';
indicator.id = 'typingIndicator';
indicator.innerHTML = '<span></span><span></span><span></span>';
container.appendChild(indicator);
container.scrollTop = container.scrollHeight;
}
function hideTypingIndicator() {
const indicator = document.getElementById('typingIndicator');
if (indicator) indicator.remove();
}
function searchProductFromChat(productName) {
// 챗봇에서 제품 클릭 시 검색창에 입력하고 검색
document.getElementById('searchInput').value = productName;
document.getElementById('animalOnly').checked = true;
searchProducts();
// 모바일에서 챗봇 닫기
if (window.innerWidth <= 1100) {
document.getElementById('chatbotPanel').classList.remove('open');
}
}
</script>
</body>
</html>