175 lines
9.4 KiB
JavaScript
175 lines
9.4 KiB
JavaScript
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 = `
|
|
<div class="form-group"><label>单数</label><input type="number" id="fQuantity" placeholder="请输入单数" oninput="calcAmount()"></div>
|
|
<div class="form-group"><label>单价(元)</label><input type="number" step="0.01" id="fPrice" placeholder="请输入单价" oninput="calcAmount()"></div>
|
|
<div class="form-group"><label>老板</label><input type="text" id="fBoss" placeholder="请输入老板名称"></div>
|
|
<div class="form-group"><label>一起的陪玩(可选)</label><input type="text" id="fPartner" placeholder="请输入陪玩名称"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<div class="computed-amount">预计收入<div class="total" id="calcTotal">0.00</div></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
} else if (currentCategory === '派单') {
|
|
html = `
|
|
<div class="form-group"><label>单数</label><input type="number" id="fQuantity" placeholder="请输入单数" oninput="calcAmount()"></div>
|
|
<div class="form-group"><label>单价(元)</label><input type="number" step="0.01" id="fPrice" placeholder="请输入单价"></div>
|
|
<div class="form-group"><label>返点(元)</label><input type="number" step="0.01" id="fRebate" placeholder="请输入返点" oninput="calcAmount()"></div>
|
|
<div class="form-group"><label>老板</label><input type="text" id="fBoss" placeholder="请输入老板名称"></div>
|
|
<div class="form-group"><label>负责打单的陪玩</label><input type="text" id="fPartner" placeholder="请输入陪玩名称"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<div class="computed-amount">预计收入<div class="total" id="calcTotal">0.00</div></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
} else if (currentCategory === '红包收入') {
|
|
html = `
|
|
<div class="form-group"><label>红包金额(元)</label><input type="number" step="0.01" id="fAmount" placeholder="请输入金额"></div>
|
|
<div class="form-group"><label>红包来源</label><input type="text" id="fSource" placeholder="发红包人的名称"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
} else if (currentCategory === '红包支出') {
|
|
html = `
|
|
<div class="form-group"><label>红包金额(元)</label><input type="number" step="0.01" id="fAmount" placeholder="请输入金额"></div>
|
|
<div class="form-group"><label>红包目的地</label><input type="text" id="fDest" placeholder="收红包人的名称"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
} else if (currentCategory === '其他支出') {
|
|
html = `
|
|
<div class="form-group"><label>金额(元)</label><input type="number" step="0.01" id="fAmount" placeholder="请输入金额"></div>
|
|
<div class="form-group"><label>支出目的地</label><input type="text" id="fDest" placeholder="收款人名称"></div>
|
|
<div class="form-group"><label>备注(可选)</label><input type="text" id="fNote" placeholder="备注信息"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
} else if (currentCategory === '奶茶') {
|
|
html = `
|
|
<div class="form-group"><label>金额(元)</label><input type="number" step="0.01" id="fAmount" placeholder="请输入金额"></div>
|
|
<div class="form-group"><label>来源</label><input type="text" id="fSource" placeholder="奶茶来源"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
} else if (currentCategory === '比心') {
|
|
html = `
|
|
<div class="form-group"><label>金额(元)</label><input type="number" step="0.01" id="fAmount" placeholder="请输入金额"></div>
|
|
<div class="form-group"><label>用途</label><input type="text" id="fDest" placeholder="请输入用途"></div>
|
|
<div class="form-group"><label>时间</label><input type="datetime-local" id="fTime" value="${defaultTime}"></div>
|
|
<button class="submit-btn" onclick="submitRecord()">保存记录</button>
|
|
`;
|
|
}
|
|
|
|
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) { showToast('请填写完整信息'); return; }
|
|
body.quantity = quantity;
|
|
body.unit_price = unit_price;
|
|
body.rebate = rebate;
|
|
body.amount = quantity * rebate;
|
|
} else {
|
|
if (!quantity || !unit_price || !boss) { showToast('请填写完整信息'); return; }
|
|
body.quantity = quantity;
|
|
body.unit_price = unit_price;
|
|
body.amount = quantity * unit_price;
|
|
}
|
|
body.boss = boss;
|
|
body.partner = partner;
|
|
} 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 fetch(API + '/api/records', {
|
|
method: 'POST',
|
|
headers: headers(),
|
|
body: JSON.stringify(body)
|
|
});
|
|
if (!res.ok) { showToast('保存失败'); return; }
|
|
showToast('保存成功');
|
|
acInvalidateCache();
|
|
setTimeout(() => showPage('homePage'), 1000);
|
|
} catch (e) {
|
|
showToast('网络错误');
|
|
}
|
|
}
|