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 AgentAddForm : Form
    {

       // bool nowyRekord;

        string starySymbol  = string.Empty;

        public  AgentAddForm(int idAgenta, int IdAgencji)
        {
            InitializeComponent();

            this.aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            this.pLANY_PROWIZJITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            this.pLANY_PROWIZJITableAdapter.Fill(this.sLOWNIKDataSet.PLANY_PROWIZJI);

            if (idAgenta == 0)
            {
                DataView table = (DataView)aGENCIBindingSource.List;
                DataRowView row = table.AddNew();
                row["id_agencji"] = IdAgencji;
               // row["f_ROZ"] = "K";
                aGENCIBindingSource.EndEdit();

             //   nowyRekord = true;
            }
            else
            {
                this.aGENCITableAdapter.FillByIdAgenta(this.sLOWNIKDataSet.AGENCI, idAgenta);
                
                starySymbol = this.sLOWNIKDataSet.AGENCI[0].Symbol;
            
            }
        }

        private void ZapiszButton_Click(object sender, EventArgs e)
        {
           //raise f_ROZTextBox.Validating;

            f_ROZTextBox.Focus();

            if (Validate())
            {
                this.aGENCIBindingSource.EndEdit();
                this.aGENCITableAdapter.Update(this.sLOWNIKDataSet.AGENCI);
                MessageBox.Show("Zmiany zosta³y zapisane");
                this.DialogResult = DialogResult.OK;
            }
        }


        private void AnulujButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       ErrorProvider errProvider = new ErrorProvider();

      private void f_ROZTextBox_Validating(object sender, CancelEventArgs e)
      {
          DataRowView row = (DataRowView)aGENCIBindingSource.Current;
          
          if (f_ROZTextBox.Text.Length != 3)
          {
              e.Cancel = true;

              errProvider.SetError((Control)sender, "Kod powienien mieæ 3 znaki.");
              return;
          }

          if (starySymbol != string.Empty)
          {
              if (!Utils.tagAgentaUnikalny(f_ROZTextBox.Text, starySymbol))
              {
                  e.Cancel = true;

                  errProvider.SetError((Control)sender, "Kod powienien byæ unikalny.");
                  return;
              }
          }
          else
          {
              if (!Utils.tagAgentaUnikalny(f_ROZTextBox.Text, symbolTextBox1.Text))
              {
                  e.Cancel = true;

                  errProvider.SetError((Control)sender, "Kod powienien byæ unikalny.");
                  return;
              }
          }

          errProvider.SetError((Control)sender, "");
        
      }

        private void AgentAddForm_Load(object sender, EventArgs e)
        {
        }

    }
}