demo_term/demko_term/AcceptStatementWindow.axaml.cs

98 lines
3.0 KiB
C#
Raw Normal View History

2025-04-14 05:39:47 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using demko_term.Models;
namespace demko_term;
public partial class AcceptStatementWindow : Window
{
2025-04-14 14:45:48 +00:00
public List<string> AcceptApplicantList { get; set; }
public List<string> AcceptSpecialtyList { get; set; }
2025-04-14 05:39:47 +00:00
public AcceptStatementWindow()
{
using var context = new DemoCourseworkContext();
InitializeComponent();
2025-04-14 14:45:48 +00:00
DisplayComboBoxes();
}
public void LoadComboBoxes()
{
using var context = new DemoCourseworkContext();
2025-04-14 05:39:47 +00:00
AcceptApplicantList = context.Applicants.Select(a => a.Code.ToString()).ToList();
AcceptSpecialtyList = context.Specialties.Select(s => s.Code).ToList();
2025-04-14 14:45:48 +00:00
}
public void DisplayComboBoxes()
{
LoadComboBoxes();
AcceptApplicantComboBox.ItemsSource = AcceptApplicantList;
2025-04-14 05:39:47 +00:00
AcceptSpecialtyListBox.ItemsSource = AcceptSpecialtyList;
}
private void AcceptStatementButton_OnClick(object? sender, RoutedEventArgs e)
{
using var context = new DemoCourseworkContext();
if (AcceptApplicantComboBox.SelectedItem == null || AcceptSpecialtyListBox.SelectedItems == null)
{
return;
}
string selectedApplicantCodeStr = AcceptApplicantComboBox.SelectedItem.ToString()!;
var applicant = context.Applicants.FirstOrDefault(a => a.Code.ToString() == selectedApplicantCodeStr);
if (applicant == null)
{
return;
}
int? points = null;
if (int.TryParse(PointsTextBox.Text, out int parsedPoints))
{
points = parsedPoints;
}
foreach (var selectedSpecialtyCode in AcceptSpecialtyListBox.SelectedItems.Cast<string>())
{
var specialty = context.Specialties.FirstOrDefault(s => s.Code == selectedSpecialtyCode);
if (specialty == null)
continue;
string personalNumber = $"{specialty.Code}-{applicant.Code}";
var statement = new Statement
{
PersonalNumber = personalNumber,
SubmissionDate = DateOnly.FromDateTime(DateTime.Now),
SubmissionTime = TimeOnly.FromDateTime(DateTime.Now),
ApplicantCode = applicant.Id,
ApplicantSpeciality = specialty.Id,
Passport = PassportCheckBox.IsChecked,
Diploma = DiplomaCheckBox.IsChecked,
Points = points
};
context.Statements.Add(statement);
}
context.SaveChanges();
}
2025-04-14 14:45:48 +00:00
private async void CreateApplicantWindowButton_OnClick(object? sender, RoutedEventArgs e)
2025-04-14 05:39:47 +00:00
{
2025-04-14 14:45:48 +00:00
CreateApplicantWindow createApplicantWindow = new CreateApplicantWindow();
await createApplicantWindow.ShowDialog(this);
DisplayComboBoxes();
2025-04-14 05:39:47 +00:00
}
}