initial commit

This commit is contained in:
robotics_x1
2026-10-12 15:24:08 +07:00
commit 55228b3772
367 changed files with 162893 additions and 0 deletions

View File

@@ -0,0 +1,404 @@
@implements IAsyncDisposable
@inject HttpClient Http
@inject StateService State
@inject ISnackbar Snackbar
@inject LangService Lang
<MudPaper Elevation="0" Class="pa-2" Style="background:transparent;">
<MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center"
Style="cursor:pointer" @onclick="ToggleExpand">
<MudText Typo="Typo.subtitle1">CONTROL ACCESS</MudText>
<div style="display:flex;align-items:center;gap:6px;">
@if (IsManager)
{
<a href="/users" @onclick:stopPropagation="true"
style="font-size:10px;color:#4a9eff;text-decoration:none;opacity:0.8;display:flex;align-items:center;gap:2px;"
title="App Settings"><MudIcon Icon="@Icons.Material.Filled.AdminPanelSettings" Size="Size.Small" /> Settings</a>
}
<MudIcon Icon="@(_expanded ? Icons.Material.Filled.ExpandMore : Icons.Material.Filled.ExpandLess)" Size="Size.Small" />
</div>
</MudStack>
@{
var ls = State.LockState;
var wfActive = ls?.Workflow is not null;
var grpActive = ls?.Groups is { } grpDict && grpDict.Values.Any(v => v is not null);
}
@if (_expanded)
{
@* ── Mode banner ── *@
@if (wfActive)
{
<div class="mode-banner mode-wf">
<MudIcon Icon="@Icons.Material.Filled.Bolt" Size="Size.Small" /> WF — @(ls!.Workflow!.Name)
<span class="mode-note">@Lang.T("Manual tắt", "Manual disabled")</span>
</div>
}
else if (grpActive)
{
<div class="mode-banner mode-manual">
<MudIcon Icon="@Icons.Material.Filled.Settings" Size="Size.Small" /> MANUAL
<span class="mode-note">@Lang.T("Workflow tắt", "Workflow disabled")</span>
</div>
}
else
{
<div class="mode-banner mode-free">@Lang.T("Không có ai nắm quyền", "No one holds control")</div>
}
@* ── OPERATIONS CONTROL row ── *@
<div class="ops-row @(grpActive && !IsManager ? "section-blocked" : "")">
<span class="ops-lbl">OPS</span>
@if (!CanWorkflow && UseReal)
{
<span class="mode-tag">Manual only</span>
}
@if (ls?.Workflow is { } wfHolder)
{
bool isMine = wfHolder.UserId == MyUserId;
<MudChip T="string" Size="Size.Small" Color="@(isMine ? Color.Success : Color.Error)"
Style="height:20px;font-size:10px;">
@(isMine ? Lang.T("Bạn giữ", "You hold") : wfHolder.Name)
</MudChip>
@if (isMine)
{
<MudButton Size="Size.Small" Variant="Variant.Outlined" Color="Color.Default"
Style="min-width:0;padding:1px 8px;" OnClick="ReleaseWorkflow">RELEASE</MudButton>
<MudIconButton Icon="@Icons.Material.Filled.Refresh" Size="Size.Small" Color="Color.Primary"
Style="padding:1px;" OnClick="RenewWorkflow" />
}
else if (IsManager)
{
<MudButton Size="Size.Small" Variant="Variant.Text" Color="Color.Error"
Style="min-width:0;padding:1px 6px;" OnClick="PreemptWorkflow">Preempt</MudButton>
}
else
{
var qPos = ls.WorkflowQueue?.ToList().FindIndex(w => w.UserId == MyUserId) ?? -1;
@if (qPos >= 0)
{
<MudText Typo="Typo.caption" Style="color:#9CA3AF;">@Lang.T("Chờ", "Queue") #@(qPos + 1)</MudText>
<MudIconButton Icon="@Icons.Material.Filled.Close" Size="Size.Small" Color="Color.Default"
Style="padding:1px;" OnClick="LeaveWorkflowQueue" />
}
else if (CanWorkflow)
{
<MudButton Size="Size.Small" Variant="Variant.Outlined" Color="Color.Warning"
Style="min-width:0;padding:1px 8px;" OnClick="JoinWorkflowQueue">JOIN</MudButton>
}
}
}
else if (!grpActive || IsManager)
{
<MudChip T="string" Size="Size.Small" Color="@(CanWorkflow ? Color.Success : Color.Default)"
Style="height:20px;font-size:10px;">Free</MudChip>
@if (CanWorkflow)
{
<MudButton Size="Size.Small" Variant="Variant.Filled" Color="Color.Success"
Style="min-width:0;padding:1px 10px;" OnClick="AcquireWorkflowControl">ACQUIRE</MudButton>
}
else
{
<span style="font-size:10px;color:#9CA3AF;font-style:italic;">@Lang.T("Cần quyền WF", "Need WF perm")</span>
}
}
else
{
<span class="ops-blocked-note">@Lang.T("Manual đang bật", "Manual is active")</span>
}
</div>
@if (ls?.WorkflowQueue is { Count: > 0 } wfQueue)
{
<div class="queue-list">
@foreach (var (w, i) in wfQueue.Select((w, i) => (w, i)))
{
<span class="queue-item @(w.IsPriority ? "q-priority" : "")">@(i + 1). @w.Name</span>
}
</div>
}
@* ── Group row ── *@
<div class="grp-row @(wfActive ? "section-blocked" : "")">
@foreach (var group in new[] { "ImrDobot", "AmrC1", "C2Asrs" })
{
var groupLabel = group switch { "ImrDobot" => "IMR·Dobot", "AmrC1" => "AMR·C1", _ => "C2·ASRS" };
LockHolderDto? groupHolder = null;
IReadOnlyList<WaiterDto>? groupQueue = null;
ls?.Groups?.TryGetValue(group, out groupHolder);
ls?.GroupQueues?.TryGetValue(group, out groupQueue);
<div class="grp-col">
<span class="grp-name">@groupLabel</span>
<div class="grp-actions">
@if (wfActive)
{
<span class="grp-wf-note">WF</span>
}
else if (groupHolder is not null)
{
bool isMine = groupHolder.UserId == MyUserId;
<MudChip T="string" Size="Size.Small" Color="@(isMine ? Color.Success : Color.Error)"
Style="height:18px;font-size:10px;margin:0;">
@(isMine ? Lang.T("Bạn", "You") : groupHolder.Name)
</MudChip>
@if (isMine)
{
var grp = group;
<MudButton Size="Size.Small" Variant="Variant.Outlined" Color="Color.Default"
Style="min-width:0;padding:1px 6px;" OnClick="() => ReleaseGroup(grp)">UNLOCK</MudButton>
}
else if (IsManager)
{
var grp = group;
<MudButton Size="Size.Small" Variant="Variant.Text" Color="Color.Error"
Style="min-width:0;padding:1px 4px;" OnClick="() => PreemptGroup(grp)">Unassign</MudButton>
}
else
{
var grp = group;
var qPos2 = groupQueue?.ToList().FindIndex(w => w.UserId == MyUserId) ?? -1;
@if (qPos2 >= 0)
{
<span style="font-size:10px;color:#9CA3AF;">#@(qPos2 + 1)</span>
<MudIconButton Icon="@Icons.Material.Filled.Close" Size="Size.Small" Color="Color.Default"
Style="padding:1px;" OnClick="() => LeaveGroupQueue(grp)" />
}
else if (CanManual)
{
<MudButton Size="Size.Small" Variant="Variant.Text" Color="Color.Warning"
Style="min-width:0;padding:1px 6px;" OnClick="() => JoinGroupQueue(grp)">JOIN</MudButton>
}
}
}
else
{
<MudChip T="string" Size="Size.Small"
Color="@(CanManual || IsManager ? Color.Success : Color.Default)"
Style="height:18px;font-size:10px;margin:0;">Free</MudChip>
var grp = group;
var assignedIds = State.LockState?.Groups?
.Where(kv => kv.Key != grp && kv.Value is not null)
.Select(kv => kv.Value!.UserId)
.ToHashSet() ?? new HashSet<string>();
var availableUsers = _users.Where(u => !assignedIds.Contains(u.Id)).ToList();
if (IsManager)
{
<MudMenu Icon="@Icons.Material.Filled.PersonAdd" Size="Size.Small"
Style="min-width:0;" IconColor="Color.Success"
>
@if (availableUsers.Count == 0)
{
<MudMenuItem Disabled="true">
<span style="font-size:11px;color:#9CA3AF;font-style:italic;">
@Lang.T("Hết user", "All assigned")
</span>
</MudMenuItem>
}
else
{
@foreach (var u in availableUsers)
{
var uid = u.Id;
var uname = u.Username;
<MudMenuItem OnClick="() => AssignGroup(grp, uid, uname)">
<span style="font-size:12px;">@u.Username</span>
<span style="font-size:10px;color:#9CA3AF;margin-left:6px;">@u.Role</span>
</MudMenuItem>
}
}
</MudMenu>
}
else if (CanManual)
{
<MudButton Size="Size.Small" Variant="Variant.Outlined" Color="Color.Primary"
Style="min-width:0;padding:1px 6px;" OnClick="() => LockGroup(grp)">LOCK</MudButton>
}
else
{
<span style="font-size:10px;color:#9CA3AF;font-style:italic;">@Lang.T("Cần Manual", "Need Manual")</span>
}
}
</div>
@if (groupQueue is { Count: > 0 })
{
<div class="grp-queue">
@foreach (var (w, i) in groupQueue.Select((w, i) => (w, i)))
{
<span class="queue-item @(w.IsPriority ? "q-priority" : "")">@(i + 1). @w.Name</span>
}
</div>
}
</div>
}
</div>
}
</MudPaper>
@code {
[Parameter] public bool UseReal { get; set; }
[Parameter] public string MyUserId { get; set; } = "";
[Parameter] public string MyRole { get; set; } = "";
[Parameter] public bool CanWorkflow { get; set; }
[Parameter] public bool CanManual { get; set; }
private bool IsManager => MyRole is "Manager" or "Admin";
private bool _expanded = true;
private bool _usersLoaded = false;
private List<UserItem> _users = new();
private void ToggleExpand() => _expanded = !_expanded;
protected override async Task OnInitializedAsync()
{
await State.StartAsync();
State.LockStateChanged += OnLockChanged;
State.SystemChanged += OnLockChanged;
Lang.LangChanged += OnLockChanged;
}
protected override async Task OnParametersSetAsync()
{
if (IsManager && !_usersLoaded)
{
_usersLoaded = true;
try
{
var list = await Http.GetFromJsonAsync<List<UserItem>>("/api/auth/users/active");
if (list is not null) _users = list;
}
catch (Exception ex)
{
Snackbar.Add($"Không tải được danh sách user: {ex.Message}", Severity.Warning);
}
}
}
record UserItem(string Id, string Username, string Role);
public ValueTask DisposeAsync()
{
State.LockStateChanged -= OnLockChanged;
State.SystemChanged -= OnLockChanged;
Lang.LangChanged -= OnLockChanged;
return ValueTask.CompletedTask;
}
private void OnLockChanged() => InvokeAsync(StateHasChanged);
// ── Workflow actions ───────────────────────────────────────────────────────
private async Task AcquireWorkflowControl()
{
var resp = await Http.PostAsync("/api/lock/workflow/acquire", null);
if ((int)resp.StatusCode == 423)
Snackbar.Add(Lang.T("Workflow đang bị giữ bởi người khác.", "Workflow is held by another user."), Severity.Warning);
else if ((int)resp.StatusCode == 403)
Snackbar.Add(Lang.T("Bạn không có quyền Workflow Mode.", "You don't have Workflow Mode permission."), Severity.Error);
else if (!resp.IsSuccessStatusCode)
Snackbar.Add(Lang.T("Không thể lấy quyền điều khiển.", "Failed to acquire control."), Severity.Error);
}
private async Task ReleaseWorkflow()
{
await Http.PostAsync("/api/lock/workflow/release", null);
}
private async Task RenewWorkflow()
{
var resp = await Http.PostAsync("/api/lock/workflow/renew", null);
if (resp.IsSuccessStatusCode) Snackbar.Add(Lang.T("Đã gia hạn quyền điều khiển.", "Control renewed."), Severity.Success);
}
private async Task PreemptWorkflow()
{
await Http.SendAsync(new HttpRequestMessage(HttpMethod.Delete, "/api/lock/workflow"));
Snackbar.Add(Lang.T("Đã thu hồi Workflow lock.", "Workflow lock preempted."), Severity.Info);
}
private async Task JoinWorkflowQueue()
{
await Http.PostAsync("/api/lock/workflow/queue", null);
Snackbar.Add(Lang.T("Đã vào hàng chờ Operations Control.", "Joined Operations Control queue."), Severity.Info);
}
private async Task LeaveWorkflowQueue()
{
await Http.SendAsync(new HttpRequestMessage(HttpMethod.Delete, "/api/lock/workflow/queue"));
}
// ── Group actions ──────────────────────────────────────────────────────────
private async Task LockGroup(string group)
{
var resp = await Http.PostAsync($"/api/lock/{group}/acquire", null);
if ((int)resp.StatusCode == 423)
Snackbar.Add(Lang.T($"Nhóm {group} đang bị giữ bởi người khác.", $"Group {group} is held by another user."), Severity.Warning);
else if ((int)resp.StatusCode == 403)
Snackbar.Add(Lang.T("Bạn không có quyền Manual Mode.", "You don't have Manual Mode permission."), Severity.Error);
else if (!resp.IsSuccessStatusCode)
Snackbar.Add(Lang.T($"Không thể khóa nhóm {group}.", $"Failed to lock group {group}."), Severity.Error);
}
private async Task ReleaseGroup(string group)
{
await Http.PostAsync($"/api/lock/{group}/release", null);
}
private async Task PreemptGroup(string group)
{
await Http.SendAsync(new HttpRequestMessage(HttpMethod.Delete, $"/api/lock/{group}"));
Snackbar.Add(Lang.T($"Đã thu hồi lock nhóm {group}.", $"Group {group} unassigned."), Severity.Info);
}
private async Task AssignGroup(string group, string userId, string displayName)
{
var resp = await Http.PostAsJsonAsync($"/api/lock/{group}/assign", new { UserId = userId, DisplayName = displayName });
if (resp.IsSuccessStatusCode)
Snackbar.Add(Lang.T($"Đã phân nhóm {group} cho {displayName}.", $"Group {group} assigned to {displayName}."), Severity.Success);
else
Snackbar.Add(Lang.T("Không thể phân nhóm.", "Failed to assign group."), Severity.Error);
}
private async Task JoinGroupQueue(string group)
{
await Http.PostAsync($"/api/lock/{group}/queue", null);
Snackbar.Add(Lang.T($"Đã vào hàng chờ nhóm {group}.", $"Joined group {group} queue."), Severity.Info);
}
private async Task LeaveGroupQueue(string group)
{
await Http.SendAsync(new HttpRequestMessage(HttpMethod.Delete, $"/api/lock/{group}/queue"));
}
}
<style>
.ops-row { display:flex; align-items:center; gap:5px; flex-wrap:wrap; margin-top:4px; padding-top:4px; border-top:1px solid rgba(255,255,255,0.05); }
.ops-lbl { font-size:10px; font-weight:700; color:#9CA3AF; letter-spacing:0.5px; white-space:nowrap; }
.ops-blocked-note { font-size:10px; color:#9CA3AF; font-style:italic; margin-left:auto; }
.grp-row { display:grid; grid-template-columns:1fr 1fr 1fr; gap:6px; margin-top:4px; padding-top:5px; border-top:1px solid rgba(255,255,255,0.05); }
.grp-row.section-blocked { opacity:0.45; pointer-events:none; }
.grp-col { display:flex; flex-direction:column; gap:3px; padding:4px 6px; border:1px solid rgba(255,255,255,0.05); border-radius:5px; }
.grp-name { font-size:10px; font-weight:700; color:#9CA3AF; white-space:nowrap; }
.grp-actions { display:flex; align-items:center; gap:3px; flex-wrap:wrap; }
.grp-wf-note { font-size:10px; color:#9CA3AF; font-style:italic; }
.grp-queue { display:flex; flex-wrap:wrap; gap:3px; padding-bottom:2px; }
.queue-list { display:flex; flex-wrap:wrap; gap:4px; margin-top:2px; padding-left:4px; }
.queue-item { font-size:10px; color:#9CA3AF; background:rgba(255,255,255,0.04); padding:1px 5px; border-radius:3px; }
.queue-item.q-priority { color:#4a9eff; }
.mode-banner { font-size:10px; font-weight:700; padding:3px 8px; border-radius:4px; margin:4px 0 2px;
display:flex; align-items:center; gap:6px; }
.mode-banner.mode-wf { background:rgba(243,156,18,0.1); border:1px solid rgba(243,156,18,0.3); color:#f39c12; }
.mode-banner.mode-manual { background:rgba(74,158,255,0.08); border:1px solid rgba(74,158,255,0.25); color:#4a9eff; }
.mode-banner.mode-free { background:rgba(255,255,255,0.03); border:1px solid rgba(255,255,255,0.07); color:#9CA3AF; }
.mode-note { font-size:9px; font-weight:400; opacity:0.7; margin-left:auto; }
.mode-tag { font-size:9px; font-weight:700; padding:1px 5px; border-radius:3px;
background:rgba(74,158,255,0.1); border:1px solid rgba(74,158,255,0.2); color:#3a7bbf; }
</style>