
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;

namespace SlnMesnac.WPF.ViewModel.Base
{
    /// <summary>
    /// ViewModel抽象类：分页
    /// </summary>
    public abstract partial class BaseViewModelAsPageQuery : ObservableObject
    {
        /// <summary>
        /// 当前页码
        /// </summary>
        [ObservableProperty]
        public int currentPage = 1;

        /// <summary>
        /// 每页显示的行数
        /// </summary>
        [ObservableProperty]
        public int pageSize = 10;

        /// <summary>
        /// 总条数
        /// </summary>
        public int totalCount = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        [ObservableProperty]
        public int totalPages = 0;

        /// <summary>
        /// 首页
        /// </summary>
        [RelayCommand]
        private void FirstPage() => ChangePage(1);

        /// <summary>
        /// 上一页
        /// </summary>
        [RelayCommand]
        private void PreviousPage() => ChangePage(CurrentPage - 1);

        /// <summary>
        /// 下一页
        /// </summary>
        [RelayCommand]
        private void NextPage() => ChangePage(CurrentPage + 1);

        /// <summary>
        /// 尾页
        /// </summary>
        [RelayCommand]
        private void LastPage() => ChangePage(TotalPages);

        private void ChangePage(int newPage)
        {
            if (newPage >= 1 && newPage <= TotalPages)
            {
                CurrentPage = newPage;
                Query();
            }
        }
        public abstract void Query();
    }
}
