@page "/Box/BoxRemove"
@using MasaBlazorApp3.Pojo.Config;
@using MasaBlazorApp3.Pojo.Vo;
@using MasaBlazorApp3.Util;
@using Microsoft.Extensions.Options;
@using Newtonsoft.Json;
@using log4net;

<RadzenStack Gap="1rem" Orientation="Orientation.Vertical" Style="height: 100%;">
    <RadzenStack>
        <RadzenStack class="rz-p-4 rz-border-radius-1" Style="border: var(--rz-grid-cell-border)" Orientation="Orientation.Horizontal" Gap="1rem">
            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">药名:</RadzenText>
            <RadzenText Class="rz-text-truncate"><b>@(channelStock.Drug.DrugName)</b></RadzenText>
            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">规格:</RadzenText>
            <RadzenText TextStyle="TextStyle.Body1" Class="rz-text-truncate"><b>@(channelStock.Drug.DrugSpec)</b></RadzenText>
            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">批次:</RadzenText>
            <RadzenText TextStyle="TextStyle.Body1" Class="rz-text-truncate"><b>@(channelStock.ManuNo)</b></RadzenText>
            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">效期:</RadzenText>
            <RadzenText TextStyle="TextStyle.Body1" Class="rz-text-truncate"><b>@(channelStock.EffDate)</b></RadzenText>
            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">库存:</RadzenText>
            <RadzenText TextStyle="TextStyle.Body1" Class="rz-text-truncate"><b>@(channelStock.Quantity)</b></RadzenText>
        </RadzenStack>
    </RadzenStack>
    <RadzenStack style="align-content:center">

        <RadzenStack class="rz-p-4 rz-border-radius-1" Style="border: var(--rz-grid-cell-border);align-content:center" Orientation="Orientation.Horizontal" Gap="1rem">
            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">移入药盒:</RadzenText>
            <RadzenDropDown @bind-Value="SelectedDrawerNo" Data="@DrawerNos" Style="display: block;" Name="DropDownChangeEvent">
                <RadzenText TextStyle="TextStyle.Subtitle2" class="mb-0">@(SelectedDrawerNo)</RadzenText>
            </RadzenDropDown>

            <RadzenText Class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">移入数量:</RadzenText>         

            <RadzenNumeric Min="0" Style="display: block" Max=@channelStock.Quantity Name="Quantity" @bind-Value="removeQuantity" />
        </RadzenStack>
    </RadzenStack>
    <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Center" Gap="0.5rem">

        <RadzenButton Click="@Save" ButtonStyle="ButtonStyle.Success" Variant="Variant.Flat" Text="完成" Style="width: 120px" />

        <RadzenButton Click="@Cancel" Variant="Variant.Flat" Text="取消" Style="width: 120px" />

    </RadzenStack>
</RadzenStack>

@code {
    @inject Radzen.DialogService dialogService;
    @inject IChannelListDao channelListDao;
    @inject IOptions<SettingConfig> setting;
    @inject NotificationService _message
    @inject PortUtil PortUtil;
    private readonly ILog logger = LogManager.GetLogger(typeof(OrderDetailDialog));

    string removeQuantity = "1-1";
    int status;

    string[] DrawerNos;
    string SelectedDrawerNo = "1-1";

    [Parameter] public ChannelStock channelStock { get; set; }
    private bool CanTakeDrug = true;

    // public List<BoxTakeVo> data { get; set; }
     

    //称重取药数量
    int[] WeightFinnalQuantity { get; set; } = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    //开抽屉前操作标识
    List<string> DrawerNoColNoList = new List<string>();

    protected override async Task OnInitializedAsync()
    {
        DrawerNos = await channelListDao.GetDrawerNumForRemove(channelStock);
        if (DrawerNos != null && DrawerNos.Count() > 0)
        {
            SelectedDrawerNo = DrawerNos[0];
            // var result = await channelListDao.GetChannelStockByDrug(channelStock, SelectedDrawerNo, 8, 0);
            // selectedDrawerData = result.Desserts;
        }
        base.OnInitializedAsync();
    }

    void RestData()
    {
        // PortUtil.speechSynthesizer.SpeakAsyncCancelAll();
        // this.status = 0;
        // data.ForEach(it =>
        // {
        //     it.Status = 0;
        //     it.BeforeQuantity = new int[9];
        //     it.AfterQuantity = new int[9];
        // });
        // this.WeightFinnalQuantity = new int[9];
        // DrawerNoColNoList.Clear();
    }
    void Cancel()
    {
        RestData();
        // 关闭弹窗
        dialogService.Close(false);
    }
    async Task Save()
    {
        if (string.IsNullOrEmpty(removeQuantity))
        {
            _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = "请输入移入数量", Duration = 4000 });
            return;
        }
        if (string.IsNullOrEmpty(SelectedDrawerNo))
        {
            _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = "无可移入的药盒", Duration = 4000 });
            return;            
        }
        var b = await channelListDao.BoxRemoveFinish(channelStock, SelectedDrawerNo,removeQuantity);
        if (!b)
        {
            _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"数据保存失败", Duration = 4000 });
            logger.Error($"药盒移出保存数据失败，数据{JsonConvert.SerializeObject(channelStock)}");
        }
        else
        {
            _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"交换完成", Duration = 4000 });
            // 关闭弹窗
            dialogService.Close(true);

        }
        //重置状态
        this.RestData();
    }
}