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

namespace BazaZamowien.Classes
{
    class ZamowieniaDBBindings
    {

        public static void bindujDzialy(ToolStripComboBox toolStripComboBox, int userIDE)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().ZamowieniaConnStr);

            command.CommandText = "SELECT Dzialy.IDE, Dzialy.Symbol, Dzialy.Roz, Dzialy.Opis FROM Dzialy LEFT OUTER JOIN ";
            command.CommandText += "UsersDzialy ON Dzialy.IDE = UsersDzialy.IDEDzial WHERE (UsersDzialy.IDEUser = @param) ";
            command.CommandText += "ORDER BY Dzialy.Symbol";
            command.Parameters.AddWithValue("@param", userIDE);
            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 bindujDzialyAll(ToolStripComboBox toolStripComboBox)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().ZamowieniaConnStr);

            command.CommandText = "SELECT Dzialy.IDE, Dzialy.Symbol, Dzialy.Roz, Dzialy.Opis FROM Dzialy ";
            command.CommandText += "ORDER BY Dzialy.Symbol";
            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();
        }

    }
}
