protected async Task Delete_Lineitem(LineItemEditModel item)
 {
     LineItemImageModels.Remove(LineItemImageModels.Where(x => x.Id == LineItemEditModels.IndexOf(item)).FirstOrDefault());
     LineItemEditModels.Remove(item);
     ClaimEditModel.TotalAmount = LineItemEditModels.Select(x => x.USDAmount).Sum(x => x);
     //NavigationManager.NavigateTo("/list", true);
 }
 protected async Task AddList_Click()
 {
     LineItemEditModel            = new LineItemEditModel();
     LineItemEditModel.Date       = DateTime.Now;
     LineItemEditModel.CurrencyId = 7;//USD
     LineItemEditModels.Add(LineItemEditModel);
 }
        protected async Task EventAmt(int i, ChangeEventArgs e)
        {
            var value = 0;

            if (Int32.TryParse(e.Value.ToString(), out int auxn))
            {
                value = auxn;
            }
            LineItemEditModels[i].USDAmount = value;
            ClaimEditModel.TotalAmount      = LineItemEditModels.Select(x => x.USDAmount).Sum(x => x);
            //StateHasChanged();
        }
        protected async Task HandleValidSubmit()
        {
            Mapper.Map(ClaimEditModel, Claim);
            Claim.RequesterComments = RequesterComments;
            Claim.ApproverComments  = ApproverComments;
            Claim.FinanceComments   = FinanceComments;
            Claim.SubmitDate        = DateTime.Now;
            Claim.Requester         = Name; //Id(email)
            Claim.Status            = Status.Requested;
            if (Claim.Id != 0)              //Edit-Submit
            {
                await ClaimService.UpdateClaim(Claim);

                StatusClass = "alert-success";
                Message     = "Employee updated successfully.";
                Saved       = true;
                if (LineItemEditModels.Count() > 0)
                {
                    foreach (var Item in LineItemEditModels)
                    {
                        Mapper.Map(Item, LineItem);
                        LineItem.ClaimId = Claim.Id;
                        await LineItemService.UpdateLineItem(LineItem);
                    }
                }
                NavigationManager.NavigateTo("/list", true);
            }
            else//Create-Submit
            {
                var result = await ClaimService.CreateClaim(Claim);

                if (result.Id != 0)
                {
                    LineItem ResultItem = null;
                    foreach (var Item in LineItemEditModels)
                    {
                        Mapper.Map(Item, LineItem);
                        LineItem.ClaimId = result.Id;
                        ResultItem       = await LineItemService.CreateLineItem(LineItem);
                    }
                    NavigationManager.NavigateTo($"/detail/{result.Id}", true);
                }
                else
                {
                    StatusClass = "alert-danger";
                    Message     = "Something went wrong Creating the new employee. Please try again.";
                    Saved       = false;
                }
            }
        }
        protected async Task OnInputFileChange(InputFileChangeEventArgs e, LineItemEditModel Liem)
        {
            var maxAllowedFiles = 1;
            var format          = "image/png";

            if (e.GetMultipleFiles(maxAllowedFiles).Count > maxAllowedFiles)
            {
                Message = "max Allowed Files are 5"; return;
            }
            foreach (var imageFile in e.GetMultipleFiles(maxAllowedFiles))
            {
                //var resizedImageFile = await imageFile.RequestImageFileAsync(format, 100, 100);
                //var buffer = new byte[resizedImageFile.Size];
                //await resizedImageFile.OpenReadStream().ReadAsync(buffer);

                var buffer = new byte[imageFile.Size];
                await imageFile.OpenReadStream().ReadAsync(buffer);

                var imageDataUrl = $"data:{format};base64,{Convert.ToBase64String(buffer)}";
                if (imageDataUrl != null)
                {
                    imageDataUrls.Clear();
                    imageDataUrls.Add(imageDataUrl);
                }

                buffer = new byte[imageFile.Size];
                await imageFile.OpenReadStream().ReadAsync(buffer);


                LineItemEditModels[LineItemEditModels.IndexOf(Liem)].Receipt = buffer;
                LineItemImageModel Lim = new LineItemImageModel {
                    Id = Liem.Id, ImageDataUrls = imageDataUrls.ToList()
                };
                if (Claim.Id != 0)// for Edit
                {
                    LineItemImageModels[LineItemImageModels.IndexOf(LineItemImageModels.Where(x => x.Id == Liem.Id).FirstOrDefault())] = Lim;
                }
                else
                {
                    LineItemImageModels.Add(Lim);
                }

                //var image = resizedImageFile.OptimizeImageSize(700, 700);
            }
        }
        protected ImageConverter _imageConverter;// = new ImageConverter();

        protected override async Task OnInitializedAsync()
        {
            var authenticationState     = await((CustomAuthenticationStateProvider)AuthenticationStateProvider).GetAuthenticationStateAsync();
            var AuthenticationStateUser = authenticationState.User;

            Name = AuthenticationStateUser.Claims.Where(x => x.Type.Equals("email")).FirstOrDefault().Value;
            if (Name == null)
            {
                Name = (await authenticationStateTask).User.Claims.Where(x => x.Type.Equals("email")).FirstOrDefault().Value;
            }
            if (!authenticationState.User.Identity.IsAuthenticated)
            {
                string returnUrl = WebUtility.UrlEncode($"/list");
                NavigationManager.NavigateTo($"/login?returnUrl={returnUrl}");
            }

            Categories = (await CategoryService.GetCategories()).ToList();
            Currencies = (await CurrencyService.GetCurrencies()).ToList();

            //Id = Id ?? "1";
            int.TryParse(Id, out int claimId);
            if (claimId != 0)// for Edit
            {
                Claim = (await ClaimService.GetClaimById(int.Parse(Id)));
                Mapper.Map(Claim, ClaimEditModel);

                if (ClaimEditModel.RequesterComments != null)
                {
                    RequesterComments = ClaimEditModel.RequesterComments;
                }
                if (ClaimEditModel.ApproverComments != null)
                {
                    ApproverComments = ClaimEditModel.ApproverComments;
                }
                if (ClaimEditModel.FinanceComments != null)
                {
                    FinanceComments = ClaimEditModel.FinanceComments;
                }

                if (Claim.LineItems.Count() > 0)
                {
                    foreach (var LineItem in Claim.LineItems)
                    {
                        LineItemEditModel = new LineItemEditModel();
                        Mapper.Map(LineItem, LineItemEditModel);
                        LineItemEditModels.Add(LineItemEditModel);
                        if (LineItem.Receipt != null)
                        {
                            imageDataUrls.Clear();
                            var format       = "image/png";
                            var imageDataUrl = $"data:{format};base64,{Convert.ToBase64String(LineItem.Receipt)}";
                            imageDataUrls.Add(imageDataUrl);

                            LineItemImageModel Lim = new LineItemImageModel {
                                Id = LineItem.Id, ImageDataUrls = imageDataUrls.ToList()
                            };
                            LineItemImageModels.Add(Lim);
                        }
                    }
                }
            }
            else // for Create
            {
                ClaimEditModel = new ClaimEditModel
                {
                    Requester  = Name,
                    SubmitDate = DateTime.Now,
                    Status     = (Status)Enum.Parse(typeof(Status), "Requested")
                };
                if (LineItemEditModels.Count() > 0)
                {
                    foreach (var lineItemEditModel in LineItemEditModels)
                    {
                        if (lineItemEditModel.Receipt != null)
                        {
                            imageDataUrls.Clear();
                            var format       = "image/png";
                            var imageDataUrl = $"data:{format};base64,{Convert.ToBase64String(lineItemEditModel.Receipt)}";
                            imageDataUrls.Add(imageDataUrl);
                            LineItemImageModel Lim = new LineItemImageModel {
                                Id = lineItemEditModel.Id, ImageDataUrls = imageDataUrls.ToList()
                            };
                            LineItemImageModels.Add(Lim);
                        }
                    }
                }
            }
        }