@page "/Box/BoxReplace"
@using MasaBlazorApp3.Pojo.Config;
@using MasaBlazorApp3.Pojo.Vo;
@using MasaBlazorApp3.Util;
@using Microsoft.Extensions.Options;
@using Newtonsoft.Json;
@using log4net;
    
        
            药名:
            @(channelStock.Drug.DrugName)
            规格:
            @(channelStock.Drug.DrugSpec)
            批次:
            @(channelStock.ManuNo)
            效期:
            @(channelStock.EffDate)
            库存:
            @(channelStock.Quantity)
            交换药盒:
            
              
                    @(SelectedDrawerNo)
           
            
        
        
            
                
                
                
                
                
                
                
                 
                    
                            
                            
                    
            
            
        
    
    
        
        
    
@code {
@inject Radzen.DialogService dialogService;
    @inject IChannelListDao channelListDao;
    @inject IOptions setting;
    @inject NotificationService _message
    @inject PortUtil PortUtil;
    private readonly ILog logger = LogManager.GetLogger(typeof(OrderDetailDialog));
    int status;
    bool isLoading;
    int count;
    string[] DrawerNos;
    string SelectedDrawerNo="";
    [Parameter] public ChannelStock channelStock { get; set; }
    private bool CanTakeDrug = true;
    // public List data { get; set; }
    public List selectedDrawerData{ get; set; }
    //称重取药数量
    int[] WeightFinnalQuantity { get; set; } = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    //开抽屉前操作标识
    List DrawerNoColNoList = new List();
    protected override async Task OnInitializedAsync()
    {
        DrawerNos = await channelListDao.GetDrawerNum(channelStock);
        base.OnInitializedAsync();
    }
    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;
        if (DrawerNos != null && DrawerNos.Count() > 0)
        {
            SelectedDrawerNo = DrawerNos[0];
            var result = await channelListDao.GetChannelStockByDrug(channelStock, SelectedDrawerNo, args.Top, args.Skip);
            selectedDrawerData = result.Desserts;
        count = result.TotalDesserts;
        }
        isLoading = false;
    }
    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 (selectedDrawerData!=null&&selectedDrawerData.Any(cl => cl.AddQuantity != 0 && cl.DrugId != null && cl.ManuNo != null))
        {
            if (selectedDrawerData.Where(cs => cs.AddQuantity != 0).Sum(cs => cs.Quantity) > channelStock.Quantity)
            {
                _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Warning, Summary = "提示", Detail = $"交换总数量不能大于库存", Duration = 4000 });
                return;
            }
            var b = await channelListDao.BoxReplaceFinish(channelStock,selectedDrawerData.Where(cl => cl.AddQuantity != 0).ToList());
            if (!b)
            {
                _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "提示", Detail = $"数据保存失败", Duration = 4000 });
                logger.Error($"药盒药品替换保存数据库失败,数据{JsonConvert.SerializeObject(selectedDrawerData.Where(cl => cl.AddQuantity != 0).ToList())}");
            }
            else
            {
                _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "提示", Detail = $"交换完成", Duration = 4000 });
                // 关闭弹窗
                dialogService.Close(true);
            }
            //重置状态
            this.RestData();
        }
        else
        {
            _message.Notify(new NotificationMessage { Severity = NotificationSeverity.Warning, Summary = "提示", Detail = $"无可交换数据或未输入交换数量", Duration = 4000 });
            return;
        }
    }
    async Task SelectedDrawerChange(object drawerNo)
    {
        var result = await channelListDao.GetChannelStockByDrug(channelStock, drawerNo.ToString(), 8, 0);
        selectedDrawerData = result.Desserts;
    }
    int currentCol = 0;
    RadzenDataGrid grid;
    void OnCellClick(DataGridCellMouseEventArgs args)
    {
        currentCol = args.Data.ColNo;
        grid.EditRow(args.Data);
    }
}