@page "/stock/Change"

<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"
                            SelectionMode="DataGridSelectionMode.Single"
                            AllowPaging="true" PageSize="10" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true" PagingSummaryFormat="{0}/{1} 共{2}条数据">
                <Columns>
                    <RadzenDataGridColumn Title="日期" Property="optDate">
                        <Template Context="mr">
                            @mr.optDate.ToString("yyyy-MM-dd")
                        </Template>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="交班发药人" Property="FromOperator"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="交班审核人" Property="FromRviewer"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="接班发药人" Property="ToOperator"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="接班审核人" Property="ToReviewer"></RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="接班日期" Property="ToDate">

                        <Template Context="mr">
                            @mr.ToDate.ToString("yyyy-MM-dd")
                        </Template>
                    </RadzenDataGridColumn>
                    <RadzenDataGridColumn Title="当班状态" Property="State">
                        <Template Context="state">
                            @if(state.State=="0")
                            {
                                <RadzenText>值班中</RadzenText>
                            }
                            else
                            {
                                <RadzenText>已交班</RadzenText>
                            }
                        </Template>
                    </RadzenDataGridColumn>
                </Columns>
            </RadzenDataGrid>
        </div>
    </div>
</div>

@code {
    @inject IHkcChangeShiftsDao hkcChangeShiftsDao;
    @inject DialogService dialogService;

    @inject NavigationManager navigate;
    RadzenDataGrid<HkcChangeShifts> grid;
    bool isLoading;
    int count;
    private IEnumerable<HkcChangeShifts>? _forecasts;
    DateTime start;
    DateTime end;


    [Parameter] public string type { get; set; }

    protected override void OnParametersSet()
    {
        if (grid != null)
        {
            reloadGrid();
        }
    }
    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;

        var result = await hkcChangeShiftsDao.GetChangeShiftRecordAsync(start, end,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();
    }
}