@page "/suppliers/new" @page "/suppliers/{Id:int}" @inject SupplierService SupplierService @inject MaterialService MaterialService @inject NavigationManager Navigation @using CutList.Core.Formatting @(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) {
Stock Lengths
@if (showStockForm) {
@(editingStock == null ? "Add Stock Length" : "Edit Stock Length")
@if (!string.IsNullOrEmpty(stockErrorMessage)) {
@stockErrorMessage
}
} @if (stocks.Count == 0) {

No stock lengths configured yet.

} else { @foreach (var stock in stocks) { }
Material Length Price Actions
@stock.Material.DisplayName @ArchUnits.FormatFromInches((double)stock.LengthInches) @(stock.Price.HasValue ? stock.Price.Value.ToString("C") : "-")
}
}
} @code { [Parameter] public int? Id { get; set; } private Supplier supplier = new(); private List stocks = new(); private List materials = new(); private bool loading = true; private bool savingSupplier; private bool savingStock; private string? supplierErrorMessage; private string? stockErrorMessage; private bool showStockForm; private SupplierStock newStock = new(); private SupplierStock? editingStock; private ConfirmDialog deleteStockDialog = null!; private SupplierStock? stockToDelete; private string deleteStockMessage = ""; private bool IsNew => !Id.HasValue; protected override async Task OnInitializedAsync() { materials = await MaterialService.GetAllAsync(); if (Id.HasValue) { var existing = await SupplierService.GetByIdAsync(Id.Value); if (existing == null) { Navigation.NavigateTo("suppliers"); return; } supplier = existing; stocks = await SupplierService.GetStocksForSupplierAsync(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 ShowAddStockForm() { editingStock = null; newStock = new SupplierStock { SupplierId = Id!.Value }; showStockForm = true; stockErrorMessage = null; } private void EditStock(SupplierStock stock) { editingStock = stock; newStock = new SupplierStock { Id = stock.Id, SupplierId = stock.SupplierId, MaterialId = stock.MaterialId, LengthInches = stock.LengthInches, Price = stock.Price, Notes = stock.Notes }; showStockForm = true; stockErrorMessage = null; } private void CancelStockForm() { showStockForm = false; editingStock = null; stockErrorMessage = null; } private async Task SaveStockAsync() { stockErrorMessage = null; savingStock = true; try { if (newStock.MaterialId == 0) { stockErrorMessage = "Please select a material"; return; } if (newStock.LengthInches <= 0) { stockErrorMessage = "Length must be greater than zero"; return; } var exists = await SupplierService.StockExistsAsync( newStock.SupplierId, newStock.MaterialId, newStock.LengthInches, editingStock?.Id); if (exists) { stockErrorMessage = "This stock length already exists for this material"; return; } if (editingStock == null) { await SupplierService.AddStockAsync(newStock); } else { await SupplierService.UpdateStockAsync(newStock); } stocks = await SupplierService.GetStocksForSupplierAsync(Id!.Value); showStockForm = false; editingStock = null; } finally { savingStock = false; } } private void ConfirmDeleteStock(SupplierStock stock) { stockToDelete = stock; deleteStockMessage = $"Are you sure you want to delete the {ArchUnits.FormatFromInches((double)stock.LengthInches)} stock length?"; deleteStockDialog.Show(); } private async Task DeleteStockConfirmed() { if (stockToDelete != null) { await SupplierService.DeleteStockAsync(stockToDelete.Id); stocks = await SupplierService.GetStocksForSupplierAsync(Id!.Value); } } }