using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demko_term.Models;
using Microsoft.EntityFrameworkCore;

namespace demko_term;

public partial class ViewApplicantWindow : Window
{
    
    private List<Applicant> dataSourceApplicants;
    
    public ViewApplicantWindow()
    {
        InitializeComponent();
        using var context = new DemoCourseworkContext();
        DisplayApplicant();
    }

    public void LoadData()
    {
        using var context = new DemoCourseworkContext();
        dataSourceApplicants = context.Applicants
            .Select(a => new Applicant
            {
                Id = a.Id,
                Firstname = a.Firstname,
                Lastname = a.Lastname,
                Patronymic = a.Patronymic,
                Code = a.Code,
                Date = a.Date,
                Address = a.Address,
                Email = a.Email,
                Phone = a.Phone
            }).ToList();
    }

    public void DisplayApplicant()
    {
        LoadData();

        var temp = dataSourceApplicants;
        
        ApplicantListBox.ItemsSource = temp;
    }
    
    private async void CreateApplicantWindowButton_OnClick(object? sender, RoutedEventArgs e)
    {
        CreateApplicantWindow сreateApplicantWindow = new CreateApplicantWindow();
        await сreateApplicantWindow.ShowDialog(this);

        DisplayApplicant();
    }
}