using Microsoft.Win32;
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.Navigation;
using System.Windows.Shapes;
namespace Golyshev43p180322
{
///
/// Логика взаимодействия для CreateOrUpdate.xaml
///
public partial class CreateOrUpdate : Page
{
Agent agent = new Agent();
bool flag;
string path;
public CreateOrUpdate()
{
InitializeComponent();
flag = true;
List AT = BaseClass.Base.AgentType.ToList();
foreach (AgentType s in AT)
{
CBType.Items.Add(s.Title);
}
btnDelete.Visibility = Visibility.Collapsed;
}
public CreateOrUpdate(Agent AgentUpdate)
{
InitializeComponent();
List AT = BaseClass.Base.AgentType.ToList();
foreach (AgentType s in AT)
{
CBType.Items.Add(s.Title);
}
agent = AgentUpdate;
TBTitle.Text = agent.Title;
CBType.SelectedIndex = agent.AgentTypeID-1;
TBPriority.Text = Convert.ToString(agent.Priority);
TBAdress.Text = agent.Address;
TBINN.Text = agent.INN;
TBKPP.Text = agent.KPP;
TBDirector.Text = agent.DirectorName;
TBPone.Text = agent.Phone;
TBEmail.Text = agent.Email;
}
private void btnGoBack_Click(object sender, RoutedEventArgs e)
{
FrameClass.MainFrame.Navigate(new ShowAgentPage());
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
string title = TBTitle.Text;
int agenttype = CBType.SelectedIndex + 1;
int priority = Convert.ToInt32(TBPriority.Text);
string image = path;
string adress = TBAdress.Text;
string inn = TBINN.Text;
string kpp = TBKPP.Text;
string director = TBDirector.Text;
string phone = TBPone.Text;
string email = TBEmail.Text;
if (priority < 0)
{
MessageBox.Show("Агент не добавлен!", "Ошибка");
}
else
{
agent.Title = title; agent.AgentTypeID = agenttype; agent.Priority = priority; agent.Logo = image; agent.Address = adress; agent.INN = inn;
agent.KPP = kpp; agent.DirectorName = director; agent.Phone = phone; agent.Email = email;
MessageBoxResult result = MessageBox.Show("Вы уверены сохранить?", "Добавление записи", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
if (flag == true)
{
BaseClass.Base.Agent.Add(agent);
}
BaseClass.Base.SaveChanges();
MessageBox.Show("Данные записаны!", "Добавление записи");
FrameClass.MainFrame.Navigate(new ShowAgentPage());
}
}
}
catch
{
MessageBox.Show("Агент не добавлен!", "Ошибка");
}
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
int id = agent.ID;
Agent AgentDelete = BaseClass.Base.Agent.FirstOrDefault(x => x.ID == id);
MessageBoxResult result = MessageBox.Show("Вы уверены что хотите удалить запись?", "Удаление записи", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
BaseClass.Base.Agent.Remove(AgentDelete);
BaseClass.Base.SaveChanges();
FrameClass.MainFrame.Navigate(new ShowAgentPage());
MessageBox.Show("Запись удалена!", "Удаление записи");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog OFD = new OpenFileDialog();
OFD.ShowDialog();
path = OFD.FileName;
int n = path.IndexOf("agent");
path = path.Substring(n);
}
}
}