import SwiftUI import LichtkastenCore /// Agentur-Check: shown before an upload when something is wrong or photos were sent before. /// Errors (red) are excluded by "Nur fehlerfreie senden", warnings (amber) don't block. struct PreflightWindow: View { @EnvironmentObject var state: AppState @Environment(\.dismissWindow) private var dismissWindow @State private var skipSent = true @State private var showOK = false var body: some View { Group { if let p = state.pendingUpload { content(p) } else { VStack(spacing: 12) { Image(systemName: "checkmark.shield").font(.system(size: 40)).foregroundStyle(LK.green) Text("Kein Upload wartet auf eine Prüfung.").foregroundStyle(LK.text2) Button("Schließen") { dismissWindow(id: "check") }.buttonStyle(LKButtonStyle()) } .frame(maxWidth: .infinity, maxHeight: .infinity) } } .onChange(of: state.pendingUpload?.id, initial: true) { _, _ in skipSent = state.pendingUpload?.skipAlreadySent ?? true showOK = false } .frame(minWidth: 760, minHeight: 520) .background(LK.ground) } // MARK: Content private func content(_ p: PendingUpload) -> some View { let problem = p.items.filter { !(p.issues[$0.id] ?? []).isEmpty || p.alreadySent.contains($0.id) } let ok = p.items.filter { (p.issues[$0.id] ?? []).isEmpty && !p.alreadySent.contains($0.id) } let sendable = toSend(p, errorFree: true) let all = toSend(p, errorFree: false) return VStack(spacing: 0) { // header HStack(spacing: 14) { Image(systemName: p.errorItems > 0 ? "exclamationmark.shield.fill" : "checkmark.shield.fill") .font(.system(size: 22)) .foregroundStyle(p.errorItems > 0 ? LK.red : LK.amber) .frame(width: 44, height: 44) .background(RoundedRectangle(cornerRadius: 11).fill(LK.raised)) VStack(alignment: .leading, spacing: 3) { Text("Agentur-Check · \(p.targetName)").font(.system(size: 18, weight: .bold)).foregroundStyle(LK.text) HStack(spacing: 12) { chip("\(p.items.count) Bild(er)", LK.text2) if p.errorItems > 0 { chip("\(p.errorItems) mit Fehlern", LK.red) } if p.warningItems > 0 { chip("\(p.warningItems) mit Hinweisen", LK.amber) } if !p.alreadySent.isEmpty { chip("\(p.alreadySent.count) schon gesendet", LK.green) } } } Spacer() Button("Erneut prüfen") { state.recheckPendingUpload() } .buttonStyle(LKButtonStyle(compact: true)) .help("Nach Korrekturen im IPTC-Fenster neu prüfen") } .padding(20) Rectangle().fill(LK.border).frame(height: 1) ScrollView { VStack(alignment: .leading, spacing: 10) { if !p.alreadySent.isEmpty { Toggle(isOn: $skipSent) { Text("Bereits an \(p.targetName) gesendete Bilder überspringen (\(p.alreadySent.count))") .font(.system(size: 13, weight: .semibold)) } .toggleStyle(.checkbox) .padding(.bottom, 4) } ForEach(problem) { item in row(item, p) } if !ok.isEmpty { DisclosureGroup(isExpanded: $showOK) { VStack(alignment: .leading, spacing: 6) { ForEach(ok) { item in Text(item.filename).font(LK.mono(12)).foregroundStyle(LK.text2) } } .padding(.top, 6) } label: { Label("\(ok.count) Bild(er) ohne Befund", systemImage: "checkmark.circle.fill") .font(.system(size: 13, weight: .semibold)) .foregroundStyle(LK.green) } .padding(12) .background(RoundedRectangle(cornerRadius: 10).fill(LK.card)) } } .padding(20) } Rectangle().fill(LK.border).frame(height: 1) HStack(spacing: 10) { Button("Abbrechen") { state.discardPendingUpload() if state.pendingUpload == nil { dismissWindow(id: "check") } } .buttonStyle(LKButtonStyle()) .keyboardShortcut(.cancelAction) Spacer() if all.count != sendable.count { Button("Alle senden (\(all.count))") { send(all) } .buttonStyle(LKButtonStyle()) .disabled(all.isEmpty) .help("Auch Bilder mit Fehlern senden") } Button(p.errorItems > 0 ? "Nur fehlerfreie senden (\(sendable.count))" : "Senden (\(sendable.count))") { send(sendable) } .buttonStyle(LKButtonStyle(primary: true)) .keyboardShortcut(.defaultAction) .disabled(sendable.isEmpty) } .padding(16) } } private func toSend(_ p: PendingUpload, errorFree: Bool) -> [PhotoItem] { p.items.filter { item in if skipSent && p.alreadySent.contains(item.id) { return false } if errorFree && p.hasError(item.id) { return false } return true } } private func send(_ items: [PhotoItem]) { state.confirmPendingUpload(items) if state.pendingUpload == nil { state.openWindow("tasks") dismissWindow(id: "check") } } // MARK: Row per photo private func row(_ item: PhotoItem, _ p: PendingUpload) -> some View { let issues = p.issues[item.id] ?? [] return HStack(alignment: .top, spacing: 12) { PreflightThumb(url: state.previewURL(for: item)) VStack(alignment: .leading, spacing: 5) { HStack { Text(item.filename).font(LK.mono(12, .semibold)).foregroundStyle(LK.text) if p.alreadySent.contains(item.id), let s = state.sentSummary(for: item) { Label(s.tooltip, systemImage: "paperplane.fill") .font(.system(size: 11)).foregroundStyle(LK.green).lineLimit(1) } } ForEach(issues) { i in HStack(alignment: .top, spacing: 6) { Image(systemName: i.severity == .error ? "xmark.octagon.fill" : "exclamationmark.triangle.fill") .font(.system(size: 11)) .foregroundStyle(i.severity == .error ? LK.red : LK.amber) Text(i.message).font(.system(size: 12)).foregroundStyle(LK.text) } } } Spacer() Button("Bearbeiten") { state.cursorID = item.id state.selection = [item.id] state.openWindow("iptc") } .buttonStyle(LKButtonStyle(compact: true)) .help("Im IPTC-Fenster öffnen – danach „Erneut prüfen“") } .padding(12) .background(RoundedRectangle(cornerRadius: 10).fill(LK.card)) .overlay(RoundedRectangle(cornerRadius: 10).stroke(p.hasError(item.id) ? LK.red.opacity(0.5) : LK.border)) } private func chip(_ text: String, _ color: Color) -> some View { Text(text).font(.system(size: 11, weight: .bold)).foregroundStyle(color) } } struct PreflightThumb: View { let url: URL @State private var image: NSImage? var body: some View { ZStack { RoundedRectangle(cornerRadius: 6).fill(LK.deep) if let image { Image(nsImage: image).resizable().scaledToFill() } } .frame(width: 84, height: 56) .clipShape(RoundedRectangle(cornerRadius: 6)) .task(id: url) { image = ImageLoader.shared.cachedThumbnail(url) if image == nil { image = await ImageLoader.shared.thumbnail(for: url) } } } }