@page "/stock/list"
@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: auto;
        /* height: 360px; */
        width: 900px;
        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="col-12 mb-4">

    <form onsubmit="@(() => grid.Reload())">
        <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 HH:mm:ss" ShowTime="true" @bind-Value="start" Style="width: 100%;" Name="Start" />
                    </RadzenColumn>
                </RadzenRow>
                <RadzenRow AlignItems="AlignItems.Center">
                    <RadzenColumn Size="4">
                        <RadzenLabel Text="结束时间" Component="End" />
                    </RadzenColumn>
                    <RadzenColumn Size="8">
                        <RadzenDatePicker DateFormat="yyyy-MM-dd HH:mm:ss" ShowTime="true" @bind-Value="end" Style="width: 100%;" Name="End" />
                    </RadzenColumn>
                </RadzenRow>
                <RadzenRow AlignItems="AlignItems.Center">
                    <RadzenColumn Size="6">
                        <RadzenLabel Text="药品名称/拼音码/编码" Component="drug" />
                    </RadzenColumn>
                    <RadzenColumn Size="6">
                        <RadzenAutoComplete Name="drug" Multiline="true" @bind-Value=@drugName Data=@drugInfo TextProperty="@nameof(DrugInfo.DrugNameSpecManufactory)"
                                            Style="width: 100%;" InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "drugInfo" } })" />
                    </RadzenColumn>
                </RadzenRow>

                <RadzenRow AlignItems="AlignItems.Center">
                    <RadzenColumn Size="12">
                         <RadzenButton Size="ButtonSize.Large" ButtonType="ButtonType.Submit" IsBusy="isLoading" Icon="search" Text="查询" /> 
                        <RadzenButton Size="ButtonSize.Large" Click="reloadGrid" IsBusy="isLoading" Icon="refresh" Text="重置" ButtonStyle="ButtonStyle.Warning" />
                    </RadzenColumn>
                </RadzenRow>
                <RadzenRow JustifyContent="JustifyContent.End" AlignItems="AlignItems.Center">
                    <RadzenButton Icon="download" Text="库存导出" Variant="Variant.Outlined" Click="StockExport" />
                    <RadzenButton Icon="download" Text="专用账册导出" Variant="Variant.Outlined" Click="AccountBookExport" />
                </RadzenRow>*@
                <RadzenRow JustifyContent="JustifyContent.End" AlignItems="AlignItems.Center">
                    @* <RadzenButton @ref=button Icon="download" Text="库存导出" Variant="Variant.Outlined" Click="@(args => popup.ToggleAsync(button.Element))" /> *@
                    <RadzenButton @ref=button Icon="download" Text="专用账册导出" Variant="Variant.Outlined" Click="@(args => popup2.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="6">
                                            <RadzenLabel Text="药品名称/拼音码/编码" Component="drug" />
                                        </RadzenColumn>
                                        <RadzenColumn Size="6">
                                            <RadzenAutoComplete Name="drug" Multiline="true" @bind-Value=@drugName Data=@drugInfo TextProperty="@nameof(DrugInfo.DrugNameSpecManufactory)"
                                                                Style="width: 100%;" InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "drugInfo" } })" />
                                        </RadzenColumn>
                                    </RadzenRow>

                                    <RadzenRow AlignItems="AlignItems.Center">
                                        <RadzenColumn Size="12">
                                            <RadzenButton Size="ButtonSize.Large" Click="reloadGrid" IsBusy="isLoading" Icon="refresh" Text="重置" ButtonStyle="ButtonStyle.Warning" />
                                        </RadzenColumn>
                                    </RadzenRow>
                                    <RadzenRow JustifyContent="JustifyContent.End" AlignItems="AlignItems.Center">
                                        <RadzenButton Icon="download" Text="导出" Variant="Variant.Outlined" Click="StockExport" />
                                    </RadzenRow>
                                </RadzenStack>
                            </RadzenFieldset>
                        </RadzenStack>
                    </Popup> *@
                    <Popup @ref=popup2 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" />
                                            <RadzenLabel Text="时间" Component="Start" />
                                        </RadzenColumn>
                                        <RadzenColumn Size="8">
                                            <RadzenDatePicker DateFormat="yyyy-MM-dd HH:mm:ss" ShowTime="true" @bind-Value="start" Style="width: 100%;" Name="Start" />
                                        </RadzenColumn>
                                    </RadzenRow>
                                    <RadzenRow AlignItems="AlignItems.Center">
                                        <RadzenColumn Size="4">
                                            <RadzenLabel Text="结束" Component="End" />
                                            <RadzenLabel Text="时间" Component="End" />
                                        </RadzenColumn>
                                        <RadzenColumn Size="8">
                                            <RadzenDatePicker DateFormat="yyyy-MM-dd HH:mm:ss" ShowTime="true" @bind-Value="end" Style="width: 100%;" Name="End" />
                                        </RadzenColumn>
                                    </RadzenRow>
                                    <RadzenRow AlignItems="AlignItems.Center">
                                        <RadzenColumn Size="5">
                                            <RadzenLabel Text="药品名称" Component="drug" />
                                            <RadzenLabel Text="拼音码" Component="drug" />
                                            <RadzenLabel Text="编码" Component="drug" />
                                        </RadzenColumn>
                                        <RadzenColumn Size="6">
                                            <RadzenAutoComplete Name="drug" Multiline="true" @bind-Value=@drugName Data=@drugInfo TextProperty="@nameof(DrugInfo.DrugNameSpecManufactory)"
                                                                Style="width: 100%;" InputAttributes="@(new Dictionary<string, object>() { { "aria-label", "drugInfo" } })" />
                                        </RadzenColumn>
                                    </RadzenRow>

                                    <RadzenRow AlignItems="AlignItems.Center">
                                        <RadzenColumn>
                                            <RadzenButton Click="reloadGrid" Icon="refresh" Text="重置" ButtonStyle="ButtonStyle.Warning" />
                                        </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>
    </form>
