function switchRecordType(type) { currentType = type; document.querySelectorAll('.type-btn').forEach((b, i) => { b.classList.toggle('active', (type === 'income' && i === 0) || (type === 'expense' && i === 1)); }); document.getElementById('incomeCats').style.display = type === 'income' ? 'grid' : 'none'; document.getElementById('expenseCats').style.display = type === 'expense' ? 'grid' : 'none'; const firstCat = type === 'income' ? document.querySelector('#incomeCats .category-item') : document.querySelector('#expenseCats .category-item'); const catName = type === 'income' ? '接单' : '红包支出'; selectCategory(firstCat, catName); } function selectCategory(el, cat) { currentCategory = cat; el.parentElement.querySelectorAll('.category-item').forEach(c => c.classList.remove('active')); el.classList.add('active'); renderForm(); } function renderForm() { const form = document.getElementById('recordForm'); const now = new Date(); const defaultTime = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString().slice(0, 16); let html = ''; if (currentCategory === '接单') { html = `
预计收入
0.00
`; } else if (currentCategory === '派单') { html = `
预计收入
0.00
`; } else if (currentCategory === '红包收入') { html = `
`; } else if (currentCategory === '红包支出') { html = `
`; } else if (currentCategory === '其他支出') { html = `
`; } else if (currentCategory === '奶茶') { html = `
`; } else if (currentCategory === '比心') { html = `
`; } form.innerHTML = html; // 绑定自动补全 if (currentCategory === '接单' || currentCategory === '派单') { acSetup('fBoss', 'boss'); acSetup('fPartner', 'partner'); } else if (currentCategory === '红包收入' || currentCategory === '奶茶') { acSetup('fSource', 'source'); } else if (currentCategory === '红包支出' || currentCategory === '其他支出') { acSetup('fDest', 'destination'); } else if (currentCategory === '比心') { acSetup('fDest', 'destination'); } } function calcAmount() { const q = parseFloat(document.getElementById('fQuantity')?.value) || 0; let price; if (currentCategory === '派单') { price = parseFloat(document.getElementById('fRebate')?.value) || 0; } else { price = parseFloat(document.getElementById('fPrice')?.value) || 0; } const el = document.getElementById('calcTotal'); if (el) el.textContent = '' + (q * price).toFixed(2); } async function submitRecord() { let body = { type: currentType, category: currentCategory }; const fTime = document.getElementById('fTime')?.value; if (fTime) body.created_at = new Date(fTime).toISOString(); if (currentCategory === '接单' || currentCategory === '派单') { const quantity = parseInt(document.getElementById('fQuantity').value); const unit_price = parseFloat(document.getElementById('fPrice').value); const boss = document.getElementById('fBoss').value.trim(); const partner = document.getElementById('fPartner')?.value.trim() || ''; if (currentCategory === '派单') { const rebate = parseFloat(document.getElementById('fRebate').value); if (!quantity || !unit_price || !rebate || !boss || !partner) { showToast('请填写完整信息(包括负责打单的陪玩)'); return; } body.quantity = quantity; body.unit_price = unit_price; body.rebate = rebate; body.amount = quantity * rebate; body.partner = partner; } else { if (!quantity || !unit_price || !boss) { showToast('请填写完整信息'); return; } body.quantity = quantity; body.unit_price = unit_price; body.amount = quantity * unit_price; } body.boss = boss; } else if (currentCategory === '红包收入' || currentCategory === '奶茶') { const amount = parseFloat(document.getElementById('fAmount').value); const source = document.getElementById('fSource').value.trim(); if (!amount || !source) { showToast('请填写完整信息'); return; } body.amount = amount; body.source = source; body.boss = source; // 红包来源就是老板,同步设置 boss 字段 } else if (currentCategory === '红包支出' || currentCategory === '其他支出' || currentCategory === '比心') { const amount = parseFloat(document.getElementById('fAmount').value); if (!amount) { showToast('请填写完整信息'); return; } body.amount = amount; if (currentCategory === '比心') { const dest = document.getElementById('fDest').value.trim(); body.destination = dest || '比心'; } else { const dest = document.getElementById('fDest').value.trim(); if (!dest) { showToast('请填写完整信息'); return; } body.destination = dest; } body.note = document.getElementById('fNote')?.value.trim() || ''; } try { const res = await apiFetch(API + '/api/records', { method: 'POST', body: JSON.stringify(body) }); if (!res.ok) { showToast('保存失败'); return; } showToast('保存成功'); acInvalidateCache(); setTimeout(() => showPage('homePage'), 1000); } catch (e) { showToast('网络错误'); } }