import SwiftUI

struct FeedbackFormView: View {
    @State private var type = "功能建议"
    @State private var content = ""
    @State private var submitted = false
    @State private var isSubmitting = false
    let types = ["Bug 反馈", "功能建议", "内容问题", "其他"]

    var body: some View {
        ZStack {
            Color.zxBg0.ignoresSafeArea()
            ScrollView {
                VStack(spacing: 16) {
                    VStack(alignment: .leading, spacing: 8) {
                        Text("反馈类型").font(.system(size: 12, weight: .semibold)).foregroundColor(Color.zxF035)
                        HStack(spacing: 8) { ForEach(types, id: \.self) { t in let sel = type == t; Button { type = t } label: { Text(t).font(.system(size: 12)).foregroundColor(sel ? .white : Color.zxF05).padding(.horizontal, 12).padding(.vertical, 6).background(sel ? AnyView(ZXGradient.brandPurple) : AnyView(Color.zxFill005)).clipShape(Capsule()) } } }
                    }
                    VStack(alignment: .leading, spacing: 8) {
                        Text("内容").font(.system(size: 12, weight: .semibold)).foregroundColor(Color.zxF035)
                        TextEditor(text: $content).frame(minHeight: 150).scrollContentBackground(.hidden).padding(12).background(Color.zxFill004).clipShape(RoundedRectangle(cornerRadius: 14)).overlay(RoundedRectangle(cornerRadius: 14).stroke(Color.zxBorder008, lineWidth: 1))
                    }
                    Button {
                        Task {
                            isSubmitting = true
                            _ = try? await FeedbackService.shared.submit(category: type, content: content)
                            submitted = true
                            isSubmitting = false
                        }
                    } label: {
                        HStack(spacing: 8) {
                            if isSubmitting { ProgressView().tint(.white) }
                            Text(submitted ? "已提交" : "提交").font(.system(size: 14, weight: .bold))
                        }
                        .foregroundColor(.white).frame(maxWidth: .infinity).frame(height: 52)
                        .background(isSubmitting ? AnyView(Color.gray) : AnyView(ZXGradient.ctaPurple))
                        .clipShape(RoundedRectangle(cornerRadius: 16))
                    }
                    .disabled(isSubmitting)
                }.padding(.horizontal, 20).padding(.top, 8).padding(.bottom, 80)
            }.scrollIndicators(.hidden)
        }.navigationBarTitleDisplayMode(.inline).animatedTabBarHide().toolbarBackground(.hidden, for: .navigationBar)
    }
}
