@page "/Box/BoxStockAAA"
@using MasaBlazorApp3.Pojo.Config

<RadzenStack Orientation="Orientation.Horizontal" class="col-12 mb-4">
    <div class="col-2 row justify-content-center align-items-center text-center">
        <div class="row justify-content-around align-items-center" style="height:600px;overflow:auto">
            @foreach (var cs in channelStockList)
            {
                <RadzenButton class="col-12" Style="margin-bottom:5px" Click="@(() => SelectDrawer((cs.DrawerNo.ToString() + "-" + cs.ColNo.ToString())))" Text="@(cs.DrawerNo==99?"急诊室药盒":cs.DrawerNo==111?"恢复室药盒":cs.DrawerNo==31?"胃镜药盒":cs.DrawerNo==32?"导管药盒":cs.DrawerNo==33?"生殖药盒":cs.DrawerNo==34?"妇门药盒":$"{cs.DrawerNo.ToString() + "-" + (cs.ColNo==1?'白':'绿')}")" Disabled="@(status > 0)" Shade="Shade.Light" Variant="@(drawerNo != (cs.DrawerNo.ToString() + "-" + cs.ColNo.ToString()) ? Variant.Outlined : Variant.Flat)" />

            }
        </div>
    </div>
    <div class="col-10">
        <RadzenDataGrid @ref="grid"
                        IsLoading="@isLoading"
                        RowRender="@RowRender"
                        EmptyText="无数据"
                        Data="@_forecasts"
                        AllowColumnResize="true" AllowAlternatingRows="false"
                        SelectionMode="DataGridSelectionMode.Single"
                        ExpandMode="DataGridExpandMode.Single">
            <Template Context="di">
                <RadzenDataGrid Data="@di.Stocks" EmptyText="无数据">
                    <Columns>
                        @* <RadzenDataGridColumn Title="库位" Property="DrawerNo">
                            <Template Context="s">
                                @s.DrawerNo - @s.ColNo
                            </Template>
                        </RadzenDataGridColumn> *@

                        <RadzenDataGridColumn Title="批次" Property="ManuNo"></RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="效期" Property="EffDate">
                            <Template Context="s">
                                @{
                                    DateTime dateTime;
                                    bool success = DateTime.TryParse(s.EffDate, out dateTime);
                                    @s.EffDate
                                }
                            </Template>
                        </RadzenDataGridColumn>
                        <RadzenDataGridColumn Title="数量" Property="Quantity">
                        </RadzenDataGridColumn>
                    </Columns>
                </RadzenDataGrid>
            </Template>
            <Columns>
                <RadzenDataGridColumn Frozen="true" Width="200px" Title="药品名称" Property="DrugName"></RadzenDataGridColumn>
                <RadzenDataGridColumn Title="规格" Property="DrugSpec"></RadzenDataGridColumn>
                <RadzenDataGridColumn Title="基数" Property="BoxBaseQuantity">
                    <Template Context="s">
                        @s.BoxBaseQuantity
                    </Template>
                </RadzenDataGridColumn>
                <RadzenDataGridColumn Title="总库存" Property="BoxBaseQuantity">
                    <Template Context="s">
                        @s.StockQuantity
                    </Template>
                </RadzenDataGridColumn>
            </Columns>
        </RadzenDataGrid>
    </div>
