@page "/take/invoice"


<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="InvoiceNo" />
                            </RadzenColumn>
                            <RadzenColumn Size="8">
                                <RadzenTextBox @bind-Value="InvoiceNo" Style="width: 100%;" Name="InvoiceNo"></RadzenTextBox>
                            </RadzenColumn>
                        </RadzenRow>
                        <RadzenRow AlignItems="AlignItems.Center">
                            <RadzenColumn Size="4">
                                <RadzenLabel Text="请领日期" Component="InvoiceDate" />
                            </RadzenColumn>
                            <RadzenColumn Size="8">
                                <RadzenDatePicker DateFormat="yyyy-MM-dd" CurrentDateChanged="@OnCurrentDateChanged" @bind-Value="InvoiceDate" Style="width: 100%;" Name="InvoiceDate" />
                            </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="@((InOutInvoice oi) => { InvoiceSelected(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="InvoiceNo"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Width="170px" Title="时间" Property="InvoiceDate"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="请领药房" Property="InPharmacyId"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="出库药房" Property="OutPharmacyId"></RadzenDataGridColumn>
                    </Columns>
                </RadzenDataGrid>
        </div>
    </div>
</div>

@code {
    @inject IInOutInvoiceDao inOutInvoiceDao;
    @inject DialogService dialogService;
    RadzenDataGrid<InOutInvoice> grid;
    bool isLoading;
    int count;
    private IEnumerable<InOutInvoice>? _forecasts;
    string InvoiceNo;
    DateTime InvoiceDate = DateTime.MinValue;

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

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

        var result = await inOutInvoiceDao.GetAllInvoiceByType(InvoiceNo, InvoiceDate, args.Top, args.Skip, 2);
        // Update the Data property
        _forecasts = result.Desserts;
        // Update the count
        count = result.TotalDesserts;

        isLoading = false;
    }

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

    async Task InvoiceSelected(InOutInvoice oi)
    {

        var b = await dialogService.OpenAsync<InvoiceOutDialog>(
                $"调拨出库详情",
              new Dictionary<string, object>() { { "invoice", oi } },
              new DialogOptions() { Width = "85vw", Resizable = true, Draggable = true, ShowClose = false }
        );
        if (b)
        {
            await reloadGrid();
        }
    }

    



}
