using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using demko6.Models;
using Tmds.DBus.Protocol;

namespace demko6;

public partial class AddFClientWindow : Window
{
    public AddFClientWindow()
    {
        InitializeComponent();
    }
    
    
    private void AddFClient_OnClick(object? sender, RoutedEventArgs e)
    {
        using var context = new DemkoNejykovContext();
        
        if (string.IsNullOrWhiteSpace(FioTextBox.Text) ||
            string.IsNullOrWhiteSpace(CodeTextBox.Text) ||
            string.IsNullOrWhiteSpace(PassportTextBox.Text) ||
            string.IsNullOrWhiteSpace(AddressTextBox.Text) ||
            string.IsNullOrWhiteSpace(EmailTextBox.Text) ||
            string.IsNullOrWhiteSpace(PasswordTextBox.Text) ||
            FClientDatePicker.SelectedDate is null)
        {
            ErrorTextBlock.Text = "Заполните все поля";
            ErrorTextBlock.IsVisible = true;
            return;
        }

        var fClient = new FClient()
        {
            Name = FioTextBox.Text,
            Code = CodeTextBox.Text,
            Passport = PasswordTextBox.Text,
            Birthday = DateOnly.FromDateTime(FClientDatePicker.SelectedDate.Value.DateTime),
            Address = AddressTextBox.Text,
            Email = EmailTextBox.Text,
            Password = PasswordTextBox.Text
        };

        var client = new Client()
        {
            UrOrNo = false,
            ClientCode = CodeTextBox.Text,
            CodeFClient = CodeTextBox.Text,
            CodeUrClient = null,
        };
        
        context.FClients.Add(fClient);
        context.Clients.Add(client);
        context.SaveChanges();
        ErrorTextBlock.Text = "Клиент создан";
        ErrorTextBlock.IsVisible = true;
    }
}