@page "/return/byRecord"

<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"  @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" @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"
                    Count="@count"
                    EmptyText="无数据"
                    Data="@_forecasts"
                    AllowColumnResize="true" AllowAlternatingRows="false"
                    AllowRowSelectOnRowClick="true"
                    CellClick="@((DataGridCellMouseEventArgs<MachineRecord> args) => OnCellClick(args))"
                    AllowPaging="true" PageSize="8" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true" PagingSummaryFormat="{0}/{1} 共{2}条数据">
                    <HeaderTemplate>
                        <RadzenRow JustifyContent="JustifyContent.End">
                            <RadzenButton ButtonStyle="ButtonStyle.Warning" Disabled="_forecasts == null || !_forecasts.Any(mr => mr.CurrentReturnQuantity > 0)" Variant="Variant.Outlined" Shade="Shade.Light" Text="归还" Click="@OpenReturnDialog" />
                        </RadzenRow>
                    </HeaderTemplate>
                    <Columns>
                        <RadzenDataGridColumn Frozen="true" Title="操作人" Property="OperatorUser.NickName"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Width="170px" Title="操作时间" Property="OperationTime"></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="ManuNo">
                            <Template Context="mr">
                                <RadzenText TextStyle="TextStyle.Subtitle2" class="mb-0">@mr.ManuNo</RadzenText>
                                <RadzenText TextStyle="TextStyle.Caption"> @mr.EffDate?.ToString("yyyy-MM-dd")</RadzenText>
                            </Template>
                        </RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="借出数量" Property="Quantity"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="已还药品" Property="ReturnQuantity1"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="已还空瓶" Property="ReturnQuantity2"></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;
    RadzenDataGrid<MachineRecord> grid;
    bool isLoading;
    int count;
    private List<MachineRecord>? _forecasts;

    DateTime start;
    DateTime end;


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

        var result = await machineRecordDao.GetCanReturnRecords(start, end, 0, "", args.Top, args.Skip);
        // Update the Data property
        _forecasts = result.Desserts;
        // Update the count
        count = result.TotalDesserts;

        isLoading = false;
    }

    async Task reloadGrid()
    {
        start = DateTime.MinValue;
        end = DateTime.MinValue;
        await grid.Reload();
    }
    async Task OpenReturnDialog()
    {
        var list = _forecasts.Where(mr => mr.CurrentReturnQuantity > 0).ToList();

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

    }

    void OnCellClick(DataGridCellMouseEventArgs<MachineRecord> args)
    {
        grid.EditRow(args.Data);
    }




}
