@page "/stock/binding"


<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="Drug" />
                            </RadzenColumn>
                            <RadzenColumn Size="8">
                                <RadzenDropDownDataGrid AllowVirtualization="true" Name="Drug" TValue="DrugInfo" @bind-Value="drugInfo" Data="@drugInfos"
                                                        Style="width:100%; display: block;" AllowFilteringByAllStringColumns="true" TextProperty="DrugName">
                                    <Columns>
                                        <RadzenDropDownDataGridColumn Property="DrugId" Title="药品编码" />
                                        <RadzenDropDownDataGridColumn Property="DrugName" Width="120px" Title="名称" />
                                        <RadzenDropDownDataGridColumn Property="DrugSpec" Title="规格" />
                                    </Columns>
                                </RadzenDropDownDataGrid>
                            </RadzenColumn>
                        </RadzenRow>
                        <RadzenRow AlignItems="AlignItems.Center">
                            <RadzenColumn Size="4">
                                <RadzenLabel Text="批次" Component="DropDownBindValue" />
                            </RadzenColumn>
                            <RadzenColumn Size="8">
                                <RadzenDropDownDataGrid @bind-Value=@drugManuNo Data=@drugInfo?.Manus Disabled="@(drugInfo == null)" Name="DropDownBindValue" TextProperty="ManuNo">
                                    <Columns>
                                        <RadzenDropDownDataGridColumn Property="ManuNo" Title="批次" />
                                        <RadzenDropDownDataGridColumn Property="EffDate" Title="效期" />
                                    </Columns>
                                </RadzenDropDownDataGrid>
                            </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" />
                                <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">

            <RadzenDataList @ref="grid" LoadData="@LoadData" WrapItems="true" Count="@count" IsLoading="@isLoading" Data="@_forecasts" PageSize="6" AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left"
            ShowPagingSummary="true" PagingSummaryFormat="{0}/{1} 共{2}条数据">
                <Template Context="channel">
                    <RadzenCard class="col-3">
                        <RadzenRow>
                            <RadzenColumn Size="8" Class="rz-text-truncate">
                                <b class="rz-pr-3">@(channel.DrawerNo) - @(channel.ColNo)</b>
                                @if(channel.DrawerType == 1)
                                {
                                    switch(channel.BoardType)
                                    {
                                        case 1:
                                            <b>物理隔板</b>
                                            break;
                                        case 2:
                                            <b>单支计数</b>
                                            break;
                                        case 3:
                                            <b>管控药盒</b>
                                            break;
                                        case 4:
                                            <b>储物箱</b>
                                            break;
                                        case 5:
                                            <b>智能显示</b>
                                            break;
                                        case 6:
                                            <b>称重计数</b>
                                            break;
                                        case 63:
                                            <b>称重计数药盒</b>
                                            break;
                                        case 653:
                                            <b>称重计数药盒智能显示</b>
                                            break;
                                    }
                                }
                                else if (channel.DrawerType == 2)
                                {
                                    switch (channel.BoardType)
                                    {
                                        case 1:
                                            <b>内置回收</b>
                                            break;
                                        case 2:
                                            <b>内置计数回收</b>
                                            break;
                                        case 3:
                                            <b>内置药盒回收</b>
                                            break;
                                        case 4:
                                            <b>储物回收箱</b>
                                            break;
                                        case 5:
                                            <b>内置智能显示回收</b>
                                            break;
                                        case 6:
                                            <b>内置称重计数回收</b>
                                            break;
                                        case 63:
                                            <b>内置称重计数药盒回收</b>
                                            break;
                                        case 653:
                                            <b>内置称重计数药盒智能显示回收</b>
                                            break;
                                    }
                                }
                                else
                                {
                                    switch (channel.BoardType)
                                    {
                                        case 1:
                                            <b>外置回收</b>
                                            break;
                                    }
                                }

                            </RadzenColumn>
                            <RadzenColumn Size="4" Class="rz-text-align-end">
                                @if(channel.Quantity == 0)
                                {
                                    <RadzenButton ButtonStyle="ButtonStyle.Secondary" Variant="Variant.Outlined" Size="ButtonSize.Small" Click="@(() => EditChannel(channel))" Text="绑定/解绑" />
                                }
                            </RadzenColumn>
                        </RadzenRow>
                        <hr style="border: none; background-color: rgba(0,0,0,.2); height: 1px; margin: 1rem 0;" />
                        <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center">
                            <RadzenStack Gap="0">
                                <RadzenText TextStyle="TextStyle.Overline" class="rz-display-flex rz-mt-2 rz-my-0">药品</RadzenText>
                                @if (channel.DrugId == null || channel.Quantity == 0)
                                {
                                    <RadzenDropDownDataGrid Change="@((args) => { channel.drugManuNo = null; })" AllowVirtualization="true" Name="@("Drug_" + channel.Id)" TValue="DrugInfo" @bind-Value="channel.Drug" Data="@drugInfos"
                                    Style="width:260px; display: block;" AllowFilteringByAllStringColumns="true" TextProperty="DrugName">
                                        <Columns>
                                            <RadzenDropDownDataGridColumn Property="DrugId" Title="药品编码" Sortable="false" />
                                            <RadzenDropDownDataGridColumn Property="DrugName"  Title="名称" Sortable="false" />
                                            <RadzenDropDownDataGridColumn Property="DrugSpec" Title="规格" Sortable="false" />
                                        </Columns>
                                    </RadzenDropDownDataGrid>
                                } else
                                {
                                    <RadzenText TextStyle="TextStyle.Body1"><b>@(channel.Drug?.DrugName)</b></RadzenText>
                                    <RadzenText TextStyle="TextStyle.Body1"><b>@(channel.Drug?.DrugSpec)</b></RadzenText>
                                }
                                @if(channel.DrawerType == 1)
                                {
                                    <RadzenText TextStyle="TextStyle.Overline" class="rz-display-flex rz-mt-4 rz-mb-0">批次</RadzenText>
                                    @if (channel.DrugId == null || channel.Quantity == 0)
                                    {
                                        <RadzenDropDownDataGrid @bind-Value=@channel.drugManuNo Data=@channel.Drug?.Manus Disabled="@(channel.Drug == null)" TValue="DrugManuNo" Name="@("Drugmanu_" + channel.Id)" TextProperty="ManuNo">
                                            <Columns>
                                                <RadzenDropDownDataGridColumn Property="ManuNo" Title="批次" Sortable="false" />
                                                <RadzenDropDownDataGridColumn Property="EffDate" Title="效期" Sortable="false">
                                                    <Template Context="data">
                                                        @data.EffDate.ToString("yyyy-MM-dd")
                                                    </Template>
                                                    </RadzenDropDownDataGridColumn>
                                            </Columns>
                                        </RadzenDropDownDataGrid>
                                    }
                                    else
                                    {
                                        <RadzenText TextStyle="TextStyle.Body1"><b>@(channel.ManuNo)</b></RadzenText>
                                        <RadzenText TextStyle="TextStyle.Body1"><b>@(channel.EffDate)</b></RadzenText>
                                    }
                                }

                                <RadzenText TextStyle="TextStyle.Overline" class="rz-display-flex rz-mt-4 rz-mb-0">库存</RadzenText>
                                <RadzenText TextStyle="TextStyle.Body1"><b>@(channel.Quantity)</b></RadzenText>
                            </RadzenStack>
                        </RadzenStack>
                    </RadzenCard>
                </Template>
            </RadzenDataList>
        </div>
    </div>
