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

namespace Baza_Reklam
{
    public partial class AddZamowienieForm : Form
    {
        private REKLAMADataSetTableAdapters.KLIENCITableAdapter klienciTableAdapter =
            new Baza_Reklam.REKLAMADataSetTableAdapters.KLIENCITableAdapter();

        public REKLAMADataSet.zamowieniaDataTable Zamowienia
        {
            get
            {
                return this.rEKLAMADataSet.zamowienia;
            }
        }

        public AddZamowienieForm(int idZam, int idKlienta)
        {
            InitializeComponent();

            this.zamowieniaTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            this.kONTATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            this.aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            this.klienciTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;

            List<BoundItem> list = new List<BoundItem>();
            list.Add(new BoundItem(7,"7 dni"));
            list.Add(new BoundItem(14,"14 dni"));
            list.Add(new BoundItem(30,"30 dni"));
            czasPlatnosciListBox.DisplayMember = "DisplayValue";
            czasPlatnosciListBox.ValueMember = "IDEvalue1";
            czasPlatnosciListBox.DataSource = list;

            procentProwizjiTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            procentProwizjiTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);

            //rABATTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
            //rABATTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);

            REKLAMADataSet.zamowieniaRow row;
            if (idZam == 0)
            {
                DataView table = (DataView)zamowieniaBindingSource.List;
                DataRowView r = table.AddNew();
                row = (REKLAMADataSet.zamowieniaRow)r.Row;

                row.nrZamowienia =  Utils.numerNowegoZamowienia(idKlienta, DateTime.Today.Year);
                row.rokZamowienia = DateTime.Today.Year;
                row.kodAgenta = User.getUser().Kod_agenta;
                row.idKlienta = idKlienta;

                row.EndEdit();
                this.zamowieniaBindingSource.EndEdit();
            }
            else
            {
                this.zamowieniaTableAdapter.FillByIdZamowienia(this.rEKLAMADataSet.zamowienia, idZam);
                row = this.rEKLAMADataSet.zamowienia[0];
            }

            REKLAMADataSet.KLIENCIRow k = (REKLAMADataSet.KLIENCIRow)klienciTableAdapter.GetDataByCustomerId(row.idKlienta)[0];
            numerZamowieniaLabel1.Text = k.kodKlienta + "/" + row.rokZamowienia + "/" + row.kodAgenta + "/" + row.nrZamowienia;

            kierownikGroupBox.Enabled = User.getUser().St_kierownik;
        }
 
        private void AddZamowienieForm_Load(object sender, EventArgs e)
        {
            string agent = ((DataRowView)zamowieniaBindingSource.Current)["kodAgenta"].ToString();
            int idAgencji = Utils.idAgencjiWgKoduAgenta(agent);          
  
            this.kONTATableAdapter.FillByIdAgencji(this.sLOWNIKDataSet.KONTA, idAgencji);
            this.aGENCITableAdapter.FillByAktywny(this.sLOWNIKDataSet.AGENCI);

            this.zamowieniaBindingSource.ResetBindings(false);
        }

        private void anulujButton_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (kONTABindingSource.Current != null)
            {
                DataRowView row = (DataRowView)kONTABindingSource.Current;
                MessageBox.Show(row["konto"].ToString());
            }
        }

        private void zapiszButton_Click(object sender, EventArgs e)
        {
            this.zamowieniaBindingSource.EndEdit();
            zamowieniaTableAdapter.Update(this.rEKLAMADataSet.zamowienia);
        }

        /// <summary>
        /// formatowanie do decimal -> %
        /// </summary>
        private void DecimalToProcent(object sender, ConvertEventArgs cevent)
        {
            if (Convert.ToDecimal(cevent.Value) > 1)
            {
                //  cevent.Value = Convert.ToDecimal(cevent.Value) / 100;
            }
            cevent.Value = String.Format("{0:P}", cevent.Value);
        }

        /// <summary>
        /// formatowanie do % -> decimal
        /// </summary>
        private void ProcentToDecimal(object sender, ConvertEventArgs cevent)
        {
            string pom = cevent.Value.ToString();
            if (cevent.Value.ToString().EndsWith("%"))
            {
                pom = cevent.Value.ToString().Substring(0, cevent.Value.ToString().Length - 1);
            }
            decimal p = Decimal.Parse(pom) / 100;
            cevent.Value = p.ToString();
        }

        private void procentProwizjiTextBox_TextChanged(object sender, EventArgs e)
        {
            this.zamowieniaBindingSource.EndEdit();
        }
    }
}