1620 lines
55 KiB
HTML
1620 lines
55 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Dashboard - Invoice Extraction Pro</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
background: #f5f7fa;
|
|
color: #333;
|
|
}
|
|
|
|
/* Navigation */
|
|
.navbar {
|
|
background: white;
|
|
border-bottom: 1px solid #e0e0e0;
|
|
padding: 15px 30px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 100;
|
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
|
|
}
|
|
|
|
.navbar-brand {
|
|
font-size: 20px;
|
|
font-weight: 700;
|
|
color: #667eea;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
}
|
|
|
|
.navbar-right {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 25px;
|
|
}
|
|
|
|
.user-info {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.user-avatar {
|
|
width: 40px;
|
|
height: 40px;
|
|
border-radius: 50%;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.logout-btn {
|
|
padding: 8px 16px;
|
|
background: #f0f0f0;
|
|
border: none;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
color: #333;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.logout-btn:hover {
|
|
background: #e0e0e0;
|
|
color: #000;
|
|
}
|
|
|
|
/* Main Container */
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
padding: 30px;
|
|
}
|
|
|
|
/* Header */
|
|
.header {
|
|
margin-bottom: 30px;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 32px;
|
|
margin-bottom: 10px;
|
|
color: #333;
|
|
}
|
|
|
|
.header p {
|
|
color: #888;
|
|
font-size: 16px;
|
|
}
|
|
|
|
/* Tabs */
|
|
.tabs {
|
|
display: flex;
|
|
gap: 20px;
|
|
border-bottom: 2px solid #e0e0e0;
|
|
margin-bottom: 30px;
|
|
}
|
|
|
|
.tab {
|
|
padding: 15px 0;
|
|
cursor: pointer;
|
|
font-weight: 600;
|
|
font-size: 16px;
|
|
color: #888;
|
|
border-bottom: 3px solid transparent;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.tab:hover {
|
|
color: #667eea;
|
|
}
|
|
|
|
.tab.active {
|
|
color: #667eea;
|
|
border-bottom-color: #667eea;
|
|
}
|
|
|
|
.tab-content {
|
|
display: none;
|
|
}
|
|
|
|
.tab-content.active {
|
|
display: block;
|
|
}
|
|
|
|
/* Upload Section */
|
|
.upload-area {
|
|
background: white;
|
|
border: 2px dashed #667eea;
|
|
border-radius: 12px;
|
|
padding: 60px 40px;
|
|
text-align: center;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
margin-bottom: 30px;
|
|
}
|
|
|
|
.upload-area:hover {
|
|
border-color: #764ba2;
|
|
background: #f8f9ff;
|
|
}
|
|
|
|
.upload-area.dragover {
|
|
border-color: #764ba2;
|
|
background: #f0f2ff;
|
|
}
|
|
|
|
.upload-icon {
|
|
font-size: 48px;
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
.upload-text h3 {
|
|
font-size: 18px;
|
|
margin-bottom: 5px;
|
|
color: #333;
|
|
}
|
|
|
|
.upload-text p {
|
|
color: #888;
|
|
font-size: 14px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.upload-btn {
|
|
display: inline-block;
|
|
padding: 12px 30px;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
border: none;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
font-weight: 600;
|
|
font-size: 16px;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.upload-btn:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
|
|
}
|
|
|
|
.file-input {
|
|
display: none;
|
|
}
|
|
|
|
/* Loading State */
|
|
.loading {
|
|
display: none;
|
|
text-align: center;
|
|
padding: 40px;
|
|
background: white;
|
|
border-radius: 12px;
|
|
margin-top: 30px;
|
|
}
|
|
|
|
.loading.show {
|
|
display: block;
|
|
}
|
|
|
|
.spinner {
|
|
width: 48px;
|
|
height: 48px;
|
|
border: 4px solid #e0e0e0;
|
|
border-top-color: #667eea;
|
|
border-radius: 50%;
|
|
animation: spin 0.8s linear infinite;
|
|
margin: 0 auto 20px;
|
|
}
|
|
|
|
@keyframes spin {
|
|
to { transform: rotate(360deg); }
|
|
}
|
|
|
|
/* Results Section */
|
|
.results {
|
|
background: white;
|
|
border-radius: 12px;
|
|
padding: 30px;
|
|
margin-top: 30px;
|
|
display: none;
|
|
}
|
|
|
|
.results.show {
|
|
display: block;
|
|
}
|
|
|
|
.result-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 20px;
|
|
padding-bottom: 15px;
|
|
border-bottom: 2px solid #e0e0e0;
|
|
}
|
|
|
|
.result-header h2 {
|
|
font-size: 22px;
|
|
color: #333;
|
|
}
|
|
|
|
.badge {
|
|
padding: 8px 16px;
|
|
border-radius: 20px;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.badge.success {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
}
|
|
|
|
.badge.warning {
|
|
background: #fff3cd;
|
|
color: #856404;
|
|
}
|
|
|
|
/* Metadata */
|
|
.metadata {
|
|
background: #f8f9fa;
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
margin-bottom: 20px;
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
gap: 15px;
|
|
}
|
|
|
|
.metadata-item {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 5px;
|
|
}
|
|
|
|
.metadata-label {
|
|
font-size: 12px;
|
|
color: #888;
|
|
font-weight: 600;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.metadata-value {
|
|
font-size: 14px;
|
|
color: #333;
|
|
font-weight: 500;
|
|
}
|
|
|
|
/* Stats */
|
|
.stats {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
|
gap: 15px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.stat {
|
|
background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%);
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
border-left: 4px solid #667eea;
|
|
}
|
|
|
|
.stat-label {
|
|
font-size: 12px;
|
|
color: #888;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.stat-value {
|
|
font-size: 24px;
|
|
font-weight: 700;
|
|
color: #333;
|
|
margin-top: 5px;
|
|
}
|
|
|
|
/* Items Table */
|
|
.items-table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 14px;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.items-table th {
|
|
background: #f8f9fa;
|
|
padding: 12px;
|
|
text-align: left;
|
|
border-bottom: 2px solid #e0e0e0;
|
|
color: #333;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.items-table td {
|
|
padding: 12px;
|
|
border-bottom: 1px solid #e0e0e0;
|
|
color: #666;
|
|
}
|
|
|
|
.items-table tbody tr:hover {
|
|
background: #f8f9fa;
|
|
}
|
|
|
|
.tag {
|
|
display: inline-block;
|
|
padding: 4px 8px;
|
|
border-radius: 4px;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.tag.matched {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
}
|
|
|
|
.tag.api {
|
|
background: #d1ecf1;
|
|
color: #0c5460;
|
|
}
|
|
|
|
.tag.missing {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
}
|
|
|
|
/* View toggle (Table / JSON) */
|
|
.view-toolbar {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
flex-wrap: wrap;
|
|
gap: 12px;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.segmented {
|
|
display: inline-flex;
|
|
background: #f0f0f0;
|
|
border-radius: 8px;
|
|
padding: 3px;
|
|
}
|
|
|
|
.seg-btn {
|
|
padding: 8px 16px;
|
|
border: none;
|
|
background: transparent;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
color: #666;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.seg-btn:hover {
|
|
color: #667eea;
|
|
}
|
|
|
|
.seg-btn.active {
|
|
background: white;
|
|
color: #667eea;
|
|
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
|
|
}
|
|
|
|
.hidden {
|
|
display: none !important;
|
|
}
|
|
|
|
/* JSON viewer */
|
|
.json-viewer {
|
|
background: #1e1e2e;
|
|
color: #cdd6f4;
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
margin-top: 20px;
|
|
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
|
|
font-size: 13px;
|
|
line-height: 1.6;
|
|
max-height: 600px;
|
|
overflow: auto;
|
|
white-space: pre;
|
|
tab-size: 2;
|
|
}
|
|
|
|
.json-key { color: #89b4fa; }
|
|
.json-string { color: #a6e3a1; }
|
|
.json-number { color: #fab387; }
|
|
.json-boolean { color: #cba6f7; }
|
|
.json-null { color: #f38ba8; }
|
|
|
|
/* History Table */
|
|
.history-table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.history-table th {
|
|
background: #f8f9fa;
|
|
padding: 12px;
|
|
text-align: left;
|
|
border-bottom: 2px solid #e0e0e0;
|
|
color: #333;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.history-table td {
|
|
padding: 12px;
|
|
border-bottom: 1px solid #e0e0e0;
|
|
color: #666;
|
|
}
|
|
|
|
.history-table tbody tr:hover {
|
|
background: #f8f9fa;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.tag.manual {
|
|
background: #e2d9f3;
|
|
color: #4b2a85;
|
|
}
|
|
|
|
.tag.edited {
|
|
background: #fff3cd;
|
|
color: #856404;
|
|
}
|
|
|
|
/* Manual item code entry */
|
|
.code-input {
|
|
width: 100%;
|
|
min-width: 140px;
|
|
padding: 8px 10px;
|
|
border: 1px solid #d0d0d0;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
font-family: inherit;
|
|
color: #333;
|
|
transition: border-color 0.2s, box-shadow 0.2s;
|
|
}
|
|
|
|
.code-input:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
|
|
}
|
|
|
|
.code-input.empty {
|
|
border-color: #f0ad4e;
|
|
background: #fffbea;
|
|
}
|
|
|
|
.file-chip {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
margin-top: 20px;
|
|
padding: 8px 14px;
|
|
background: #f0f2ff;
|
|
border-radius: 20px;
|
|
font-size: 14px;
|
|
color: #333;
|
|
}
|
|
|
|
.file-chip button {
|
|
border: none;
|
|
background: transparent;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
color: #888;
|
|
}
|
|
|
|
.file-chip button:hover {
|
|
color: #c33;
|
|
}
|
|
|
|
.btn:disabled {
|
|
opacity: 0.5;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
.hint {
|
|
color: #888;
|
|
font-size: 13px;
|
|
margin: 15px 0 5px;
|
|
}
|
|
|
|
.table-scroll {
|
|
overflow-x: auto;
|
|
}
|
|
|
|
/* Action Buttons */
|
|
.action-buttons {
|
|
display: flex;
|
|
gap: 10px;
|
|
margin-top: 20px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.btn {
|
|
padding: 10px 20px;
|
|
border: none;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: #667eea;
|
|
color: white;
|
|
}
|
|
|
|
.btn-primary:hover {
|
|
background: #764ba2;
|
|
}
|
|
|
|
.btn-secondary {
|
|
background: #e0e0e0;
|
|
color: #333;
|
|
}
|
|
|
|
.btn-secondary:hover {
|
|
background: #d0d0d0;
|
|
}
|
|
|
|
.btn-success {
|
|
background: #28a745;
|
|
color: white;
|
|
}
|
|
|
|
.btn-success:hover {
|
|
background: #218838;
|
|
}
|
|
|
|
/* Messages */
|
|
.message {
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
margin-bottom: 20px;
|
|
display: none;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.message.show {
|
|
display: block;
|
|
}
|
|
|
|
.message.error {
|
|
background: #fee;
|
|
color: #c33;
|
|
border-left: 4px solid #c33;
|
|
}
|
|
|
|
.message.success {
|
|
background: #efe;
|
|
color: #3c3;
|
|
border-left: 4px solid #3c3;
|
|
}
|
|
|
|
.message.warning {
|
|
background: #ffe;
|
|
color: #a80;
|
|
border-left: 4px solid #a80;
|
|
}
|
|
|
|
/* Responsive */
|
|
@media (max-width: 768px) {
|
|
.navbar {
|
|
flex-direction: column;
|
|
gap: 15px;
|
|
}
|
|
|
|
.container {
|
|
padding: 15px;
|
|
}
|
|
|
|
.upload-area {
|
|
padding: 30px 20px;
|
|
}
|
|
|
|
.items-table,
|
|
.history-table {
|
|
font-size: 12px;
|
|
}
|
|
|
|
.items-table th,
|
|
.items-table td,
|
|
.history-table th,
|
|
.history-table td {
|
|
padding: 8px;
|
|
}
|
|
|
|
.action-buttons {
|
|
flex-direction: column;
|
|
}
|
|
|
|
.btn {
|
|
width: 100%;
|
|
}
|
|
}
|
|
|
|
/* Empty State */
|
|
.empty-state {
|
|
text-align: center;
|
|
padding: 40px;
|
|
color: #888;
|
|
}
|
|
|
|
.empty-state-icon {
|
|
font-size: 48px;
|
|
margin-bottom: 15px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<!-- Navigation -->
|
|
<nav class="navbar">
|
|
<div class="navbar-brand">
|
|
📋 Invoice Pro
|
|
</div>
|
|
<div class="navbar-right">
|
|
<div class="user-info">
|
|
<div class="user-avatar" id="userAvatar"></div>
|
|
<div>
|
|
<strong id="userName">{{ username }}</strong><br>
|
|
<small style="color: #888;">Logged in</small>
|
|
</div>
|
|
</div>
|
|
<a href="/logout" class="logout-btn">Logout</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- Main Container -->
|
|
<div class="container">
|
|
<!-- Header -->
|
|
<div class="header">
|
|
<h1>📦 Invoice Extraction</h1>
|
|
<p>Upload invoices to extract and enrich with item codes from your drug database</p>
|
|
</div>
|
|
|
|
<!-- Tabs -->
|
|
<div class="tabs">
|
|
<div class="tab active" onclick="switchTab('extract')">🔄 Extract Invoice</div>
|
|
<div class="tab" onclick="switchTab('manual')">✏️ Manual Item Codes</div>
|
|
<div class="tab" onclick="switchTab('history')">📜 History</div>
|
|
<div class="tab" onclick="switchTab('profile')">👤 Profile</div>
|
|
</div>
|
|
|
|
<!-- Tab 1: Extract -->
|
|
<div id="extract" class="tab-content active">
|
|
<div class="message" id="message"></div>
|
|
|
|
<!-- Upload Area -->
|
|
<div class="upload-area" id="uploadArea">
|
|
<div class="upload-icon">📄</div>
|
|
<div class="upload-text">
|
|
<h3>Upload Invoice</h3>
|
|
<p>Drag and drop your PDF, PNG, or JPG file here, or click to select</p>
|
|
</div>
|
|
<button type="button" class="upload-btn" onclick="document.getElementById('fileInput').click()">
|
|
Choose File
|
|
</button>
|
|
<input type="file" id="fileInput" class="file-input" accept=".pdf,.png,.jpg,.jpeg" onchange="handleFileSelect(event)">
|
|
<p style="margin-top: 15px; color: #999; font-size: 12px;">
|
|
Max 100MB • Supported: PDF, PNG, JPG, JPEG
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Loading -->
|
|
<div class="loading" id="loading">
|
|
<div class="spinner"></div>
|
|
<p>Extracting invoice data...</p>
|
|
<p style="color: #999; margin-top: 10px; font-size: 12px;">This may take 10-30 seconds</p>
|
|
</div>
|
|
|
|
<!-- Results -->
|
|
<div class="results" id="results">
|
|
<!-- Result Header -->
|
|
<div class="result-header">
|
|
<h2>Extraction Results</h2>
|
|
<div id="resultBadge"></div>
|
|
</div>
|
|
|
|
<!-- Metadata -->
|
|
<div class="metadata" id="metadata"></div>
|
|
|
|
<!-- Stats -->
|
|
<div class="stats">
|
|
<div class="stat">
|
|
<div class="stat-label">Total Items</div>
|
|
<div class="stat-value" id="totalItems">0</div>
|
|
</div>
|
|
<div class="stat">
|
|
<div class="stat-label">With Codes</div>
|
|
<div class="stat-value" id="itemsWithCode">0</div>
|
|
</div>
|
|
<div class="stat">
|
|
<div class="stat-label">From Database</div>
|
|
<div class="stat-value" id="itemsMatched">0</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- View Toggle -->
|
|
<div class="view-toolbar">
|
|
<div class="segmented">
|
|
<button class="seg-btn active" id="viewTableBtn" onclick="setView('table')">📊 Table</button>
|
|
<button class="seg-btn" id="viewJsonBtn" onclick="setView('json')">{ } JSON</button>
|
|
</div>
|
|
<div class="segmented hidden" id="jsonSourceToggle">
|
|
<button class="seg-btn active" id="srcApiBtn" onclick="setJsonSource('api')">API response</button>
|
|
<button class="seg-btn" id="srcEnrichedBtn" onclick="setJsonSource('enriched')">With item codes</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Table View -->
|
|
<div id="tableView">
|
|
<table class="items-table" id="itemsTable">
|
|
<thead>
|
|
<tr>
|
|
<th>Description</th>
|
|
<th>Quantity</th>
|
|
<th>Unit Price</th>
|
|
<th>Item Code</th>
|
|
<th>Source</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="itemsTableBody">
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- JSON View -->
|
|
<div id="jsonView" class="hidden">
|
|
<pre class="json-viewer" id="jsonViewer"></pre>
|
|
</div>
|
|
|
|
<!-- Action Buttons -->
|
|
<div class="action-buttons">
|
|
<button class="btn btn-primary" onclick="copyJSON()">📋 Copy JSON</button>
|
|
<button class="btn btn-success" onclick="downloadJSON()">⬇️ Download JSON</button>
|
|
<button class="btn btn-secondary" onclick="resetForm()">🔄 Upload Another</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Tab: Manual Item Codes -->
|
|
<div id="manual" class="tab-content">
|
|
<div class="message" id="manualMessage"></div>
|
|
|
|
<!-- Upload Area -->
|
|
<div class="upload-area" id="manualUploadArea">
|
|
<div class="upload-icon">✏️</div>
|
|
<div class="upload-text">
|
|
<h3>Upload Invoice & Add Item Codes Manually</h3>
|
|
<p>Choose a file, click Extract, then type item codes for any items that don't have one.</p>
|
|
</div>
|
|
<button type="button" class="upload-btn" onclick="document.getElementById('manualFileInput').click()">
|
|
Choose File
|
|
</button>
|
|
<input type="file" id="manualFileInput" class="file-input" accept=".pdf,.png,.jpg,.jpeg" onchange="handleManualFileSelect(event)">
|
|
<div>
|
|
<div class="file-chip hidden" id="manualFileChip">
|
|
<span id="manualFileName"></span>
|
|
<button type="button" onclick="clearManualFile()" title="Remove file">✕</button>
|
|
</div>
|
|
</div>
|
|
<div style="margin-top: 20px;">
|
|
<button type="button" class="btn btn-primary" id="manualExtractBtn" onclick="extractForManual()" disabled>
|
|
🔍 Extract Invoice
|
|
</button>
|
|
</div>
|
|
<p style="margin-top: 15px; color: #999; font-size: 12px;">
|
|
Max 100MB • Supported: PDF, PNG, JPG, JPEG
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Loading -->
|
|
<div class="loading" id="manualLoading">
|
|
<div class="spinner"></div>
|
|
<p>Extracting invoice data...</p>
|
|
<p style="color: #999; margin-top: 10px; font-size: 12px;">This may take 10-30 seconds</p>
|
|
</div>
|
|
|
|
<!-- Results -->
|
|
<div class="results" id="manualResults">
|
|
<div class="result-header">
|
|
<h2>Add Item Codes</h2>
|
|
<div id="manualBadge"></div>
|
|
</div>
|
|
|
|
<div class="metadata" id="manualMetadata"></div>
|
|
|
|
<p class="hint">Type a code for any item. Rows highlighted in yellow have no item code yet. Press Enter to jump to the next row.</p>
|
|
|
|
<div class="table-scroll">
|
|
<table class="items-table">
|
|
<thead>
|
|
<tr>
|
|
<th>#</th>
|
|
<th>Description</th>
|
|
<th>Quantity</th>
|
|
<th>Unit Price</th>
|
|
<th>Item Code</th>
|
|
<th>Source</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="manualTableBody"></tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="action-buttons">
|
|
<button class="btn btn-primary" id="manualSaveBtn" onclick="saveManualCodes()" disabled>💾 Save Item Codes</button>
|
|
<button class="btn btn-secondary" id="manualJsonBtn" onclick="toggleManualJson()">{ } Show JSON</button>
|
|
<button class="btn btn-success" onclick="downloadManualJSON()">⬇️ Download JSON</button>
|
|
<button class="btn btn-secondary" onclick="resetManual()">🔄 Upload Another</button>
|
|
</div>
|
|
|
|
<pre class="json-viewer hidden" id="manualJsonViewer"></pre>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Tab 2: History -->
|
|
<div id="history" class="tab-content">
|
|
<table class="history-table" id="historyTable">
|
|
<thead>
|
|
<tr>
|
|
<th>Invoice ID</th>
|
|
<th>File Name</th>
|
|
<th>Date</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="historyTableBody">
|
|
<tr>
|
|
<td colspan="4" style="text-align: center; padding: 40px; color: #999;">
|
|
Loading history...
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Tab 3: Profile -->
|
|
<div id="profile" class="tab-content">
|
|
<div style="background: white; padding: 30px; border-radius: 12px; max-width: 600px;">
|
|
<h2 style="margin-bottom: 20px;">Your Profile</h2>
|
|
<div class="metadata" id="profileInfo"></div>
|
|
|
|
<div style="margin-top: 30px;">
|
|
<h3 style="margin-bottom: 15px; font-size: 16px;">Account Information</h3>
|
|
<p style="color: #666; margin-bottom: 15px; line-height: 1.6;">
|
|
This is your Invoice Extraction Pro account. You can use it to extract and enrich invoices with item codes from the drug database.
|
|
</p>
|
|
<button class="btn btn-secondary" onclick="alert('Feature coming soon!')">Change Password</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
let lastExtractedData = null;
|
|
let currentView = 'table'; // 'table' | 'json'
|
|
let jsonSource = 'api'; // 'api' (as returned by the API) | 'enriched' (with item codes)
|
|
|
|
// Drag and drop
|
|
const uploadArea = document.getElementById('uploadArea');
|
|
|
|
uploadArea.addEventListener('dragover', (e) => {
|
|
e.preventDefault();
|
|
uploadArea.classList.add('dragover');
|
|
});
|
|
|
|
uploadArea.addEventListener('dragleave', () => {
|
|
uploadArea.classList.remove('dragover');
|
|
});
|
|
|
|
uploadArea.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
uploadArea.classList.remove('dragover');
|
|
if (e.dataTransfer.files.length > 0) {
|
|
handleFile(e.dataTransfer.files[0]);
|
|
}
|
|
});
|
|
|
|
function handleFileSelect(event) {
|
|
const files = event.target.files;
|
|
if (files.length > 0) {
|
|
handleFile(files[0]);
|
|
}
|
|
}
|
|
|
|
function handleFile(file) {
|
|
const validTypes = ['application/pdf', 'image/png', 'image/jpeg'];
|
|
|
|
if (!validTypes.includes(file.type)) {
|
|
showMessage('Invalid file type. Please upload PDF, PNG, or JPG', 'error');
|
|
return;
|
|
}
|
|
|
|
if (file.size > 100 * 1024 * 1024) {
|
|
showMessage('File size exceeds 100MB limit', 'error');
|
|
return;
|
|
}
|
|
|
|
uploadInvoice(file);
|
|
}
|
|
|
|
async function uploadInvoice(file) {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
|
|
document.getElementById('loading').classList.add('show');
|
|
document.getElementById('results').classList.remove('show');
|
|
hideMessage();
|
|
|
|
try {
|
|
const response = await fetch('/api/upload', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
const result = await response.json();
|
|
|
|
if (response.ok && result.success) {
|
|
lastExtractedData = result.data;
|
|
displayResults(result.data);
|
|
showMessage('Invoice extracted successfully!', 'success');
|
|
} else {
|
|
showMessage(result.error || 'Extraction failed', 'error');
|
|
}
|
|
} catch (error) {
|
|
showMessage('Network error. Please try again.', 'error');
|
|
} finally {
|
|
document.getElementById('loading').classList.remove('show');
|
|
}
|
|
}
|
|
|
|
function displayResults(data) {
|
|
// Metadata
|
|
const metadata = document.getElementById('metadata');
|
|
metadata.innerHTML = `
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Upload ID</span>
|
|
<span class="metadata-value">${data._metadata?.upload_id || '-'}</span>
|
|
</div>
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Timestamp</span>
|
|
<span class="metadata-value">${new Date(data._metadata?.upload_timestamp).toLocaleString()}</span>
|
|
</div>
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Invoice ID</span>
|
|
<span class="metadata-value">${data.invoice_id || '-'}</span>
|
|
</div>
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Seller</span>
|
|
<span class="metadata-value">${data.seller_name || '-'}</span>
|
|
</div>
|
|
`;
|
|
|
|
// Stats
|
|
const items = data.items || [];
|
|
const itemsWithCode = items.filter(i => i.item_code).length;
|
|
const itemsMatched = items.filter(i => i.item_code_source === 'drug_list_match').length;
|
|
|
|
document.getElementById('totalItems').textContent = items.length;
|
|
document.getElementById('itemsWithCode').textContent = itemsWithCode;
|
|
document.getElementById('itemsMatched').textContent = itemsMatched;
|
|
|
|
// Badge
|
|
const badge = document.getElementById('resultBadge');
|
|
if (items.length > 0 && itemsWithCode === items.length) {
|
|
badge.innerHTML = '<span class="badge success">✓ All items have codes</span>';
|
|
} else if (itemsWithCode > 0) {
|
|
badge.innerHTML = `<span class="badge warning">✓ ${itemsWithCode}/${items.length} items have codes</span>`;
|
|
} else {
|
|
badge.innerHTML = '<span class="badge warning">⚠ Some items missing codes</span>';
|
|
}
|
|
|
|
// Items table
|
|
const tbody = document.getElementById('itemsTableBody');
|
|
tbody.innerHTML = '';
|
|
|
|
items.forEach((item) => {
|
|
const code = item.item_code || '-';
|
|
const source = item.item_code_source || 'unknown';
|
|
|
|
let tagClass = 'missing';
|
|
let tagText = 'Not Found';
|
|
|
|
if (source === 'api_extracted') {
|
|
tagClass = 'api';
|
|
tagText = 'API';
|
|
} else if (source === 'drug_list_match') {
|
|
tagClass = 'matched';
|
|
tagText = 'Matched';
|
|
} else if (source === 'manual') {
|
|
tagClass = 'manual';
|
|
tagText = 'Manual';
|
|
}
|
|
|
|
const row = document.createElement('tr');
|
|
row.innerHTML = `
|
|
<td>${item.description || '-'}</td>
|
|
<td>${item.quantity || '-'}</td>
|
|
<td>${item.unit_price || '-'}</td>
|
|
<td><strong>${code}</strong></td>
|
|
<td><span class="tag ${tagClass}">${tagText}</span></td>
|
|
`;
|
|
tbody.appendChild(row);
|
|
});
|
|
|
|
renderJson();
|
|
document.getElementById('results').classList.add('show');
|
|
}
|
|
|
|
// ---------- JSON view ----------
|
|
|
|
function setView(view) {
|
|
currentView = view;
|
|
const isJson = view === 'json';
|
|
document.getElementById('tableView').classList.toggle('hidden', isJson);
|
|
document.getElementById('jsonView').classList.toggle('hidden', !isJson);
|
|
document.getElementById('jsonSourceToggle').classList.toggle('hidden', !isJson);
|
|
document.getElementById('viewTableBtn').classList.toggle('active', !isJson);
|
|
document.getElementById('viewJsonBtn').classList.toggle('active', isJson);
|
|
}
|
|
|
|
function setJsonSource(source) {
|
|
jsonSource = source;
|
|
document.getElementById('srcApiBtn').classList.toggle('active', source === 'api');
|
|
document.getElementById('srcEnrichedBtn').classList.toggle('active', source === 'enriched');
|
|
renderJson();
|
|
}
|
|
|
|
// Builds the JSON object for the selected source
|
|
function getJsonPayload() {
|
|
if (!lastExtractedData) return null;
|
|
|
|
const { _raw_api_response, ...enriched } = lastExtractedData;
|
|
|
|
if (jsonSource === 'enriched') {
|
|
return enriched; // items with item_code / item_code_source, plus _metadata
|
|
}
|
|
|
|
// Exactly what the API returned
|
|
if (_raw_api_response) return _raw_api_response;
|
|
|
|
// Older history rows were saved before the raw response was stored.
|
|
// Rebuild it approximately by removing what this app added.
|
|
const { _metadata, ...rest } = enriched;
|
|
if (Array.isArray(rest.items)) {
|
|
rest.items = rest.items.map(item => {
|
|
if (!item || typeof item !== 'object') return item;
|
|
const { item_code_source, ...clean } = item;
|
|
if (item_code_source === 'drug_list_match' || item_code_source === 'manual') delete clean.item_code;
|
|
return clean;
|
|
});
|
|
}
|
|
return rest;
|
|
}
|
|
|
|
// Pretty-print + syntax highlight (HTML-escaped first, so invoice text can't inject markup)
|
|
function highlightJson(obj) {
|
|
const json = JSON.stringify(obj, null, 2)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>');
|
|
|
|
return json.replace(
|
|
/("(?:\\u[a-fA-F0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(?:true|false|null)\b|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/g,
|
|
(match) => {
|
|
let cls = 'json-number';
|
|
if (match.startsWith('"')) {
|
|
cls = match.endsWith(':') ? 'json-key' : 'json-string';
|
|
} else if (match === 'true' || match === 'false') {
|
|
cls = 'json-boolean';
|
|
} else if (match === 'null') {
|
|
cls = 'json-null';
|
|
}
|
|
return `<span class="${cls}">${match}</span>`;
|
|
}
|
|
);
|
|
}
|
|
|
|
function renderJson() {
|
|
const payload = getJsonPayload();
|
|
document.getElementById('jsonViewer').innerHTML =
|
|
payload ? highlightJson(payload) : '';
|
|
}
|
|
|
|
// Copy / Download use whatever JSON is currently selected
|
|
function copyJSON() {
|
|
const json = JSON.stringify(getJsonPayload(), null, 2);
|
|
navigator.clipboard.writeText(json).then(() => {
|
|
showMessage('JSON copied to clipboard!', 'success');
|
|
}).catch(() => {
|
|
showMessage('Failed to copy', 'error');
|
|
});
|
|
}
|
|
|
|
function downloadJSON() {
|
|
const json = JSON.stringify(getJsonPayload(), null, 2);
|
|
const blob = new Blob([json], { type: 'application/json' });
|
|
const url = window.URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
const id = lastExtractedData._metadata?.upload_id || 'export';
|
|
a.download = `invoice_${id}_${jsonSource === 'api' ? 'api' : 'enriched'}.json`;
|
|
a.click();
|
|
window.URL.revokeObjectURL(url);
|
|
}
|
|
|
|
function resetForm() {
|
|
document.getElementById('fileInput').value = '';
|
|
lastExtractedData = null;
|
|
document.getElementById('results').classList.remove('show');
|
|
hideMessage();
|
|
}
|
|
|
|
function switchTab(tabName) {
|
|
// Hide all tabs
|
|
document.querySelectorAll('.tab-content').forEach(tab => {
|
|
tab.classList.remove('active');
|
|
});
|
|
document.querySelectorAll('.tab').forEach(tab => {
|
|
tab.classList.remove('active');
|
|
});
|
|
|
|
// Show selected tab
|
|
document.getElementById(tabName).classList.add('active');
|
|
event.target.classList.add('active');
|
|
|
|
if (tabName === 'history') {
|
|
loadHistory();
|
|
} else if (tabName === 'profile') {
|
|
loadProfile();
|
|
}
|
|
}
|
|
|
|
async function loadHistory() {
|
|
try {
|
|
const response = await fetch('/api/history');
|
|
const result = await response.json();
|
|
|
|
if (result.success) {
|
|
const tbody = document.getElementById('historyTableBody');
|
|
if (result.data.length === 0) {
|
|
tbody.innerHTML = '<tr><td colspan="4" style="text-align: center; padding: 40px; color: #999;">No extractions yet</td></tr>';
|
|
} else {
|
|
tbody.innerHTML = result.data.map(item => `
|
|
<tr onclick="loadExtraction(${item.id})">
|
|
<td>${item.invoice_id || '-'}</td>
|
|
<td>${item.filename}</td>
|
|
<td>${new Date(item.created_at).toLocaleString()}</td>
|
|
<td><span class="tag api">View</span></td>
|
|
</tr>
|
|
`).join('');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error loading history:', error);
|
|
}
|
|
}
|
|
|
|
async function loadExtraction(id) {
|
|
try {
|
|
const response = await fetch(`/api/extraction/${id}`);
|
|
const result = await response.json();
|
|
|
|
if (result.success) {
|
|
lastExtractedData = result.data;
|
|
displayResults(result.data);
|
|
switchTab('extract');
|
|
}
|
|
} catch (error) {
|
|
showMessage('Failed to load extraction', 'error');
|
|
}
|
|
}
|
|
|
|
async function loadProfile() {
|
|
try {
|
|
const response = await fetch('/api/profile');
|
|
const result = await response.json();
|
|
|
|
if (result.success) {
|
|
const data = result.data;
|
|
document.getElementById('profileInfo').innerHTML = `
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Username</span>
|
|
<span class="metadata-value">${data.username}</span>
|
|
</div>
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Email</span>
|
|
<span class="metadata-value">${data.email}</span>
|
|
</div>
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Joined</span>
|
|
<span class="metadata-value">${new Date(data.created_at).toLocaleDateString()}</span>
|
|
</div>
|
|
<div class="metadata-item">
|
|
<span class="metadata-label">Last Login</span>
|
|
<span class="metadata-value">${data.last_login ? new Date(data.last_login).toLocaleString() : 'Just now'}</span>
|
|
</div>
|
|
`;
|
|
|
|
// Set user avatar
|
|
const initials = data.username.substring(0, 1).toUpperCase();
|
|
document.getElementById('userAvatar').textContent = initials;
|
|
}
|
|
} catch (error) {
|
|
console.error('Error loading profile:', error);
|
|
}
|
|
}
|
|
|
|
// ---------- Manual Item Codes tab ----------
|
|
|
|
let manualFile = null;
|
|
let manualData = null; // extraction as saved on the server
|
|
let manualExtractionId = null; // DB id, needed to save codes
|
|
|
|
const manualUploadArea = document.getElementById('manualUploadArea');
|
|
|
|
manualUploadArea.addEventListener('dragover', (e) => {
|
|
e.preventDefault();
|
|
manualUploadArea.classList.add('dragover');
|
|
});
|
|
|
|
manualUploadArea.addEventListener('dragleave', () => {
|
|
manualUploadArea.classList.remove('dragover');
|
|
});
|
|
|
|
manualUploadArea.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
manualUploadArea.classList.remove('dragover');
|
|
if (e.dataTransfer.files.length > 0) {
|
|
handleManualFile(e.dataTransfer.files[0]);
|
|
}
|
|
});
|
|
|
|
function handleManualFileSelect(event) {
|
|
if (event.target.files.length > 0) {
|
|
handleManualFile(event.target.files[0]);
|
|
}
|
|
}
|
|
|
|
function handleManualFile(file) {
|
|
const validTypes = ['application/pdf', 'image/png', 'image/jpeg'];
|
|
|
|
if (!validTypes.includes(file.type)) {
|
|
showManualMessage('Invalid file type. Please upload PDF, PNG, or JPG', 'error');
|
|
return;
|
|
}
|
|
if (file.size > 100 * 1024 * 1024) {
|
|
showManualMessage('File size exceeds 100MB limit', 'error');
|
|
return;
|
|
}
|
|
|
|
manualFile = file;
|
|
hideManualMessage();
|
|
document.getElementById('manualFileName').textContent = file.name;
|
|
document.getElementById('manualFileChip').classList.remove('hidden');
|
|
document.getElementById('manualExtractBtn').disabled = false;
|
|
}
|
|
|
|
function clearManualFile() {
|
|
manualFile = null;
|
|
document.getElementById('manualFileInput').value = '';
|
|
document.getElementById('manualFileChip').classList.add('hidden');
|
|
document.getElementById('manualExtractBtn').disabled = true;
|
|
}
|
|
|
|
async function extractForManual() {
|
|
if (!manualFile) return;
|
|
|
|
const formData = new FormData();
|
|
formData.append('file', manualFile);
|
|
formData.append('enrich', 'false'); // don't fill codes from drug_list.csv; leave blanks for manual entry
|
|
|
|
document.getElementById('manualLoading').classList.add('show');
|
|
document.getElementById('manualResults').classList.remove('show');
|
|
document.getElementById('manualExtractBtn').disabled = true;
|
|
hideManualMessage();
|
|
|
|
try {
|
|
const response = await fetch('/api/upload', { method: 'POST', body: formData });
|
|
const result = await response.json();
|
|
|
|
if (response.ok && result.success) {
|
|
manualData = result.data;
|
|
manualExtractionId = result.extraction_id || null;
|
|
renderManualResults();
|
|
document.getElementById('manualResults').classList.add('show');
|
|
|
|
const items = Array.isArray(manualData.items) ? manualData.items : [];
|
|
if (!manualExtractionId) {
|
|
showManualMessage('Invoice extracted, but it could not be saved to history, so manual codes cannot be saved.', 'warning');
|
|
} else if (items.length === 0) {
|
|
showManualMessage('Invoice extracted, but no items were found.', 'warning');
|
|
} else {
|
|
showManualMessage('Invoice extracted. Add item codes below, then click Save.', 'success');
|
|
}
|
|
} else {
|
|
showManualMessage(result.error || 'Extraction failed', 'error');
|
|
}
|
|
} catch (error) {
|
|
showManualMessage('Network error. Please try again.', 'error');
|
|
} finally {
|
|
document.getElementById('manualLoading').classList.remove('show');
|
|
document.getElementById('manualExtractBtn').disabled = !manualFile;
|
|
}
|
|
}
|
|
|
|
function makeMetaItem(label, value) {
|
|
const wrap = document.createElement('div');
|
|
wrap.className = 'metadata-item';
|
|
const l = document.createElement('span');
|
|
l.className = 'metadata-label';
|
|
l.textContent = label;
|
|
const v = document.createElement('span');
|
|
v.className = 'metadata-value';
|
|
v.textContent = (value === undefined || value === null || value === '') ? '-' : value;
|
|
wrap.append(l, v);
|
|
return wrap;
|
|
}
|
|
|
|
function makeSourceTag(source, hasCode) {
|
|
const tag = document.createElement('span');
|
|
tag.className = 'tag';
|
|
if (source === 'api_extracted') {
|
|
tag.classList.add('api'); tag.textContent = 'API';
|
|
} else if (source === 'drug_list_match') {
|
|
tag.classList.add('matched'); tag.textContent = 'Matched';
|
|
} else if (source === 'manual') {
|
|
tag.classList.add('manual'); tag.textContent = 'Manual';
|
|
} else {
|
|
tag.classList.add('missing'); tag.textContent = 'Not Found';
|
|
}
|
|
return tag;
|
|
}
|
|
|
|
function renderManualResults() {
|
|
const items = Array.isArray(manualData.items) ? manualData.items : [];
|
|
|
|
// Metadata
|
|
const meta = document.getElementById('manualMetadata');
|
|
meta.replaceChildren(
|
|
makeMetaItem('Invoice ID', manualData.invoice_id),
|
|
makeMetaItem('Seller', manualData.seller_name),
|
|
makeMetaItem('Items', items.length)
|
|
);
|
|
|
|
// Editable rows (built with DOM APIs so invoice text is never parsed as HTML)
|
|
const tbody = document.getElementById('manualTableBody');
|
|
tbody.replaceChildren();
|
|
|
|
items.forEach((rawItem, i) => {
|
|
const item = (rawItem && typeof rawItem === 'object') ? rawItem : {};
|
|
const code = (item.item_code === undefined || item.item_code === null) ? '' : String(item.item_code).trim();
|
|
|
|
const tr = document.createElement('tr');
|
|
tr.dataset.source = item.item_code_source || 'not_found';
|
|
|
|
[i + 1, item.description, item.quantity, item.unit_price].forEach(value => {
|
|
const td = document.createElement('td');
|
|
td.textContent = (value === undefined || value === null || value === '') ? '-' : value;
|
|
tr.appendChild(td);
|
|
});
|
|
|
|
const codeTd = document.createElement('td');
|
|
const input = document.createElement('input');
|
|
input.type = 'text';
|
|
input.className = 'code-input' + (code ? '' : ' empty');
|
|
input.value = code;
|
|
input.placeholder = 'Enter item code';
|
|
input.maxLength = 50;
|
|
input.dataset.index = i;
|
|
input.dataset.original = code;
|
|
input.addEventListener('input', onManualCodeInput);
|
|
input.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter') {
|
|
e.preventDefault();
|
|
const inputs = [...document.querySelectorAll('#manualTableBody .code-input')];
|
|
const next = inputs[inputs.indexOf(input) + 1];
|
|
if (next) next.focus();
|
|
}
|
|
});
|
|
codeTd.appendChild(input);
|
|
tr.appendChild(codeTd);
|
|
|
|
const statusTd = document.createElement('td');
|
|
statusTd.className = 'status-cell';
|
|
statusTd.appendChild(makeSourceTag(tr.dataset.source, !!code));
|
|
tr.appendChild(statusTd);
|
|
|
|
tbody.appendChild(tr);
|
|
});
|
|
|
|
updateManualSummary();
|
|
refreshManualJson();
|
|
}
|
|
|
|
function onManualCodeInput(e) {
|
|
const input = e.target;
|
|
const value = input.value.trim();
|
|
const changed = value !== input.dataset.original;
|
|
const tr = input.closest('tr');
|
|
const statusCell = tr.querySelector('.status-cell');
|
|
|
|
input.classList.toggle('empty', value === '');
|
|
|
|
if (changed) {
|
|
const tag = document.createElement('span');
|
|
tag.className = 'tag edited';
|
|
tag.textContent = 'Edited';
|
|
statusCell.replaceChildren(tag);
|
|
} else {
|
|
statusCell.replaceChildren(makeSourceTag(tr.dataset.source, value !== ''));
|
|
}
|
|
|
|
updateManualSummary();
|
|
refreshManualJson();
|
|
}
|
|
|
|
function updateManualSummary() {
|
|
const inputs = [...document.querySelectorAll('#manualTableBody .code-input')];
|
|
const total = inputs.length;
|
|
const withCode = inputs.filter(i => i.value.trim() !== '').length;
|
|
const unsaved = inputs.filter(i => i.value.trim() !== i.dataset.original).length;
|
|
|
|
const badge = document.getElementById('manualBadge');
|
|
if (total > 0 && withCode === total) {
|
|
badge.innerHTML = '<span class="badge success">✓ All items have codes</span>';
|
|
} else {
|
|
badge.innerHTML = `<span class="badge warning">⚠ ${total - withCode} of ${total} items missing codes</span>`;
|
|
}
|
|
|
|
const saveBtn = document.getElementById('manualSaveBtn');
|
|
saveBtn.disabled = unsaved === 0 || !manualExtractionId;
|
|
saveBtn.textContent = unsaved > 0 ? `💾 Save Item Codes (${unsaved})` : '💾 Save Item Codes';
|
|
}
|
|
|
|
async function saveManualCodes() {
|
|
if (!manualExtractionId) {
|
|
showManualMessage('This extraction was not saved to history, so codes cannot be saved.', 'error');
|
|
return;
|
|
}
|
|
|
|
const item_codes = [...document.querySelectorAll('#manualTableBody .code-input')]
|
|
.filter(i => i.value.trim() !== i.dataset.original)
|
|
.map(i => ({ index: Number(i.dataset.index), item_code: i.value.trim() }));
|
|
|
|
if (item_codes.length === 0) return;
|
|
|
|
const saveBtn = document.getElementById('manualSaveBtn');
|
|
saveBtn.disabled = true;
|
|
saveBtn.textContent = 'Saving...';
|
|
|
|
try {
|
|
const response = await fetch(`/api/extraction/${manualExtractionId}/item-codes`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ item_codes })
|
|
});
|
|
const result = await response.json();
|
|
|
|
if (response.ok && result.success) {
|
|
manualData = result.data;
|
|
renderManualResults();
|
|
showManualMessage(`Saved ${result.updated} item code(s).`, 'success');
|
|
} else {
|
|
showManualMessage(result.error || 'Failed to save item codes', 'error');
|
|
}
|
|
} catch (error) {
|
|
showManualMessage('Network error. Please try again.', 'error');
|
|
} finally {
|
|
updateManualSummary();
|
|
}
|
|
}
|
|
|
|
// JSON of what is currently on screen (saved data + any unsaved edits)
|
|
function getManualPayload() {
|
|
if (!manualData) return null;
|
|
const { _raw_api_response, ...out } = manualData;
|
|
|
|
out.items = (Array.isArray(out.items) ? out.items : []).map((item, i) => {
|
|
const input = document.querySelector(`#manualTableBody .code-input[data-index="${i}"]`);
|
|
if (!input || !item || typeof item !== 'object') return item;
|
|
const value = input.value.trim();
|
|
if (value === input.dataset.original) return item;
|
|
return { ...item, item_code: value, item_code_source: value ? 'manual' : 'not_found' };
|
|
});
|
|
return out;
|
|
}
|
|
|
|
function toggleManualJson() {
|
|
const viewer = document.getElementById('manualJsonViewer');
|
|
const nowHidden = viewer.classList.toggle('hidden');
|
|
document.getElementById('manualJsonBtn').textContent = nowHidden ? '{ } Show JSON' : '{ } Hide JSON';
|
|
refreshManualJson();
|
|
}
|
|
|
|
function refreshManualJson() {
|
|
const viewer = document.getElementById('manualJsonViewer');
|
|
if (viewer.classList.contains('hidden')) return;
|
|
const payload = getManualPayload();
|
|
viewer.innerHTML = payload ? highlightJson(payload) : '';
|
|
}
|
|
|
|
function downloadManualJSON() {
|
|
const payload = getManualPayload();
|
|
if (!payload) return;
|
|
const blob = new Blob([JSON.stringify(payload, null, 2)], { type: 'application/json' });
|
|
const url = window.URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = `invoice_${payload._metadata?.upload_id || 'export'}_manual.json`;
|
|
a.click();
|
|
window.URL.revokeObjectURL(url);
|
|
}
|
|
|
|
function resetManual() {
|
|
if (document.querySelectorAll('#manualTableBody .code-input').length &&
|
|
!document.getElementById('manualSaveBtn').disabled &&
|
|
!confirm('You have unsaved item codes. Discard them?')) {
|
|
return;
|
|
}
|
|
clearManualFile();
|
|
manualData = null;
|
|
manualExtractionId = null;
|
|
document.getElementById('manualResults').classList.remove('show');
|
|
document.getElementById('manualJsonViewer').classList.add('hidden');
|
|
document.getElementById('manualJsonBtn').textContent = '{ } Show JSON';
|
|
hideManualMessage();
|
|
}
|
|
|
|
function showManualMessage(msg, type) {
|
|
const msgDiv = document.getElementById('manualMessage');
|
|
msgDiv.textContent = msg;
|
|
msgDiv.className = `message show ${type}`;
|
|
}
|
|
|
|
function hideManualMessage() {
|
|
document.getElementById('manualMessage').classList.remove('show');
|
|
}
|
|
|
|
function showMessage(msg, type) {
|
|
const msgDiv = document.getElementById('message');
|
|
msgDiv.textContent = msg;
|
|
msgDiv.className = `message show ${type}`;
|
|
}
|
|
|
|
function hideMessage() {
|
|
document.getElementById('message').classList.remove('show');
|
|
}
|
|
|
|
// Load initial data
|
|
loadProfile();
|
|
</script>
|
|
</body>
|
|
</html>
|