using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using demko6.Models;

namespace demko6;

public partial class AddUrClientWindow : Window
{
    public AddUrClientWindow()
    {
        InitializeComponent();
    }
    
    private void AddUrClient_OnClick(object? sender, RoutedEventArgs e)
    {
        using var context = new DemkoNejykovContext();
        
        if (string.IsNullOrWhiteSpace(FioTextBox.Text) ||
            string.IsNullOrWhiteSpace(InnTextBox.Text) ||
            string.IsNullOrWhiteSpace(PcTextBox.Text) ||
            string.IsNullOrWhiteSpace(BikTextBox.Text) ||
            string.IsNullOrWhiteSpace(FioRukTextBox.Text) ||
            string.IsNullOrWhiteSpace(FioKontaktTextBox.Text) ||
            string.IsNullOrWhiteSpace(PhoneKontaktTextBox.Text) ||
            string.IsNullOrWhiteSpace(AddressTextBox.Text) ||
            string.IsNullOrWhiteSpace(EmailTextBox.Text) ||
            string.IsNullOrWhiteSpace(PasswordTextBox.Text) ||
            string.IsNullOrWhiteSpace(CodeTextBox.Text))
        {
            ErrorTextBlock.Text = "Заполните все поля";
            ErrorTextBlock.IsVisible = true;
            return;
        }

        var urClient = new UrClient()
        {
            Name = FioTextBox.Text,
            Inn = InnTextBox.Text,
            Pc = PcTextBox.Text,
            Bik = BikTextBox.Text,
            FioRuk = FioRukTextBox.Text,
            FioKontakt = FioKontaktTextBox.Text,
            PhoneKontakt = PhoneKontaktTextBox.Text,
            Adress = AddressTextBox.Text,
            Email = EmailTextBox.Text,
            Password = PasswordTextBox.Text,
            Code = CodeTextBox.Text
        };

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