Files
Lab505/Lab505.Client/Pages/Home.razor
2026-10-12 15:24:08 +07:00

225 lines
9.4 KiB
Plaintext

@page "/"
@page "/{Username}"
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
@implements IAsyncDisposable
@inject HttpClient Http
@inject ISnackbar Snackbar
@inject LangService Lang
<MudThemeProvider IsDarkMode="true" />
<MudPopoverProvider />
<MudDialogProvider />
<MudSnackbarProvider />
<NotificationProvider Muted="@_notifMuted" />
<div class="right-top">
<div class="right-header">
<div style="display:flex;align-items:center;gap:6px;">
@if (!string.IsNullOrEmpty(_username))
{
<span style="font-size:11px;color:#444;font-weight:500;">@_username</span>
}
</div>
<div class="mode-toggle">
<MudTooltip Text="@Lang.T("Tài khoản của tôi", "My Account")" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Material.Filled.AccountCircle"
Size="Size.Small" Style="color:#aaa;padding:4px;" Href="/profile" />
</MudTooltip>
@if (_isManager)
{
<MudTooltip Text="App Settings — Quản lý tài khoản" Placement="Placement.Bottom">
<MudIconButton Icon="@Icons.Material.Filled.AdminPanelSettings"
Size="Size.Small" Style="color:#aaa;padding:4px;" Href="/users" />
</MudTooltip>
}
@* SIM / REAL toggle — chỉ hiện khi tài khoản có quyền real *@
@if (_canUseReal)
{
<MudTooltip Text="@(_useReal ? Lang.T("Đang dùng Real — nhấn để chuyển Sim", "Using Real — click to switch to Sim") : Lang.T("Đang dùng Sim — nhấn để chuyển Real", "Using Sim — click to switch to Real"))"
Placement="Placement.Bottom">
<MudChip T="string"
Color="@(_useReal ? Color.Warning : Color.Info)"
Size="Size.Small"
Style="cursor:pointer;height:24px;font-size:11px;font-weight:700;padding:0 8px;"
OnClick="ToggleStack">
@(_useReal ? "REAL" : "SIM")
</MudChip>
</MudTooltip>
}
else
{
<span style="font-size:11px;font-weight:700;color:#3a7bbf;padding:0 6px;opacity:0.7;">SIM</span>
}
<MudTooltip Text="@(_notifMuted ? Lang.T("Thông báo đang tắt", "Notifications Off") : Lang.T("Thông báo đang bật", "Notifications On"))" Placement="Placement.Bottom">
<MudIconButton Icon="@(_notifMuted ? Icons.Material.Filled.NotificationsOff : Icons.Material.Filled.Notifications)"
Size="Size.Small" OnClick="ToggleNotif"
Style="@(_notifMuted ? "color:#444;padding:4px;" : "color:#f0c040;padding:4px;")" />
</MudTooltip>
<MudTooltip Text="@Lang.T("Đăng xuất", "Logout")" Placement="Placement.Bottom">
<form method="post" action="/auth/do-logout" style="margin:0;display:inline;">
<MudIconButton ButtonType="ButtonType.Submit" Icon="@Icons.Material.Filled.Logout"
Size="Size.Small" Style="color:#666;padding:4px;" />
</form>
</MudTooltip>
</div>
</div>
<StatusPanel />
</div>
@{ var wfBlocker = WfBlocker; }
@if (wfBlocker is not null)
{
<div class="wf-block-banner">
<MudIcon Icon="@Icons.Material.Filled.Bolt" Size="Size.Small" Style="flex-shrink:0;" />
<span style="font-weight:800;letter-spacing:.4px;">WORKFLOW MODE</span>
<span class="wf-block-sep">—</span>
<strong style="color:#ffe082;">@wfBlocker.Name</strong>
<span class="wf-block-note">@Lang.T("đang điều khiển · Bạn không thể thao tác", "is controlling · You cannot perform actions")</span>
</div>
}
<div id="right-scroll" class="right-scroll @(wfBlocker is not null ? "panels-blocked" : "")">
<div id="p-ctrl" class="panel-card"><ControlPanel /></div>
@if (!State.System.IsSimulation || _isManager)
{
<div id="p-lock" class="panel-card"><LockPanel UseReal="@_useReal" MyUserId="@_userId" MyRole="@_role"
CanWorkflow="@(_isManager || _canWorkflow)"
CanManual="@(_isManager || (_canUseReal && !_canWorkflow))" /></div>
}
<div id="p-ops" class="panel-card"><FlowPhasePanel /></div>
<div id="p-conveyor" class="panel-card"><ConveyorSectionPanel /></div>
<div id="p-imr" class="panel-card"><ImrPanel /></div>
<div id="p-amr" class="panel-card"><AmrPanel /></div>
<div id="p-dobot" class="panel-card"><DobotPanel /></div>
<div id="p-asrs" class="panel-card"><AsrsPanel /></div>
</div>
@code {
[Parameter] public string? Username { get; set; }
[Inject] StateService State { get; set; } = default!;
bool _notifMuted = true;
bool _isManager = false;
bool _canUseReal = false;
bool _canWorkflow = false;
bool _useReal = false;
bool _stackBusy = false;
string _username = "";
string _userId = "";
string _role = "";
protected override async Task OnInitializedAsync()
{
await State.StartAsync();
State.SystemChanged += Refresh;
State.LockStateChanged += Refresh;
State.StackChanged += OnStackChanged;
Lang.LangChanged += Refresh;
await Lang.LoadAsync(Http);
try
{
var me = await Http.GetFromJsonAsync<MeDto>("/api/auth/me");
_isManager = me?.Role is "Manager" or "Admin";
_username = me?.Username ?? Username ?? "";
_canUseReal = me?.CanUseReal == true;
_canWorkflow = me?.CanWorkflow == true;
_useReal = me?.UseReal == true;
_userId = me?.UserId ?? "";
_role = me?.Role ?? "";
}
catch { }
try
{
var prefs = await Http.GetFromJsonAsync<PrefsDto>("/api/prefs");
if (prefs is not null) _notifMuted = prefs.NotifMuted;
// Lang already loaded via Lang.LoadAsync above
}
catch { }
}
private async Task ToggleStack()
{
if (_stackBusy || !_canUseReal) return;
_stackBusy = true;
try
{
bool newUseReal = !_useReal;
var url = newUseReal ? "/api/mode/real" : "/api/mode/simulation";
var resp = await Http.PostAsync(url, null);
if (resp.IsSuccessStatusCode)
{
_useReal = newUseReal;
await State.SwitchStackAsync(newUseReal);
}
}
catch { }
finally
{
_stackBusy = false;
}
}
private void Refresh() => InvokeAsync(StateHasChanged);
private void OnStackChanged(bool useReal)
{
_useReal = useReal;
InvokeAsync(StateHasChanged);
}
public ValueTask DisposeAsync()
{
State.SystemChanged -= Refresh;
State.LockStateChanged -= Refresh;
State.StackChanged -= OnStackChanged;
Lang.LangChanged -= Refresh;
return ValueTask.CompletedTask;
}
/// <summary>Workflow lock holder khi KHÔNG phải mình — cần hiện banner cảnh báo.</summary>
LockHolderDto? WfBlocker =>
!State.System.IsSimulation && State.LockState?.Workflow is { } h && h.UserId != _userId ? h : null;
private async Task ToggleLang() => await Lang.ToggleAsync(Http);
private async Task ToggleNotif()
{
_notifMuted = !_notifMuted;
try { await Http.PutAsJsonAsync("/api/prefs", new { Lang = (string?)null, NotifMuted = (bool?)_notifMuted }); }
catch { }
}
record MeDto(string Role, string Username, bool CanUseReal, bool UseReal, string? UserId, bool CanWorkflow);
record PrefsDto(string Lang, bool NotifMuted);
}
<style>
.right-top { flex-shrink:0; padding:12px 12px 0; background:#27292b; border-bottom:1px solid rgba(255,255,255,0.06); padding-bottom:8px; }
.right-scroll { flex:1; overflow-y:auto; padding:10px 12px 12px; height:0; scrollbar-width:none; background:#27292b; }
.right-scroll::-webkit-scrollbar { display:none; }
.right-header { display:flex; justify-content:space-between; align-items:center; padding-bottom:8px; margin-bottom:8px; border-bottom:1px solid rgba(255,255,255,0.08); }
.mode-toggle { display:flex; gap:4px; align-items:center; }
/* ── Workflow block banner ── */
.wf-block-banner {
display: flex; align-items: center; gap: 7px;
padding: 7px 14px;
background: rgba(243,156,18,0.13);
border-bottom: 1px solid rgba(243,156,18,0.4);
color: #f39c12; font-size: 11px; font-weight: 600;
flex-shrink: 0;
}
.wf-block-sep { opacity: 0.5; }
.wf-block-note { margin-left: auto; font-weight: 400; opacity: 0.75; font-size: 10px; }
/* dim all panels when someone else holds workflow */
.panels-blocked .panel-card { opacity: 0.45; pointer-events: none; }
/* but keep LockPanel interactive so user can see status / join queue */
.panels-blocked #p-lock { opacity: 1; pointer-events: auto; }
.panel-card { background:rgba(255,255,255,0.02); border:1px solid rgba(255,255,255,0.07); border-radius:8px; margin-bottom:8px; }
</style>