@page "/Box/BoxStock"
@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.ToString() + "-" + cs.ColNo.ToString()}")" Disabled="@(status > 0)" Shade="Shade.Light" Variant="@(drawerNo != (cs.DrawerNo.ToString() + "-" + cs.ColNo.ToString()) ? Variant.Outlined : Variant.Flat)" />

            }
        </div>
    </div>
    <div class="col-10">
        <RadzenDataList @ref="grid" LoadData="@LoadData" WrapItems="true" Count="@count" IsLoading="@isLoading" style="height:600px;overflow:auto"
                        Data="@_forecasts" PageSize="6" AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left"
                        ShowPagingSummary="true" PagingSummaryFormat="{0}/{1} 共{2}条数据">
            <Template Context="pd">
                <RadzenCard Variant="Variant.Outlined" class="rz-p-0" Style="width: 100%;">
                    <RadzenRow Gap="0">
                        <RadzenColumn Size="2">
                            <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-color-on-secondary-lighter">@(pd._DrugInfo.DrugName)</RadzenText>
                        </RadzenColumn>
                        <RadzenColumn Size="5">
                            <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-color-on-secondary-lighter">@(pd._DrugInfo.Manufactory)</RadzenText>
                        </RadzenColumn>
                        <RadzenColumn Size="3">
                            <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-color-on-secondary-lighter">@(pd._DrugInfo.DrugSpec)</RadzenText>
                        </RadzenColumn>
                        <RadzenColumn Size="1">
                            <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-color-on-secondary-lighter">@(pd.BaseQuantity)</RadzenText>
                        </RadzenColumn>
                        <RadzenColumn Size="1">
                            <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-color-on-secondary-lighter">@(pd.channelStocks.Sum(cs => cs.Quantity))</RadzenText>
                        </RadzenColumn>

                    </RadzenRow>
                    @*  <RadzenColumn Size="12" SizeLG="2" class="rz-p-4">
                            <RadzenButton Text="替换药品" Style="width: 100%" Click="@(() => replaceClick(pd))" />
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeLG="2" class="rz-p-4">
                            <RadzenButton Text="移出药品" Style="width: 100%" Click="@(()=>removeClick(pd))" />
                        </RadzenColumn> *@
                    @if (pd.channelStocks != null && pd.channelStocks.Count > 0)
                    {
                        for (int i = 0; i < pd.channelStocks.Count; i++)
                        {
                            var manuInfo = pd.channelStocks[i];
                            var manuNo = 1;
                            if (i == 0)
                            {
                                manuNo = 0;
                            }
                            <RadzenRow Gap="0">
                                <RadzenColumn Size="12">
                                    @if (manuInfo != null && manuInfo.ManuNo != null)
                                    {
                                        <RadzenRow Gap="0">
                                            <RadzenColumn Size="3">
                                                @if (manuNo == 0)
                                                {
                                                    <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-mb-0">批次</RadzenText>
                                                }
                                                <RadzenText TextStyle="TextStyle.Body2">@(manuInfo.ManuNo)</RadzenText>
                                            </RadzenColumn>
                                            <RadzenColumn Size="3">
                                                @if (manuNo == 0)
                                                {
                                                    <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-mb-0">效期</RadzenText>
                                                }
                                                <RadzenText TextStyle="TextStyle.Body2">@(manuInfo.EffDate)</RadzenText>
                                            </RadzenColumn>
                                            <RadzenColumn Size="1">
                                                @if (manuNo == 0)
                                                {
                                                    <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-mb-0">库存</RadzenText>
                                                }
                                                <RadzenText TextStyle="TextStyle.Body2">@(manuInfo.Quantity)</RadzenText>
                                            </RadzenColumn>
                                            <RadzenColumn Size="2">
                                                <RadzenText style="margin-top:5px;"></RadzenText>
                                                <RadzenButton Text="替换药品" Style="margin:1px" Click="@(() => replaceClick(manuInfo))" />
                                            </RadzenColumn>
                                            <RadzenColumn Size="2">
                                                <RadzenText style="margin-top:5px;"></RadzenText>
                                                <RadzenButton Text="移出药品" Click="@(() => removeClick(manuInfo))" />
                                            </RadzenColumn>

                                        </RadzenRow>
                                    }
                                    else
                                    {
                                        <RadzenRow Gap="0">
                                            <RadzenColumn Size="5">
                                                <RadzenText TextStyle="TextStyle.H6" TagName="TagName.H5" class="rz-mb-0">未绑定批次</RadzenText>
                                            </RadzenColumn>
                                        </RadzenRow>
                                    }
                                </RadzenColumn>
                            </RadzenRow>
                        }
                    }
                </RadzenCard>
            </Template>
        </RadzenDataList>

    </div>
</RadzenStack>
@code {
    @inject IOrderInfoDao orderInfoDao;
    @inject IChannelListDao channelListDao;
    @inject DialogService dialogService;
    @inject Microsoft.Extensions.Options.IOptions<Pojo.Config.SettingConfig> setting;
    @inject NotificationService _message

    private IEnumerable<PlanDetails>? _forecasts;
    bool allowRowSelectOnRowClick = true;
    // IEnumerable<DrugManuNo> drugManuNos;
    // IList<DrugManuNo> selectedDrugManuNos;
    RadzenDataList<PlanDetails> 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;
    void SelectDrawer(string strDrawerNoColNo)
    {
        this.drawerNo = strDrawerNoColNo;
        grid.Reload();
    }
    // void OnCurrentDateChanged(DateTime args)
    // {
    //     OrderDate = new DateTime(args.Year, args.Month, args.Day);
    // }
    //重置
    async Task reloadGrid()
    {
        OrderNo = "";
        OrderDate = DateTime.MinValue;
        await grid.Reload();
    }
    //确认
    async Task Confirm()
    {
        // orderInfoDao.CheckOrderInfo(selectedOrderInfos, drawerNo);
    }

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

        var result = await channelListDao.GetBoxDrugInfo(drawerNo, args.Top, args.Skip);
        // Update the Data property
        _forecasts = result.Desserts;
        // 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 }
        );
        }
    }
}