@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Utilities.ProgramManager @using Indotalent.Features.Utilities.ProgramManager.Cqrs @using Microsoft.AspNetCore.Components.Web @using Microsoft.JSInterop @using MudBlazor @using Features.Root.Shared @using ClosedXML.Excel @using System.IO @inject ProgramManagerService ProgramManagerService @inject IDialogService DialogService @inject ISnackbar Snackbar @inject IJSRuntime JSRuntime
Program Management Oversee and manage programs, resources, and priorities.
/ Utilities / ProgramManager
Search
@if (_isExporting) { } else { Excel } @if (_isRefreshing) { Refreshing... } else { Refresh } @if (_selectedItem != null) { View Edit Remove } else { Add New Program }
No Title Resource Priority Status @context.AutoNumber @context.Title @context.ResourceName @context.Priority?.ToUpper() @context.Status?.ToUpper()
Rows per page: Showing @(GetFilteredData().Count() == 0 ? 0 : _skip + 1)-@Math.Min(_skip + _top, GetFilteredData().Count()) of @GetFilteredData().Count()
Prev @{ var totalPages = _totalPage == 0 ? 1 : _totalPage; var maxVisible = 5; var startPage = Math.Max(1, _currentPage - maxVisible / 2); var endPage = Math.Min(totalPages, startPage + maxVisible - 1); if (endPage - startPage < maxVisible - 1) { startPage = Math.Max(1, endPage - maxVisible + 1); } } @for (int i = startPage; i <= endPage; i++) { var pageNum = i; var isActive = pageNum == _currentPage; @pageNum } Next
@code { [Parameter] public EventCallback OnAdd { get; set; } [Parameter] public EventCallback OnEdit { get; set; } [Parameter] public EventCallback OnView { get; set; } private List _items = new(); private GetProgramManagerListResponse? _selectedItem; private string _searchString = ""; private bool _isRefreshing = false; private bool _isExporting = false; private int _skip = 0; private int _top = 5; private int _totalPage => GetFilteredData().Count() == 0 ? 0 : (int)Math.Ceiling((double)GetFilteredData().Count() / (_top == 0 ? 1 : _top)); private int _currentPage => (_top >= GetFilteredData().Count() || _top == 0) ? 1 : (_skip / _top) + 1; protected override async Task OnInitializedAsync() => await LoadData(); private async Task LoadData() { _isRefreshing = true; _selectedItem = null; StateHasChanged(); try { var response = await ProgramManagerService.GetProgramManagerListAsync(); await Task.Delay(500); if (response != null && response.IsSuccess) { _items = response.Value ?? new(); } } finally { _isRefreshing = false; StateHasChanged(); } } private IEnumerable GetFilteredData() { if (string.IsNullOrWhiteSpace(_searchString)) return _items; return _items.Where(x => (x.Title?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) || (x.AutoNumber?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) || (x.ResourceName?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) ); } private IEnumerable GetPagedData() => GetFilteredData().Skip(_skip).Take(_top); private Color GetPriorityColor(string? priority) => priority switch { "Critical" => Color.Error, "High" => Color.Warning, "Normal" => Color.Info, _ => Color.Default }; private Color GetStatusColor(string? status) => status switch { "Done" => Color.Success, "OnProgress" => Color.Info, "Confirmed" => Color.Primary, "Cancelled" => Color.Error, _ => Color.Default }; private async Task ExportToExcel() { _isExporting = true; try { await Task.Delay(1000); using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Programs"); worksheet.Cell(1, 1).Value = "No"; worksheet.Cell(1, 2).Value = "Title"; worksheet.Cell(1, 3).Value = "Resource"; worksheet.Cell(1, 4).Value = "Priority"; worksheet.Cell(1, 5).Value = "Status"; var row = 1; foreach (var item in GetFilteredData()) { row++; worksheet.Cell(row, 1).Value = item.AutoNumber; worksheet.Cell(row, 2).Value = item.Title; worksheet.Cell(row, 3).Value = item.ResourceName; worksheet.Cell(row, 4).Value = item.Priority; worksheet.Cell(row, 5).Value = item.Status; } using (var stream = new MemoryStream()) { workbook.SaveAs(stream); var content = Convert.ToBase64String(stream.ToArray()); await JSRuntime.InvokeVoidAsync("downloadFile", "Program_List.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", content); } } } finally { _isExporting = false; } } private void OnSearchClick() { _skip = 0; _selectedItem = null; StateHasChanged(); } private void HandleSearchKeyDown(KeyboardEventArgs e) { if (e.Key == "Enter") OnSearchClick(); } private void OnPageChanged(int page) { _skip = (page - 1) * _top; _selectedItem = null; } private void OnPageSizeChanged(int size) { _top = size; _skip = 0; } private async Task InvokeEdit() { if (_selectedItem == null) return; var request = await MapToUpdateRequest(_selectedItem.Id!); if (request != null) await OnEdit.InvokeAsync(request); } private async Task InvokeView() { if (_selectedItem == null) return; var request = await MapToUpdateRequest(_selectedItem.Id!); if (request != null) await OnView.InvokeAsync(request); } private async Task MapToUpdateRequest(string id) { var response = await ProgramManagerService.GetProgramManagerByIdAsync(id); if (response != null && response.IsSuccess) { var d = response.Value!; return new UpdateProgramManagerRequest { Id = d.Id, Title = d.Title, Summary = d.Summary, Status = d.Status, Priority = d.Priority, ProgramManagerResourceId = d.ProgramManagerResourceId, CreatedAt = d.CreatedAt, CreatedBy = d.CreatedBy, UpdatedAt = d.UpdatedAt, UpdatedBy = d.UpdatedBy }; } return null; } private async Task OnDelete() { if (_selectedItem == null) return; var parameters = new DialogParameters<_DeleteConfirmation> { { x => x.ContentText, _selectedItem.Title } }; var dialog = await DialogService.ShowAsync<_DeleteConfirmation>("", parameters, new DialogOptions { MaxWidth = MaxWidth.ExtraSmall, FullWidth = true }); var result = await dialog.Result; if (result != null && !result.Canceled) { if (await ProgramManagerService.DeleteProgramManagerAsync(_selectedItem.Id!)) { await LoadData(); Snackbar.Add("Deleted successfully", Severity.Success); } } } }