using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BazaZamowien.Classes;

namespace BazaZamowien
{
    public partial class AdminFormZamowienie : Form
    {
        public AdminFormZamowienie()
        {
            InitializeComponent();

            usersTableAdapter1.Connection.ConnectionString = ConnString.getConnString().ZamowieniaConnStr;
            dzialyTableAdapter1.Connection.ConnectionString = ConnString.getConnString().ZamowieniaConnStr;
            usersDzialyTableAdapter1.Connection.ConnectionString = ConnString.getConnString().ZamowieniaConnStr;
            
        }

        private void adminForm_Load(object sender, EventArgs e)
        {

            dzialyListBox.DisplayMember = "Symbol";
            dzialyListBox.ValueMember = "IDE";

            usersListBox.SelectedValueChanged += new EventHandler(usersListBox_SelectedValueChanged);

            usersListBox.DisplayMember = "Symbol";
            usersListBox.ValueMember = "IDE";
            usersListBox.DataSource = usersTableAdapter1.GetData();
      
            dzialyAllListBox.DataSource = dzialyTableAdapter1.GetDataByAll();
            dzialyAllListBox.DisplayMember = "Symbol";
            dzialyAllListBox.ValueMember = "IDE";
        }

        private void dodajUzytkownikaButton_Click(object sender, EventArgs e)
        {
            UserAddFormZamowienia uaf = new UserAddFormZamowienia(0);
            if (uaf.ShowDialog() == DialogResult.OK)
            {
                usersListBox.DataSource = usersTableAdapter1.GetData();
            }
        }

        private void edytujUzytkownikaButton_Click(object sender, EventArgs e)
        {
            if (usersListBox.SelectedValue != null)
            {
                UserAddFormZamowienia uaf = new UserAddFormZamowienia(Convert.ToInt32(usersListBox.SelectedValue));
                if (uaf.ShowDialog() == DialogResult.OK)
                {
                    usersListBox.DataSource = usersTableAdapter1.GetData();
                }
            }
        }

        private void usunUzytkownikaButton_Click(object sender, EventArgs e)
        {
            if (usersListBox.SelectedValue != null)
            {
                if (MessageBox.Show("Czy na pewno chcesz usunąc rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    usersTableAdapter1.Delete(Convert.ToInt32(usersListBox.SelectedValue));
                    usersListBox.DataSource = usersTableAdapter1.GetData();
                }
            }
        }

        private void usersListBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (usersListBox.SelectedValue != null)
            {
                dzialyListBox.DataSource = dzialyTableAdapter1.GetDataByIDEUser((int)usersListBox.SelectedValue);
            }
        }

        private void dodajDzialUztkownikaButton_Click(object sender, EventArgs e)
        {
            if ((usersListBox.SelectedValue != null) & (dzialyAllListBox.SelectedValue != null))
            {
                try
                {
                    usersDzialyTableAdapter1.Insert(Convert.ToInt32(usersListBox.SelectedValue), Convert.ToInt32(dzialyAllListBox.SelectedValue));
                    dzialyListBox.DataSource = dzialyTableAdapter1.GetDataByIDEUser((int)usersListBox.SelectedValue);
                } catch (SqlException e1){
                    if (e1.Number == 2627)
                    {
                        MessageBox.Show("Użytkownikowi już przydzielono ten dział");
                    }
                }
            }
        }

        private void usunDzailUzytkownikaButton_Click(object sender, EventArgs e)
        {
            if ((usersListBox.SelectedValue != null) & (dzialyListBox.SelectedValue != null))
            {
                usersDzialyTableAdapter1.Delete(Convert.ToInt32(usersListBox.SelectedValue), Convert.ToInt32(dzialyListBox.SelectedValue));
                dzialyListBox.DataSource = dzialyTableAdapter1.GetDataByIDEUser((int)usersListBox.SelectedValue);
            }
        }

        private void usunDzialButton_Click(object sender, EventArgs e)
        {

            if (dzialyAllListBox.SelectedValue != null)
            {
                if (MessageBox.Show("Czy na pewno chcesz usunąc rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    dzialyTableAdapter1.Delete(Convert.ToInt32(dzialyAllListBox.SelectedValue));
                    dzialyAllListBox.DataSource = dzialyTableAdapter1.GetDataByAll();
                }
            }

        }

        private void dodajDzialButton_Click(object sender, EventArgs e)
        {
            DzialAddForm daf = new DzialAddForm(0);
            if (daf.ShowDialog() == DialogResult.OK)
            {
                dzialyAllListBox.DataSource = dzialyTableAdapter1.GetDataByAll();               
            }
        }

        private void edytujDzialButton_Click(object sender, EventArgs e)
        {
            if (dzialyAllListBox.SelectedValue != null)
            {
                DzialAddForm daf = new DzialAddForm(Convert.ToInt32(dzialyAllListBox.SelectedValue));
                if (daf.ShowDialog() == DialogResult.OK)
                {
                  dzialyAllListBox.DataSource = dzialyTableAdapter1.GetDataByAll();
                }
            }
        }
        
    }
}