@page "/Box/BoxAddBoxDetail"
@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>
        <RadzenDataGrid Data="@channelStockList" AllowAlternatingRows="true" LoadData="@LoadData"
                      AllowPaging="true" PageSize="10" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true" PagingSummaryFormat="{0}/{1} 共{2}条数据">
                <Columns>
                <RadzenDataGridColumn Property="Drug.DrugName" Title="药品" />
                <RadzenDataGridColumn Property="Drug.DrugSpec" Title="规格" />
                <RadzenDataGridColumn Property="Quantity" Title="库存" />
                <RadzenDataGridColumn  Title="药品批次">
                    <Template Context="channelStockList">
                        @for (int i = 0; i < channelStockList.drugManuNoList.Count; i++)
                        {
                            DrugManuNo manu = channelStockList.drugManuNoList[i];
                            <RadzenText TextStyle="TextStyle.Subtitle2" class="rz-mb-0">@manu.ManuNo</RadzenText>
                        }
                    </Template>
                </RadzenDataGridColumn>
                <RadzenDataGridColumn Title="效期">
                    <Template Context="channelStockList">
                        @for (int i = 0; i < channelStockList.drugManuNoList.Count; i++)
                        {
                            DrugManuNo manu = channelStockList.drugManuNoList[i];
                            <RadzenText TextStyle="TextStyle.Subtitle2" class="rz-mb-0">@manu.EffDate?.ToString("yyyy-MM-dd")</RadzenText>
                        }
                    </Template>
                </RadzenDataGridColumn>
                <RadzenDataGridColumn Title="入药盒数量">
                    <Template Context="channelStockList">
                        @for (int i = 0; i < channelStockList.drugManuNoList.Count; i++)
                        {
                            DrugManuNo manu = channelStockList.drugManuNoList[i];
                            <RadzenText TextStyle="TextStyle.Subtitle2" class="rz-mb-0">@manu.Quantity</RadzenText>
                        }
                    </Template>
                </RadzenDataGridColumn>
                </Columns>
            </RadzenDataGrid>
            </RadzenStack>
    <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Center" Gap="0.5rem">
        <RadzenButton Click="@Finish" 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<DrawerConfig> setting;
    @inject NotificationService _message
    @inject PortUtil PortUtil;
    private readonly ILog logger = LogManager.GetLogger(typeof(OrderDetailDialog));

    // int status;
    bool isLoading;
    int count;
    [Parameter] public ChannelList boxChannelList { get; set; }

    List<ChannelStock> channelStockList = new List<ChannelStock>();

    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()
    {
        channelStockList = await channelListDao.GetBoxAddToBox(boxChannelList);
        base.OnInitializedAsync();
    }
    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;

        data = await channelListDao.getBoxWaitByBox(boxChannelList, args.Top, args.Skip);
        // Update the Data property
        isLoading = false;
    }
    // async Task OpenDrawer()
    // {
    //     this.status = 1;
    //     // 解析需要打开的抽屉列表
    //     List<BoxTakeVo> drawerNos = this.data.GroupBy(it => it.ChannelStock.DrawerNo).Select(it => it.First()).ToList();

    //     // 根据抽屉类型来决定打开前是否需要查询数量
    //     var promiseUtil = new PromiseUtil<int>();

    //     await promiseUtil.taskAsyncLoop(500, 0, async (options, next, stop) =>
    //     {
    //         var orderTakeVo = drawerNos[options._data];
    //         var drawerNo = orderTakeVo.ChannelStock.DrawerNo;
    //         try
    //         {
    //             if (this.status == 0)
    //             {
    //                 stop();
    //             }
    //             // 开启抽屉
    //             else if (this.status == 1)
    //             {
    //                 if (orderTakeVo.Status == 0)
    //                 {
    //                     // 判断是否为单支抽屉
    //                     if (setting.Value.single.Contains(drawerNo))
    //                     {
    //                         byte[] quantity = await PortUtil.CheckQuantityByDrawer(drawerNo);
    //                         orderTakeVo.BeforeQuantity = quantity.Select(it => Convert.ToInt32(it)).ToArray().Skip(3).Take(9).ToArray();
    //                         logger.Info($"单支抽屉，开抽屉前检测数量【{string.Join(",", orderTakeVo.BeforeQuantity)}】");

    //                         await PortUtil.HasLightOnByCol(drawerNo, data.Where(ot => ot.ChannelStock.DrawerNo == drawerNo).Select(ot => ot.ChannelStock.ColNo).ToArray());
    //                     }
    //                     for (int i = 0; i < data.Count; i++)
    //                     {
    //                         if (!DrawerNoColNoList.Contains(data[i].ChannelStock.DrawerNo.ToString() + data[i].ChannelStock.ColNo))
    //                         {
    //                             if (data[i].ChannelStock.BoardType.ToString().Contains("6"))
    //                             {
    //                                 PortUtil.DrawerNo = data[i].ChannelStock.DrawerNo;
    //                                 PortUtil.ColNoLst.Add(data[i].ChannelStock.ColNo);

    //                                 data[i].BeforeQuantity[data[i].ChannelStock.ColNo - 1] = await PortUtil.CheckQuantityForSingle(data[i].ChannelStock.ColNo);
    //                                 logger.Info($"称重抽屉，开抽屉前检测数量【{string.Join(",", orderTakeVo.BeforeQuantity)}】");
    //                                 await Task.Delay(200);
    //                             }

    //                             //是药盒抽屉开药盒
    //                             if (data[i].ChannelStock.BoardType.ToString().Contains("3"))
    //                             {
    //                                 PortUtil.DrawerNo = data[i].ChannelStock.DrawerNo;
    //                                 await PortUtil.OpenBoxByColNo(data[i].ChannelStock.ColNo);
    //                                 await Task.Delay(200);
    //                             }
    //                             DrawerNoColNoList.Add(data[i].ChannelStock.DrawerNo.ToString() + data[i].ChannelStock.ColNo);
    //                         }
    //                     }
    //                     var b = await PortUtil.OpenDrawerStatus(drawerNo);
    //                     if (b)
    //                     {
    //                         PortUtil.speechSynthesizer.SpeakAsyncCancelAll();
    //                         PortUtil.SpeakAsync($"{drawerNo}号抽屉已经打开，请，取药");
    //                         orderTakeVo.Status = 1;
    //                         next();
    //                     }
    //                     else
    //                     {
    //                         _message.Notify(
    //                         new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"抽屉【{drawerNo}】打开失败，请检测硬件", Duration = 4000 }
    //                     );
    //                         logger.Info($"抽屉打开失败");
    //                         PortUtil.AllLightOff();
    //                         RestData();
    //                         stop();
    //                     }
    //                 }
    //                 // 检测状态
    //                 else if (orderTakeVo.Status == 1)
    //                 {
    //                     // 查询抽屉是否为关闭状态
    //                     var b = await PortUtil.CheckDrawerStatus2(drawerNo);
    //                     // 关闭则改变状态并终止循环
    //                     if (b)
    //                     {

    //                         data.ForEach(cl =>
    //                         {
    //                             if (cl.ChannelStock.DrawerNo == drawerNo)
    //                             {
    //                                 cl.GetQuantity = cl.Quantity;

    //                             }
    //                         });
    //                         orderTakeVo.Status = 2;
    //                         PortUtil.AllLightOff();
    //                         if (options._data == drawerNos.Count - 1)
    //                         {
    //                             PortUtil.speechSynthesizer.SpeakAsyncCancelAll();
    //                             PortUtil.speechSynthesizer.SpeakAsyncCancelAll();
    //                             PortUtil.SpeakAsync($"取药完成，请，点击完成按钮进行确认");
    //                             this.status = 2;
    //                             string alertMessage = string.Empty;
    //                             //检查是否称重抽屉，核对实际取出数量是否与应取数量一致，不一致则弹出提示
    //                             for (int i = 0; i < data.Count; i++)
    //                             {
    //                                 if (data[i].ChannelStock.BoardType.ToString().Contains("6"))
    //                                 {
    //                                     PortUtil.DrawerNo = data[i].ChannelStock.DrawerNo;// drawerNo;
    //                                     PortUtil.ColNoLst.Add(data[i].ChannelStock.ColNo);

    //                                     orderTakeVo.AfterQuantity[data[i].ChannelStock.ColNo - 1] = await PortUtil.CheckQuantityForSingle(data[i].ChannelStock.ColNo);
    //                                     logger.Info($"称重抽屉，关抽屉后检测数量【{string.Join(",", orderTakeVo.AfterQuantity)}】");
    //                                     WeightFinnalQuantity[data[i].ChannelStock.ColNo - 1] = data[i].BeforeQuantity[data[i].ChannelStock.ColNo - 1] - orderTakeVo.AfterQuantity[data[i].ChannelStock.ColNo - 1];
    //                                     if (data[i].Quantity != WeightFinnalQuantity[data[i].ChannelStock.ColNo - 1])
    //                                     {
    //                                         //称重自动计数数量与实际要取数量不一致弹出提示，确认后保存数据
    //                                         alertMessage += $"{data[i].Drug.DrugName}应取数量【{data[i].Quantity}】，实际取出数量【{WeightFinnalQuantity[data[i].ChannelStock.ColNo - 1]}】";

    //                                     }
    //                                     await Task.Delay(200);
    //                                 }

    //                                 if (data[i].ChannelStock.BoardType.ToString().Contains("2"))
    //                                 {
    //                                     byte[] quantity = await PortUtil.CheckQuantityByDrawer(drawerNo);
    //                                     orderTakeVo.AfterQuantity = quantity.Select(it => Convert.ToInt32(it)).ToArray().Skip(3).Take(9).ToArray();
    //                                     logger.Info($"单支抽屉，关抽屉后检测数量【{string.Join(",", orderTakeVo.AfterQuantity)}】");
    //                                     WeightFinnalQuantity[data[i].ChannelStock.ColNo - 1] = orderTakeVo.BeforeQuantity[data[i].ChannelStock.ColNo - 1] - orderTakeVo.AfterQuantity[data[i].ChannelStock.ColNo - 1];
    //                                     if (data[i].Quantity != WeightFinnalQuantity[data[i].ChannelStock.ColNo - 1])
    //                                     {
    //                                         alertMessage += $"{data[i].Drug.DrugName}应取数量【{orderTakeVo.Quantity}】,实际取出数量【{WeightFinnalQuantity[data[i].ChannelStock.ColNo - 1]}】";
    //                                     }
    //                                 }
    //                             }
    //                             if (!string.IsNullOrEmpty(alertMessage))
    //                             {
    //                                 //弹出确认对话框
    //                                 alertMessage += "应取数与实际取出数不一致确认要保存吗？";
    //                                 //弹出确认提示框
    //                                 var confirm = await dialogService.OpenAsync<ConfirmDialog>(
    //                                       $"保存确认",
    //                                       new Dictionary<string, object>() { { "confirmInfo", alertMessage } },
    //                                       new DialogOptions() { Width = "45vw", Resizable = true, Draggable = true, ShowClose = false });
    //                                 logger.Info(alertMessage);
    //                                 if (!confirm)
    //                                 {
    //                                     RestData();
    //                                     logger.Info("取消保存");
    //                                     // 关闭弹窗
    //                                     dialogService.Close(false);
    //                                 }
    //                             }
    //                             stop();
    //                         }
    //                         else
    //                         {
    //                             options._data += 1;
    //                             next();
    //                         }
    //                     }
    //                     else
    //                     {
    //                         next(); // continue iteration
    //                     }
    //                 }


    //             }

    //         }
    //         catch (Exception e)
    //         {
    //             RestData();
    //             logger.Info($"{boxChannelList.DrawerNo}药盒取药发生错误，{e.Message}");
    //             _message.Notify(
    //             new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"发生错误，{e.Message}", Duration = 4000 }
    //         );
    //             if (setting.Value.single.Contains(drawerNo))
    //             {
    //                 PortUtil.AllLightOff();
    //             }
    //             stop();
    //         }
    //     });
    // }

    void Cancel()
    {
        // 关闭弹窗
        dialogService.Close(false);
    }
    async Task Finish()
    {

        // 保存账册、操作记录
        var b = await channelListDao.BoxAddBoxFinish(boxChannelList);
        if (!b)
        {
            _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"数据保存失败", Duration = 4000 });
            logger.Error($"手术室药盒取药保存数据库失败，数据{JsonConvert.SerializeObject(data)}");
            // 关闭弹窗
            dialogService.Close(false);
        }
        else
        {
            // 关闭弹窗
            dialogService.Close(true);

        }
    }


}
