new_demo/OrderWindow.axaml.cs
2025-02-26 15:54:51 +03:00

116 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demo_hard.Models;
using Tmds.DBus.Protocol;
namespace demo_hard;
public partial class OrderWindow : Window
{
private ObservableCollection<int> _ordersIds = new();
private ObservableCollection<string> _clientsNames = new();
private ObservableCollection<string> _servicesNames = new();
private List<Service> SelectedServices = new();
public List<int> OrderIdList;
public List<string> ClientNamesList;
public List<string> ServicesList;
public OrderWindow()
{
InitializeComponent();
NewOrderNumber();
SearchItems();
using var context = new User2Context();
OrderIdList = context.Orders.Select(it => it.Id).ToList();
var services = context.Services.ToList();
serviceName.ItemsSource = services;
var clients = context.Clients.ToList();
clientName.ItemsSource = clients;
}
private void SearchItems()
{
using var context = new User2Context();
string searchText = SearchBox.Text?.ToLower() ?? "";
if (string.IsNullOrWhiteSpace(searchText))
{
SearchResultsList.ItemsSource = new List<string>();
return;
}
var clientRes = context.Clients
.Where(c => c.Fio.ToLower().Contains(searchText))
.Select(c => c.Fio);
var serviceRes = context.Services
.Where(s => s.ServiceName.ToLower().Contains(searchText))
.Select(s => s.ServiceName);
var results = clientRes.Concat(serviceRes).ToList();
SearchResultsList.ItemsSource = results;
}
private void NewOrderNumber()
{
using var context = new User2Context();
var OrderId = context.Orders.Max(o => o.Id) + 1;
if (context.Orders.Any(o => o.Id == OrderId)) throw new ArgumentException("Номера Id не должны совпадать");
if (OrderId < 1) throw new ArgumentException("OrderId должен быть на 1 больше");
orderNumberBox.ItemsSource = new string[] { OrderId.ToString() };
}
private void SearchBox_OnTextChanged(object? sender, TextChangedEventArgs e)
{
SearchItems();
}
private void AddService_OnClick(object? sender, RoutedEventArgs e)
{
if (serviceName.SelectedItem is Service selectedService && !SelectedServices.Contains(selectedService))
{
SelectedServices.Add(selectedService);
UpdateServiceList();
}
}
private void UpdateServiceList()
{
SelectedServicesListBox.ItemsSource = null;
SelectedServicesListBox.ItemsSource = SelectedServices.Select(s => s.ServiceName).ToList();
}
private void Go_Back_Button(object? sender, RoutedEventArgs e)
{
new FunctionWindow().ShowDialog(this);
}
private void AddUser_Button(object? sender, RoutedEventArgs e)
{
new AddClient().ShowDialog(this);
}
private void Create_Order(object? sender, RoutedEventArgs e)
{
string orderNumber = orderNumberBox.SelectedItem.ToString();
Client selectedClient = clientName.SelectedItem as Client;
List<Service> selectedServices = new List<Service>(SelectedServices);
if (selectedClient != null && !string.IsNullOrEmpty(orderNumber) && selectedServices.Any())
{
new NewOrderWindow(orderNumber, selectedClient, selectedServices).ShowDialog(this);
}
}
}