267 lines
8.0 KiB
C#
267 lines
8.0 KiB
C#
using Blazored.Modal;
|
|
using Blazored.Modal.Services;
|
|
using Blazored.Toast.Services;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.JSInterop;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using ReserveManagementSystemLib.Models;
|
|
using ReserveManagementSystemUI.Services;
|
|
using ReserveManagementSystemUI.Shared.Components;
|
|
|
|
namespace ReserveManagementSystemUI.Pages
|
|
{
|
|
public partial class Loans : ComponentBase
|
|
{
|
|
[Inject]
|
|
private ReserveService ReserveService { get; set; }
|
|
|
|
[Inject]
|
|
IToastService ToastService { get; set; }
|
|
|
|
[Inject]
|
|
IModalService Modal { get; set; }
|
|
|
|
[Inject]
|
|
NavigationManager NavigationManager { get; set; }
|
|
|
|
[Inject]
|
|
IJSRuntime JSRuntime { get; set; }
|
|
|
|
[Parameter]
|
|
public int? ImportId { get; set; }
|
|
|
|
private LoanFilter currentLoanFilter = new LoanFilter();
|
|
|
|
private PaginatedListResponse pagedResponse = new PaginatedListResponse();
|
|
|
|
private const int TopImports = 5;
|
|
private List<Import> Imports = new List<Import>();
|
|
private List<bool?> Actions = new List<bool?>();
|
|
|
|
private Import SelectedImport;
|
|
private Dealer SelectedDealer;
|
|
|
|
private bool ascending = true;
|
|
|
|
private IModalReference openModal;
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
currentLoanFilter = new LoanFilter
|
|
{
|
|
PageIndex = 1,
|
|
MaxRecordsPerPage = 15,
|
|
MaxPagesPerRange = 7,
|
|
};
|
|
Imports = await ReserveService.GetRecentImports(TopImports);
|
|
|
|
AddActions();
|
|
|
|
if (ImportId.HasValue)
|
|
{
|
|
SelectedImport = Imports.Where(i => ImportId.Value == (i.ImportId)).FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
SelectedImport = null;
|
|
}
|
|
currentLoanFilter.ImportId = SelectedImport?.ImportId;
|
|
await LoadLoanDataAsync(1);
|
|
await base.OnParametersSetAsync();
|
|
}
|
|
|
|
protected async Task BlazorFocusElement()
|
|
{
|
|
try
|
|
{
|
|
await JSRuntime.InvokeVoidAsync("BlazorFocusElement");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//do nothing
|
|
}
|
|
}
|
|
|
|
private async Task LoadLoanDataAsync(int pageIndex)
|
|
{
|
|
currentLoanFilter.PageIndex = pageIndex;
|
|
|
|
openModal = Modal.Show<LoadingSpinner>();
|
|
|
|
pagedResponse = await ReserveService.GetFilteredLoans(currentLoanFilter);
|
|
|
|
if (pagedResponse.LoanList == null)
|
|
{
|
|
ToastService.ShowError("There was an issue retrieving loan data.");
|
|
pagedResponse.LoanList = new List<Loan>();
|
|
}
|
|
|
|
StateHasChanged();
|
|
await BlazorFocusElement();
|
|
|
|
openModal.Close();
|
|
|
|
GetPageButtonClass(currentLoanFilter.PageIndex);
|
|
}
|
|
|
|
private async Task OnMemberNameTextChangeAsync(ChangeEventArgs args)
|
|
{
|
|
currentLoanFilter.MemberNameText = args.Value.ToString();
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task OnLoanDateStartChangeAsync(ChangeEventArgs args)
|
|
{
|
|
if (DateTime.TryParse(args.Value.ToString(), out DateTime startDate))
|
|
{
|
|
currentLoanFilter.LoanDateStart = startDate;
|
|
}
|
|
else
|
|
{
|
|
currentLoanFilter.LoanDateStart = null;
|
|
}
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task OnLoanDateEndChangeAsync(ChangeEventArgs args)
|
|
{
|
|
if (DateTime.TryParse(args.Value.ToString(), out DateTime endDate))
|
|
{
|
|
currentLoanFilter.LoanDateEnd = endDate;
|
|
}
|
|
else
|
|
{
|
|
currentLoanFilter.LoanDateEnd = null;
|
|
}
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task<IEnumerable<string>> GetMemberNumbersAsync(string searchText)
|
|
{
|
|
var result = await ReserveService.GetMemberNumbers(searchText);
|
|
return result;
|
|
}
|
|
|
|
private async Task SelectedMemberNumberResultChangedAsync(string result)
|
|
{
|
|
currentLoanFilter.MemberNumberText = result;
|
|
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task<IEnumerable<string>> GetLoanNumbersAsync(string searchText)
|
|
{
|
|
var result = await ReserveService.GetLoanNumbers(searchText);
|
|
return result;
|
|
}
|
|
|
|
private async Task SelectedLoanNumberResultChangedAsync(string result)
|
|
{
|
|
currentLoanFilter.LoanNumberText = result;
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task<IEnumerable<string>> GetApplicationIdsAsync(string searchText)
|
|
{
|
|
var result = await ReserveService.GetApplicationIds(searchText);
|
|
return result;
|
|
}
|
|
|
|
private async Task SelectedApplicationIdResultChangedAsync(string result)
|
|
{
|
|
currentLoanFilter.ApplicationIdText = result;
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task SelectedImportResultChangedAsync(Import result)
|
|
{
|
|
currentLoanFilter.ImportId = result?.ImportId;
|
|
SelectedImport = result;
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task<IEnumerable<Import>> GetImportsAsync(string searchText)
|
|
{
|
|
var search = await Task.FromResult(Imports);
|
|
return search;
|
|
}
|
|
|
|
private string GetPageButtonClass(int currentPage)
|
|
{
|
|
return currentPage == pagedResponse.PageIndex ? "btn btn-page-focus" : "btn btn-page";
|
|
}
|
|
|
|
private async Task<IEnumerable<Dealer>> GetDealersAsync(string searchText)
|
|
{
|
|
var dealers = new List<Dealer>();
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchText))
|
|
{
|
|
dealers = await ReserveService.GetDealers(searchText);
|
|
}
|
|
|
|
return dealers;
|
|
}
|
|
|
|
private async Task SelectedDealerResultChangedAsync(Dealer selectedDealer)
|
|
{
|
|
SelectedDealer = selectedDealer;
|
|
currentLoanFilter.DealerCodeText = selectedDealer?.DealerCode;
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private async Task<IEnumerable<bool?>> GetActionRequiredLoansAsync(string searchText)
|
|
{
|
|
bool action;
|
|
var results = new List<bool?>();
|
|
if (!string.IsNullOrWhiteSpace(searchText) && bool.TryParse(searchText, out action))
|
|
{
|
|
results = await Task.FromResult(Actions.Where(x => x.Value.ToString() == searchText || x == null).ToList());
|
|
}
|
|
else
|
|
{
|
|
results = await Task.FromResult(Actions.ToList());
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private async Task SelectedActionResultChangedAsync(bool? result)
|
|
{
|
|
currentLoanFilter.ActionRequired = result;
|
|
|
|
await LoadLoanDataAsync(1);
|
|
}
|
|
|
|
private void AddActions()
|
|
{
|
|
if (Actions != null && Actions.Count == 0)
|
|
{
|
|
Actions.Add(true);
|
|
Actions.Add(false);
|
|
}
|
|
}
|
|
|
|
private async void OnSortClickAsync(string sortBy)
|
|
{
|
|
if (sortBy == currentLoanFilter.SortField)
|
|
{
|
|
ascending = !ascending;
|
|
}
|
|
|
|
currentLoanFilter.SortField = sortBy;
|
|
currentLoanFilter.SortDirection = ascending ? "ASC" : "DESC";
|
|
currentLoanFilter.PageIndex = 1;
|
|
pagedResponse = await ReserveService.GetFilteredLoans(currentLoanFilter);
|
|
StateHasChanged();
|
|
}
|
|
|
|
void GoToLoan(int loanId)
|
|
{
|
|
NavigationManager.NavigateTo($"LoanView/{loanId}");
|
|
}
|
|
}
|
|
}
|