form.js
17 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* A3 报告表单页脚本(新增/编辑/查看三合一)
*
* 依赖 window 全局变量(由 form.html Thymeleaf 内联注入):
* - __MODE__ : 'add' | 'edit' | 'view'
* - __REPORT_ID__ : 主表 id(新增时为 null)
* - __DETAIL__ : A3ReportDetailDTO 序列化对象
* - __BLOCKS__ : BlockInfo 列表
* - __INDICATORS_BY_BLOCK__: {blockId: [CompanyIndicator...]}
* - __GZJHZT_LIST__ : [{code, desc}...] 工作计划状态
* - __ZT_LIST__ : [{code, desc}...] 推进步骤状态
*/
(function () {
'use strict';
var MODE = window.__MODE__ || 'add';
var DETAIL = window.__DETAIL__ || {};
var REPORT = DETAIL.report || {};
var BLOCKS = window.__BLOCKS__ || [];
var INDICATORS_BY_BLOCK = window.__INDICATORS_BY_BLOCK__ || {};
var ZT_LIST = window.__ZT_LIST__ || [];
/* =============== 初始化 =============== */
document.addEventListener('DOMContentLoaded', function () {
initMasterForm();
renderPlanStatusRows(DETAIL.planStatusList || []);
renderActionStepRows(DETAIL.actionStepList || []);
renderProblemEventRows(DETAIL.problemEventList || []);
var bkSel = document.getElementById('f-bk');
if (bkSel) {
bkSel.addEventListener('change', function () {
refreshAllIndicatorOptions();
});
}
});
function initMasterForm() {
setVal('f-bk', REPORT.bk);
setVal('f-fzr', REPORT.fzr);
setVal('f-gzjhzt', REPORT.gzjhzt);
setVal('f-mdjxqkh', REPORT.mdjxqkh);
setVal('f-zlkl', REPORT.zlkl);
setVal('f-tjdxwh', REPORT.tjdxwh);
setVal('f-xsjdwt', REPORT.xsjdwt);
// nf 为 yyyy-MM-dd 字符串(Controller 已用 SimpleDateFormatSerializer 格式化)
if (REPORT.nf) {
var year = String(REPORT.nf).substring(0, 4);
setVal('f-nf-year', year);
}
}
function setVal(id, val) {
var el = document.getElementById(id);
if (el) {
el.value = val == null ? '' : val;
}
}
function getVal(id) {
var el = document.getElementById(id);
return el ? el.value : '';
}
/* =============== 工作计划状态和 BIC =============== */
function renderPlanStatusRows(list) {
var tbody = document.getElementById('tbody-plan-status');
if (!tbody) return;
tbody.innerHTML = '';
if (!list || list.length === 0) {
if (MODE === 'view') {
tbody.appendChild(emptyRow(13, '暂无数据'));
}
return;
}
list.forEach(function (item, idx) {
tbody.appendChild(buildPlanStatusRow(item, idx + 1));
});
}
function buildPlanStatusRow(item, seq) {
item = item || {};
var tr = document.createElement('tr');
tr.className = 'row-plan-status';
// 序号
tr.appendChild(cellIndex(seq));
// 指标(select,随板块联动)
var tdZb = document.createElement('td');
tdZb.appendChild(buildIndicatorSelect(item.zb));
tr.appendChild(tdZb);
tr.appendChild(cellInput('text', 'cx', item.cx, '成效'));
tr.appendChild(cellInput('date', 'snd', item.snd, '上年度'));
tr.appendChild(cellInput('text', 'sndz', item.sndz, '上年度值'));
tr.appendChild(cellInput('date', 'bnd', item.bnd, '本年度'));
tr.appendChild(cellInput('text', 'bndz', item.bndz, '本年度值'));
tr.appendChild(cellInput('date', 'hn', item.hn, '后年'));
tr.appendChild(cellInput('text', 'hndz', item.hndz, '后年度值'));
tr.appendChild(cellInput('text', 'bicbiw', item.bicbiw, 'BIC 比率'));
// 公司指标:只读,由后端根据 zb 反查回填,前端仅展示 DETAIL.zbNameMap
var zbNameMap = DETAIL.zbNameMap || {};
var tdGszb = document.createElement('td');
var gszbInput = document.createElement('input');
gszbInput.type = 'text';
gszbInput.readOnly = true;
gszbInput.className = 'gszb-display';
gszbInput.value = item.zb && zbNameMap[item.zb] ? zbNameMap[item.zb] : (item.gszb || '');
tdGszb.appendChild(gszbInput);
tr.appendChild(tdGszb);
tr.appendChild(cellInput('text', 'b', item.b, '备注'));
if (MODE !== 'view') {
tr.appendChild(cellRemove('删除此行'));
}
return tr;
}
window.addPlanStatusRow = function () {
var tbody = document.getElementById('tbody-plan-status');
if (!tbody) return;
clearEmptyRow(tbody);
var seq = tbody.querySelectorAll('tr.row-plan-status').length + 1;
tbody.appendChild(buildPlanStatusRow({}, seq));
};
function buildIndicatorSelect(selectedZb) {
var sel = document.createElement('select');
sel.className = 'indicator-select';
sel.setAttribute('data-field', 'zb');
sel.disabled = (MODE === 'view');
var optEmpty = document.createElement('option');
optEmpty.value = '';
optEmpty.textContent = '请选择';
sel.appendChild(optEmpty);
var currentBk = getVal('f-bk');
var indicators = currentBk ? (INDICATORS_BY_BLOCK[currentBk] || []) : allIndicators();
indicators.forEach(function (ind) {
var opt = document.createElement('option');
opt.value = ind.id;
opt.textContent = ind.mc || '(未命名)';
if (selectedZb && ind.id === selectedZb) {
opt.selected = true;
}
sel.appendChild(opt);
});
// 编辑/查看模式下,若 selectedZb 不属于当前板块的指标集,仍需保留一个占位项显示已选值
if (selectedZb && !hasOption(sel, selectedZb)) {
var zbNameMap = DETAIL.zbNameMap || {};
var opt = document.createElement('option');
opt.value = selectedZb;
opt.textContent = (zbNameMap[selectedZb] || selectedZb) + '(历史)';
opt.selected = true;
sel.appendChild(opt);
}
sel.addEventListener('change', function () {
var td = sel.closest('td');
var tr = sel.closest('tr');
if (!td || !tr) return;
var gszbInput = tr.querySelector('.gszb-display');
if (gszbInput) {
var opt = sel.options[sel.selectedIndex];
gszbInput.value = opt && opt.value ? opt.textContent.replace('(历史)', '') : '';
}
});
return sel;
}
function allIndicators() {
var arr = [];
Object.keys(INDICATORS_BY_BLOCK).forEach(function (k) {
arr = arr.concat(INDICATORS_BY_BLOCK[k] || []);
});
return arr;
}
function hasOption(sel, val) {
for (var i = 0; i < sel.options.length; i++) {
if (sel.options[i].value === val) return true;
}
return false;
}
function refreshAllIndicatorOptions() {
var selects = document.querySelectorAll('#tbody-plan-status .indicator-select');
selects.forEach(function (sel) {
var current = sel.value;
sel.innerHTML = '';
var optEmpty = document.createElement('option');
optEmpty.value = '';
optEmpty.textContent = '请选择';
sel.appendChild(optEmpty);
var bk = getVal('f-bk');
var list = bk ? (INDICATORS_BY_BLOCK[bk] || []) : allIndicators();
list.forEach(function (ind) {
var opt = document.createElement('option');
opt.value = ind.id;
opt.textContent = ind.mc || '(未命名)';
sel.appendChild(opt);
});
if (current && hasOption(sel, current)) {
sel.value = current;
}
});
}
/* =============== 推进的行为和步骤 =============== */
function renderActionStepRows(list) {
var tbody = document.getElementById('tbody-action-step');
if (!tbody) return;
tbody.innerHTML = '';
if (!list || list.length === 0) {
if (MODE === 'view') {
tbody.appendChild(emptyRow(MODE === 'view' ? 3 : 4, '暂无数据'));
}
return;
}
list.forEach(function (item, idx) {
tbody.appendChild(buildActionStepRow(item, idx + 1));
});
}
function buildActionStepRow(item, seq) {
item = item || {};
var tr = document.createElement('tr');
tr.className = 'row-action-step';
// 序号(可编辑)
var tdXh = document.createElement('td');
var xhInput = document.createElement('input');
xhInput.type = 'text';
xhInput.setAttribute('data-field', 'xh');
xhInput.value = item.xh != null && item.xh !== '' ? item.xh : String(seq);
xhInput.disabled = (MODE === 'view');
tdXh.appendChild(xhInput);
tr.appendChild(tdXh);
tr.appendChild(cellInput('text', 'tjdxwhbz', item.tjdxwhbz, '推进的行为和步骤'));
// 状态下拉
var tdZt = document.createElement('td');
var ztSel = document.createElement('select');
ztSel.setAttribute('data-field', 'zt');
ztSel.disabled = (MODE === 'view');
var optEmpty = document.createElement('option');
optEmpty.value = '';
optEmpty.textContent = '请选择';
ztSel.appendChild(optEmpty);
ZT_LIST.forEach(function (z) {
var opt = document.createElement('option');
opt.value = z.code;
opt.textContent = z.desc;
if (item.zt === z.code) opt.selected = true;
ztSel.appendChild(opt);
});
tdZt.appendChild(ztSel);
tr.appendChild(tdZt);
if (MODE !== 'view') {
tr.appendChild(cellRemove('删除此行'));
}
return tr;
}
window.addActionStepRow = function () {
var tbody = document.getElementById('tbody-action-step');
if (!tbody) return;
clearEmptyRow(tbody);
var seq = tbody.querySelectorAll('tr.row-action-step').length + 1;
tbody.appendChild(buildActionStepRow({}, seq));
};
/* =============== 显示阶段问题事件 =============== */
function renderProblemEventRows(list) {
var tbody = document.getElementById('tbody-problem-event');
if (!tbody) return;
tbody.innerHTML = '';
if (!list || list.length === 0) {
if (MODE === 'view') {
tbody.appendChild(emptyRow(MODE === 'view' ? 4 : 5, '暂无数据'));
}
return;
}
list.forEach(function (item, idx) {
tbody.appendChild(buildProblemEventRow(item, idx + 1));
});
}
function buildProblemEventRow(item, seq) {
item = item || {};
var tr = document.createElement('tr');
tr.className = 'row-problem-event';
tr.appendChild(cellIndex(seq));
tr.appendChild(cellInput('text', 'xsjdwtsj', item.xsjdwtsj, '问题事件'));
tr.appendChild(cellInput('text', 'zrr', item.zrr, '责任人'));
tr.appendChild(cellInput('date', 'sj', item.sj, '时间'));
if (MODE !== 'view') {
tr.appendChild(cellRemove('删除此行'));
}
return tr;
}
window.addProblemEventRow = function () {
var tbody = document.getElementById('tbody-problem-event');
if (!tbody) return;
clearEmptyRow(tbody);
var seq = tbody.querySelectorAll('tr.row-problem-event').length + 1;
tbody.appendChild(buildProblemEventRow({}, seq));
};
/* =============== 通用单元格构造 =============== */
function cellIndex(seq) {
var td = document.createElement('td');
td.className = 'row-index';
td.textContent = seq;
return td;
}
function cellInput(type, field, value, placeholder) {
var td = document.createElement('td');
var input = document.createElement('input');
input.type = type;
input.setAttribute('data-field', field);
input.value = value == null ? '' : value;
input.placeholder = placeholder || '';
input.disabled = (MODE === 'view');
td.appendChild(input);
return td;
}
function cellRemove(title) {
var td = document.createElement('td');
td.className = 'op-cell';
var btn = document.createElement('button');
btn.type = 'button';
btn.className = 'btn btn-mini btn-danger';
btn.textContent = '删除';
btn.title = title || '删除';
btn.onclick = function () {
var tr = btn.closest('tr');
if (tr && tr.parentNode) {
tr.parentNode.removeChild(tr);
reindexRows();
}
};
td.appendChild(btn);
return td;
}
function emptyRow(colspan, text) {
var tr = document.createElement('tr');
tr.className = 'empty-row';
var td = document.createElement('td');
td.colSpan = colspan;
td.className = 'empty-tip';
td.textContent = text;
tr.appendChild(td);
return tr;
}
function clearEmptyRow(tbody) {
var empties = tbody.querySelectorAll('tr.empty-row');
empties.forEach(function (tr) {
tr.parentNode.removeChild(tr);
});
}
function reindexRows() {
['plan-status', 'action-step', 'problem-event'].forEach(function (key) {
var tbody = document.getElementById('tbody-' + key);
if (!tbody) return;
var rows = tbody.querySelectorAll('tr.row-' + key);
rows.forEach(function (tr, idx) {
var cell = tr.querySelector('td.row-index');
if (cell) cell.textContent = idx + 1;
if (key === 'action-step') {
var xhInput = tr.querySelector('input[data-field="xh"]');
if (xhInput && (!xhInput.value || xhInput.getAttribute('data-auto') === '1')) {
xhInput.value = String(idx + 1);
xhInput.setAttribute('data-auto', '1');
}
}
});
});
}
/* =============== 保存 =============== */
window.saveReport = function () {
var payload = collectForm();
if (!payload.bk) {
alert('请选择板块');
return;
}
var btnSave = document.getElementById('btn-save');
if (btnSave) btnSave.disabled = true;
fetch('/a3Report/save', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload)
}).then(function (resp) {
return resp.json();
}).then(function (json) {
if (json && (json.success === true || json.success === 'true')) {
alert('保存成功');
window.location.href = '/a3Report/page/list';
} else {
alert('保存失败:' + ((json && (json.message || json.msg)) || '未知错误'));
if (btnSave) btnSave.disabled = false;
}
}).catch(function (err) {
alert('保存请求异常:' + err);
if (btnSave) btnSave.disabled = false;
});
};
function collectForm() {
var yearStr = getVal('f-nf-year');
var nf = null;
if (yearStr) {
var y = parseInt(yearStr, 10);
if (!isNaN(y)) {
nf = y + '-01-01';
}
}
var dto = {
id: window.__REPORT_ID__ || null,
bk: getVal('f-bk') || null,
fzr: getVal('f-fzr') || null,
gzjhzt: getVal('f-gzjhzt') || null,
mdjxqkh: getVal('f-mdjxqkh') || null,
zlkl: getVal('f-zlkl') || null,
tjdxwh: getVal('f-tjdxwh') || null,
xsjdwt: getVal('f-xsjdwt') || null,
nf: nf,
planStatusList: collectRows('tbody-plan-status', 'row-plan-status',
['zb', 'cx', 'snd', 'sndz', 'bnd', 'bndz', 'hn', 'hndz', 'bicbiw', 'b']),
actionStepList: collectRows('tbody-action-step', 'row-action-step',
['xh', 'tjdxwhbz', 'zt']),
problemEventList: collectRows('tbody-problem-event', 'row-problem-event',
['xsjdwtsj', 'zrr', 'sj'])
};
return dto;
}
function collectRows(tbodyId, rowClass, fields) {
var tbody = document.getElementById(tbodyId);
if (!tbody) return [];
var rows = tbody.querySelectorAll('tr.' + rowClass);
var list = [];
rows.forEach(function (tr) {
var obj = {};
var hasValue = false;
fields.forEach(function (f) {
var el = tr.querySelector('[data-field="' + f + '"]');
var v = el ? (el.value || '') : '';
obj[f] = v === '' ? null : v;
if (v !== '') hasValue = true;
});
if (hasValue) {
list.push(obj);
}
});
return list;
}
})();