Files
UP01-Task2/UP01Task2App/UP01Task2App/Windows/ClientWindow.xaml.cs
2026-04-04 13:38:14 +05:00

263 lines
9.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UP01Task2App.Models;
using UP01Task2App.Services;
namespace UP01Task2App.Windows
{
/// <summary>
/// Логика взаимодействия для ClientWindow.xaml
/// </summary>
public partial class ClientWindow : Window
{
private JsonService Json = new();
public List<Client> ClientsList = new();
public List<Trainer> TrainersList = new();
public List<Record> RecordsList = new();
public ObservableCollection<Client> ClientsUI { get; set; } = new();
public ClientWindow()
{
InitializeComponent();
ClientsList = Json.ReadClientDataFromJson();
TrainersList = Json.ReadTrainerDataFromJson();
RecordsList = Json.ReadRecordDataFromJson();
UpdateClientUI();
this.DataContext = this;
}
private void UpdateClientUI()
{
ClientsUI.Clear();
foreach (var item in ClientsList)
{
ClientsUI.Add(item);
}
}
private bool ValidateFields()
{
try
{
ValidationHelper.ValidateNotNullTextBox(SurnameTextBox, "Фамилия");
ValidationHelper.ValidateCorrectlyFullName(SurnameTextBox, "Фамилия");
ValidationHelper.ValidateNotNullTextBox(NameTextBox, "Имя");
ValidationHelper.ValidateCorrectlyFullName(NameTextBox, "Имя");
ValidationHelper.ValidateNotNullTextBox(PatronymicTextBox, "Отчество");
ValidationHelper.ValidateCorrectlyFullName(PatronymicTextBox, "Отчество");
ValidationHelper.ValidateNotNullDatePicker(BirthDateDatePicker, "Дата рождения");
ValidationHelper.ValidateDateNotInFutureTextBox(BirthDateDatePicker, "Дата рождения");
ValidationHelper.CheckBirthdate(BirthDateDatePicker);
ValidationHelper.ValidateNotNullTextBox(AdressTextBox, "Фактический адрес");
ValidationHelper.ValidateNotNullTextBox(PhoneNumberTextBox, "Телефон");
ValidationHelper.ValidateNumTextBox(PhoneNumberTextBox, "Телефон");
ValidationHelper.ValidatePhoneNumber(PhoneNumberTextBox, "Телефон");
ValidationHelper.ValidateNotNullTextBox(SubscriptionMumberTextBox, "Номер абонемента");
ValidationHelper.ValidateNumTextBox(SubscriptionMumberTextBox, "Номер абонемента");
ValidationHelper.ValidateIntRangeTextBox(SubscriptionMumberTextBox, "Номер абонемента");
ValidationHelper.ValidateEightDigitLenght(SubscriptionMumberTextBox, "Номер абонемента");
if (rbMan.IsChecked != true && rbWoman.IsChecked != true)
{
throw new ArgumentException("Выберите пол");
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ValidateFields())
{
int subscriptionNumber = int.Parse(SubscriptionMumberTextBox.Text);
bool exists = ClientsList.Any(c =>
c.SubscriptionNumber == subscriptionNumber
);
if (!exists)
{
string gender = "";
if (rbMan.IsChecked == true)
gender = "М";
else
gender = "Ж";
ClientsList.Add(new Client
{
Surname = SurnameTextBox.Text,
Name = NameTextBox.Text,
Adress = AdressTextBox.Text,
BirthDate = BirthDateDatePicker.SelectedDate,
Patronymic = PatronymicTextBox.Text,
PhoneNumber = PhoneNumberTextBox.Text,
SubscriptionNumber = int.Parse(SubscriptionMumberTextBox.Text),
Gender = gender
});
try
{
Json.SaveClientDataToJson(ClientsList);
MessageBox.Show("Данные сохранены");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
UpdateClientUI();
}
else
{
MessageBox.Show("Клиент с таким абонементом уже существует");
}
}
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (ClientsUI[ClientsListBox.SelectedIndex] == null)
return;
var client = ClientsUI[ClientsListBox.SelectedIndex];
if (client != null)
{
SurnameTextBox.Text = client.Surname;
NameTextBox.Text = client.Name;
PatronymicTextBox.Text = client.Patronymic;
BirthDateDatePicker.SelectedDate = client.BirthDate;
AdressTextBox.Text = client.Adress;
PhoneNumberTextBox.Text = client.PhoneNumber;
SubscriptionMumberTextBox.Text = client.SubscriptionNumber.ToString();
if (client.Gender == "М")
{
rbMan.IsChecked = true;
}
else
{
rbWoman.IsChecked = true;
}
}
}
catch { }
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
if (ClientsListBox.SelectedIndex == -1)
{
MessageBox.Show("Выберите клиента перед изменением");
return;
}
ValidationHelper.ValidateNotNullTextBox(AdressTextBox, "Фактический адрес");
ValidationHelper.ValidateNotNullTextBox(PhoneNumberTextBox, "Телефон");
ValidationHelper.ValidateNumTextBox(PhoneNumberTextBox, "Телефон");
ValidationHelper.ValidatePhoneNumber(PhoneNumberTextBox, "Телефон");
ClientsList[ClientsListBox.SelectedIndex].PhoneNumber = PhoneNumberTextBox.Text;
ClientsList[ClientsListBox.SelectedIndex].Adress = AdressTextBox.Text;
try
{
Json.SaveClientDataToJson(ClientsList);
MessageBox.Show("Данные сохранены");
UpdateClientUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (ClientsListBox.SelectedIndex != -1)
{
AppointmentWindow wnd = new(TrainersList, (Client)ClientsListBox.SelectedValue);
bool? res = wnd.ShowDialog();
if (res == true)
{
var record = wnd.OutputRecord;
RecordsList.Add(record);
try
{
Json.SaveRecordDataToJson(RecordsList);
MessageBox.Show("Запись успешно добавлена");
}
catch
{
MessageBox.Show("Не удалось добавить запись");
}
}
}
else
MessageBox.Show("Выберите клиента перед записью");
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
if (ClientsListBox.SelectedIndex != -1)
{
ClientAppointmentList wnd = new(RecordsList, (Client)ClientsListBox.SelectedValue);
bool? res = wnd.ShowDialog();
if (res == true)
{
try
{
RecordsList.Remove(wnd.RecordForDelete);
Json.SaveRecordDataToJson(RecordsList);
MessageBox.Show("Запись успешно удалена");
}
catch
{
MessageBox.Show("Не удалось сохранить данные");
}
}
}
else
MessageBox.Show("Выберите клиента перед просмотром");
}
}
}