Files
Denso/srcs/RobotNet10/FleetManager/RobotNet10.FleetManager.Client/Components/RobotModelManager/Dialogs/DeleteRobotModelDialog.razor
2026-07-03 16:31:37 +07:00

124 lines
3.9 KiB
Plaintext

@using RobotNet10.FleetManager.Shared.DTOs.RobotModel
@using RobotNet10.FleetManager.Client.Services
@inject ISnackbar Snackbar
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icons.Material.Filled.Delete" Color="Color.Error" Class="mr-2" />
Delete Robot Model
</MudText>
</TitleContent>
<DialogContent>
<MudStack Spacing="3">
<MudText Typo="Typo.body1">
Are you sure you want to delete the robot model <strong>@RobotModel.ModelName</strong>?
</MudText>
@if (usageInfo != null)
{
@if (usageInfo.RobotCount > 0)
{
<MudAlert Severity="Severity.Error">
<MudText>This robot model is currently being used by <strong>@usageInfo.RobotCount</strong> robot(s).</MudText>
<MudText>You must delete or reassign all robots using this model before you can delete it.</MudText>
</MudAlert>
}
else
{
<MudAlert Severity="Severity.Warning">
<MudText>This action cannot be undone.</MudText>
</MudAlert>
}
}
else if (isLoadingUsageInfo)
{
<MudProgressCircular Size="Size.Small" Indeterminate="true" />
}
</MudStack>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Error"
Variant="Variant.Filled"
OnClick="Submit"
Disabled="@(isDeleting || (usageInfo != null && !usageInfo.CanDelete))">
@if (isDeleting)
{
<MudProgressCircular Class="mr-2" Size="Size.Small" Indeterminate="true" />
<span>Deleting...</span>
}
else
{
<span>Delete</span>
}
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
[Parameter] public RobotModelApiService ApiService { get; set; } = null!;
[Parameter] public RobotModelDto RobotModel { get; set; } = null!;
private RobotModelUsageInfoDto? usageInfo;
private bool isLoadingUsageInfo = true;
private bool isDeleting = false;
protected override async Task OnInitializedAsync()
{
await LoadUsageInfoAsync();
}
private async Task LoadUsageInfoAsync()
{
isLoadingUsageInfo = true;
try
{
usageInfo = await ApiService.GetUsageInfoAsync(RobotModel.Id);
}
catch (Exception ex)
{
Snackbar.Add($"Error loading usage info: {ex.Message}", Severity.Error);
}
finally
{
isLoadingUsageInfo = false;
StateHasChanged();
}
}
private void Cancel()
{
MudDialog?.Cancel();
}
private async Task Submit()
{
if (usageInfo != null && !usageInfo.CanDelete)
{
Snackbar.Add("Cannot delete robot model that is in use", Severity.Warning);
return;
}
isDeleting = true;
StateHasChanged();
try
{
await ApiService.DeleteAsync(RobotModel.Id);
Snackbar.Add($"Robot model '{RobotModel.ModelName}' deleted successfully", Severity.Success);
MudDialog?.Close(DialogResult.Ok(true));
}
catch (Exception ex)
{
Snackbar.Add($"Error deleting robot model: {ex.Message}", Severity.Error);
}
finally
{
isDeleting = false;
StateHasChanged();
}
}
}