using System;
using System.Windows.Forms;

namespace Baza_Reklam
{
    public partial class PaymentForm2 : Form
    {
        private int idWplaty;

        public int IdWplaty
        {
            get { return idWplaty; }
            set { idWplaty = value; }
        }
        private readonly int idFaktury;

        public PaymentForm2(int idFaktury)
        {
            this.idFaktury = idFaktury;
            InitializeComponent();
        }

        public PaymentForm2(int idWplaty, int idFaktury)
        {
            this.idWplaty = idWplaty;
            this.idFaktury = idFaktury;
            InitializeComponent();
        }

        private void PaymentForm_Load(object sender, EventArgs e)
        {

            rEKLAMADataSet.Wplaty.Constraints.Remove("FAKTURY_Wplaty");

            wplatyTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            VIEW_ZESTAWIENIE_FAKTUR_NOWETableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            zestawienieFakturTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;

            if (idWplaty != 0)
            {
                wplatyTableAdapter.FillByIdWplaty(rEKLAMADataSet.Wplaty, idWplaty);
                VIEW_ZESTAWIENIE_FAKTUR_NOWETableAdapter.FillByIdFaktury(rEKLAMADataSet.VIEW_ZESTAWIENIE_FAKTUR_NOWE, idFaktury);
            }
            else
            {
                REKLAMADataSet.WplatyRow wplata = rEKLAMADataSet.Wplaty.FindByidWplaty(idWplaty);
                if (wplata == null || wplata.IsdataWplatyNull())
                {
                    //stara faktura
                    UstawBindowanieDlaStarychFaktur();

                    zestawienieFakturTableAdapter.FillByIdFaktury(rEKLAMADataSet.ZestawienieFaktur, idFaktury);
                }
            }

            //REKLAMADataSet.WplatyRow wplata = rEKLAMADataSet.Wplaty.FindByidWplaty(idWplaty);

            //if (wplata == null || wplata.IsdataWplatyNull())
            //{
            //    //stara faktura
            //    UstawBindowanieDlaStarychFaktur();

            //    zestawienieFakturTableAdapter.FillByIdFaktury(rEKLAMADataSet.ZestawienieFaktur, idFaktury);
            //}
            //else
            //{
            //    VIEW_ZESTAWIENIE_FAKTUR_NOWETableAdapter.FillByIdFaktury(rEKLAMADataSet.VIEW_ZESTAWIENIE_FAKTUR_NOWE, idFaktury);
            //}
        }

        private void UstawBindowanieDlaStarychFaktur()
        {
            VIEW_ZESTAWIENIE_FAKTUR_NOWEBindingSource.DataMember = "ZestawienieFaktur";

            iD_REKLAMYTextBox.DataBindings.Clear();
            iD_REKLAMYTextBox.DataBindings.Add(new Binding("Text", VIEW_ZESTAWIENIE_FAKTUR_NOWEBindingSource, "NR_FK", true));

            suma_zaplatTextBox.DataBindings.Clear();
            suma_zaplatTextBox.DataBindings.Add(new Binding("Text", VIEW_ZESTAWIENIE_FAKTUR_NOWEBindingSource, "suma_zaplat", true, DataSourceUpdateMode.OnValidation, null, "C2"));

            waluta_bruttoTextBox1.DataBindings.Clear();
            waluta_bruttoTextBox1.DataBindings.Add(new Binding("Text", VIEW_ZESTAWIENIE_FAKTUR_NOWEBindingSource, "waluta_brutto", true, DataSourceUpdateMode.OnValidation, null, "N2"));

            bRUTTOTextBox.DataBindings.Clear();
            bRUTTOTextBox.DataBindings.Add(new Binding("Text", VIEW_ZESTAWIENIE_FAKTUR_NOWEBindingSource, "BRUTTO", true, DataSourceUpdateMode.OnValidation, null, "C2"));
        }

        private void dodajButton_Click(object sender, EventArgs e)
        {
            /*
            if ((sYMBOL_POTWIERDZENIATextBox.Text == "") | (rODZAJ_POTWIERDZENIAComboBox.SelectedIndex == -1))
            {
                MessageBox.Show("Uzupełnij wszystkie pola");
                return;
            }
            
            this.Validate();

            this.wplatyBindingSource.EndEdit();
            this.wplatyTableAdapter.Update(this.rEKLAMADataSet.Wplaty);
            this.idWplaty = rEKLAMADataSet.Wplaty[0].idWplaty;
             */
            DialogResult = DialogResult.OK;
        }

        private void anulujButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}