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 REKLAMADataSetTableAdapters.KLIENCITableAdapter();

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

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

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

            procentProwizjiTextBox.DataBindings[0].Format += DecimalToProcent;
            procentProwizjiTextBox.DataBindings[0].Parse += 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();
                zamowieniaBindingSource.EndEdit();
            }
            else
            {
                zamowieniaTableAdapter.FillByIdZamowienia(this.rEKLAMADataSet.zamowienia, idZam);
                row = rEKLAMADataSet.zamowienia[0];
            }

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

            kierownikGroupBox.Enabled = User.getUser().St_kierownik;

            List<BoundItem> list = new List<BoundItem>();
            list.Add(new BoundItem(7, "7 dni"));
            list.Add(new BoundItem(14, "14 dni"));
            list.Add(new BoundItem(21, "21 dni"));
            list.Add(new BoundItem(30, "30 dni"));
            if (User.getUser().St_kierownik || row.czasPlatnosci == 60)
                list.Add(new BoundItem(60, "60 dni"));
            czasPlatnosciListBox.DisplayMember = "DisplayValue";
            czasPlatnosciListBox.ValueMember = "IDEvalue1";
            czasPlatnosciListBox.DataSource = list;
        }
 
        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();
        }
    }
}