using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace WhiteList
{
    /// <summary>
    /// Логика взаимодействия для AddAndRedactAgent.xaml
    /// </summary>
    public partial class AddAndRedactAgent : Window
    {
        public AddAndRedactAgent()
        {
            InitializeComponent();
            foreach(AgentType at in DB.dbCon.AgentType.ToList())
            {
                ComboBoxType.Items.Add(at.Title);
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int idType = DB.dbCon.AgentType.FirstOrDefault(x => x.Title == ComboBoxType.SelectedValue.ToString()).ID;
                Agent a = new Agent() //создаем агента
                {
                    Title = TextBoxTitle.Text,
                    AgentTypeID = idType,
                    Address = TextBoxAdres.Text,
                    INN = TextBoxINN.Text,
                    KPP = TextBoxKPP.Text,
                    DirectorName = TextBoxNameDirector.Text,
                    Phone = TextBoxPhone.Text,
                    Email = TextBoxEmail.Text,
                    Priority = Convert.ToInt32(TextBoxPriority.Text)
                };
                DB.dbCon.Agent.Add(a);
                DB.dbCon.SaveChanges(); //сохраняем
                MessageBox.Show("Данные добавлены!");
                Close();
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.Message, "Ошибка");
            }
        }
    }
}
