@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" style="height:50vh;overflow:auto">

            <RadzenDataGrid @ref="grid" Style="height:48vh;"
                            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 Width="60px" Title="药盒" Property="OperationTime">
                        <Template Context="mr">

                            @if (mr.BoxDrawer > 0)
                            {
                                if (mr.DrawerNo == 99)
                                {
                                    <b class="rz-pr-3">@($"急诊药盒")</b>
                                }
                                else if (mr.DrawerNo == 111)
                                {
                                    <b class="rz-pr-3">@($"恢复室药盒")</b>
                                }
                                else
                                {
                                    <b class="rz-pr-3">@($"{mr.DrawerNo}-{(mr.ColNo == 1 ? '白' : '绿')}")</b>
                                }
                            }
                            else
                            {
                                <b class="rz-pr-3">无</b>
                            }
                        </Template>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="批次" Property="ManuNo" Width="123px">
                        <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">
                        <HeaderTemplate>
                            <div style="text-align: center;">
                                借出<br>数量
                            </div>
                        </HeaderTemplate>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="已还药品" Property="ReturnQuantity1">
                        <HeaderTemplate>
                            <div style="text-align: center;">
                                已还<br>药品
                            </div>
                        </HeaderTemplate>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="已还空瓶" Property="ReturnQuantity2">
                        <HeaderTemplate>
                            <div style="text-align: center;">
                                已还<br>空瓶
                            </div>
                        </HeaderTemplate>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="本次归还" Property="CurrentReturnQuantity" Width="90px">
                        <HeaderTemplate>
                            <div style="text-align: center;">
                                本次<br>归还
                            </div>
                        </HeaderTemplate>
                        <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);
    }




}
