using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using Baza_Reklam.Classes.Helpers;
using Baza_Reklam.Classes.Interfaces;

namespace Baza_Reklam
{
    public partial class ListaReklamNaWydanieForm : Form, IForm
    {
        private static ListaReklamNaWydanieForm lrnwForm;

        public static ListaReklamNaWydanieForm getListaReklamNaWydanieForm(MDIBazaReklam parent)
        {
            if (lrnwForm == null)
            {
                lrnwForm = new ListaReklamNaWydanieForm(parent);
            }
            return lrnwForm;        
        }

        private ListaReklamNaWydanieForm(MDIBazaReklam parent)
        {
            InitializeComponent();

            MdiParent = parent;
            listaReklamNaWydanie2TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
         
            bindingNavigator1.Items.Insert(17, new ToolStripLabel("DR"));
            bindingNavigator1.Items.Insert(18, new ToolStripControlHost(new CheckBox(), "DRCheckBox"));
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).Checked = true;

            bindingNavigator1.Items.Insert(19, new ToolStripLabel("A"));
            bindingNavigator1.Items.Insert(20, new ToolStripControlHost(new CheckBox(), "AktywneCheckBox"));
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["AktywneCheckBox"]).Control).Checked = true;
       
            bindingNavigator1.Items.Insert(21, new ToolStripLabel("W"));
            bindingNavigator1.Items.Insert(22, new ToolStripControlHost(new CheckBox(), "WyrCheckBox"));
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["WyrCheckBox"]).Control).Checked = false;

        }

        private void ListaReklamNaWydanieForm_Load(object sender, EventArgs e)
        {
            DBBindings.bindujTytuly(tytulToolStripComboBox);
            DBBindings.bindujTytuly(tytToolStripComboBox2);
            DBBindings.bindujGrzbiety2(grzbietToolStripComboBox);
            DBBindings.bindujAgencje(agencjaToolStripComboBox);
            DBBindings.bindujAgentow(agentToolStripComboBox);
            DBBindings.bindujModuly(modToolStripComboBox);
            
            ((ComboBox)tytToolStripComboBox2.Control).SelectedValueChanged += tytToolStripComboBox2_SelectedIndexChanged;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE.Clear();

            if (tytulToolStripComboBox.Text.Trim() == "")
            {
                MessageBox.Show("Wybierz tytuł.");
                return;
            }

            if (nrToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(nrToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłowy nr wydania.");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Wybierz nr wydania.");
                return;
            }

            Cursor = Cursors.WaitCursor;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConnString.getConnString().Value;

            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT TOP 1000 * FROM LISTA_REKLAM_NA_WYDANIE " +
                        " WHERE (TYTUŁ = @tytul) AND (NR = @nrwydania) ";
                       

            command.Parameters.AddWithValue("@tytul", tytulToolStripComboBox.Text.Trim());
            command.Parameters.AddWithValue("@nrwydania", nrToolStripTextBox.Text.Trim());

            if (((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).CheckState == CheckState.Checked)
            {
                command.CommandText += " AND ([ZATWIERDZONO DO DRUKU] = 1) ";
            }

            if (((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["AktywneCheckBox"]).Control).CheckState == CheckState.Checked)
            {
                command.CommandText += " AND ([status] <> 2) ";
            }

            if (((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).CheckState == CheckState.Checked)
            {
                command.CommandText += " AND ([ZATWIERDZONO DO DRUKU] = 1) ";
            }

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

            if (agentToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND ([symbol akwizytora] = @agent) ";
                command.Parameters.AddWithValue("@agent", agentToolStripComboBox.Text.Trim());
            }

            if (grzbietToolStripComboBox.SelectedItem != null)
            {
                command.CommandText += " AND (GRZBIET = @grzbiet) ";
                command.Parameters.AddWithValue("@grzbiet", ((BoundItem)grzbietToolStripComboBox.SelectedItem).IDEvalue1);
            }

          //  ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).Checked = true;

         

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;

            adapter.Fill(rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE);
            
            Cursor = Cursors.Default;
            
        }

        private void klientToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
                int idRek = Convert.ToInt32(row["ReklamaId"]);

                int custId = DbUtils.customerId(idRek);

                ClientsForm.getClientsForm((MDIBazaReklam)MdiParent).pokazKlienta(custId);

                Hide();

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

        private void zamToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
                int idRek = Convert.ToInt32(row["ReklamaId"]);
               // this.reklamaTableAdapter.FillByReklamaId(this.rEKLAMADataSet.REKLAMA, idRek);

                OrderDetails.getOrderDetails().pokazSzczegolyZamowienia(idRek);
                OrderDetails.getOrderDetails().ShowDialog();
                /*
                OrderDetails od = new OrderDetails();
                od.pokazSzczegolyZamowienia(idRek);
                od.ShowDialog(); */
            }     
        }

        private void tytToolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ComboBox)tytToolStripComboBox2.Control).SelectedItem != null)
            {
                ((BindingSource)((ComboBox)modToolStripComboBox.Control).DataSource).Filter = "tytul = '" + tytToolStripComboBox2.Control.Text + "'";
                modToolStripComboBox.SelectedIndex = -1;
                modToolStripComboBox.SelectedIndex = -1;
            }
        }


        private void wyczyscFiltrToolStripButton_Click(object sender, EventArgs e)
        {            
            tytulToolStripComboBox.Text = "";
            nrToolStripTextBox.Text = "";
            agencjaToolStripComboBox.SelectedIndex = -1;
            agencjaToolStripComboBox.Text = "";

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

            grzbietToolStripComboBox.SelectedIndex = -1;
            grzbietToolStripComboBox.Text = "";

            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["DRCheckBox"]).Control).CheckState = CheckState.Indeterminate;
            ((CheckBox)((ToolStripControlHost)bindingNavigator1.Items["WyrCheckBox"]).Control).CheckState = CheckState.Indeterminate;          

        }

        private void szukajRezerToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConnString.getConnString().Value;

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

            command.CommandText = "SELECT TOP 1000 * FROM LISTA_REKLAM_NA_WYDANIE WHERE 1=1 ";

            rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE.Clear();       

            command.Parameters.Clear();


            if (odToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(odToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłową wartość.");
                    Cursor = Cursors.Default;
                    return;
                }

                command.CommandText += " AND NR >=@od ";
                command.Parameters.AddWithValue("@od", odToolStripTextBox.Text.Trim());
            }


            if (doToolStripTextBox.Text.Trim() != "")
            {
                int i;
                if (!Int32.TryParse(doToolStripTextBox.Text.Trim(), out i))
                {
                    MessageBox.Show("Podaj prawidłową wartość.");
                    Cursor = Cursors.Default;
                    return;
                }

                command.CommandText += " AND NR <=@do ";
                command.Parameters.AddWithValue("@do", doToolStripTextBox.Text.Trim());
            }

            if (modToolStripComboBox.Text.Trim() != "")
            {
                command.CommandText += " AND MOD_TYP=@modul ";
                command.Parameters.AddWithValue("@modul", modToolStripComboBox.Text.Trim());
            }

            if (tytToolStripComboBox2.Text.Trim() != "")
            {
                command.CommandText += " AND TYTUŁ=@tyt ";
                command.Parameters.AddWithValue("@tyt", tytToolStripComboBox2.Text.Trim());

            }
          
            
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;

            adapter.Fill(rEKLAMADataSet.LISTA_REKLAM_NA_WYDANIE);
                        
            Cursor = Cursors.Default; 
        }

        private void wyczyscToolStripButton2_Click(object sender, EventArgs e)
        {
            odToolStripTextBox.Clear();
            doToolStripTextBox.Clear();
            ((BindingSource)((ComboBox)modToolStripComboBox.Control).DataSource).Filter = "";          
            modToolStripComboBox.SelectedIndex = -1;
            modToolStripComboBox.SelectedIndex = -1;
            modToolStripComboBox.Text = "";
            tytToolStripComboBox2.SelectedIndex = -1;
            tytToolStripComboBox2.SelectedIndex = -1;
            tytToolStripComboBox2.Text = "";
        }

        private void drukujToolStripButton_Click(object sender, EventArgs e)
        {
            PrintDGV.Print_DataGridView(listaReklamDataGridView,50);
        }

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

                int reklamaId = Convert.ToInt32(row["reklamaId"]);
                LogoWReklamieForm lwrf = new LogoWReklamieForm(reklamaId);

                lwrf.ShowDialog();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current != null)
            {
                DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
                int reklamaId = Convert.ToInt32(row["reklamaId"]);
                int custId = DbUtils.customerId(reklamaId);

                LogaKlientowForm logaForm = new LogaKlientowForm(custId);
                logaForm.ShowDialog();
            }
        }

        private void listaReklamDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.Reset)
            {
                foreach (DataGridViewRow r in listaReklamDataGridView.Rows)
                {
                    if (
                        !((REKLAMADataSet.LISTA_REKLAM_NA_WYDANIERow)
                        ((DataRowView)r.DataBoundItem).Row).ZATWIERDZONO_DO_DRUKU
                        ||
                        ((REKLAMADataSet.LISTA_REKLAM_NA_WYDANIERow)
                        ((DataRowView)r.DataBoundItem).Row).sTATUS == 2)
                    {

                        r.DefaultCellStyle.ForeColor = Color.Red;
                    }
                }
            }
        }

        private void podgladToolStripButton_Click(object sender, EventArgs e)
        {
            if (listaReklamNaWydanie2BindingSource.Current == null) return;

            DataRowView row = (DataRowView)listaReklamNaWydanie2BindingSource.Current;
            string symbolReklamy = row["id reklamy"].ToString();

            Cursor = Cursors.WaitCursor;
            try
            {
                PdfHelper pdfHelper = new PdfHelper(symbolReklamy);
                if (!pdfHelper.DisplayPdf())
                    MessageBox.Show(pdfHelper.ErrorMessage);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        public void CloseForm()
        {
            lrnwForm = null;
        }
    }
}