//
//  CloseDatePicker.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 08/07/2024.
//

import SwiftUI
import LeStorage

struct CloseDatePicker: View {
    @State var closedRegistrationDate: Date
    @Environment(Tournament.self) var tournament: Tournament
    @EnvironmentObject var dataStore: DataStore

    var body: some View {
        DatePicker(selection: $closedRegistrationDate) {
            Text("Date de clôture").font(.caption)
            Text(closedRegistrationDate.localizedWeekDay().capitalized)
        }
        .onChange(of: closedRegistrationDate) {
            tournament.closedRegistrationDate = closedRegistrationDate
            do {
                try dataStore.tournaments.addOrUpdate(instance: tournament)
            } catch {
                Logger.error(error)
            }
        }
    }
}
