@page "/suppliers/new" @page "/suppliers/{Id:int}" @inject SupplierService SupplierService @inject NavigationManager Navigation @inject CutList.Web.Data.ApplicationDbContext DbContext @using CutList.Core.Formatting @using Microsoft.EntityFrameworkCore @(IsNew ? "Add Supplier" : "Edit Supplier")

@(IsNew ? "Add Supplier" : supplier.Name)

@if (loading) {

Loading...

} else {
Supplier Details
@if (!string.IsNullOrEmpty(supplierErrorMessage)) {
@supplierErrorMessage
}
@(IsNew ? "Cancel" : "Back to List")
@if (!IsNew) {
Supplier Offerings
@if (showOfferingForm) {
@(editingOffering == null ? "Add Offering" : "Edit Offering")
@if (!string.IsNullOrEmpty(offeringErrorMessage)) {
@offeringErrorMessage
}
} @if (offerings.Count == 0) {

No offerings configured yet.

} else { @foreach (var offering in offerings) { }
Stock Item Part # Price Actions
@offering.StockItem.Material.DisplayName - @ArchUnits.FormatFromInches((double)offering.StockItem.LengthInches) @(offering.PartNumber ?? "-") @(offering.Price.HasValue ? offering.Price.Value.ToString("C") : "-")
}
}
} @code { [Parameter] public int? Id { get; set; } private Supplier supplier = new(); private List offerings = new(); private List stockItems = new(); private bool loading = true; private bool savingSupplier; private bool savingOffering; private string? supplierErrorMessage; private string? offeringErrorMessage; private bool showOfferingForm; private SupplierOffering newOffering = new(); private SupplierOffering? editingOffering; private ConfirmDialog deleteOfferingDialog = null!; private SupplierOffering? offeringToDelete; private string deleteOfferingMessage = ""; private bool IsNew => !Id.HasValue; protected override async Task OnInitializedAsync() { stockItems = await DbContext.StockItems .Include(si => si.Material) .Where(si => si.IsActive) .OrderBy(si => si.Material.Shape) .ThenBy(si => si.Material.Size) .ThenBy(si => si.LengthInches) .ToListAsync(); if (Id.HasValue) { var existing = await SupplierService.GetByIdAsync(Id.Value); if (existing == null) { Navigation.NavigateTo("suppliers"); return; } supplier = existing; offerings = await SupplierService.GetOfferingsForSupplierAsync(Id.Value); } loading = false; } private async Task SaveSupplierAsync() { supplierErrorMessage = null; savingSupplier = true; try { if (string.IsNullOrWhiteSpace(supplier.Name)) { supplierErrorMessage = "Name is required"; return; } if (IsNew) { var created = await SupplierService.CreateAsync(supplier); Navigation.NavigateTo($"suppliers/{created.Id}"); } else { await SupplierService.UpdateAsync(supplier); } } finally { savingSupplier = false; } } private void ShowAddOfferingForm() { editingOffering = null; newOffering = new SupplierOffering { SupplierId = Id!.Value }; showOfferingForm = true; offeringErrorMessage = null; } private void EditOffering(SupplierOffering offering) { editingOffering = offering; newOffering = new SupplierOffering { Id = offering.Id, SupplierId = offering.SupplierId, StockItemId = offering.StockItemId, PartNumber = offering.PartNumber, SupplierDescription = offering.SupplierDescription, Price = offering.Price, Notes = offering.Notes }; showOfferingForm = true; offeringErrorMessage = null; } private void CancelOfferingForm() { showOfferingForm = false; editingOffering = null; offeringErrorMessage = null; } private async Task SaveOfferingAsync() { offeringErrorMessage = null; savingOffering = true; try { if (newOffering.StockItemId == 0) { offeringErrorMessage = "Please select a stock item"; return; } var exists = await SupplierService.OfferingExistsAsync( newOffering.SupplierId, newOffering.StockItemId, editingOffering?.Id); if (exists) { offeringErrorMessage = "This supplier already has an offering for this stock item"; return; } if (editingOffering == null) { await SupplierService.AddOfferingAsync(newOffering); } else { await SupplierService.UpdateOfferingAsync(newOffering); } offerings = await SupplierService.GetOfferingsForSupplierAsync(Id!.Value); showOfferingForm = false; editingOffering = null; } finally { savingOffering = false; } } private void ConfirmDeleteOffering(SupplierOffering offering) { offeringToDelete = offering; deleteOfferingMessage = $"Are you sure you want to delete the offering for {offering.StockItem.Material.DisplayName} - {ArchUnits.FormatFromInches((double)offering.StockItem.LengthInches)}?"; deleteOfferingDialog.Show(); } private async Task DeleteOfferingConfirmed() { if (offeringToDelete != null) { await SupplierService.DeleteOfferingAsync(offeringToDelete.Id); offerings = await SupplierService.GetOfferingsForSupplierAsync(Id!.Value); } } }