using AntDesign.TableModels;
using EasyTemplate.Tool;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using SqlSugar;

namespace EasyTemplate.Blazor.Web.Components.Pages.Management.Logs.LoginLog;

public partial class LoginLog
{
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await NavigationManager.RedirectLogin(IJSRuntime);
            //await Query();
        }
    }

    /// <summary>
    /// 查
    /// </summary>
    /// <returns></returns>
    private async Task Query()
    {
        Loading = true;
        RefAsync<int> total = 0;
        var res = await Repository.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(Q_Name), x => x.Info.Contains(Q_Name))
            .OrderByDescending(x => x.Id)
            .ToPageListAsync(Pi, Ps, total);
        Total = total.Value;
        DataSource = res;
        Loading = false;
        //_table.ReloadData();
    }

    /// <summary>
    /// 重置查询
    /// </summary>
    private async Task ResetQuery()
    {
        Q_Name = string.Empty;
        Pi = 1;
        await Query();
    }

    private async Task OnChange(QueryModel<SystemLogLogin> query)
        => await Query();

    private async Task Search()
    {
        Pi = 1;
        await Query();
    }

    /// <summary>
    /// 注入实例
    /// </summary>
    [Inject] private SqlSugarRepository<SystemLogLogin> Repository { get; set; }
    /// <summary>
    /// 数据源
    /// </summary>
    [Inject] NavigationManager NavigationManager { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [Inject] IJSRuntime IJSRuntime { get; set; }
    /// <summary>
    /// 账号
    /// </summary>
    private string Q_Name { get; set; }
    /// <summary>
    ///
    /// </summary>
    private ITable Table;
    /// <summary>
    ///
    /// </summary>
    private IEnumerable<SystemLogLogin> SelectedRows = [];
    /// <summary>
    ///
    /// </summary>
    private List<SystemLogLogin> DataSource;
    /// <summary>
    ///
    /// </summary>
    private int Pi = 1;
    /// <summary>
    ///
    /// </summary>
    private int Ps = 20;
    /// <summary>
    ///
    /// </summary>
    private int Total;
    /// <summary>
    ///
    /// </summary>
    private bool Loading = false;
    /// <summary>
    ///
    /// </summary>
    Func<PaginationTotalContext, string> ShowTotal => x => $"总共 {Total} 条";
}


