@page "/"
@using AI.Platform.Core.Util
@attribute [ReuseTabsPage(Title = "主页", Pin = true, Closable = false, Order = 0)]

<GridContent>
    <Row Type="flex" Gutter="24">
        <AntDesign.Col Xs="24" Sm="12" Md="12" Lg="12" Xl="6" Style="margin-bottom: 24px;">
            <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard Title="Total Sales"
                       Total="$ 126,560"
                       ContentHeight="46">
                <ChildContent>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Trend Flag="up">
                        WoW Change
                        <span class="trendText">12%</span>
                    </AI.Platform.Web.Components.Pages.Dashboard.Components.Trend>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Trend Flag="down">
                        DoD Change
                        <span class="trendText">11%</span>
                    </AI.Platform.Web.Components.Pages.Dashboard.Components.Trend>
                </ChildContent>
                <Footer>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.Field Label="Daily Sale" Value="$12,423" />
                </Footer>
            </AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard>
        </AntDesign.Col>
        <AntDesign.Col Xs="24" Sm="12" Md="12" Lg="12" Xl="6" Style="margin-bottom: 24px;">
            <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard Title="今日Api访问次数"
                                                                                            Total="@SystemStatistics.ApiDaily.ToString()"
                       ContentHeight="46">
                <Footer>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.Field Label="Api访问总数" Value="@SystemStatistics.ApiTotal.ToString()" />
                </Footer>
            </AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard>
        </AntDesign.Col>
        <AntDesign.Col Xs="24" Sm="12" Md="12" Lg="12" Xl="6" Style="margin-bottom: 24px;">
            <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard Title="Payments"
                       Total="6560"
                       ContentHeight="46">
                <ChildContent>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.MiniBar />
                </ChildContent>
                <Footer>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.Field Label="Conversion Rate" Value="60%" />
                </Footer> 
            </AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard>
        </AntDesign.Col>
        <AntDesign.Col Xs="24" Sm="12" Md="12" Lg="12" Xl="6" Style="margin-bottom: 24px;">
            <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard Title="Operational Effect"
                       Total="78%"
                       ContentHeight="46">
                <ChildContent>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.MiniProgress Percent="78" 
                        StrokeWidth="8" 
                        Target="80" 
                        Color="#13C2C2" />
                </ChildContent>
                <Footer>
                    <AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.Field Label="Conversion Rate" Value="60%" />
                </Footer>
            </AI.Platform.Web.Components.Pages.Dashboard.Components.Charts.ChartCard>
        </AntDesign.Col>
    </Row>
    <div class="salesCard">
        <Card>
            <Tabs TabBarStyle="margin-bottom: 24px;">
                <TabPane Key="1" Tab="服务管理">
                    <Descriptions Bordered>
                        <DescriptionsItem Title="Mysql服务" Span="3">
                            <Badge Status="@MySqlStatus" Text="@MySqlText"></Badge>
                        </DescriptionsItem>
                        <DescriptionsItem Title="账号异常更新状态服务" Span="3">
                            <Badge Status="BadgeStatus.Success" Text="运行中"></Badge>
                        </DescriptionsItem>
                    </Descriptions>
                </TabPane>
                <TabPane Key="2" Tab="服务器信息">
                    <Descriptions Bordered>
                        <DescriptionsItem Title="服务器名称" Span="3">@Environment.MachineName</DescriptionsItem>
                        <DescriptionsItem Title="操作系统" Span="3">@System.Runtime.InteropServices.RuntimeInformation.OSDescription</DescriptionsItem>
                        <DescriptionsItem Title="系统架构" Span="3">@System.Runtime.InteropServices.RuntimeInformation.OSArchitecture</DescriptionsItem>
                        <DescriptionsItem Title="已使用内存/总内存" Span="3">@ComputerInfo.RAMUsage</DescriptionsItem>
                        <DescriptionsItem Title="内存使用率" Span="3">
                            <Badge Status="@RAMStatus" Text="@ComputerInfo.RAMUsageRate"></Badge>
                        </DescriptionsItem>
                        <DescriptionsItem Title="系统盘占用/系统盘总空间" Span="3">@ComputerInfo.SystemDiskUsage</DescriptionsItem>
                        <DescriptionsItem Title="系统盘使用率" Span="3">
                            <Badge Status="@SystemDiskStatus" Text="@ComputerInfo.SystemDiskUsageRate"></Badge>
                        </DescriptionsItem>
                    </Descriptions>
                </TabPane>
                <TabPane Key="3" Tab="项目框架">
                    <Descriptions Bordered>
                        <DescriptionsItem Title="环境变量" Span="3">@Environment.MachineName</DescriptionsItem>
                        <DescriptionsItem Title=".Net版本" Span="3">@System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription</DescriptionsItem>
                        <DescriptionsItem Title="启动时间" Span="3">@System.Diagnostics.Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss")</DescriptionsItem>
                    </Descriptions>
                </TabPane>
            </Tabs>
        </Card>
    </div>
