import SwiftUI struct AddTransactionView: View { @Binding var isPresented: Bool @EnvironmentObject var authManager: AuthManager let categories: [FinanceCategory] let onAdded: () async -> Void @State private var amount = "" @State private var description = "" @State private var type = "expense" @State private var selectedCategoryId: Int? = nil @State private var isLoading = false var filteredCategories: [FinanceCategory] { categories.filter { $0.type == type } } var body: some View { NavigationView { ZStack { Color(hex: "0a0a1a").ignoresSafeArea() VStack(spacing: 16) { Picker("", selection: $type) { Text("Расход").tag("expense") Text("Доход").tag("income") } .pickerStyle(.segmented) TextField("Сумма", text: $amount) .keyboardType(.decimalPad) .padding().background(Color.white.opacity(0.08)).cornerRadius(12).foregroundColor(.white) TextField("Описание", text: $description) .padding().background(Color.white.opacity(0.08)).cornerRadius(12).foregroundColor(.white) if !filteredCategories.isEmpty { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 8) { ForEach(filteredCategories) { cat in Button(action: { selectedCategoryId = cat.id }) { HStack(spacing: 4) { Text(cat.icon ?? "").font(.caption) Text(cat.name).font(.caption) } .padding(.horizontal, 12).padding(.vertical, 6) .background(RoundedRectangle(cornerRadius: 20) .fill(selectedCategoryId == cat.id ? Color(hex: "00d4aa").opacity(0.3) : Color.white.opacity(0.08))) .foregroundColor(selectedCategoryId == cat.id ? Color(hex: "00d4aa") : .white) } } }.padding(.horizontal) } } Spacer() }.padding() } .navigationTitle("Новая операция") .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Отмена") { isPresented = false } } ToolbarItem(placement: .confirmationAction) { Button("Добавить") { guard let a = Double(amount.replacingOccurrences(of: ",", with: ".")) else { return } isLoading = true Task { let req = CreateTransactionRequest( amount: a, categoryId: selectedCategoryId, description: description.isEmpty ? nil : description, type: type ) try? await APIService.shared.createTransaction(token: authManager.token, request: req) await onAdded() await MainActor.run { isPresented = false } } } .disabled(amount.isEmpty || isLoading) } } .preferredColorScheme(.dark) } } }