133 lines
5.8 KiB
JavaScript
133 lines
5.8 KiB
JavaScript
const SUPABASE_URL = 'https://chixssrphfgxvqqigkzo.supabase.co';
|
|
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNoaXhzc3JwaGZneHZxcWlna3pvIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzQ2OTE0OTEsImV4cCI6MjA5MDI2NzQ5MX0.Az_Ew2J2zdOMcSV0UNAjBS-LPqGpqhsaN4IyZ5R7iqU';
|
|
const sbClient = supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
|
|
|
let html5QrCode = null;
|
|
|
|
document.getElementById('startScanBtn').addEventListener('click', async () => {
|
|
document.getElementById('scanner-overlay').style.display = 'flex';
|
|
if (html5QrCode) {
|
|
try {
|
|
await html5QrCode.stop();
|
|
} catch (e) {}
|
|
}
|
|
html5QrCode = new Html5Qrcode("reader");
|
|
const config = { fps: 15, qrbox: { width: 250, height: 250 }, aspectRatio: 1.0 };
|
|
try {
|
|
await html5QrCode.start({ facingMode: "environment" }, config, (text) => {
|
|
document.getElementById('searchTerm').value = text;
|
|
closeScanner();
|
|
handleSearch();
|
|
});
|
|
} catch (err) {
|
|
alert("摄像头启动失败。");
|
|
closeScanner();
|
|
}
|
|
});
|
|
|
|
async function closeScanner() {
|
|
if (html5QrCode && html5QrCode.isScanning) {
|
|
await html5QrCode.stop();
|
|
}
|
|
document.getElementById('scanner-overlay').style.display = 'none';
|
|
}
|
|
|
|
document.getElementById('stopScanBtn').addEventListener('click', closeScanner);
|
|
|
|
async function handleSearch() {
|
|
const searchTerm = document.getElementById('searchTerm').value.trim();
|
|
const dateFrom = document.getElementById('dateFrom').value;
|
|
const dateTo = document.getElementById('dateTo').value;
|
|
const resultsDiv = document.getElementById('searchResults');
|
|
|
|
resultsDiv.innerHTML = '<div class="loading-state">正在连接单据数据库并执行检索...</div>';
|
|
|
|
try {
|
|
let query = sbClient.from('tickets').select('*');
|
|
if (searchTerm) {
|
|
query = query.or(`ticket_number.ilike.%${searchTerm}%,customer_name.ilike.%${searchTerm}%`);
|
|
}
|
|
if (dateFrom) query = query.gte('created_at', dateFrom);
|
|
if (dateTo) query = query.lte('created_at', dateTo);
|
|
|
|
const { data, error } = await query.order('created_at', { ascending: false });
|
|
if (error) throw error;
|
|
|
|
displayResults(data);
|
|
} catch (error) {
|
|
resultsDiv.innerHTML = `<div class="error-state">查询失败:${error.message}</div>`;
|
|
}
|
|
}
|
|
|
|
function displayResults(tickets) {
|
|
const resultsDiv = document.getElementById('searchResults');
|
|
if (!tickets || tickets.length === 0) {
|
|
resultsDiv.innerHTML = '<div class="empty-state">未找到符合条件的单据记录。</div>';
|
|
return;
|
|
}
|
|
|
|
let html = `
|
|
<div class="table-wrapper">
|
|
<table class="result-table">
|
|
<thead>
|
|
<tr>
|
|
<th>编号</th>
|
|
<th>姓名</th>
|
|
<th>事由</th>
|
|
<th>金额</th>
|
|
<th>日期</th>
|
|
<th>状态</th>
|
|
<th>操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
`;
|
|
|
|
tickets.forEach(ticket => {
|
|
const amount = (ticket.amount || 0).toLocaleString('zh-CN', { style: 'currency', currency: 'CNY' });
|
|
const reason = ticket.reason || '';
|
|
const displayReason = reason.length > 18
|
|
? `<span class="text-truncate" title="${reason}">${reason}</span>`
|
|
: reason;
|
|
|
|
html += `
|
|
<tr>
|
|
<td class="table-highlight">${ticket.ticket_number || ''}</td>
|
|
<td>${ticket.customer_name || ''}</td>
|
|
<td>${displayReason}</td>
|
|
<td class="table-highlight">${amount}</td>
|
|
<td>${ticket.created_at ? new Date(ticket.created_at).toLocaleDateString('zh-CN') : '-'}</td>
|
|
<td>
|
|
<span class="status-pill ${ticket.processed ? 'done' : 'pending'}">
|
|
${ticket.processed ? '已处理' : '未处理'}
|
|
</span>
|
|
</td>
|
|
<td><button class="tech-link-button secondary" type="button" onclick="location.href='detail.html?id=${ticket.id}'">查看详情</button></td>
|
|
</tr>
|
|
`;
|
|
});
|
|
|
|
html += '</tbody></table></div>';
|
|
resultsDiv.innerHTML = html;
|
|
}
|
|
|
|
function initializeSearchTermFromUrl() {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const presetTerm =
|
|
params.get('searchTerm') ||
|
|
params.get('term') ||
|
|
params.get('keyword') ||
|
|
params.get('ticket_number') ||
|
|
params.get('ticketNumber') ||
|
|
params.get('number') ||
|
|
params.get('q') ||
|
|
'';
|
|
|
|
if (presetTerm) {
|
|
document.getElementById('searchTerm').value = presetTerm;
|
|
}
|
|
}
|
|
|
|
document.getElementById('searchBtn').addEventListener('click', handleSearch);
|
|
initializeSearchTermFromUrl();
|