@page "/return/empty"

<style>

    .rz-custom-header {
    width: 100%;
    }
</style>
<div class="container-fluid">
    <div class="row">
        @*  <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="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>
                    </RadzenStack>
                </RadzenFieldset>
            </form>
        </div> *@
        <div class="col-12 mb-4">

            <RadzenDataGrid @ref="grid"
            LoadData="@LoadData"
            IsLoading="@isLoading"
            EmptyText="无数据"
            Data="@_forecasts"
            AllowColumnResize="true" AllowAlternatingRows="false"
            AllowRowSelectOnRowClick="true"
            CellClick="@((DataGridCellMouseEventArgs<ChannelStock> args) => OnCellClick(args))" >
                @*           <HeaderTemplate>
                    <RadzenRow JustifyContent="JustifyContent.End">
                        <RadzenButton ButtonStyle="ButtonStyle.Warning" Disabled="_forecasts == null || !_forecasts.Any(mr => mr.DrawerNo > 0)" Variant="Variant.Outlined" Shade="Shade.Light" Text="归还" Click="@OpenReturnDialog" />
                    </RadzenRow>
                </HeaderTemplate> *@
                <Columns>
                    <RadzenDataGridColumn Frozen="true" Title="库位" Property="Location"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Width="200px" Title="药品名称" Property="Drug.DrugName">
                        @* <Template Context="mr">
                            <RadzenText TextStyle="TextStyle.Subtitle2" class="mb-0">@mr.Drug?.DrugName</RadzenText>
                            <RadzenText TextStyle="TextStyle.Caption">@mr.Drug?.DrugSpec</RadzenText>
                        </Template> *@
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="厂家" Property="Drug.Manufactory"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="库存" Property="Quantity"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="可归还数量" Property="CanReturnQuantity"></RadzenDataGridColumn>
                    @*<RadzenDataGridColumn Title="本次归还" Property="CurrentReturnQuantity">
                        <EditTemplate Context="mr">
                             <RadzenNumeric Min="0" Style="display: block" Max="@(mr.Quantity - mr.ReturnQuantity1 - mr.ReturnQuantity2)" Name="Quantity" @bind-Value=@mr.CurrentReturnQuantity /> 
                        </EditTemplate>
                    </RadzenDataGridColumn>*@
                </Columns>
            </RadzenDataGrid>
        </div>
    </div>
</div>

@code {
    @inject IMachineRecordDao machineRecordDao;
    @inject DialogService dialogService;
    @inject NotificationService _message
    RadzenDataGrid<ChannelStock> grid;
    bool isLoading;
    int count;
    private List<ChannelStock>? _forecasts;

    // DateTime start;
    // DateTime end;


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

        _forecasts = await machineRecordDao.GetReturnEmptyWithCanReturnQuantiy();
        // _forecasts = await machineRecordDao.GetReturnEmpty();

        isLoading = false;
    }

    async Task reloadGrid()
    {
        await grid.Reload();
    }
    async Task OnCellClick(DataGridCellMouseEventArgs<ChannelStock> args)
    {
        if (string.IsNullOrEmpty(args.Data.DrugId))
        {
            _message.Notify(
                new NotificationMessage { Style = "position: absolute; inset-inline-start: -1000px;", Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"该库位无药品，请先绑药！", Duration = 4000 }
            );
            return;
        }
        // grid.EditRow(args.Data);
        var list = args.Data;// _forecasts.Where(mr => mr.DrawerNo > 0).ToList();

        var b = await dialogService.OpenAsync<RecordReturnEmptyDialog>(
                $"归还空瓶",
              new Dictionary<string, object>() { { "records", list } },
              new DialogOptions() { Width = "85vw", Resizable = true, Draggable = true, ShowClose = false }
        );
        if (b)
        {
            await reloadGrid();
        }
    }
}
