using System;
using Avalonia.Collections;
using ReactiveUI;

namespace detect.gui.ViewModels;

public class PageModel<T, F> : PageModelBase where F : new()
{
    /// <summary>
    /// 查询条件
    /// </summary>
    private F _filters = new();
    public F Filters
    {
        get => _filters;
        set => this.RaiseAndSetIfChanged(ref _filters, value);
    }
    
    /// <summary>
    /// 数据列表
    /// </summary>
    private AvaloniaList<T>? _result;
    public AvaloniaList<T>? Result
    {
        get => _result;
        set => this.RaiseAndSetIfChanged(ref _result, value);
    }

    public PageModel()
    {
        this.WhenAnyValue(x => x.RecordCount, x=>x.PageSize, x=>x.PageNum,
                (n1,n2,n3) => new { _recordCount = n1, _pageSize = n2, _pageNum = n3 })
            .Subscribe(x =>
            {
                PageCount =  (int)Math.Ceiling((double)x._recordCount / x._pageSize);
                StartIndex = (x._pageNum - 1) * x._pageSize;
                EndIndex = x._pageNum * x._pageSize < x._recordCount ? x._pageNum * x._pageSize : x._recordCount;
            });
    }
}