</div>
<div class="col-12 mb-4">

    <RadzenDataGrid @ref="grid"
                    IsLoading="@isLoading"
                    RowRender="@RowRender"
                    EmptyText="无数据"
                    Data="@_forecasts"
                    AllowColumnResize="true" AllowAlternatingRows="false"
                    SelectionMode="DataGridSelectionMode.Single"
                    ExpandMode="DataGridExpandMode.Multiple">
        @* <HeaderTemplate>
            <RadzenRow JustifyContent="JustifyContent.End" AlignItems="AlignItems.Center">
                <RadzenButton Icon="download" Text="库存导出" Variant="Variant.Outlined" Click="StockExport" />
                <RadzenButton Icon="download" Text="专用账册导出" Variant="Variant.Outlined" Click="AccountBookExport" />
            </RadzenRow>
        </HeaderTemplate> *@
        <Template Context="di">
            <RadzenDataGrid Data="@di.Stocks" EmptyText="无数据">
                <Columns>
                    <RadzenDataGridColumn Title="库位" Property="DrawerNo">
                        <Template Context="s">
                            @s.DrawerNo - @s.ColNo
                        </Template>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="数量" Property="Quantity">
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="批次" Property="ManuNo"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="效期" Property="EffDate">
                        <Template Context="s">
                            @{
                                DateTime dateTime;
                                bool success = DateTime.TryParse(s.EffDate, out dateTime);
                                // if (success && dateTime <= DateTime.Now.AddMonths(3))
                                // {
                                //     @s.EffDate

                                //     <p class="shaky-text">近效期药品！</p>
                                // }
                                // else
                                //{
                                    @s.EffDate
                                //}
                            }
                        </Template>
                    </RadzenDataGridColumn>
                </Columns>
            </RadzenDataGrid>
        </Template>
        <Columns>
            <RadzenDataGridColumn Frozen="true" Width="200px" Title="药品名称" Property="DrugName"></RadzenDataGridColumn>
            <RadzenDataGridColumn Title="规格" Property="DrugSpec"></RadzenDataGridColumn>
            <RadzenDataGridColumn Title="总库存" Property="StockQuantity">
                <Template Context="s">
                  @*   @if (s.StockQuantity < 10)
                    {
                        @s.StockQuantity

                        <p class="shaky-text">库存预警！</p>
                    }
                    else
                    { *@
                        @s.StockQuantity
                    @* } *@
                </Template>
            </RadzenDataGridColumn>

        </Columns>
    </RadzenDataGrid>
</div>

@code {
    @inject IDrugInfoDao drugInfoDao;
    @inject DialogService dialogService;
    @inject IReportDataDao reportDataDao;
    RadzenDataGrid<DrugInfo> grid;
    bool isLoading;
    int count;
    private IEnumerable<DrugInfo>? _forecasts;
    DateTime? start;
    DateTime? end;

    string drugName;
    RadzenButton button;
 
    Popup popup2;
    IEnumerable<DrugInfo> drugInfo;

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

        var result = await drugInfoDao.GetAllDrugAndStock();
        // Update the Data property
        _forecasts = result;

        drugInfo = await drugInfoDao.GetAllDrug();

        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<DrugInfo> args)
    {
        args.Expandable = args.Data.Stocks.Count > 0;
    }


    // //库存导出
    // async Task StockExport()
    // {
    //     PageData<ReportStockDateInfo> pageData = await reportDataDao.GetStockExportData(drugName);
    //     GridReportUtil.PrintReport("stock_template.grf", pageData);
    // }
    //专用账册导出
    async Task AccountBookExport()
    {
        // string drugId = drugName != null ? drugName.Split('/').Last().Trim():"";
        // GridReportUtil gridReportUtil = new GridReportUtil();
        PageData<ReportAccountDateInfo> pageData = await reportDataDao.GetAccountExportData(start, end, drugName);
        GridReportUtil.PrintReport("account_book_temp.grf", pageData);
    }

    //重置
    async Task reloadGrid()
    {
        start = DateTime.MinValue;
        end = DateTime.MinValue;
        drugName = string.Empty;
        await grid.Reload();
    }

}
