function openEditModal(record) { document.getElementById('editId').value = record.id; document.getElementById('editCategory').value = record.category || ''; const cat = record.category; const body = document.getElementById('editFormBody'); let localTime = ''; if (record.created_at) { const dt = new Date(record.created_at); localTime = new Date(dt.getTime() - dt.getTimezoneOffset() * 60000).toISOString().slice(0, 16); } if (cat === '接单' || cat === '派单') { document.getElementById('editModalTitle').textContent = cat === '派单' ? '编辑派单记录' : '编辑接单记录'; let fields = `
`; if (cat === '派单') { const rebateVal = record.rebate || (record.quantity ? (record.amount / record.quantity) : ''); fields += `
`; } fields += `
`; body.innerHTML = fields; } else if (cat === '红包收入' || cat === '奶茶') { document.getElementById('editModalTitle').textContent = '编辑' + cat + '记录'; body.innerHTML = `
`; } else if (cat === '红包支出' || cat === '比心' || cat === '其他支出') { document.getElementById('editModalTitle').textContent = '编辑' + cat + '记录'; let fields = `
`; if (cat !== '比心') { fields += `
`; } else { fields += `
`; } if (cat === '其他支出') { fields += `
`; } fields += `
`; body.innerHTML = fields; } else { document.getElementById('editModalTitle').textContent = '编辑记录'; body.innerHTML = `
`; } // 绑定自动补全 if (cat === '接单' || cat === '派单') { acSetup('editBoss', 'boss'); acSetup('editPartner', 'partner'); } else if (cat === '红包收入' || cat === '奶茶') { acSetup('editSource', 'source'); } else if (cat === '红包支出' || cat === '比心' || cat === '其他支出') { acSetup('editDest', 'destination'); } document.getElementById('editModal').classList.add('show'); } function closeEditModal() { document.getElementById('editModal').classList.remove('show'); } function refreshCurrentList() { const cat = document.getElementById('editCategory').value; if (cat === '派单') { dispatchPage = 1; dispatchHasMore = true; document.getElementById('dispatchList').innerHTML = ''; loadDispatches(); } else if (cat === '接单') { orderPage = 1; orderHasMore = true; document.getElementById('orderList').innerHTML = ''; loadOrders(); } else { otherIncomePage = 1; otherIncomeHasMore = true; document.getElementById('otherIncomeList').innerHTML = ''; loadOtherIncome(); } // 如果在日详情页也刷新 if (document.getElementById('dailyDetailPage').classList.contains('active') && currentDailyDate) { openDailyDetail(currentDailyDate); } } async function saveEditRecord() { const id = document.getElementById('editId').value; const cat = document.getElementById('editCategory').value; const time = document.getElementById('editTime')?.value; let body = {}; if (cat === '接单' || cat === '派单') { const quantity = parseInt(document.getElementById('editQuantity').value) || 0; const unit_price = parseFloat(document.getElementById('editPrice').value) || 0; const boss = document.getElementById('editBoss').value.trim(); const partner = document.getElementById('editPartner').value.trim(); if (cat === '派单') { const rebate = parseFloat(document.getElementById('editRebate').value) || 0; if (!quantity || !unit_price || !rebate || !boss) { showToast('请填写完整信息'); return; } body = { quantity, unit_price, rebate, amount: quantity * rebate, boss, partner }; } else { if (!quantity || !unit_price || !boss) { showToast('请填写完整信息'); return; } body = { quantity, unit_price, amount: quantity * unit_price, boss, partner }; } } else if (cat === '红包收入' || cat === '奶茶') { const amount = parseFloat(document.getElementById('editAmount').value); const source = document.getElementById('editSource').value.trim(); if (!amount || !source) { showToast('请填写完整信息'); return; } body = { amount, source }; } else if (cat === '红包支出' || cat === '比心' || cat === '其他支出') { const amount = parseFloat(document.getElementById('editAmount').value); if (!amount) { showToast('请填写完整信息'); return; } body = { amount }; if (cat === '比心') { const dest = document.getElementById('editDest').value.trim(); body.destination = dest || '比心'; } else { const dest = document.getElementById('editDest').value.trim(); if (!dest) { showToast('请填写完整信息'); return; } body.destination = dest; } body.note = document.getElementById('editNote')?.value.trim() || ''; } else { const amount = parseFloat(document.getElementById('editAmount').value); if (!amount) { showToast('请填写完整信息'); return; } body = { amount }; } if (time) body.created_at = new Date(time).toISOString(); try { const res = await fetch(API + '/api/records/' + id, { method: 'PUT', headers: headers(), body: JSON.stringify(body) }); if (!res.ok) { showToast('保存失败'); return; } showToast('保存成功'); acInvalidateCache(); closeEditModal(); refreshCurrentList(); } catch (e) { showToast('网络错误'); } } async function deleteEditRecord() { const id = document.getElementById('editId').value; if (!confirm('确定删除这条记录?')) return; try { const res = await fetch(API + '/api/records/' + id, { method: 'DELETE', headers: headers() }); if (!res.ok) { showToast('删除失败'); return; } showToast('已删除'); closeEditModal(); refreshCurrentList(); } catch (e) { showToast('网络错误'); } }