File size: 16,770 Bytes
8f4045a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>库存管理ERP系统</title>
<style>

* { margin:0; padding:0; box-sizing:border-box; }

body { font-family:'Segoe UI',system-ui,-apple-system,sans-serif; background:#f0f2f5; color:#333; }

.header { background:linear-gradient(135deg, #1a237e, #283593); color:#fff; padding:18px 24px; display:flex; align-items:center; justify-content:space-between; box-shadow:0 2px 8px rgba(0,0,0,0.2); }

.header h1 { font-size:22px; font-weight:600; }

.header .badge { background:rgba(255,255,255,0.2); padding:6px 14px; border-radius:20px; font-size:13px; }

.container { max-width:1400px; margin:24px auto; padding:0 20px; }

.stats { display:grid; grid-template-columns:repeat(auto-fit,minmax(220px,1fr)); gap:16px; margin-bottom:24px; }

.stat-card { background:#fff; border-radius:12px; padding:20px; box-shadow:0 2px 6px rgba(0,0,0,0.08); }

.stat-card .label { font-size:13px; color:#666; margin-bottom:8px; }

.stat-card .value { font-size:28px; font-weight:700; color:#1a237e; }

.stat-card.warn .value { color:#ef6c00; }

.toolbar { background:#fff; border-radius:12px; padding:16px 20px; margin-bottom:20px; box-shadow:0 2px 6px rgba(0,0,0,0.08); display:flex; flex-wrap:wrap; gap:12px; align-items:center; }

.toolbar input[type="text"], .toolbar select { padding:8px 12px; border:1px solid #ccc; border-radius:6px; font-size:14px; outline:none; }

.toolbar input[type="text"] { width:240px; }

.toolbar .btn-primary { background:#1a237e; color:#fff; border:none; padding:9px 18px; border-radius:6px; cursor:pointer; font-size:14px; }

.toolbar .btn-primary:hover { background:#283593; }

.table-wrapper { background:#fff; border-radius:12px; box-shadow:0 2px 6px rgba(0,0,0,0.08); overflow-x:auto; }

table { width:100%; border-collapse:collapse; min-width:1100px; }

th { background:#f5f5f5; color:#555; font-size:13px; font-weight:600; text-align:left; padding:12px 14px; border-bottom:2px solid #e0e0e0; white-space:nowrap; }

td { padding:10px 14px; border-bottom:1px solid #f0f0f0; font-size:14px; white-space:nowrap; }

tr:hover { background:#f9f9ff; }

.status { padding:4px 10px; border-radius:12px; font-size:12px; font-weight:600; }

.status.ok { background:#e8f5e9; color:#2e7d32; }

.status.low { background:#fff3e0; color:#ef6c00; }

.status.out { background:#ffebee; color:#c62828; }

tr.low-row { background:#fff8e1; }

tr.out-row { background:#ffebee; }

.actions { display:flex; gap:6px; flex-wrap:wrap; }

.actions button { padding:4px 10px; border:none; border-radius:4px; cursor:pointer; font-size:12px; background:#e3f2fd; color:#1565c0; }

.actions button:hover { background:#bbdefb; }

.actions button.warn-btn { background:#fff3e0; color:#e65100; }

.actions button.danger { background:#ffebee; color:#c62828; }

.actions button.purchase-btn { background:#e8f5e9; color:#2e7d32; animation:pulse 2s infinite; }

@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:0.5; } }

.modal { display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5); z-index:1000; justify-content:center; align-items:center; }

.modal.active { display:flex; }

.modal-content { background:#fff; border-radius:12px; padding:30px; width:400px; box-shadow:0 4px 20px rgba(0,0,0,0.3); }

.modal-content h3 { margin-bottom:20px; color:#1a237e; }

.modal-content label { display:block; margin-bottom:6px; font-size:13px; color:#555; }

.modal-content input, .modal-content select, .modal-content textarea { width:100%; padding:8px 12px; border:1px solid #ccc; border-radius:6px; margin-bottom:16px; font-size:14px; }

.modal-btns { display:flex; justify-content:flex-end; gap:10px; }

.modal-btns button { padding:8px 16px; border:none; border-radius:6px; cursor:pointer; font-size:14px; }

.modal-btns .cancel { background:#eee; color:#333; }

.modal-btns .confirm { background:#1a237e; color:#fff; }

.toast { position:fixed; bottom:20px; left:50%; transform:translateX(-50%); background:#333; color:#fff; padding:12px 24px; border-radius:8px; font-size:14px; opacity:0; transition:opacity 0.3s; pointer-events:none; z-index:2000; }

.toast.show { opacity:1; }

#csvLink { color:#fff; text-decoration:none; background:rgba(255,255,255,0.2); padding:6px 12px; border-radius:6px; font-size:13px; }

</style>
</head>
<body>
<div class="header">
  <h1>📦 库存管理ERP系统</h1>
  <div class="badge">库存管理 | 出库 · 入库 · 占用 · 预采购</div>
</div>
<div class="container">
  <div class="stats">
    <div class="stat-card"><div class="label">总SKU数</div><div class="value" id="statTotal">-</div></div>
    <div class="stat-card"><div class="label">总库存量</div><div class="value" id="statOnHand">-</div></div>
    <div class="stat-card warn"><div class="label">低库存预警</div><div class="value" id="statLow">-</div></div>
    <div class="stat-card"><div class="label">总占用量</div><div class="value" id="statReserved">-</div></div>
  </div>
  <div class="toolbar">
    <input type="text" id="searchInput" placeholder="搜索SKU或商品名称...">
    <select id="warehouseFilter">
      <option value="">全部仓库</option>
      <option>华东仓</option><option>华南仓</option><option>华北仓</option>
    </select>
    <select id="statusFilter">
      <option value="">全部状态</option>
      <option>正常</option><option>低库存</option><option>缺货</option>
    </select>
    <select id="categoryFilter">
      <option value="">全部分类</option>
      <option>电子产品</option><option>办公用品</option><option>家居用品</option><option>食品饮料</option>
      <option>服装鞋帽</option><option>汽车配件</option><option>医药健康</option><option>运动户外</option>
    </select>
    <button class="btn-primary" onclick="openCreateModal()">+ 新增商品</button>
    <span style="flex:1"></span>
    <span id="csvLink">CSV 数据保存在当前隔离预览中</span>
  </div>
  <div class="table-wrapper">
    <table>
      <thead>
        <tr>
          <th>SKU</th><th>名称</th><th>分类</th><th>仓库</th><th>现有库存</th><th>占用</th>
          <th>可用</th><th>安全库存</th><th>待采购</th><th>状态</th><th>单价</th><th>操作</th>
        </tr>
      </thead>
      <tbody id="tableBody"></tbody>
    </table>
  </div>
</div>

<div class="toolbar"><button id="prevPage" onclick="currentPage--;loadData()">上一页</button><span id="pageInfo"></span><button id="nextPage" onclick="currentPage++;loadData()">下一页</button></div>
<div class="modal" id="modal">
  <div class="modal-content">
    <h3 id="modalTitle"></h3>
    <div id="modalBody"></div>
    <div class="modal-btns">
      <button class="cancel" onclick="closeModal()">取消</button>
      <button class="confirm" onclick="confirmAction()">确认</button>
    </div>
  </div>
</div>

<div class="toast" id="toast"></div>

<script>

let allRecords = [];

let currentPage=1,totalRecords=0,summary={};

let currentModal = { type: '', id: '' };



async function loadData() {

  try {

    const q = document.getElementById('searchInput').value.trim();

    const wh = document.getElementById('warehouseFilter').value;

    const status = document.getElementById('statusFilter').value;

    const cat = document.getElementById('categoryFilter').value;

    let path = '/api/app/list?page='+currentPage+'&pageSize=20&';

    if (q) path += 'q=' + encodeURIComponent(q) + '&';

    if (wh) path += 'warehouse=' + encodeURIComponent(wh) + '&';

    if (status) path += 'status=' + encodeURIComponent(status) + '&';

    if (cat) path += 'category=' + encodeURIComponent(cat) + '&';

    const data = await window.zkRequest('GET', path);

    allRecords = data.records || [];

    totalRecords=data.total;currentPage=data.page;summary=data.summary;

    document.getElementById("pageInfo").textContent=`第 ${currentPage} / ${Math.max(1,Math.ceil(totalRecords/20))} 页 · 共 ${totalRecords} 条`;

    document.getElementById("prevPage").disabled=currentPage<=1;document.getElementById("nextPage").disabled=currentPage*20>=totalRecords;

    renderTable();

    renderStats();

  } catch (e) {

    showToast('加载失败: ' + e.message);

  }

}



function renderStats() {

  let total=totalRecords,onHand=summary.onHand||0,low=summary.low||0,reserved=summary.reserved||0;

  document.getElementById('statTotal').textContent = total;

  document.getElementById('statOnHand').textContent = onHand;

  document.getElementById('statLow').textContent = low;

  document.getElementById('statReserved').textContent = reserved;

}



function renderTable() {

  const tbody = document.getElementById('tableBody');

  tbody.innerHTML = '';

  if (!allRecords.length) {

    tbody.innerHTML = '<tr><td colspan="11" style="text-align:center;padding:40px;color:#999">暂无数据</td></tr>';

    return;

  }

  allRecords.forEach(r => {

    const tr = document.createElement('tr');

    if (r.status === '低库存') tr.className = 'low-row';

    else if (r.status === '缺货') tr.className = 'out-row';

    let statusClass = 'ok';

    if (r.status === '低库存') statusClass = 'low';

    else if (r.status === '缺货') statusClass = 'out';

    let actions = '';

    actions += `<button onclick="openQtyModal('stockIn','${r.id}')">入库</button>`;

    actions += `<button onclick="openQtyModal('stockOut','${r.id}')">出库</button>`;

    actions += `<button onclick="openQtyModal('reserve','${r.id}')">占用</button>`;

    actions += `<button onclick="openQtyModal('release','${r.id}')">释放</button>`;

    if (r.status === '低库存' || r.status === '缺货') {

      actions += `<button class="purchase-btn" onclick="openPurchaseModal('${r.id}')">预采购</button>`;

    }

    actions += `<button class="danger" onclick="deleteItem('${r.id}')">删除</button>`;

    tr.innerHTML = `

      <td><strong>${r.sku}</strong></td>

      <td>${r.name}</td>

      <td>${r.category}</td>

      <td>${r.warehouse}</td>

      <td>${r.onHand}</td>

      <td>${r.reserved}</td>

      <td>${r.available}</td>

      <td>${r.safetyStock}</td><td>${r.pendingPurchase||0}</td>

      <td><span class="status ${statusClass}">${r.status}</span></td>

      <td>¥${r.unitPrice.toFixed(2)}</td>

      <td class="actions">${actions}</td>

    `;

    tbody.appendChild(tr);

  });

}



function openQtyModal(action, id) {

  const r = allRecords.find(x => x.id === id);

  if (!r) return;

  const labels = { stockIn: '入库', stockOut: '出库', reserve: '库存占用', release: '释放占用' };

  const btnText = labels[action] || action;

  currentModal = { type: action, id: id };

  document.getElementById('modalTitle').textContent = btnText + ' - ' + r.sku;

  document.getElementById('modalBody').innerHTML = `

    <label>数量</label>

    <input type="number" id="qtyInput" min="1" step="1" placeholder="请输入数量">

    <label>备注</label>

    <textarea id="remarkInput" rows="2" placeholder="可选备注"></textarea>

    <p style="font-size:12px;color:#888">当前可用: ${r.available} | 占用: ${r.reserved}</p>

  `;

  document.getElementById('modal').classList.add('active');

}



function openPurchaseModal(id) {

  const r = allRecords.find(x => x.id === id);

  if (!r) return;

  const need = Math.max(r.safetyStock - r.available, 1);

  currentModal = { type: 'prePurchase', id: id };

  document.getElementById('modalTitle').textContent = '预采购建议 - ' + r.sku;

  document.getElementById('modalBody').innerHTML = `

    <label>建议采购数量</label>

    <input type="number" id="qtyInput" value="${need}" min="1" step="1">

    <label>供应商</label>

    <input type="text" id="supplierInput" value="默认供应商" placeholder="供应商名称">

    <label>备注</label>

    <textarea id="remarkInput" rows="2" placeholder="采购备注"></textarea>

    <p style="font-size:12px;color:#ef6c00">该商品可用库存低于安全库存,建议补货</p>

  `;

  document.getElementById('modal').classList.add('active');

}



function openCreateModal() {

  currentModal = { type: 'create', id: '' };

  document.getElementById('modalTitle').textContent = '新增库存商品';

  document.getElementById('modalBody').innerHTML = `

    <label>SKU *</label>

    <input type="text" id="createSku" placeholder="唯一SKU编码">

    <label>名称 *</label>

    <input type="text" id="createName" placeholder="商品名称">

    <label>分类 *</label>

    <select id="createCategory">

      <option>电子产品</option><option>办公用品</option><option>家居用品</option><option>食品饮料</option>

      <option>服装鞋帽</option><option>汽车配件</option><option>医药健康</option><option>运动户外</option>

    </select>

    <label>仓库 *</label>

    <select id="createWarehouse">

      <option>华东仓</option><option>华南仓</option><option>华北仓</option>

    </select>

    <label>现有库存</label>

    <input type="number" id="createOneHand" value="0" min="0">

    <label>安全库存</label>

    <input type="number" id="createSafety" value="10" min="0">

    <label>单价</label>

    <input type="number" id="createPrice" value="100" min="0" step="0.01">

  `;

  document.getElementById('modal').classList.add('active');

}



function closeModal() {

  document.getElementById('modal').classList.remove('active');

}



async function confirmAction() {

  const m = currentModal;

  if (!m.type) return;

  try {

    let path = '/api/app/action?action=' + m.type + '&id=' + encodeURIComponent(m.id);

    if (m.type === 'create') {

      const sku = document.getElementById('createSku').value.trim();

      const name = document.getElementById('createName').value.trim();

      const category = document.getElementById('createCategory').value;

      const warehouse = document.getElementById('createWarehouse').value;

      const onHand = document.getElementById('createOneHand').value || '0';

      const safety = document.getElementById('createSafety').value || '0';

      const price = document.getElementById('createPrice').value || '0';

      if (!sku || !name) { showToast('请填写SKU和名称'); return; }

      path += '&sku=' + encodeURIComponent(sku) + '&name=' + encodeURIComponent(name) + '&category=' + encodeURIComponent(category) + '&warehouse=' + encodeURIComponent(warehouse) + '&onHand=' + onHand + '&safetyStock=' + safety + '&unitPrice=' + price;

    } else {

      const qty = document.getElementById('qtyInput').value.trim();

      if (!qty || parseInt(qty) <= 0) { showToast('请输入有效的数量'); return; }

      path += '&quantity=' + qty;

      const remark = document.getElementById('remarkInput') ? document.getElementById('remarkInput').value.trim() : '';

      if (remark) path += '&remark=' + encodeURIComponent(remark);

      if (m.type === 'prePurchase') {

        const supplier = document.getElementById('supplierInput').value.trim() || '默认供应商';

        path += '&supplier=' + encodeURIComponent(supplier);

      }

    }

    const result = await window.zkRequest('POST', path);

    if (result.ok) {

      showToast('操作成功');

      closeModal();

      loadData();

    } else {

      showToast('操作失败: ' + (result.error || '未知错误'));

    }

  } catch (e) {

    showToast('错误: ' + e.message);

  }

}



async function deleteItem(id) {

  if (!confirm('确定删除该商品?')) return;

  try {

    const result = await window.zkRequest('POST', '/api/app/action?action=delete&id=' + encodeURIComponent(id));

    if (result.ok) {

      showToast('删除成功');

      loadData();

    } else {

      showToast('删除失败: ' + (result.error || '请确保库存和占用为0'));

    }

  } catch (e) {

    showToast('错误: ' + e.message);

  }

}



function showToast(msg) {

  const t = document.getElementById('toast');

  t.textContent = msg;

  t.classList.add('show');

  setTimeout(() => t.classList.remove('show'), 2500);

}



let debounceTimer;

document.getElementById('searchInput').addEventListener('input', () => {

  clearTimeout(debounceTimer);

  debounceTimer = setTimeout(()=>{currentPage=1;loadData();}, 400);

});

document.getElementById('warehouseFilter').addEventListener('change', ()=>{currentPage=1;loadData();});

document.getElementById('statusFilter').addEventListener('change', ()=>{currentPage=1;loadData();});

document.getElementById('categoryFilter').addEventListener('change', ()=>{currentPage=1;loadData();});



loadData();

</script>
</body>
</html>