using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace Baza_Reklam
{
    public partial class StartForm : Form
    {

        private bool pierwszaAktywacja = false;

        public static StartForm startForm;

        public static StartForm getStartForm(MDIBazaReklam parent)
        {
            if (startForm == null) startForm = new StartForm(parent);
            return startForm;
        }

        private StartForm(MDIBazaReklam parent)
        {
            InitializeComponent();

            this.MdiParent = parent;
            
            shedulerTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            zastawienieKontaktowTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            view_Podsumowanie_Wykonania_PlanuTableAdapter1.Connection.ConnectionString = ConnString.getConnString().Value;
            view_Podsumowanie_Wykonania_Planu_AgencjaTableAdapter1.Connection.ConnectionString = ConnString.getConnString().Value;

            if (User.getUser().St_kierownik) 
            {
                wynikAgencji();
            }
            else if (User.getUser().St_handlowiec | User.getUser().St_subhandlowiec) 
            {
                wynikAgenta();                   
            }
        }

        private void shedulerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.shedulerBindingSource.EndEdit();
            this.shedulerTableAdapter.Update(this.rEKLAMADataSet.sheduler);
        }

        private void zrobioneToolStripButton_Click(object sender, EventArgs e)
        {
            if (shedulerBindingSource.Current != null) {
                DataRowView row = (DataRowView)shedulerBindingSource.Current;
                row["zalatwione"] = false;
                shedulerBindingSource.EndEdit();
                shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
                shedulerDataGridView.Refresh();
            }
        }

        private void niezrobioneToolStripButton_Click(object sender, EventArgs e)
        {
            if (shedulerBindingSource.Current != null)
            {
                DataRowView row = (DataRowView)shedulerBindingSource.Current;
                row["zalatwione"] = true;
                shedulerBindingSource.EndEdit();
                shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
                shedulerDataGridView.Refresh();
            }
        }

        private void filtrujToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            filtruj();
        }

        private void StartForm_Activated(object sender, EventArgs e)
        {
            if (pierwszaAktywacja)
            {
                wypelnijShedulerWyniki();
            }
        }

        private void StartForm_Load(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().Value);
            SqlDataReader reader;

            if (User.getUser().St_kierownik)
            {            
                command.CommandText = "select Symbol from dbo.AGENCI where aktywny=1 and ID_AGENCJI=@param order by Symbol";
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@param", User.getUser().IdAgencji);
                command.Connection.Open();

                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    agenciToolStripComboBox.Items.Add(reader.GetValue(0).ToString());

                }
                agenciToolStripComboBox.Visible = true;
            }

            command.Connection.Close();

            DBBindings.bindujAgencje(agencjaToolStripComboBox);
            DBBindings.bindujAgentow(agentToolStripComboBox);
   
            filtrujToolStripComboBox.SelectedIndex = 0;
            agenciToolStripComboBox.SelectedIndex = 0;
        }

        private void agenciToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            filtruj();   
        }

        private void filtruj() {

            if ((filtrujToolStripComboBox.SelectedItem != null) & (agenciToolStripComboBox.SelectedItem != null))
            {
                DateTime ostatniDzienMiesiaca = DateTime.Today.AddMonths(1).AddDays(-(DateTime.Today.Day));
                DateTime ostatniDzienTygodnia = DateTime.Today.AddDays(7).AddDays(-((int)DateTime.Today.DayOfWeek));

                DateTime pierwszyDzienMiesiaca = DateTime.Today.AddDays(-DateTime.DaysInMonth(DateTime.Today.Year,DateTime
                    .Today.Month)).AddDays(DateTime.DaysInMonth(DateTime.Today.Year,DateTime.Today.Month) - DateTime.Today.Day);
                DateTime pierwszyDzienTygodnia = DateTime.Today.AddDays(-7).AddDays(((int)DateTime.Today.DayOfWeek));

                switch (filtrujToolStripComboBox.SelectedItem.ToString())
                {

                    case "NA DZISIAJ":
                        shedulerBindingSource.Filter = "[DataPrzypomnienia] >= '" + DateTime.Today.Date.ToString() + "' AND [DataPrzypomnienia] < '" + DateTime.Today.Date.AddDays(1).ToString() + "'";
                        break;
                    case "NA JUTRO":
                        shedulerBindingSource.Filter = "[DataPrzypomnienia] >= '" + DateTime.Today.Date.AddDays(1).ToString() + "' AND [DataPrzypomnienia] < '" + DateTime.Today.Date.AddDays(2).ToString() + "'";
                        break;
                    case "NA TYDZIEŃ":
                        shedulerBindingSource.Filter = "[DataPrzypomnienia] >= '" +  pierwszyDzienTygodnia.ToString() + "' AND [DataPrzypomnienia] <= '" + ostatniDzienTygodnia.ToString() + "'";
                        break;
                    case "NA MIESIĄC":
                        shedulerBindingSource.Filter = "[DataPrzypomnienia] >= '" + pierwszyDzienMiesiaca.ToString() + "' AND [DataPrzypomnienia] <= '" + ostatniDzienMiesiaca.ToString() + "'";
                        break;
                    case "WSZYSTKIE":
                        shedulerBindingSource.Filter = " 1 = 1";
                        break;
                    case "NIEZAŁATWIONE ":
                        shedulerBindingSource.Filter = "zalatwione = 1";
                        break;
                }

                if (agenciToolStripComboBox.SelectedItem.ToString() != "WSZYSCY")
                {
                    shedulerBindingSource.Filter += " AND [akwizytor]='" + agenciToolStripComboBox.SelectedItem.ToString() + "'";
                }
            }
        }

        private void klientToolStripButton_Click(object sender, EventArgs e)
        {
            if (shedulerBindingSource.Current != null)
            {
                DataRowView row = (DataRowView)shedulerBindingSource.Current;

                int custID = Convert.ToInt32(row["CustomerId"]);

                ClientsForm.getClientsForm((MDIBazaReklam)this.MdiParent).pokazKlienta(custID);

                this.Hide();

                ClientsForm.getClientsForm((MDIBazaReklam)this.MdiParent).Show();
            }
        }

        private void usunToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Czy na pewno chcesz usunąć rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (shedulerBindingSource.Current != null)
                {
                    shedulerBindingSource.RemoveCurrent();
                    shedulerBindingSource.EndEdit();
                    shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
                }
            }
        }

        private void shedulerDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Wprowadź poprawnie dane");
        }

        private void zapiszToolStripButton_Click(object sender, EventArgs e)
        {
            shedulerDataGridView.EndEdit();
            shedulerBindingSource.EndEdit();
            shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);            
            MessageBox.Show("Dane zapisane");  
        }

        private void shedulerDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            shedulerDataGridView.EndEdit();
        }


        private void shedulerDataGridView_Leave(object sender, EventArgs e)
        {
            shedulerDataGridView.EndEdit();
        }

        private void sprDoZalatwieniaTabPage_Leave(object sender, EventArgs e)
        {
            shedulerBindingSource.EndEdit();

            REKLAMADataSet.shedulerDataTable changes = rEKLAMADataSet.sheduler.GetChanges(DataRowState.Modified)
                as REKLAMADataSet.shedulerDataTable;

            if (changes != null)
            {
                if (MessageBox.Show("Czy zapisać zmiany?", "", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
                }
                else
                {
                    rEKLAMADataSet.sheduler.RejectChanges();
                }
            }
        }

        private void klient2ToolStripButton_Click(object sender, EventArgs e)
        {
            if (zastawienieKontaktowBindingSource.Current != null)
            {
                DataRowView row = (DataRowView)zastawienieKontaktowBindingSource.Current;

                int custID = Convert.ToInt32(row["CustomerId"]);

                ClientsForm.getClientsForm((MDIBazaReklam)this.MdiParent).pokazKlienta(custID);

                this.Hide();

                ClientsForm.getClientsForm((MDIBazaReklam)this.MdiParent).Show();
            }
        }

        private void szukajToolStripButton_Click(object sender, EventArgs e)
        {
            string query = "SELECT TOP 1000 KLIENCI.FirstName, KONTAKTY.symbol_agenta, KONTAKTY.data, KONTAKTY.rodzaj,";
            query += " KONTAKTY.opis, KLIENCI.CustomerID, KLIENCI.UserName FROM KONTAKTY LEFT OUTER JOIN KLIENCI ON KONTAKTY.customerId = KLIENCI.CustomerID ";
            query += "left outer join AGENCI on KONTAKTY.symbol_agenta = AGENCI.Symbol left outer join ";
            query += "AGENCJE on AGENCI.ID_AGENCJI = AGENCJE.Id_agencji where 1=1 ";

            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);      
   
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.Connection = conn;        
            command.CommandText = query;
            command.Parameters.Clear();

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);


            if (rokToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(rokToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłowy rok.");
                    return;
                }
                command.CommandText += " AND datepart(year,KONTAKTY.data) = @rok ";
                command.Parameters.AddWithValue("@rok", rokToolStripTextBox.Text.Trim());
            }

            if (msToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(msToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłowy miesiąc.");
                    return;
                }                
                command.CommandText += " AND datepart(month,KONTAKTY.data) = @ms ";
                command.Parameters.AddWithValue("@ms", msToolStripTextBox.Text.Trim());
            }

            if (agencjaToolStripComboBox.Text.Trim() != "") 
            {
                command.CommandText += " AND AGENCJE.Symbol = @agencja ";
                command.Parameters.AddWithValue("@agencja", agencjaToolStripComboBox.Text.Trim());
            }

            if (agentToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND AGENCI.Symbol = @agent ";
                command.Parameters.AddWithValue("@agent", agentToolStripComboBox.Text.Trim());
            }

            if (rodzajToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND KONTAKTY.rodzaj = @rodzaj ";
                command.Parameters.AddWithValue("@rodzaj", rodzajToolStripComboBox.Text.Trim());
            }

            if (klientToolStripTextBox.Text.Trim() != "")
            {
                command.CommandText += " AND KLIENCI.FirstName like '%' + @klient + '%' ";
                command.Parameters.AddWithValue("@klient", klientToolStripTextBox.Text.Trim());               
            }
                        
            command.CommandText += " order by KONTAKTY.data";

            sqlDataAdapter.SelectCommand = command;

            this.Cursor = Cursors.WaitCursor;

            this.rEKLAMADataSet.zastawienieKontaktow.Clear();

            try
            {
                sqlDataAdapter.Fill(this.rEKLAMADataSet.zastawienieKontaktow);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            this.Cursor = Cursors.Default;

        }

        private void wyczyscToolStripButton_Click(object sender, EventArgs e)
        {
            rEKLAMADataSet.KONTAKTY.Clear();

            rokToolStripTextBox.Clear();
            msToolStripTextBox.Clear();
            klientToolStripTextBox.Clear();               
        
            agencjaToolStripComboBox.SelectedIndex = -1;
            agenciToolStripComboBox.Text = "";

            agentToolStripComboBox.SelectedIndex = -1;
            agentToolStripComboBox.Text = "";
            
            rodzajToolStripComboBox.SelectedIndex = -1;
            rodzajToolStripComboBox.Text = "";
        }

        private void zastawienieKontaktowDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            zastawienieKontaktowBindingSource.CancelEdit();
        }

        private void wynikAgenta() {

            rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu.Clear();
            view_Podsumowanie_Wykonania_PlanuTableAdapter1.Fill(rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, User.getUser().Login, (short)DateTime.Today.Month, (short)DateTime.Today.Year);
                     
            //wynik miesieczny
            Label l1 = new Label();
            l1.Text = "BUDŻET";
            Label lw1 = new Label();
            lw1.TextAlign = ContentAlignment.MiddleCenter;
            lw1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "W_BUDZET",true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            Label lp1 = new Label();
            lp1.TextAlign = ContentAlignment.MiddleCenter;
            lp1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            tableLayoutPanel1.Controls.Add(l1);
            tableLayoutPanel1.Controls.Add(lp1);
            tableLayoutPanel1.Controls.Add(lw1);

            Label l2 = new Label();
            l2.Text = "SPOTKANIA";
            Label lw2 = new Label();
            lw2.TextAlign = ContentAlignment.MiddleCenter;
            lw2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "W_SPOTKANIA"));
            Label lp2 = new Label();
            lp2.TextAlign = ContentAlignment.MiddleCenter;
            lp2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "SPOTKANIA"));
            tableLayoutPanel1.Controls.Add(l2);
            tableLayoutPanel1.Controls.Add(lp2);
            tableLayoutPanel1.Controls.Add(lw2);

            Label l3 = new Label();
            l3.Text = "KONTAKTY";
            Label lw3 = new Label();
            lw3.TextAlign = ContentAlignment.MiddleCenter;
            lw3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "W_KONTAKTY"));
            Label lp3 = new Label();
            lp3.TextAlign = ContentAlignment.MiddleCenter;
            lp3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "KONTAKTY"));
            tableLayoutPanel1.Controls.Add(l3);
            tableLayoutPanel1.Controls.Add(lp3);
            tableLayoutPanel1.Controls.Add(lw3);

            Label l4 = new Label();
            l4.Text = "NOWI KLIENCI";
            Label lw4 = new Label();
            lw4.TextAlign = ContentAlignment.MiddleCenter;
            lw4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "W_N_KLIENCI"));            
            Label lp4 = new Label();
            lp4.TextAlign = ContentAlignment.MiddleCenter;
            lp4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "N_KLIENCI"));
            tableLayoutPanel1.Controls.Add(l4);
            tableLayoutPanel1.Controls.Add(lp4);
            tableLayoutPanel1.Controls.Add(lw4);

            Label l5 = new Label();
            l5.Text = "ILOŚĆ REKLAM";
            Label lw5 = new Label();
            lw5.TextAlign = ContentAlignment.MiddleCenter;           
            lw5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "W_REKLAMY"));
            Label lp5 = new Label();
            lp5.TextAlign = ContentAlignment.MiddleCenter;           
            lp5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "REKLAMY"));
            tableLayoutPanel1.Controls.Add(l5);
            tableLayoutPanel1.Controls.Add(lp5);
            tableLayoutPanel1.Controls.Add(lw5);

            //wynik roczny
            Label rl1 = new Label();
            rl1.Text = "BUDŻET";
            Label rlw1 = new Label();
            rlw1.TextAlign = ContentAlignment.MiddleCenter;           
            rlw1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_W_BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            Label rlp1 = new Label();
            rlp1.TextAlign = ContentAlignment.MiddleCenter;           
            rlp1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            tableLayoutPanel2.Controls.Add(rl1);
            tableLayoutPanel2.Controls.Add(rlp1);
            tableLayoutPanel2.Controls.Add(rlw1);

            Label rl2 = new Label();
            rl2.Text = "SPOTKANIA";
            Label rlw2 = new Label();
            rlw2.TextAlign = ContentAlignment.MiddleCenter;           
            rlw2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_W_SPOTKANIA"));
            Label rlp2 = new Label();
            rlp2.TextAlign = ContentAlignment.MiddleCenter;           
            rlp2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_SPOTKANIA"));
            tableLayoutPanel2.Controls.Add(rl2);
            tableLayoutPanel2.Controls.Add(rlp2);
            tableLayoutPanel2.Controls.Add(rlw2);

            Label rl3 = new Label();
            rl3.Text = "KONTAKTY";
            Label rlw3 = new Label();
            rlw3.TextAlign = ContentAlignment.MiddleCenter;           
            rlw3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_W_KONTAKTY"));
            Label rlp3 = new Label();
            rlp3.TextAlign = ContentAlignment.MiddleCenter;           
            rlp3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_KONTAKTY"));
            tableLayoutPanel2.Controls.Add(rl3);
            tableLayoutPanel2.Controls.Add(rlp3);
            tableLayoutPanel2.Controls.Add(rlw3);

            Label rl4 = new Label();
            rl4.Text = "NOWI KLIENCI";
            Label rlw4 = new Label();
            rlw4.TextAlign = ContentAlignment.MiddleCenter;           
            rlw4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_W_N_KLIENCI"));
            Label rlp4 = new Label();
            rlp4.TextAlign = ContentAlignment.MiddleCenter;           
            rlp4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_N_KLIENCI"));
            tableLayoutPanel2.Controls.Add(rl4);
            tableLayoutPanel2.Controls.Add(rlp4);
            tableLayoutPanel2.Controls.Add(rlw4);

            Label rl5 = new Label();
            rl5.Text = "ILOŚĆ REKLAM";
            Label rlw5 = new Label();
            rlw5.TextAlign = ContentAlignment.MiddleCenter;           
            rlw5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_W_REKLAMY"));
            Label rlp5 = new Label();
            rlp5.TextAlign = ContentAlignment.MiddleCenter;           
            rlp5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu, "R_REKLAMY"));
            tableLayoutPanel2.Controls.Add(rl5);
            tableLayoutPanel2.Controls.Add(rlp5);
            tableLayoutPanel2.Controls.Add(rlw5);
        }

        private void wynikAgencji()
        {
            rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja.Clear();
            view_Podsumowanie_Wykonania_Planu_AgencjaTableAdapter1.Fill(
                rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja,
                User.getUser().SymbolAgencji, (short)DateTime.Today.Month, (short)DateTime.Today.Year);

            //wynik miesieczny
            Label l1 = new Label();
            l1.Text = "BUDŻET";
            Label lw1 = new Label();
            lw1.TextAlign = ContentAlignment.MiddleCenter;           
            lw1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "W_BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            Label lp1 = new Label();
            lp1.TextAlign = ContentAlignment.MiddleCenter;           
            lp1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            tableLayoutPanel1.Controls.Add(l1);
            tableLayoutPanel1.Controls.Add(lp1);
            tableLayoutPanel1.Controls.Add(lw1);

            Label l2 = new Label();
            l2.Text = "SPOTKANIA";
            Label lw2 = new Label();
            lw2.TextAlign = ContentAlignment.MiddleCenter;                       
            lw2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "W_SPOTKANIA"));
            Label lp2 = new Label();
            lp2.TextAlign = ContentAlignment.MiddleCenter;                       
            lp2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "SPOTKANIA"));
            tableLayoutPanel1.Controls.Add(l2);
            tableLayoutPanel1.Controls.Add(lp2);
            tableLayoutPanel1.Controls.Add(lw2);

            Label l3 = new Label();
            l3.Text = "KONTAKTY";
            Label lw3 = new Label();
            lw3.TextAlign = ContentAlignment.MiddleCenter;                       
            lw3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "W_KONTAKTY"));
            Label lp3 = new Label();
            lp3.TextAlign = ContentAlignment.MiddleCenter;                       
            lp3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "KONTAKTY"));
            tableLayoutPanel1.Controls.Add(l3);
            tableLayoutPanel1.Controls.Add(lp3);
            tableLayoutPanel1.Controls.Add(lw3);

            Label l4 = new Label();
            l4.Text = "NOWI KLIENCI";
            Label lw4 = new Label();
            lw4.TextAlign = ContentAlignment.MiddleCenter;                       
            lw4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "W_N_KLIENCI"));
            Label lp4 = new Label();
            lp4.TextAlign = ContentAlignment.MiddleCenter;                       
            lp4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "N_KLIENCI"));
            tableLayoutPanel1.Controls.Add(l4);
            tableLayoutPanel1.Controls.Add(lp4);
            tableLayoutPanel1.Controls.Add(lw4);

            Label l5 = new Label();
            l5.Text = "ILOŚĆ REKLAM";
            Label lw5 = new Label();
            lw5.TextAlign = ContentAlignment.MiddleCenter;                       
            lw5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "W_REKLAMY"));
            Label lp5 = new Label();
            lp5.TextAlign = ContentAlignment.MiddleCenter;                       
            lp5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "REKLAMY"));
            tableLayoutPanel1.Controls.Add(l5);
            tableLayoutPanel1.Controls.Add(lp5);
            tableLayoutPanel1.Controls.Add(lw5);

            //wynik roczny
            Label rl1 = new Label();
            rl1.Text = "BUDŻET";
            Label rlw1 = new Label();
            rlw1.TextAlign = ContentAlignment.MiddleCenter;                      
            rlw1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_W_BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            Label rlp1 = new Label();
            rlp1.TextAlign = ContentAlignment.MiddleCenter;                      
            rlp1.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_BUDZET", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "0", "C2"));
            tableLayoutPanel2.Controls.Add(rl1);
            tableLayoutPanel2.Controls.Add(rlp1);
            tableLayoutPanel2.Controls.Add(rlw1);

            Label rl2 = new Label();
            rl2.Text = "SPOTKANIA";
            Label rlw2 = new Label();
            rlw2.TextAlign = ContentAlignment.MiddleCenter;                      
            rlw2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_W_SPOTKANIA"));
            Label rlp2 = new Label();
            rlp2.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_SPOTKANIA"));
            rlp2.TextAlign = ContentAlignment.MiddleCenter;                      
            tableLayoutPanel2.Controls.Add(rl2);
            tableLayoutPanel2.Controls.Add(rlp2);
            tableLayoutPanel2.Controls.Add(rlw2);

            Label rl3 = new Label();
            rl3.Text = "KONTAKTY";
            Label rlw3 = new Label();
            rlw3.TextAlign = ContentAlignment.MiddleCenter;       
            rlw3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_W_KONTAKTY"));
            Label rlp3 = new Label();
            rlp3.TextAlign = ContentAlignment.MiddleCenter;                      
            rlp3.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_KONTAKTY"));
            tableLayoutPanel2.Controls.Add(rl3);
            tableLayoutPanel2.Controls.Add(rlp3);
            tableLayoutPanel2.Controls.Add(rlw3);

            Label rl4 = new Label();
            rl4.Text = "NOWI KLIENCI";
            Label rlw4 = new Label();
            rlw4.TextAlign = ContentAlignment.MiddleCenter;                      
            rlw4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_W_N_KLIENCI"));
            Label rlp4 = new Label();
            rlp4.TextAlign = ContentAlignment.MiddleCenter;                      
            rlp4.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_N_KLIENCI"));
            tableLayoutPanel2.Controls.Add(rl4);
            tableLayoutPanel2.Controls.Add(rlp4);
            tableLayoutPanel2.Controls.Add(rlw4);

            Label rl5 = new Label();
            rl5.Text = "ILOŚĆ REKLAM";
            Label rlw5 = new Label();
            rlw5.TextAlign = ContentAlignment.MiddleCenter;                      
            rlw5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_W_REKLAMY"));            
            Label rlp5 = new Label();
            rlp5.TextAlign = ContentAlignment.MiddleCenter;                     
            rlp5.DataBindings.Add(new Binding("Text", rEKLAMADataSet.View_Podsumowanie_Wykonania_Planu_Agencja, "R_REKLAMY"));
            tableLayoutPanel2.Controls.Add(rl5);
            tableLayoutPanel2.Controls.Add(rlp5);
            tableLayoutPanel2.Controls.Add(rlw5);               
        }

        private void kryteriumWyszukiwania_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                szukajToolStripButton.PerformClick();
            }
        }

        private void StartForm_Shown(object sender, EventArgs e)
        {
           // MessageBox.Show("ddd");

            if (User.getUser().St_kierownik)
            {
                agencjaToolStripComboBox.Text = User.getUser().SymbolAgencji;
            }
            else if (User.getUser().St_handlowiec | User.getUser().St_subhandlowiec)
            {
                agentToolStripComboBox.Text = User.getUser().Login;
            }

            pierwszaAktywacja = true;
            
            wypelnijShedulerWyniki();
        }

        private void wypelnijShedulerWyniki()
        {          

            if (User.getUser().St_kierownik)
            {
                shedulerTableAdapter.FillByAgencjaWgDaty(rEKLAMADataSet.sheduler, User.getUser().IdAgencji);
            }
            else
            {
                shedulerTableAdapter.FillByAgentWgDaty(rEKLAMADataSet.sheduler, User.getUser().Login);
            }

            if (pierwszaAktywacja) return;
            
            if (User.getUser().St_kierownik)
            {
                wynikAgencji();
            }
            else if (User.getUser().St_handlowiec | User.getUser().St_subhandlowiec)
            {
                wynikAgenta();
            }
        }
    }
}