94 lines
2.5 KiB
C#
94 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using BathHouseManagmet.Database;
|
|
using BathHouseManagmet.Models;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace BathHouseManagmet.ViewModels;
|
|
|
|
public partial class EmployeeViewModel : ViewModelBase
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
[ObservableProperty] ObservableCollection<Employee> _employees;
|
|
[ObservableProperty] ObservableCollection<Position> _positions;
|
|
[ObservableProperty] int _currentPageSize;
|
|
[ObservableProperty] List<int> _pageSizes;
|
|
[ObservableProperty] string _pageInfo;
|
|
|
|
private int totalPages;
|
|
private int currentPage = 0;
|
|
|
|
public EmployeeViewModel(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
|
|
using (var rep = _serviceProvider.GetRequiredService<EmployeeRepository>())
|
|
Employees = new ObservableCollection<Employee>(rep.GetPage());
|
|
using (var rep = _serviceProvider.GetRequiredService<PositionRepository>())
|
|
Positions = new ObservableCollection<Position>(rep.GetPage());
|
|
|
|
PageSizes = new List<int>([5, 10, 15]);
|
|
CurrentPageSize = PageSizes.First();
|
|
}
|
|
|
|
partial void OnCurrentPageSizeChanged(int value)
|
|
{
|
|
CalculatePage();
|
|
}
|
|
|
|
void CalculatePage()
|
|
{
|
|
using var db = new DiscountRepository();
|
|
var rowsCount = db.GetRowsCount();
|
|
totalPages = (int)Math.Ceiling(((double)rowsCount / CurrentPageSize));
|
|
ShowFirstPage();
|
|
}
|
|
|
|
void ShowPage(int pageIndex)
|
|
{
|
|
currentPage = pageIndex;
|
|
using var db = new EmployeeRepository();
|
|
Employees.Clear();
|
|
var rows = db.GetPage(pageIndex, CurrentPageSize);
|
|
rows.ForEach(e => Employees.Add(e));
|
|
PageInfo = $"Страница {currentPage + 1} из {totalPages}";
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ShowFirstPage()
|
|
{
|
|
ShowPage(0);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ShowLastPage()
|
|
{
|
|
ShowPage(totalPages - 1);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ShowNextPage()
|
|
{
|
|
if (currentPage < totalPages - 1)
|
|
ShowPage(currentPage + 1);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ShowPreviousPage()
|
|
{
|
|
if (currentPage > 0)
|
|
ShowPage(currentPage - 1);
|
|
}
|
|
|
|
|
|
[RelayCommand]
|
|
public void AddWindow()
|
|
{
|
|
|
|
}
|
|
} |