using LiteCharms.Features.Hasher; using LiteCharms.Features.MidrandBooks.Customers; using LiteCharms.Features.MidrandBooks.Customers.Models; using LiteCharms.Features.MidrandBooks.Orders; using LiteCharms.Features.MidrandBooks.Payments; using LiteCharms.Features.MidrandBooks.Products; namespace MidrandBookshop.Components.Pages; public partial class Account : ComponentBase { [Inject] private AuthenticationStateProvider AuthStateProvider { get; set; } = default!; [Inject] private CustomerService CustomerService { get; set; } = default!; [Inject] private OrderService OrderService { get; set; } = default!; [Inject] private PaymentService PaymentService { get; set; } = default!; [Inject] private ProductService ProductService { get; set; } = default!; [Inject] private HashService HashService { get; set; } = default!; [Inject] private CancellationToken CancellationToken { get; set; } = default!; [Inject] private IToastService ToasterService { get; set; } = default!; private ClaimsPrincipal? User { get; set; } private Customer? customer; private bool showAddForm = false; private AddressItem? editingAddress = null; private string newAddressName = ""; private string newStreetAddress = ""; private string newCity = ""; private string newPostalCode = ""; private bool isBilling, isShipping; private List orderHistory = []; private List savedAddresses = new() { new AddressItem { Id = 1, Name = "Home Address", Street = "12 Main Road", City = "Midrand", PostalCode = "1685", IsBilling = true, IsShipping = true, IsPrimary = true }, new AddressItem { Id = 2, Name = "Midrand Warehouse", Street = "Corner of Church & Third Roads", City = "Midrand", PostalCode = "1685", IsBilling = false, IsShipping = false, IsPrimary = false } }; protected override async Task OnInitializedAsync() { var authState = await AuthStateProvider.GetAuthenticationStateAsync(); User = authState?.User; var customerFetch = await CustomerService.GetCustomerAsync(User?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)!.Value!, CancellationToken); if (customerFetch.IsSuccess) customer = customerFetch.Value; await LoadOrdersAsync(); } private async Task LoadOrdersAsync() { if (customer is null) { ToasterService.ShowError("There was a problem loading your details, please contact the administrator"); return; } var ordersFetch = await OrderService.GetOrdersByCustomerAsync(customer.Id, CancellationToken); if (ordersFetch.IsFailed) { ToasterService.ShowWarning("No orders were found"); return; } orderHistory.Clear(); foreach (var order in ordersFetch.Value) { var paymentFetch = await PaymentService.GetOrderPaymentAsync(order.Id, CancellationToken); var orderEntry = new OrderItem { OrderDate = order.CreatedAt, OrderId = HashService.HashEncodeLongId(order.Id).Value, Status = order.Status.ToString(), PaymentStatus = paymentFetch.IsSuccess ? paymentFetch.Value.Status.ToString() : "NotPaid", ShippingStatus = "Processing", ShippingAddressName = "TBA", Total = order.Total, InvoiceUrl = order.InvoiceUrl!, }; var orderItemsFetch = await OrderService.GetOrderItemsAsync(order.Id, CancellationToken); if (orderItemsFetch.IsFailed) continue; foreach (var item in orderItemsFetch.Value) { var productPriceFetch = await ProductService.GetProductPriceAsync(item.ProductPriceId, CancellationToken); if (productPriceFetch.IsFailed) continue; var productFetch = await ProductService.GetProductAsync(productPriceFetch.Value.ProductId, CancellationToken); var itemEntry = new PurchasedBook { Quantity = item.Quantity, PriceUnitPrice = productPriceFetch.Value.Amount, CoverImageUrl = productFetch.Value.ImageUrl!, Title = productFetch.Value.Name!, }; orderEntry.PurchasedBooks.Add(itemEntry); } orderHistory.Add(orderEntry); } } private void DownloadInvoice(string orderId) { var order = orderHistory.FirstOrDefault(o => o.OrderId == orderId)!; if (string.IsNullOrWhiteSpace(order.InvoiceUrl)) ToasterService.ShowWarning("Your invoice is currently not availabe for viewing"); else Navigation.NavigateTo(orderHistory.FirstOrDefault(o => o.OrderId == orderId)!.InvoiceUrl, forceLoad: true); } // Badge Style 1: Core Order Lifecycle Status Mapping private string GetOrderStatusClass(string? status) { return status?.ToLower() switch { "completed" => "order-completed", "processing" => "order-processing", "cancelled" => "order-cancelled", _ => "order-hold" }; } // Badge Style 2: Financial Payment Status Mapping private string GetPaymentStatusClass(string? status) { return status?.ToLower() switch { "paid" => "pay-paid", "refunded" => "pay-refunded", _ => "pay-pending" }; } // Badge Style 3: Logistics Shipment Status Mapping private string GetShippingStatusClass(string? status) { return status?.ToLower() switch { "delivered" => "status-delivered", "shipped" => "status-shipped", _ => "status-processing" }; } // Implemented to resolve UI registration click events private void SaveAddress() { if (string.IsNullOrWhiteSpace(newAddressName) || string.IsNullOrWhiteSpace(newStreetAddress)) { ToasterService.ShowWarning("Please fill in the required fields."); return; } if (editingAddress != null) { editingAddress.Name = newAddressName; editingAddress.Street = newStreetAddress; editingAddress.City = newCity; editingAddress.PostalCode = newPostalCode; editingAddress.IsBilling = isBilling; editingAddress.IsShipping = isShipping; } else { var nextId = savedAddresses.Any() ? savedAddresses.Max(a => a.Id) + 1 : 1; savedAddresses.Add(new AddressItem { Id = nextId, Name = newAddressName, Street = newStreetAddress, City = newCity, PostalCode = newPostalCode, IsBilling = isBilling, IsShipping = isShipping, IsPrimary = !savedAddresses.Any() }); } CancelAddressActions(); } private void CancelAddressActions() { showAddForm = false; editingAddress = null; ResetFormFields(); } private void ResetFormFields() { newAddressName = ""; newStreetAddress = ""; newCity = ""; newPostalCode = ""; isBilling = false; isShipping = false; } private void EditAddress(AddressItem addr) { editingAddress = addr; newAddressName = addr.Name; newStreetAddress = addr.Street; newCity = addr.City; newPostalCode = addr.PostalCode; isBilling = addr.IsBilling; isShipping = addr.IsShipping; showAddForm = false; } private void DeleteAddress(AddressItem addr) { if (editingAddress?.Id == addr.Id) editingAddress = null; savedAddresses.Remove(addr); if (addr.IsPrimary && savedAddresses.Any()) savedAddresses.First().IsPrimary = true; } private void SetPrimary(AddressItem target, ChangeEventArgs e) { var isChecked = (bool)(e.Value ?? false); if (isChecked) { foreach (var addr in savedAddresses) addr.IsPrimary = (addr.Id == target.Id); } else target.IsPrimary = false; } private void TriggerLogout() { Navigation.NavigateTo("/logout", forceLoad: true); } public class AddressItem { public int Id { get; set; } public string Name { get; set; } = ""; public string Street { get; set; } = ""; public string City { get; set; } = ""; public string PostalCode { get; set; } = ""; public bool IsBilling { get; set; } public bool IsShipping { get; set; } public bool IsPrimary { get; set; } } public class OrderItem { public string OrderId { get; set; } = ""; public DateTime OrderDate { get; set; } public string ShippingAddressName { get; set; } = ""; public string Status { get; set; } = ""; public string PaymentStatus { get; set; } = ""; public string ShippingStatus { get; set; } = ""; public string InvoiceUrl { get; set; } = ""; public decimal Total { get; set; } public List PurchasedBooks { get; set; } = new(); } public class PurchasedBook { public string Title { get; set; } = ""; public string CoverImageUrl { get; set; } = ""; public int Quantity { get; set; } public decimal PriceUnitPrice { get; set; } } }