import SwiftUI import LabWiseKit struct SpreadsheetView: View { let chemicals: [Chemical] let onDismiss: () -> Void private struct ColumnDef { let label: String let width: CGFloat let value: (Chemical) -> String } private static let columnDefs: [ColumnDef] = [ ColumnDef(label: "PI First Name", width: 130) { $0.piFirstName }, ColumnDef(label: "Physical State", width: 120) { $0.physicalState }, ColumnDef(label: "Chemical Name", width: 180) { $0.chemicalName }, ColumnDef(label: "Bldg Code", width: 100) { $0.bldgCode }, ColumnDef(label: "LAB", width: 80) { $0.lab }, ColumnDef(label: "Storage Location", width: 145) { $0.storageLocation }, ColumnDef(label: "Storage Device", width: 140) { $0.storageDevice }, ColumnDef(label: "# of Containers", width: 120) { $0.numberOfContainers }, ColumnDef(label: "Amount / Container", width: 150) { $0.amountPerContainer }, ColumnDef(label: "Unit of Measure", width: 130) { $0.unitOfMeasure }, ColumnDef(label: "CAS #", width: 115) { $0.casNumber }, ColumnDef(label: "Chemical Formula", width: 140) { $0.chemicalFormula ?? "" }, ColumnDef(label: "Molecular Weight", width: 130) { $0.molecularWeight ?? "" }, ColumnDef(label: "Vendor", width: 130) { $0.vendor ?? "" }, ColumnDef(label: "Catalog #", width: 120) { $0.catalogNumber ?? "" }, ColumnDef(label: "Lot Number", width: 120) { $0.lotNumber ?? "" }, ColumnDef(label: "Concentration", width: 120) { $0.concentration ?? "" }, ColumnDef(label: "Expiration Date", width: 130) { $0.expirationDate ?? "" }, ColumnDef(label: "Contact", width: 130) { $0.contact ?? "" }, ColumnDef(label: "Barcode", width: 130) { $0.barcode ?? "" }, ColumnDef(label: "Comments", width: 180) { $0.comments ?? "" }, ColumnDef(label: "% Full", width: 80) { c in c.percentageFull.map { String(format: "%.0f%%", $0) } ?? "" }, ColumnDef(label: "Date Entered", width: 130) { c in c.createdAt.map { String($0.prefix(10)) } ?? "" }, ColumnDef(label: "Last Changed", width: 130) { c in c.updatedAt.map { String($0.prefix(10)) } ?? "" }, ] private static let headerHeight: CGFloat = 48 var body: some View { VStack(spacing: 0) { // Top bar HStack(spacing: 4) { Button { restorePortrait() onDismiss() } label: { Image(systemName: "xmark.circle.fill") .font(.title2) .foregroundStyle(.secondary) } .padding(.leading, 12) Text("Inventory Spreadsheet") .font(.headline) .padding(.leading, 4) Spacer() Text("\(chemicals.count) item\(chemicals.count == 1 ? "" : "s")") .font(.subheadline) .foregroundStyle(.secondary) .padding(.trailing, 16) } .frame(height: 50) .background(Color(.systemBackground)) Divider() // Table GeometryReader { proxy in ScrollView(.horizontal, showsIndicators: true) { VStack(alignment: .leading, spacing: 0) { // Sticky header row HStack(spacing: 0) { ForEach(Array(Self.columnDefs.enumerated()), id: \.offset) { i, col in Text(col.label) .font(.caption.weight(.semibold)) .foregroundStyle(.white) .lineLimit(2) .multilineTextAlignment(.center) .frame(width: col.width, height: Self.headerHeight) .padding(.horizontal, 4) if i < Self.columnDefs.count - 1 { Rectangle() .fill(Color.white.opacity(0.35)) .frame(width: 1, height: Self.headerHeight) } } } .background(Color.accentColor) Divider() // Scrollable data rows ScrollView(.vertical, showsIndicators: true) { LazyVStack(spacing: 0) { ForEach(Array(chemicals.enumerated()), id: \.element.id) { idx, chemical in HStack(spacing: 0) { ForEach(Array(Self.columnDefs.enumerated()), id: \.offset) { i, col in Text(col.value(chemical)) .font(.caption) .lineLimit(1) .frame(width: col.width, alignment: .leading) .padding(.vertical, 7) .padding(.horizontal, 6) .background(idx % 2 == 0 ? Color(.systemBackground) : Color(.systemGray6)) if i < Self.columnDefs.count - 1 { Rectangle() .fill(Color(.separator)) .frame(width: 0.5) .frame(height: 30) } } } Divider() } } } // Subtract header height and the 1-pt divider .frame(height: proxy.size.height - Self.headerHeight - 1) } } } } .onAppear { // Slight delay so the full-screen cover animation finishes before rotating DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { setLandscape() } } .onDisappear { // Safety net in case something else dismisses this view restorePortrait() } } // MARK: - Orientation helpers private func setLandscape() { AppDelegate.orientationLock = .landscape guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return } scene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape)) } private func restorePortrait() { AppDelegate.orientationLock = .allButUpsideDown guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return } scene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait)) } }