using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace BazaZamowien.Classes
{
    class PremieDBBindings
    {

        public static void bindujDzialy(ComboBox toolStripComboBox, int IDEGrupy)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().PremieConnStr);

            command.CommandText = "SELECT Dzialy.IDE, Dzialy.Dzial FROM Dzialy LEFT OUTER JOIN GrupaDzial " +
                " ON Dzialy.IDE = GrupaDzial.IDEDzial WHERE (GrupaDzial.IDEGrupy = @param) ORDER BY Dzialy.Dzial";
            command.Parameters.AddWithValue("@param", IDEGrupy);
            command.Connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    toolStripComboBox.Items.Add(new BoundItem((int)reader.GetValue(0), reader.GetValue(1).ToString()));
                }
            }
            command.Connection.Close();

            
        }

        public static void bindujPracownikow(ComboBox toolStripComboBox, int IDEGrupy)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().PremieConnStr);

            command.CommandText = "select distinct IdePracownik,Pracownik from dbo.PODWLADNOSC where ideDzial in " +
                "(select IDEDzial from dbo.GrupaDzial where IDEGrupy = " + IDEGrupy + ")";
            
            if (User.getUser().IDEDzialuCT == 8){
                command.CommandText += " AND IDEoddzial IN (select IDEOddzial from dbo.Kierownicy WHERE IDEPracownik=" + User.getUser().IDE_CT + ")";            
            }

            command.CommandText += " order by Pracownik";
            command.Connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    toolStripComboBox.Items.Add(new BoundItem((int)reader.GetValue(0), reader.GetValue(1).ToString()));
                }
            }
            command.Connection.Close();
        }

    }

}