@using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums
@*
M10 residual: body component for the template "Add / Edit Alarm" dialog hosted
by IDialogService.ShowAsync. Renders ONLY the form fields + action buttons
inside the host's .modal-body; the host owns the backdrop, header, focus trap,
Escape, and focus restoration.
Same contract as TemplateAttributeDialog: OnSaveAsync returns null on success
(dialog closes with true) or the error to render inline (dialog stays open).
*@
@code {
/// Host-supplied context: Close(true) on a successful save, Cancel on dismiss.
[Parameter, EditorRequired] public DialogContext Context { get; set; } = default!;
/// True when editing an existing alarm (name + trigger type become read-only).
[Parameter] public bool Editing { get; set; }
[Parameter] public string InitialName { get; set; } = string.Empty;
[Parameter] public int InitialPriority { get; set; }
[Parameter] public AlarmTriggerType InitialTriggerType { get; set; }
[Parameter] public string? InitialTriggerConfig { get; set; }
[Parameter] public bool InitialIsLocked { get; set; }
/// Attribute choices offered by the structured trigger editor's picker.
[Parameter] public IReadOnlyList AvailableAttributes { get; set; }
= Array.Empty();
///
/// Persists the draft. Returns null on success (the dialog closes with
/// true) or the message to render inline, leaving the dialog open.
///
[Parameter, EditorRequired] public Func> OnSaveAsync { get; set; } = default!;
private string _name = string.Empty;
private int _priority;
private AlarmTriggerType _triggerType;
private string? _triggerConfig;
private bool _isLocked;
private string? _error;
private bool _busy;
protected override void OnInitialized()
{
_name = InitialName;
_priority = InitialPriority;
_triggerType = InitialTriggerType;
_triggerConfig = InitialTriggerConfig;
_isLocked = InitialIsLocked;
}
private async Task Submit()
{
if (_busy) return;
_error = null;
if (string.IsNullOrWhiteSpace(_name)) { _error = "Name is required."; return; }
_busy = true;
try
{
var error = await OnSaveAsync(new AlarmDraft(_name, _triggerType, _priority, _triggerConfig, _isLocked));
if (error == null)
{
Context.Close(true);
}
else
{
_error = error;
}
}
finally
{
_busy = false;
}
}
/// The authored alarm as submitted; the page performs the add/update.
/// Alarm name (ignored on edit — the stored name is fixed).
/// Selected trigger type (ignored on edit — server-fixed).
/// Priority level.
/// Structured trigger configuration payload.
/// Whether instances may override the alarm.
public sealed record AlarmDraft(
string Name,
AlarmTriggerType TriggerType,
int Priority,
string? TriggerConfig,
bool IsLocked);
}