using AI.Platform.Core;
using Microsoft.JSInterop;
using SqlSugar;

namespace AI.Platform.Web.Components.Pages.Management.Setting;

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

            var area = await Area.AsQueryable().OrderBy(x => x.Sort).ToTreeAsync(x => x.Children, x => x.ParentCode, 0, x => x.AreaCode);
            Areas = area.Select(ConvertToAreaItem).ToList();

            StateHasChanged();
        }
    }

    private CascaderNode ConvertToAreaItem(SystemArea area)
    {
        return new CascaderNode
        {
            Value = area.Id.ToString(),
            Label = area.AreaName,
            Children = area.Children?.Select(ConvertToAreaItem).ToArray()
        };
    }

    private void OnChange(CascaderNode[] selectedNodes)
    {
        AreadIds = string.Join(",", selectedNodes.Select(x => x.Value));
    }

    /// <summary>
    /// 查
    /// </summary>
    /// <returns></returns>
    private async Task Query()
    {
        Loading = true;

        var data = await Repository.AsQueryable().FirstAsync();
        if (!string.IsNullOrWhiteSpace(data?.AreaIds))
        {
            var ids = data.AreaIds?.Split(',');
            data.LastAreadId = ids[^1].Trim();

            var area = Area.AsQueryable()
                .Where(item => SqlFunc.SplitIn(data.AreaIds, item.Id.ToString()))
                .Select(item => item.AreaName)
                .ToArray();
            data.Area = $"{string.Join(" ", area)} {data.Address}";
        }
        DataSource = data;

        Loading = false;
    }

    private async Task Update()
    {
        var data = DataSource;
        var flag = await Repository.AsUpdateable()
            .SetColumns(x => x.CompanyName == data.CompanyName)
            .SetColumns(x => x.CompanyNo == data.CompanyNo)
            .SetColumns(x => x.CopyRight == data.CopyRight)
            .SetColumns(x => x.RegisterDate == data.RegisterDate)
            .SetColumns(x => x.AreaIds == AreadIds)
            .SetColumns(x => x.Address == data.Address)
            .SetColumns(x => x.ContactPhone == data.ContactPhone)
            .SetColumns(x => x.ContactEmail == data.ContactEmail)
            .SetColumns(x => x.WebsiteUrl == data.WebsiteUrl)
            .Where(x=>x.Id == data.Id)
            .ExecuteCommandAsync() > 0;
        if (flag)
        {
            MessageService.Success("修改成功");
        }
        else
        {
            MessageService.Error("修改失败");
        }
    }

    /// <summary>
    /// 注入实例
    /// </summary>
    [Inject] SqlSugarRepository<SystemSetting> Repository { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [Inject] private SqlSugarRepository<SystemArea> Area { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [Inject] NavigationManager NavigationManager { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [Inject] IJSRuntime IJSRuntime { get; set; }
    /// <summary>
    /// 
    /// </summary>
    private string AreadIds { get; set; }
    /// <summary>
    /// 
    /// </summary>
    private List<CascaderNode> Areas { get; set; } = new List<CascaderNode>();
    /// <summary>
    /// 
    /// </summary>
    [Inject] IMessageService MessageService { get; set; }
    /// <summary>
    /// 
    /// </summary>
    private IForm Form;
    /// <summary>
    /// 
    /// </summary>
    private SystemSetting DataSource;
    /// <summary>
    ///
    /// </summary>
    private bool Loading = false;
}


