Files
gamer/public/js/edit-modal.js
T

161 lines
7.6 KiB
JavaScript
Raw Normal View History

2026-03-12 11:23:10 +08:00
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 = `
<div class="form-group"><label>单数</label><input type="number" id="editQuantity" value="${record.quantity || ''}"></div>
<div class="form-group"><label>单价(元)</label><input type="number" step="0.01" id="editPrice" value="${record.unit_price || ''}"></div>
`;
if (cat === '派单') {
const rebateVal = record.rebate || (record.quantity ? (record.amount / record.quantity) : '');
fields += `<div class="form-group"><label>返点(元)</label><input type="number" step="0.01" id="editRebate" value="${rebateVal}"></div>`;
}
fields += `
<div class="form-group"><label>老板</label><input type="text" id="editBoss" value="${record.boss || ''}"></div>
<div class="form-group"><label>${cat === '派单' ? '负责打单的陪玩' : '一起的陪玩'}</label><input type="text" id="editPartner" value="${record.partner || ''}"></div>
<div class="form-group"><label>时间</label><input type="datetime-local" id="editTime" value="${localTime}"></div>
`;
body.innerHTML = fields;
} else if (cat === '红包收入' || cat === '奶茶') {
document.getElementById('editModalTitle').textContent = '编辑' + cat + '记录';
body.innerHTML = `
<div class="form-group"><label>金额(元)</label><input type="number" step="0.01" id="editAmount" value="${record.amount || ''}"></div>
<div class="form-group"><label>来源</label><input type="text" id="editSource" value="${record.source || ''}"></div>
<div class="form-group"><label>时间</label><input type="datetime-local" id="editTime" value="${localTime}"></div>
`;
} else if (cat === '红包支出' || cat === '比心' || cat === '其他支出') {
document.getElementById('editModalTitle').textContent = '编辑' + cat + '记录';
let fields = `
<div class="form-group"><label>金额(元)</label><input type="number" step="0.01" id="editAmount" value="${record.amount || ''}"></div>
`;
if (cat !== '比心') {
fields += `<div class="form-group"><label>目的地</label><input type="text" id="editDest" value="${record.destination || ''}"></div>`;
} else {
fields += `<div class="form-group"><label>用途</label><input type="text" id="editDest" value="${record.destination || ''}"></div>`;
}
if (cat === '其他支出') {
fields += `<div class="form-group"><label>备注</label><input type="text" id="editNote" value="${record.note || ''}"></div>`;
}
fields += `<div class="form-group"><label>时间</label><input type="datetime-local" id="editTime" value="${localTime}"></div>`;
body.innerHTML = fields;
} else {
document.getElementById('editModalTitle').textContent = '编辑记录';
body.innerHTML = `
<div class="form-group"><label>金额(元)</label><input type="number" step="0.01" id="editAmount" value="${record.amount || ''}"></div>
<div class="form-group"><label>时间</label><input type="datetime-local" id="editTime" value="${localTime}"></div>
`;
}
// 绑定自动补全
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 === '派单') {
2026-03-25 22:12:11 +08:00
initList('dispatchList');
loadListData('dispatchList');
2026-03-12 11:23:10 +08:00
} else if (cat === '接单') {
2026-03-25 22:12:11 +08:00
initList('orderList');
loadListData('orderList');
2026-03-12 11:23:10 +08:00
} else {
2026-03-25 22:12:11 +08:00
initList('otherIncomeList');
loadListData('otherIncomeList');
2026-03-12 11:23:10 +08:00
}
// 如果在日详情页也刷新
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 {
2026-07-01 16:15:23 +08:00
const res = await apiFetch(API + '/api/records/' + id, {
2026-03-12 11:23:10 +08:00
method: 'PUT',
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 {
2026-07-01 16:15:23 +08:00
const res = await apiFetch(API + '/api/records/' + id, { method: 'DELETE' });
2026-03-12 11:23:10 +08:00
if (!res.ok) { showToast('删除失败'); return; }
showToast('已删除');
closeEditModal();
refreshCurrentList();
} catch (e) { showToast('网络错误'); }
}