</RadzenStack>
@code {
    @inject IOrderInfoDao orderInfoDao;
    @inject IChannelListDao channelListDao;
    @inject DialogService dialogService;
    @inject Microsoft.Extensions.Options.IOptions<Pojo.Config.SettingConfig> setting;
    @inject NotificationService _message

    @inject IDrugInfoDao drugInfoDao;

    private IEnumerable<DrugInfo>? _forecasts;
    bool allowRowSelectOnRowClick = true;
    // IEnumerable<DrugManuNo> drugManuNos;
    // IList<DrugManuNo> selectedDrugManuNos;

    RadzenDataGrid<DrugInfo> grid;
    // RadzenDataGrid<DrugManuNo> gridManuNo;
    bool isLoading;
    int count;
    int[] DrawerNos = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    List<ChannelStock> channelStockList = new();
    int status = 0;
    string drawerNo = "1-1";
    string OrderNo;
    DateTime OrderDate;
    async Task SelectDrawer(string strDrawerNoColNo)
    {
        this.drawerNo = strDrawerNoColNo;

        BoxModel boxModel = new BoxModel();
        boxModel.BoxName = 1;
        boxModel.BoxNo = 1;
        // grid.Reload();
        if (drawerNo != null)
        {
            string[] strArr = drawerNo.Split('-');
            boxModel.BoxName = Convert.ToInt32(strArr[0]);
            boxModel.BoxNo = Convert.ToInt32(strArr[1]);
        }
        var result = await drugInfoDao.GetAllDrugAndStockByBox(boxModel);
        _forecasts = result;
    }
    // void OnCurrentDateChanged(DateTime args)
    // {
    //     OrderDate = new DateTime(args.Year, args.Month, args.Day);
    // }
    //重置
    async Task reloadGrid()
    {
        OrderNo = "";
        OrderDate = DateTime.MinValue;
        await grid.Reload();
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        isLoading = true;
        BoxModel boxModel = new BoxModel();

        boxModel.BoxName = 1;
        boxModel.BoxNo = 1;
        isLoading = true;
        if (drawerNo != null)
        {
            string[] strArr = drawerNo.Split('-');
            boxModel.BoxName = Convert.ToInt32(strArr[0]);
            boxModel.BoxNo = Convert.ToInt32(strArr[1]);
        }
        var result = await drugInfoDao.GetAllDrugAndStockByBox(boxModel);// await channelListDao.GetBoxDrugInfo(drawerNo, args.Top, args.Skip);
        // Update the Data property
        _forecasts = result;
        // Update the count
        //count = result.TotalDesserts;

        isLoading = false;

        channelStockList = await orderInfoDao.GetDrawerNum(setting.Value.boxMachineId);

        isLoading = false;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        if (firstRender)
        {
            await grid.ExpandRows(grid.PagedView.Where(di => di.Stocks.Count > 0));
        }
    }

    void RowRender(RowRenderEventArgs<DrugInfo> args)
    {
        args.Expandable = args.Data.Stocks.Count > 0;
    }
    //确认
    async Task Confirm()
    {
        // orderInfoDao.CheckOrderInfo(selectedOrderInfos, drawerNo);
    }

    async Task LoadData(LoadDataArgs args)
    {
        BoxModel boxModel = new BoxModel();

        boxModel.BoxName = 1;
        boxModel.BoxNo = 1;
        isLoading = true;
        if (drawerNo != null)
        {
            string[] strArr = drawerNo.Split(',');
            boxModel.BoxName = Convert.ToInt32(strArr[0]);
            boxModel.BoxNo = Convert.ToInt32(strArr[1]);
        }
        var result = await drugInfoDao.GetAllDrugAndStockByBox(boxModel);// await channelListDao.GetBoxDrugInfo(drawerNo, args.Top, args.Skip);
        // Update the Data property
        _forecasts = result;
        // Update the count
        //count = result.TotalDesserts;

        isLoading = false;

        channelStockList = await orderInfoDao.GetDrawerNum(setting.Value.boxMachineId);
    }
    //替换按钮
    async Task replaceClick(ChannelStock cs)
    {
        if (cs.Quantity > 0)
        {
            var b = await dialogService.OpenAsync<BoxReplace>(
                   $"{cs.DrawerNo}号药盒替换药品详情",
                 new Dictionary<string, object>() { { "channelStock", cs } },
                 new DialogOptions() { Width = "85vw", Resizable = true, Draggable = true, ShowClose = false }
           );
            if (b)
            {
                await reloadGrid();
            }
        }
        else
        {
            //库存为0无法操作
            _message.Notify(
            new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"库存为0无法替换", Duration = 4000 }
        );
        }
    }
    //移除按钮
    async Task removeClick(ChannelStock cs)
    {
        if (cs.Quantity > 0)
        {
            var b = await dialogService.OpenAsync<BoxRemoveDialog>(
                   $"{cs.DrawerNo}号药盒替换药品详情",
                 new Dictionary<string, object>() { { "channelStock", cs } },
                 new DialogOptions() { Width = "85vw", Resizable = true, Draggable = true, ShowClose = false }
           );
            if (b)
            {
                await reloadGrid();
            }
        }
        else
        {
            //库存为0无法操作
            _message.Notify(
            new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"库存为0，没有要移出的药品", Duration = 4000 }
        );
        }
    }
}