//
//  PlayerPayView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 22/11/2023.
//

import SwiftUI
import LeStorage

struct PlayerPayView: View {
    @EnvironmentObject var dataStore: DataStore
    @Bindable var player: PlayerRegistration

    var body: some View {
        Picker(selection: $player.paymentType) {
            Text("Non réglé").tag(nil as PlayerRegistration.PlayerPaymentType?)
            ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in
                Text(type.localizedLabel()).tag(type as PlayerRegistration.PlayerPaymentType?)
            }
        } label: {
        }
        .pickerStyle(.menu)
        .fixedSize()
        .onChange(of: player.paymentType) {
            _save()
        }
    }
    
    private func _save() {
        do {
            try dataStore.playerRegistrations.addOrUpdate(instance: player)
        } catch {
            Logger.error(error)
        }
    }
}
