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;
using BazaZamowien.Classes;

namespace BazaZamowien
{
    public partial class premiaForm : Form
    {
        bool nowa =false;
        Premia premia;

        public premiaForm(int id)
        {
            InitializeComponent();

            this.pREMIE2TableAdapter.Connection.ConnectionString = ConnString.getConnString().PremieConnStr;

            PremieDBBindings.bindujDzialy(dZIALComboBox,User.getUser().PremieGrupa);
            PremieDBBindings.bindujPracownikow(nAMEComboBox, User.getUser().PremieGrupa);
            
            int rok = 1997;
            while (rok != DateTime.Today.Year +1)
            {
                rOKComboBox.Items.Add(rok);
                rok++;
            }
            
            if (id == 0)
            {
                nowa = true;

                DataView table = (DataView)pREMIE2BindingSource.List;
                DataRowView row = table.AddNew();

                row["ROK"] = DateTime.Today.Year;
                row["MIESIAC"] = DateTime.Today.Month;
                row["data_wprowadzenia"] = DateTime.Now;
                row["wprowadzil"] = User.getUser().Symbol.ToLower();

                pREMIE2BindingSource.EndEdit();
            }
            else
            {
                this.pREMIE2TableAdapter.FillByID(this.pREMIEDataSet.PREMIE2, id);
                dodajButton.Text = "Zapisz";
            }


            premia = new Premia();

            this.nAMEComboBox.SelectedIndexChanged += new System.EventHandler(this.nAMEComboBox_SelectedIndexChanged);
         
        }

        private void anulujButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dodajButton_Click(object sender, EventArgs e)
        {
            pREMIE2BindingSource.EndEdit();

            DataRowView row = (DataRowView)pREMIE2BindingSource.Current;

            if (nowa)
            {
                if (nAMEComboBox.SelectedItem == null)
                {
                    MessageBox.Show("Wybierz pracownika.");
                    return;
                }

                if (dZIALComboBox.SelectedItem == null)
                {
                    MessageBox.Show("Wybierz dział.");
                    return;
                }

                if (rOKComboBox.SelectedItem == null)
                {
                    MessageBox.Show("Wybierz rok.");
                    return;
                }

                if (mIESIACComboBox.SelectedItem == null)
                {
                    MessageBox.Show("Wybierz miesiąc.");
                    return;
                }

                row["name"] = nAMEComboBox.Text;
            }

            DataTable changes = pREMIEDataSet.PREMIE2.GetChanges();

            if (changes != null)
            {
                row["data_modyfikacji"] = nowa ? row["data_wprowadzenia"] : DateTime.Now;
                row["zmodyfikowal"] = User.getUser().Symbol.ToLower();
                pREMIE2BindingSource.EndEdit();
                pREMIE2TableAdapter.Update(this.pREMIEDataSet);
                MessageBox.Show("Zmiany zostały zapisane.");
            }
                       
            this.DialogResult = DialogResult.OK;
        }

        private void bRUTTOTextBox_Leave(object sender, EventArgs e)
        {
            pREMIE2BindingSource.EndEdit();
            DataRowView row = (DataRowView)pREMIE2BindingSource.Current;
            
            decimal result;
            if (Decimal.TryParse(row["brutto"].ToString(), out result))
            {
                row["netto"] = premia.BruttoToNetto(result);
                pREMIE2BindingSource.EndEdit();    
            }
        }

        private void nETTOTextBox_Leave(object sender, EventArgs e)
        {           
            pREMIE2BindingSource.EndEdit();
            DataRowView row = (DataRowView)pREMIE2BindingSource.Current;

            decimal result;
            if (Decimal.TryParse(row["netto"].ToString(), out result))
            {
                row["brutto"] = premia.NettoToBrutto(result);
                pREMIE2BindingSource.EndEdit();          
            }
        }

        private void IloscLubWycena_Leave(object sender, EventArgs e)
        {
            pREMIE2BindingSource.EndEdit();
            DataRowView row = (DataRowView)pREMIE2BindingSource.Current;

            decimal result1;
            int result2;

            if (Decimal.TryParse(row["wycena"].ToString(), out result1) && (Int32.TryParse(row["il"].ToString(),out result2)))
            {
                decimal netto = (Math.Round(result1, 2) * result2);
                row["netto"] = netto.ToString();
                row["brutto"] = premia.NettoToBrutto(netto).ToString();
            }
        }

        private void nAMEComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView row = (DataRowView)pREMIE2BindingSource.Current;
            row["IDEPracownika"] = ((BoundItem)nAMEComboBox.SelectedItem).IDEvalue1;
            pREMIE2BindingSource.EndEdit();            
        }


    }
}