import SwiftUI struct EditTaskView: View { @Binding var isPresented: Bool @EnvironmentObject var authManager: AuthManager let task: PulseTask let onSaved: () async -> Void @State private var title: String @State private var description: String @State private var priority: Int @State private var selectedIcon: String @State private var selectedColor: String @State private var hasDueDate: Bool @State private var dueDate: Date @State private var isRecurring: Bool @State private var recurrenceType: String @State private var recurrenceInterval: String @State private var hasRecurrenceEnd: Bool @State private var recurrenceEndDate: Date @State private var isLoading = false let recurrenceTypes: [(String, String)] = [ ("daily", "Ежедневно"), ("weekly", "Еженедельно"), ("monthly", "Ежемесячно"), ("custom", "Каждые N дней") ] let priorities: [(Int, String, String)] = [ (1, "Низкий", "8888aa"), (2, "Средний", "ffa502"), (3, "Высокий", "ff4757") ] let icons = ["✅","📌","🎯","💼","🏠","🛒","📞","🎓","💊","🚗", "📅","⚡","🔧","📬","💡","🏋️","🌿","🎵","✍️","🌏"] let colors = ["#0D9488","#7c3aed","#ff4757","#ffa502","#6366f1", "#ec4899","#14b8a6","#f59e0b","#10b981","#3b82f6"] init(isPresented: Binding, task: PulseTask, onSaved: @escaping () async -> Void) { self._isPresented = isPresented self.task = task self.onSaved = onSaved self._title = State(initialValue: task.title) self._description = State(initialValue: task.description ?? "") self._priority = State(initialValue: task.priority ?? 2) self._selectedIcon = State(initialValue: task.icon ?? "✅") self._selectedColor = State(initialValue: task.color ?? "#0D9488") if let dueDateStr = task.dueDate, let parsed = Self.parseDate(dueDateStr) { self._hasDueDate = State(initialValue: true) self._dueDate = State(initialValue: parsed) } else { self._hasDueDate = State(initialValue: false) self._dueDate = State(initialValue: Date()) } self._isRecurring = State(initialValue: task.isRecurring ?? false) self._recurrenceType = State(initialValue: task.recurrenceType ?? "daily") self._recurrenceInterval = State(initialValue: String(task.recurrenceInterval ?? 1)) if let endStr = task.recurrenceEndDate, let parsed = Self.parseDate(endStr) { self._hasRecurrenceEnd = State(initialValue: true) self._recurrenceEndDate = State(initialValue: parsed) } else { self._hasRecurrenceEnd = State(initialValue: false) self._recurrenceEndDate = State(initialValue: Date().addingTimeInterval(86400 * 30)) } } static func parseDate(_ str: String) -> Date? { let df = DateFormatter() df.dateFormat = "yyyy-MM-dd" return df.date(from: String(str.prefix(10))) } var body: some View { ZStack { Color(hex: "06060f").ignoresSafeArea() VStack(spacing: 0) { RoundedRectangle(cornerRadius: 3) .fill(Color.white.opacity(0.2)).frame(width: 40, height: 4).padding(.top, 12) HStack { Button("Отмена") { isPresented = false } .font(.callout).foregroundColor(Color(hex: "8888aa")) Spacer() Text("Редактировать").font(.headline).foregroundColor(.white) Spacer() Button(action: save) { if isLoading { ProgressView().tint(Theme.teal).scaleEffect(0.8) } else { Text("Готово").font(.callout.bold()).foregroundColor(title.isEmpty ? Color(hex: "8888aa") : Theme.teal) } }.disabled(title.isEmpty || isLoading) } .padding(.horizontal, 16).padding(.vertical, 14) Divider().background(Color.white.opacity(0.1)) ScrollView { VStack(spacing: 16) { VStack(alignment: .leading, spacing: 8) { Label("Название", systemImage: "pencil").font(.caption).foregroundColor(Color(hex: "8888aa")) TextField("Что нужно сделать?", text: $title, axis: .vertical) .lineLimit(1...3).foregroundColor(.white).padding(14) .background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07))) } VStack(alignment: .leading, spacing: 8) { Label("Описание", systemImage: "text.alignleft").font(.caption).foregroundColor(Color(hex: "8888aa")) TextField("Детали...", text: $description, axis: .vertical) .lineLimit(2...4).foregroundColor(.white).padding(14) .background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07))) } VStack(alignment: .leading, spacing: 8) { Label("Приоритет", systemImage: "flag.fill").font(.caption).foregroundColor(Color(hex: "8888aa")) HStack(spacing: 8) { ForEach(priorities, id: \.0) { p in Button(action: { priority = p.0 }) { Text(p.1).font(.caption.bold()) .foregroundColor(priority == p.0 ? .black : Color(hex: p.2)) .padding(.horizontal, 12).padding(.vertical, 8) .background(RoundedRectangle(cornerRadius: 20).fill(priority == p.0 ? Color(hex: p.2) : Color(hex: p.2).opacity(0.15))) } } } } VStack(alignment: .leading, spacing: 8) { HStack { Label("Срок выполнения", systemImage: "calendar").font(.caption).foregroundColor(Color(hex: "8888aa")) Spacer() Toggle("", isOn: $hasDueDate).tint(Color(hex: "0D9488")).labelsHidden() } if hasDueDate { DatePicker("", selection: $dueDate, in: Date()..., displayedComponents: .date) .datePickerStyle(.compact) .colorInvert() .colorMultiply(Color(hex: "0D9488")) } } VStack(alignment: .leading, spacing: 8) { Label("Иконка", systemImage: "face.smiling").font(.caption).foregroundColor(Color(hex: "8888aa")) LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 5), spacing: 8) { ForEach(icons, id: \.self) { icon in Button(action: { selectedIcon = icon }) { Text(icon).font(.title3) .frame(width: 44, height: 44) .background(Circle().fill(selectedIcon == icon ? Color(hex: "0D9488").opacity(0.25) : Color.white.opacity(0.05))) .overlay(Circle().stroke(selectedIcon == icon ? Color(hex: "0D9488") : Color.clear, lineWidth: 2)) } } } } VStack(alignment: .leading, spacing: 8) { Label("Цвет", systemImage: "paintpalette").font(.caption).foregroundColor(Color(hex: "8888aa")) LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 5), spacing: 10) { ForEach(colors, id: \.self) { c in Button(action: { selectedColor = c }) { Circle().fill(Color(hex: String(c.dropFirst()))).frame(width: 32, height: 32) .overlay(Circle().stroke(.white, lineWidth: selectedColor == c ? 2 : 0)) .scaleEffect(selectedColor == c ? 1.15 : 1.0) } } } } // Recurrence VStack(alignment: .leading, spacing: 10) { HStack { Label("Повторение", systemImage: "repeat").font(.caption).foregroundColor(Color(hex: "8888aa")) Spacer() Toggle("", isOn: $isRecurring).tint(Color(hex: "0D9488")).labelsHidden() } if isRecurring { VStack(spacing: 8) { ForEach(recurrenceTypes, id: \.0) { rt in Button(action: { recurrenceType = rt.0 }) { HStack { Text(rt.1).foregroundColor(recurrenceType == rt.0 ? .white : Color(hex: "8888aa")) Spacer() if recurrenceType == rt.0 { Image(systemName: "checkmark").foregroundColor(Color(hex: "0D9488")) } } .padding(12) .background(RoundedRectangle(cornerRadius: 10).fill(recurrenceType == rt.0 ? Color(hex: "0D9488").opacity(0.15) : Color.white.opacity(0.05))) } } if recurrenceType == "custom" { HStack { Text("Каждые").foregroundColor(Color(hex: "8888aa")).font(.callout) TextField("1", text: $recurrenceInterval).keyboardType(.numberPad) .foregroundColor(.white).frame(width: 50).padding(8) .background(RoundedRectangle(cornerRadius: 8).fill(Color.white.opacity(0.07))) Text("дней").foregroundColor(Color(hex: "8888aa")).font(.callout) } } HStack { Label("Дата окончания", systemImage: "calendar.badge.minus").font(.caption).foregroundColor(Color(hex: "8888aa")) Spacer() Toggle("", isOn: $hasRecurrenceEnd).tint(Color(hex: "0D9488")).labelsHidden() } if hasRecurrenceEnd { DatePicker("", selection: $recurrenceEndDate, in: Date()..., displayedComponents: .date) .labelsHidden() .colorInvert() .colorMultiply(Color(hex: "0D9488")) } } } } }.padding(20) } } } } func save() { isLoading = true let df = DateFormatter(); df.dateFormat = "yyyy-MM-dd" let dueDateStr = hasDueDate ? df.string(from: dueDate) : nil let recEndStr = (isRecurring && hasRecurrenceEnd) ? df.string(from: recurrenceEndDate) : nil let interval = recurrenceType == "custom" ? (Int(recurrenceInterval) ?? 1) : nil Task { let req = UpdateTaskRequest( title: title, description: description.isEmpty ? nil : description, priority: priority, dueDate: dueDateStr, completed: nil, icon: selectedIcon, color: selectedColor, isRecurring: isRecurring ? true : nil, recurrenceType: isRecurring ? recurrenceType : nil, recurrenceInterval: interval, recurrenceEndDate: recEndStr ) try? await APIService.shared.updateTask(token: authManager.token, id: task.id, request: req) await onSaved() await MainActor.run { isPresented = false } } } }