</div>

@code {
    @inject IChannelListDao channelListDao;
    @inject IDrugInfoDao drugInfoDao;
    @inject DialogService dialogService;
    @inject NotificationService _message
    RadzenDataList<ChannelStock> grid;
    bool isLoading;
    int count;
    private IEnumerable<ChannelStock>? _forecasts;

    List<DrugInfo> drugInfos;
    List<DrugManuNo> drugManuNos;

    DrugInfo drugInfo;

    DrugManuNo drugManuNo;



    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync(); 
        //drugInfos = await drugInfoDao.GetAllDrug();
    }

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

        // var result = await channelListDao.GetAllChannelList(0, "", args.Top, args.Skip);
        var result = await channelListDao.GetAllChannelListWithDrug(0, "", args.Top, args.Skip);

        // Update the Data property
        _forecasts = result.Desserts;
        // Update the count
        count = result.TotalDesserts;

        isLoading = false;

        drugInfos = result.Other;
    }

    async Task reloadGrid()
    {
        await grid.Reload();
    }

    async Task EditChannel(ChannelStock stock)
    {
        if(stock.drugManuNo != null)
        {
            // 说明之前有绑定并且未改变，那么就是在进行解绑
            if(stock.drugManuNo.ManuNo.Equals(stock.ManuNo))
            {
                var b = await channelListDao.UnBind(stock.Id);
                if(b)
                {
                    _message.Notify(
                        new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"解除绑定成功", Duration = 4000 }
                    );
                    await reloadGrid();
                }

            }
            // 否则是进行绑定
            else
            {
                var b = await channelListDao.Bind(stock);
                if (b)
                {
                    _message.Notify(
                        new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"绑定成功", Duration = 4000 }
                    );
                    await reloadGrid();
                }
            }
        } else
        {
            if (stock.Drug != null)
            {
                // 说明只设置了药品
                var b = await channelListDao.Bind(stock);
                if (b)
                {
                    _message.Notify(
                        new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"绑定成功", Duration = 4000 }
                    );
                    await reloadGrid();
                }
            }
            // 什么都没有选择或者么有改变，只是点击了按钮，此时不操作
            else
            {
                
            }
        }
    }





}
