@page "/return/order"


<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="OrderNo" />
                            </RadzenColumn>
                            <RadzenColumn Size="8">
                                <RadzenTextBox @bind-Value="OrderNo" Style="width: 100%;" Name="OrderNo"></RadzenTextBox>
                            </RadzenColumn>
                        </RadzenRow>
                        <RadzenRow AlignItems="AlignItems.Center">
                            <RadzenColumn Size="4">
                                <RadzenLabel Text="处方时间" Component="OrderDate" />
                            </RadzenColumn>
                            <RadzenColumn Size="8">
                                <RadzenDatePicker DateFormat="yyyy-MM-dd" CurrentDateChanged="@OnCurrentDateChanged" @bind-Value="OrderDate" Style="width: 100%;" Name="OrderDate" />
                            </RadzenColumn>
                        </RadzenRow>
                    
                        <RadzenRow AlignItems="AlignItems.Center">
                            <RadzenColumn Size="12">
                                <RadzenButton Size="ButtonSize.Medium" ButtonType="ButtonType.Submit" IsBusy="isLoading" Icon="search"  Text="查询" />
                                <RadzenButton Size="ButtonSize.Medium" 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"
                    RowSelect="@((OrderInfo oi) => { OrderSelected(oi); })"
                    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 Frozen="true" Title="处方号" Property="OrderNo"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Width="170px" Title="开方时间" Property="OrderDate"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="医生" Property="DoctorName"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="姓名" Property="PatientName"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="性别" Property="Sex"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="年龄" Property="Age"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="科室" Property="DeptName"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="诊断" Property="Disease"></RadzenDataGridColumn>
                    </Columns>
                </RadzenDataGrid>
        </div>
    </div>
</div>

@code {
    @inject IOrderInfoDao orderInfoDao;
    @inject DialogService dialogService;
    RadzenDataGrid<OrderInfo> grid;
    bool isLoading;
    int count;
    private IEnumerable<OrderInfo>? _forecasts;
    string OrderNo;
    DateTime OrderDate;

    void OnCurrentDateChanged(DateTime args)
    {
        OrderDate = new DateTime(args.Year, args.Month, args.Day);
    }

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

        var result = await orderInfoDao.GetAllCanReturnOrderInfo(OrderNo, OrderDate, args.Top, args.Skip);
        // Update the Data property
        _forecasts = result.Desserts;
        // Update the count
        count = result.TotalDesserts;

        isLoading = false;
    }

    async Task reloadGrid()
    {
        OrderNo = "";
        OrderDate = DateTime.MinValue;
        await grid.Reload();
    }

    async Task OrderSelected(OrderInfo oi)
    {
        
        var b = await dialogService.OpenAsync<OrderDetailReturnDialog>(
                $"退药处方详情",
              new Dictionary<string, object>() { { "order", oi } },
              new DialogOptions() { Width = "85vw", Resizable = true, Draggable = true, ShowClose = false }
        );
        if(b)
        {
            await reloadGrid();
        }
    }

    



}