</GridContent>

@inject NavigationManager NavigationManager;
@inject IMessageService MessageService;
@inject IJSRuntime IJSRuntime;

@code {

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await NavigationManager.RedirectLogin(IJSRuntime);
            await Statistics();
            await Information();
            await ServiceStatistics();
        }
    }

    private async Task ServiceStatistics()
    {
        try
        {
            using var con = Sql.Connect();
            await con.Queryable<SystemUser>().FirstAsync();

            MySqlStatus = BadgeStatus.Success;
            MySqlText = "运行中";
        }
        catch (Exception ex)
        {
            MySqlStatus = BadgeStatus.Error;
            MySqlText = $"服务异常（{ex.Message}）";
        }

        StateHasChanged();
    }

    private async Task Statistics()
    {
        SystemStatistics.ApiDaily = await Cache.Get<int>($"request_{DateTime.Now.ToString("yyyyMMdd")}"); 
        SystemStatistics.ApiTotal = await Cache.Get<int>($"request_total"); 
    }

    private async Task Information()
    {
        //windows环境
        ComputerInfo = Computer.GetComputerInfo();

        switch (ComputerInfo.RAMStatus)
        {
            default:
            case ComputerStatus.Normal: RAMStatus = BadgeStatus.Success; break;
            case ComputerStatus.Error: RAMStatus = BadgeStatus.Error; break;
            case ComputerStatus.Warning: RAMStatus = BadgeStatus.Warning; break;
        }

        switch (ComputerInfo.SystemDiskStatus)
        {
            default:
            case ComputerStatus.Normal: SystemDiskStatus = BadgeStatus.Success; break;
            case ComputerStatus.Error: SystemDiskStatus = BadgeStatus.Error; break;
            case ComputerStatus.Warning: SystemDiskStatus = BadgeStatus.Warning; break;
        }
    }

    IChartComponent chart1;

    private async Task OnTabChanged(string activeKey)
    {
        if (activeKey == "1")
        {

        }
        //    await _saleChart.ChangeData(data.SalesData);
        //else
        //    await _visitChart.ChangeData(data.SalesData);
    }

    private async Task onChart1_FirstRender(IChartComponent chart)
    {
        //var data1 = await ChartsDemoData.FireworksSalesAsync(NavigationManager, HttpClient);
        //chart1.ChangeData(data1);
    }

    readonly AreaConfig config1 = new AreaConfig()
        {
            XField = "Date",
            YField = "scales",
            XAxis = new ValueCatTimeAxis()
            {
                Range = new[] { 0, 1 },
                TickCount = 5
            },
            AreaStyle = new GraphicStyle()
            {
                Fill = "l(270) 0:#ffffff 0.5:#7ec2f3 1:#1890ff"
            }
        };

    /// <summary>
    ///
    /// </summary>
    private SystemStatistics SystemStatistics = new SystemStatistics();
    /// <summary>
    ///
    /// </summary>
    private ComputerInfo ComputerInfo = new ComputerInfo();
    /// <summary>
    ///
    /// </summary>
    private BadgeStatus RAMStatus { get; set; }
    /// <summary>
    ///
    /// </summary>
    private BadgeStatus SystemDiskStatus { get; set; }
    /// <summary>
    /// 
    /// </summary>
    private BadgeStatus MySqlStatus { get; set; }
    /// <summary>
    /// 
    /// </summary>
    private string MySqlText { get; set; }
}