/**
 * @file    param.js
 * @brief   定值管理页面 — 多定值区展开 + 内联输入框
 */

Router.register('#param', function(c) {
	var signals = SignalStore.get('param');
	var rows = '';

	if (signals.length === 0)
	{
		rows = '<tr class="empty-row"><td colspan="6">' + I18n.t('signal.no_data') + '</td></tr>';
	}
	else
	{
		signals.forEach(function(s, idx) {
			var zones = s.setting_zone_list || [];
			var zoneHtml = '';
			zones.forEach(function(z) {
				zoneHtml += '<div style="display:flex;gap:6px;align-items:center;padding:2px 0;">'
					+ '<span style="color:var(--text-muted);font-family:var(--font-mono);width:28px;font-size:11px">#' + esc(z.id) + '</span>'
					+ '<span class="param-zone-val" data-zone="' + esc(z.id) + '" style="font-family:var(--font-mono);color:var(--accent);font-size:12px">' + esc(z.val) + '</span>'
					+ '<input class="param-zone-input" data-action="param-set" data-saddr="' + esc(s.saddr) + '" data-zone="' + esc(z.id) + '" placeholder="new" style="width:50px;padding:1px 4px;font-size:11px;border:1px solid var(--border);border-radius:3px;background:var(--bg-input);color:var(--text-primary);font-family:var(--font-mono)">'
					+ '<button class="btn btn-sm" data-action="param-set-go" data-saddr="' + esc(s.saddr) + '" data-zone="' + esc(z.id) + '" style="padding:1px 5px;font-size:11px">' + I18n.t('signal.set') + '</button>'
					+ '</div>';
			});
			rows += '<tr data-saddr="' + esc(s.saddr) + '">'
				+ '<td class="cell-mono" style="color:var(--text-muted)">' + (idx + 1) + '</td>'
				+ '<td class="cell-mono">' + esc(s.saddr) + '</td>'
				+ '<td class="cell-desc">' + esc(s.desc || '') + '</td>'
				+ '<td class="cell-mono">' + esc(s.type || '?') + '</td>'
				+ '<td>' + (zoneHtml || '\u2014') + '</td>'
				+ '<td><button class="btn btn-danger btn-sm" data-action="del" data-saddr="' + esc(s.saddr) + '">' + Icon.get('trash') + '</button></td>'
				+ '</tr>';
		});
	}

	c.innerHTML = '<div class="card">'
		+ '<div class="card-header"><span>PARAM — ' + signals.length + ' signals</span>'
		+ '<button class="btn btn-primary btn-sm" onclick="SignalActions.add(\'param\')">' + Icon.get('plus') + ' ' + I18n.t('signal.add') + '</button>'
		+ '</div>'
		+ '<div class="table-wrap"><table class="signal-table">'
		+ '<thead><tr><th style="width:36px">#</th><th>' + I18n.t('signal.saddr') + '</th><th>' + I18n.t('signal.desc') + '</th><th>' + I18n.t('signal.type') + '</th><th>' + I18n.t('signal.zone') + '</th><th>' + I18n.t('signal.action') + '</th></tr></thead>'
		+ '<tbody data-type="param">' + rows + '</tbody>'
		+ '</table></div></div>';
});

/* Param event delegation */
document.addEventListener('click', function(e) {
	var btn = e.target.closest('[data-action="param-set-go"]');
	if (!btn) return;
	var saddr = btn.getAttribute('data-saddr');
	var zone = btn.getAttribute('data-zone') || '0';
	var row = btn.closest('tr');
	var inp = row ? row.querySelector('[data-action="param-set"][data-zone="' + zone + '"]') : null;
	var val = inp ? inp.value.trim() : '';
	if (!val) { toast(I18n.t('toast.val_required'), 'error'); return; }
	WsClient.send({ saddr: saddr, signal_type: 'param', curd: 'set', signal_data: val, setting_zone: zone });
	/* 立即更新本地 SignalStore + DOM */
	var arr2 = SignalStore.get('param');
	for (var k2 = 0; k2 < arr2.length; k2++) {
		if (arr2[k2].saddr === saddr && arr2[k2].setting_zone_list) {
			for (var z = 0; z < arr2[k2].setting_zone_list.length; z++) {
				if (arr2[k2].setting_zone_list[z].id === zone) { arr2[k2].setting_zone_list[z].val = val; break; }
			}
			break;
		}
	}
	var zc = row.querySelector('.param-zone-val[data-zone="' + zone + '"]');
	if (zc) { zc.textContent = val; zc.style.transition = 'color 0.3s'; zc.style.color = '#2ed573'; setTimeout(function(){zc.style.color='';}, 800); }
	inp.value = '';
});
/* Enter key on param inline input */
document.addEventListener('keydown', function(e) {
	if (e.key === 'Enter' && e.target.classList.contains('param-zone-input')) {
		e.preventDefault();
		var inp = e.target;
		var row = inp.closest('tr');
		var zone = inp.getAttribute('data-zone');
		var btn = row ? row.querySelector('[data-action="param-set-go"][data-zone="' + zone + '"]') : null;
		if (btn) btn.click();
	}
});
