//
//  Accomplishments.swift
//  Masterplan
//
//  Created by Harin Wu on 2022-03-23.
//

import SwiftUI

struct Accomplishments: View {
    @Environment(\.dismiss) private var dismiss
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Work.completedTime, ascending: false)],
        predicate: NSPredicate(
            format: "completed == true"
        ),
        animation: .default)
    private var items: FetchedResults<Work>
    
    var body: some View {
        ZStack {
            VStack {
                HStack {
                    Button(action: {
                        dismiss()
                    }, label: {
                        BackButton()
                            .padding([.leading, .trailing], 20)
                    })
                    Text("Your accomplishments...")
                        .fontWeight(.bold)
                        .font(.system(size: 22.0))
                        .foregroundColor(Color.white)
                    Spacer()
                }
                ScrollView(showsIndicators: false) {
                    ForEach(items, id: \.self) { work in
                        HStack() {
                            Spacer()
                            NavigationLink {
                                WorkDetail(keyWork: work)
                            } label: {
                                WorkItemView(workObject: work)
                            }
                            Spacer()
                        }
                    }
                }
                .listStyle(.plain)
                .padding([.leading, .trailing], 20)
                .onAppear(perform: {
                    UITableView.appearance().contentInset.bottom = 80
                    PersistenceController.shared.save()
                })
            }
        }
        .background(appPrimaryColor)
        .navigationBarHidden(true)
    }
}

struct Accomplishments_Previews: PreviewProvider {
    static var previews: some View {
        Accomplishments()
    }
}
