@page "/Box/BoxAdd"
@using MasaBlazorApp3.Report
@using Radzen.Blazor.Rendering
<style>
    @@keyframes shake {
        0% {
            opacity: 1;
            color: red;
        }

        25% {
            opacity: 1;
            color: orange;
        }

        50% {
            opacity: 0;
            color: yellow;
        }

        75% {
            opacity: 1;
            color: crimson;
        }

        100% {
            opacity: 1;
            color: red;
        }
    }

    .rz-custom-header {
        width: 100%;
    }

    .shaky-text {
        animation: shake 2s infinite ease-in-out;
        /* 你可以根据需要调整动画的持续时间和迭代次数 */
        transition: color 0.5s linear,opacity 0.5s linear;
    }

    .my-popup {
        display: none;
        position:absolute;
        overflow: hidden;
        /*     height: 360px;
            width: 600px; */
        border: var(--rz-panel-border);
        background-color: var(--rz-panel-background-color);
        box-shadow: var(--rz-panel-shadow);
        border-radius: var(--rz-border-radius);
    }
</style>

<div class="container-fluid">
@*     <RadzenFieldset Text="报表导出">
        <RadzenStack Orientation="Orientation.Horizontal" Gap="1rem">
            <RadzenRow JustifyContent="JustifyContent.End" AlignItems="AlignItems.Center">
                <RadzenButton @ref=button Icon="download" Text="请领登记表导出" Variant="Variant.Outlined" Click="@(args => popup.ToggleAsync(button.Element))" />
                <Popup @ref=popup Lazy=true class="my-popup">
                    <RadzenStack Orientation="Orientation.Vertical" Gap="1rem" class="rz-h-100 rz-p-4">
                        <RadzenFieldset Text="条件选择">
                            <RadzenStack Orientation="Orientation.Horizontal" Gap="1rem">
                                <RadzenRow AlignItems="AlignItems.Center">
                                    <RadzenColumn Size="4">
                                        <RadzenLabel Text="查询时间" Component="Start" />
                                    </RadzenColumn>
                                    <RadzenColumn Size="8">
                                        <RadzenDatePicker DateFormat="yyyy-MM-dd" @bind-Value="start" Style="width: 100%;" Name="Start" />
                                    </RadzenColumn>
                                </RadzenRow>
                                <RadzenRow JustifyContent="JustifyContent.End" AlignItems="AlignItems.Center">
                                    <RadzenButton Icon="download" Text="导出" Variant="Variant.Outlined" Click="AccountBookExport" />
                                </RadzenRow>
                            </RadzenStack>
                        </RadzenFieldset>
                    </RadzenStack>
                </Popup>
            </RadzenRow>
        </RadzenStack>
    </RadzenFieldset> *@
    <RadzenDataGrid @ref="grid" RowSelect="@OnRowSelect" TItem="ChannelList"
                    RowRender="@RowRender"
                    EmptyText="无数据"
                    Data="@_forecasts" @bind-Value=@selectedChannelLists
                    AllowColumnResize="true" AllowAlternatingRows="false"
                    SelectionMode="DataGridSelectionMode.Single"
                    ExpandMode="DataGridExpandMode.Multiple" LoadData="@LoadData" IsLoading="@isLoading" Count="@count"
                    AllowPaging="true" PageSize="10" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true" PagingSummaryFormat="{0}/{1} 共{2}条数据">
        <Template Context="cs">
            <RadzenDataGrid Data="@cs.ChannelStocks" EmptyText="无数据">
                <Columns>
                    <RadzenDataGridColumn Title="药品名称" Property="Drug.DrugName">
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="规格" Property="Drug.DrugSpec">
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="厂家" Property="Drug.Manufactory">
                    </RadzenDataGridColumn>
                    @*  <RadzenDataGridColumn Title="批次" Property="ManuNo"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="效期" Property="EffDate">
                    </RadzenDataGridColumn> *@
                    <RadzenDataGridColumn Title="基数" Property="BaseQuantity">
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="需补药数" Property="NeedQuantity">
                    </RadzenDataGridColumn>
                </Columns>
            </RadzenDataGrid>
        </Template>
        <Columns>
            <RadzenDataGridColumn Frozen="true" Width="200px" Title="药盒号" Property="DrawerNo">
                <Template Context="DrawerNo">

                    @(DrawerNo.DrawerNo == 99 ? "急诊药盒" : DrawerNo.DrawerNo == 111 ? "恢复室药盒" :
                        DrawerNo.DrawerNo == 31 ? "胃镜药盒" :
                        DrawerNo.DrawerNo == 32 ? "导管药盒" :
                        DrawerNo.DrawerNo == 33 ? "生殖药盒" :
                        DrawerNo.DrawerNo == 34 ? "妇门药盒" : $"{DrawerNo.DrawerNo}-{(DrawerNo.ColNo == 1 ? '白' : '绿')}")
                    @* @(DrawerNo.DrawerNo == 99 ? "急诊药盒" : DrawerNo.DrawerNo == 111 ? "恢复室药盒" : $"{DrawerNo.DrawerNo}-{(DrawerNo.ColNo==1?'白':'绿')}") *@

                    @* @($"{DrawerNo.DrawerNo}-{DrawerNo.ColNo}") *@
                </Template>
            </RadzenDataGridColumn>
            <RadzenDataGridColumn Title="总库存" Property="TotalQuantity"></RadzenDataGridColumn>
        </Columns>
    </RadzenDataGrid>
</div>

@code {
    @inject IChannelListDao channelStockDao;
    @inject DialogService dialogService;
    @inject IReportDataDao reportDataDao;
    RadzenDataGrid<ChannelList> grid;
    bool isLoading;
    int count;
    private IEnumerable<ChannelList>? _forecasts;
    DateTime start;
    DateTime end;
    RadzenButton button;
    Popup popup;
    IList<ChannelList> selectedChannelLists;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

    }
    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;

        var result = await channelStockDao.GetAllBoxAddDrug(args.Top, args.Skip);
        // Update the Data property
        _forecasts = result.Desserts;
        isLoading = false;
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        if (firstRender)
        {
            // await grid.ExpandRows(grid.PagedView.Where(di => di.Stocks.Count > 0));
        }
    }

    void RowRender(RowRenderEventArgs<ChannelList> args)
    {
        args.Expandable = args.Data.ChannelStocks.Count > 0;
    }

    async void OnRowSelect(ChannelList cl)
    {
        var b = await dialogService.OpenAsync<BoxAddDetailDialog>(
               cl.DrawerNo==99?"急诊药盒":cl.DrawerNo==111?"恢复室药盒":$"{cl.DrawerNo}-{(cl.ColNo==1?'白':'绿')} 药盒加药",
             new Dictionary<string, object>() { { "boxChannelList", cl } },
             new DialogOptions() { Width = "85vw", Resizable = true, Draggable = true, ShowClose = false }
       );
        if (b)
        {
            await grid.Reload();
        }
    }



    //请领登记表导出
    async Task AccountBookExport()
    {
        // string drugId = drugName != null ? drugName.Split('/').Last().Trim():"";
        // GridReportUtil gridReportUtil = new GridReportUtil();
        PageData<ReportApplyInfo> pageData = await reportDataDao.GetApplyInfoDate(start);
        GridReportUtil.PrintReport("account_book_temp.grf", pageData);
    }



}
