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;

namespace Baza_Reklam
{
    public partial class OcenaAddForm : Form
    {
        public OcenaAddForm()
        {
            InitializeComponent();

            this.agenciTableAdapter1.Connection.ConnectionString = ConnString.getConnString().Value;
            this.aGENCJETableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
        }

        private void OcenaAddForm_Load(object sender, EventArgs e)
        {
            this.aGENCJETableAdapter.Fill(this.sLOWNIKDataSet.AGENCJE);
            comboBox1.SelectedValue = User.Instance().IdAgencji;

            numericUpDown1.Value = DateTime.Today.Month;

            numericUpDown2.Minimum = DateTime.Today.Year - 1;
            numericUpDown2.Maximum = DateTime.Today.Year + 2;
            numericUpDown2.Value = DateTime.Today.Year;       
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                checkedListBox1.DataSource = this.agenciTableAdapter1.GetDataByIdAgencjiAktywni(Convert.ToInt32(comboBox1.SelectedValue));
                checkedListBox1.DisplayMember = "Symbol";
                checkedListBox1.ValueMember = "ID_Agenta";

                for (int i = 0; i <= checkedListBox1.Items.Count-1; i++)
                {
                    checkedListBox1.SetItemChecked(i, true);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for(int i=0 ; i<= checkedListBox1.Items.Count-1; i++)
            {
                if (!checkedListBox1.CheckedIndices.Contains(i))
                {
                    checkedListBox1.SetItemChecked(i, true);
                }
                else
                {
                    checkedListBox1.SetItemChecked(i, false);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.CheckedItems.Count != 0)
            {
                SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);

                SqlCommand command = new SqlCommand();
                command.Connection = conn;

                command.CommandText = "INSERT INTO [PLAN] (SYMBOL, ROK, MS, BUDZET, SPOTKANIA, KONTAKTY, N_KLIENCI, KLIENCI," +
                " REKLAMY, WIEDZA_O_FIRMIE, WIEDZA_O_PRODUKCIE, WIEDZA_O_RYNKU, WIEDZA_OGOLNO_HANDLOWA, JAKOSC_ROZMOWY_TEL," +
                " JAKOSC_SPOTKAN, JAKOSC_KORESPONDENCJI, JAKOSC_OFERT, W_BUDZET, W_SPOTKANIA, W_TEL, W_EMAIL, W_LIST, W_FAX, " +
                " W_N_KLIENCI, W_KLIENCI, W_REKLAMY, EP, ESP, EPR, EPK, ECP, ELK, QO, WH, OCENA, PR_PROWIZJI) VALUES " +
                " (@SYMBOL,@ROK,@MS,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) ";

                command.Parameters.AddWithValue("@rok", numericUpDown2.Value);
                command.Parameters.AddWithValue("@ms", numericUpDown1.Value);

                foreach (object checkedItem in checkedListBox1.CheckedItems)
                {
                    command.Parameters.AddWithValue("@SYMBOL", ((DataRowView)checkedItem)["symbol"]);
                    conn.Open();
                    command.ExecuteNonQuery();
                    conn.Close();
                    command.Parameters.RemoveAt("@SYMBOL");
                }

                MessageBox.Show("Rekordy zostały dodane");
            }
            else
            {
                MessageBox.Show("Nie zaznaczono żadnego agenta");           
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}