using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
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 Session1
{
    /// <summary>
    /// Логика взаимодействия для EditAgent.xaml
    /// </summary>
    public partial class EditAgent : Window
    {
        Agent selectedAgent;
        public EditAgent(Agent selectedAgent)
        {
            InitializeComponent();
            this.selectedAgent = selectedAgent;
            foreach (AgentType type in BaseConnect.baseModel.AgentType)
            {
                agentType.Items.Add(type);
            }
            agentType.DisplayMemberPath = "Title";
            LoadAgentData();
        }

        private void LoadAgentData()
        {
            agentImg.Source = new BitmapImage(new Uri(selectedAgent.AgentImage, UriKind.Relative));
            imgPath.Text = selectedAgent.Logo;
            agentName.Text = selectedAgent.Title;
            agentType.SelectedItem = selectedAgent.AgentType;
            agentPriority.Text = selectedAgent.Priority.ToString();
            agentAddress.Text = selectedAgent.Address;
            agentINN.Text = selectedAgent.INN;
            agentKPP.Text = selectedAgent.KPP;
            agentPhone.Text = selectedAgent.Phone;
            agentDirector.Text = selectedAgent.DirectorName;
            agentEmail.Text = selectedAgent.Email;
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            Agent updatedAgent = new Agent();
            selectedAgent.Logo = imgPath.Text = selectedAgent.Logo;
            selectedAgent.Title = agentName.Text;
            selectedAgent.AgentTypeID = ((AgentType)agentType.SelectedItem).ID;
            selectedAgent.Priority = Convert.ToInt32(agentPriority.Text);
            selectedAgent.Address = agentAddress.Text;
            selectedAgent.INN = agentINN.Text;
            selectedAgent.KPP = agentKPP.Text;
            selectedAgent.Phone = agentPhone.Text;
            selectedAgent.DirectorName = agentDirector.Text;
            selectedAgent.Email = agentEmail.Text;
            try
            {
                BaseConnect.baseModel.SaveChanges();
                MessageBox.Show("Данные успешно изменены.");
                Close();
            }
            catch
            {
                MessageBox.Show("Не удалось изменить данные.");
            }
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void addImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "*.jpg | *.png";
            openFileDialog.ShowDialog();
            string filePath = openFileDialog.FileName;
            imgPath.Text = filePath;
        }
    }
}
