@page "/Box/Plan"
    
        
            
                
                    
                        
                            
                            
                        
                    
                
            
        
        
            
                
                    
                        
                            
                            
                        
                    
                    
                        
                            
                            
                        
                    
                    
                        
                            
                            
                            
                            
                        
                        
                            
                            
                            
                            
                            
                            
                        
                    
                
            
        
        
            
                @* 
                        
                            
                        
                     *@
                
                    
                        
                            @(SelectedPlan.FirstOrDefault()?._PlanDetails.IndexOf(data) + 1)
                        
                    
                    
                        
                            @(data._DrugInfo?.DrugName)
                            @(data._DrugInfo?.DrugSpec)
                        
                        
                            
                                
                                    
                                    
                                    
                                
                            
                            
                            
                        
                    
                    
                        
                            
                            
                        
                    
                    
                        
                            
                            
                            
                            
                        
                        
                            
                            
                            
                            
                            
                            
                        
                    
                
            
            
                
            
         
     
 
@code {
    @inject IPlanDao planDao;
    @inject DialogService dialogService;
    @inject NotificationService _message
    @inject GlobalStateService globalStateService;
    @inject Toolbelt.Blazor.I18nText.I18nText I18nText;
    private I18nText.local myText = new I18nText.local();
    RadzenDataGrid grid;
    RadzenDataGrid PlanDetailsGrid;
    bool isLoading;
    int count;
    private IEnumerable? _forecasts;
    IList? SelectedPlan { get; set; } = new List();
    List SelectedPlanDetails = new List();
    List drugInfos;
    // int preValue = 1;
    protected override async Task OnInitializedAsync()
    {
        myText = await I18nText.GetTextTableAsync(this);
        base.OnInitializedAsync();
    }
    async Task LoadData(LoadDataArgs args)
    {
        isLoading = true;
        var result = await planDao.GetAllPlanInfo();
        // Update the Data property
        _forecasts = result.Desserts;
        count = result.TotalDesserts;
        SelectedPlan = new List() { _forecasts.FirstOrDefault() };
        isLoading = false;
        drugInfos = result.Other;
    }
    // async Task LoadDetailDate(LoadDataArgs args)
    // {
    //     // SelectedPlan = new List() { await planDao.GetPlanById(SelectedPlan.FirstOrDefault().Id) };
    // }
    async Task reloadGrid()
    {
        await grid.Reload();
    }
    void OnUpdatePlanRow(Plan dl)
    {
        // Reset(dl);
        // 数据库更新
        planDao.UpdatePlanInfo(dl);
        grid.Reload();
    }
    void OnCreatePlanRow(Plan dl)
    {
        // 数据库添加套餐
        planDao.InsertPlanInfo(dl);
        SelectedPlan.Remove(dl);
        grid.Reload();
    }
    //新增套餐
    async Task InsertPlanRow()
    {
        var planInfo = new Plan();
        if (_forecasts.Count() <= 0)
        {
            _forecasts = new List
            {
                new Plan()
            };
            await grid.InsertRow(_forecasts.FirstOrDefault());
        }
        else
        {
            await grid.InsertRow(planInfo);
        }
    }
    //修改套餐
    async Task EditPlanRow(Plan planInfo)
    {
        await grid.EditRow(planInfo);
    }
    //删除套餐
    async Task DeletePlanRow(Plan planInfo)
    {
        if (planInfo.Id > 0)
        {
            //弹出确认提示框
            var b = await dialogService.OpenAsync(
               $"删除确认",
             new Dictionary() { { "confirmInfo", "确认要删除套餐:" + planInfo.Name + "吗?" } },
             new DialogOptions() { Width = "45vw", Resizable = true, Draggable = true, ShowClose = false });
            if (b)
            {
                //查询要删除的套餐下是否有绑定,且绑定有库存
                if (!(await planDao.CheckPlanBind(planInfo.Id)))
                {
                    _message.Notify
                  (
                      new NotificationMessage
                      {
                          Severity = NotificationSeverity.Error,
                          Summary = "提示",
                          Detail = $"该套餐还有库存绑定且有库存,请先清库存",
                          Duration = 4000
                      }
                  );
                    return;
                }
                // 数据库删除
                if (await planDao.DeletePlanInfo(planInfo.Id))
                {
                    _message.Notify
                    (
                        new NotificationMessage
                        {
                            Severity = NotificationSeverity.Success,
                            Summary = "提示",
                            Detail = $"删除成功",
                            Duration = 4000
                        }
                    );
                }
                else
                {
                    _message.Notify
                    (
                        new NotificationMessage
                        {
                            Severity = NotificationSeverity.Error,
                            Summary = "提示",
                            Detail = "删除失败",
                            Duration = 4000
                        }
                    );
                }
            }
            await grid.Reload();
        }
        else
        {
            grid.CancelEditRow(planInfo);
            await grid.Reload();
        }
    }
    //保存套餐信息
    async Task SavePlanRow(Plan planInfo)
    {
        await grid.UpdateRow(planInfo);
    }
    async Task CancelPlanEdit(Plan planInfo)
    {
        grid.CancelEditRow(planInfo);
        await grid.Reload();
        await PlanDetailsGrid.Reload();
    }
    void Reset(PlanDetails pd)
    {
        SelectedPlanDetails.Remove(pd);
    }
    //修改药品
    async Task EditRow(PlanDetails planDetail)
    {
        // planDetail.DrugId = null;
        await PlanDetailsGrid.EditRow(planDetail);
    }
    //保存修改药品
    async Task InsertRow(PlanDetails planDetail)
    {
        await PlanDetailsGrid.UpdateRow(planDetail);
    }
    //取消修改药品
    async Task CancelEdit(PlanDetails planDetail)
    {
        PlanDetailsGrid.CancelEditRow(planDetail);
        SelectedPlan.FirstOrDefault()._PlanDetails.RemoveAll(pd => pd.Id == 0);
        await PlanDetailsGrid.Reload();
    }
    //删除药品
    async Task DeleteRow(PlanDetails planDetail)
    {
        // Reset(planDetail);
        if (planDetail.Id > 0)
        {
            //弹出确认提示框
            var b = await dialogService.OpenAsync(
               $"确认删除",
             new Dictionary() { { "confirmInfo", "确认要删除药品:" + planDetail._DrugInfo.DrugName + "吗?" } },
             new DialogOptions() { Width = "45vw", Resizable = true, Draggable = true, ShowClose = false }
           );
            if (b)
            {
                //查询要删除的药品是否有绑定且库存大于0
                if (!(await planDao.CheckPlanDetailBind(planDetail)))
                {
                    _message.Notify(new NotificationMessage
                    {
                        Severity = NotificationSeverity.Error,
                        Summary = "提示",
                        Detail = "该药品有绑定且库存不为0,请先清库存",
                        Duration = 4000
                    });
                    return;
                }
                // 数据库删除
                if (await planDao.DeletePlanDetail(planDetail))
                {
                    _message.Notify(new NotificationMessage
                    {
                        Severity = NotificationSeverity.Success,
                        Summary = "提示",
                        Detail = "删除成功",
                        Duration = 4000
                    }
                    );
                    SelectedPlan.FirstOrDefault()._PlanDetails.Remove(planDetail);
                }
                else
                {
                    _message.Notify(new NotificationMessage
                    {
                        Severity = NotificationSeverity.Error,
                        Summary = "提示",
                        Detail = "删除失败",
                        Duration = 4000
                    }
                    );
                }
                await PlanDetailsGrid.Reload();
            }
        }
        else
        {
            PlanDetailsGrid.CancelEditRow(planDetail);
        }
        SelectedPlan.FirstOrDefault()._PlanDetails.RemoveAll(pd => pd.Id == 0);
    }
    async void OnUpdateRow(PlanDetails planDetail)
    {
        // Reset(planDetail);
        SelectedPlanDetails.Add(planDetail);
        // 数据库更新
        if (await planDao.UpdatePlanDetail(planDetail))
        {
            _message.Notify(new NotificationMessage
            {
                Severity = NotificationSeverity.Success,
                Summary = "提示",
                Detail = "修改成功",
                Duration = 4000
            }
               );
        }
        else
        {
            _message.Notify(new NotificationMessage
            {
                Severity = NotificationSeverity.Error,
                Summary = "提示",
                Detail = "修改失败",
                Duration = 4000
            }
               );
        }
        await PlanDetailsGrid.Reload();
    }
    //保存套餐下添加的药品
    async void OnCreateRow(PlanDetails planDetail)
    {
        if (SelectedPlan.FirstOrDefault() != null)
        {
            planDetail.PlanId = SelectedPlan.FirstOrDefault().Id;
            planDetail.DrugId = planDetail.DrugId == null ? planDetail._DrugInfo.DrugId : planDetail.DrugId;
            planDetail.UseState = 1;
            planDetail.OperatorUser = globalStateService.Operator?.Username;
            planDetail.ReviewerUser = globalStateService.Reviewer?.Username;
            if (await planDao.AddPlanDetail(planDetail))
            {
                _message.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Success,
                    Summary = "提示",
                    Detail = "保存成功",
                    Duration = 4000
                }
                );
            }
            else
            {
                _message.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = "提示",
                    Detail = "保存失败",
                    Duration = 4000
                }
                );
            }
        }
        // await grid.Reload();
        await PlanDetailsGrid.Reload();
    }
    //新增药品
    async Task InsertRow()
    {
        var detail = new Pojo.PlanDetails()
        {
            PlanId = SelectedPlan.FirstOrDefault().Id
        };
        SelectedPlan.FirstOrDefault()._PlanDetails.Add(detail);
        await PlanDetailsGrid.InsertRow(detail);
    }
    //验证药品是否已存在套餐中
    bool ValidatroDrug(PlanDetails planDetail)
    {
        if (planDetail.PlanId == 0)
        {
            //新增的
            planDetail.PlanId = SelectedPlan.FirstOrDefault().Id;
        }
        return planDao.CheckDrugById(planDetail);
    }
}