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 AdminForm : Form
    {

        private static AdminForm _adminForm;


        public static AdminForm getAdminForm(MDIBazaReklam parent)
        {
            if (_adminForm == null)
            {
                _adminForm = new AdminForm(parent);
            }
            return _adminForm;
        }

        private ErrorProvider errProvider;

        private AdminForm(MDIBazaReklam parent)
        {
            InitializeComponent();

            this.MdiParent = parent;

            errProvider = new ErrorProvider();

            uZYTKOWNICYTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
        }

        private void AdminForm_Load(object sender, EventArgs e)
        {
            this.uZYTKOWNICYTableAdapter.Fill(this.sLOWNIKDataSet.UZYTKOWNICY);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (Validate())
            {
                uZYTKOWNICYBindingSource.EndEdit();
                uZYTKOWNICYTableAdapter.Update(this.sLOWNIKDataSet.UZYTKOWNICY);
                MessageBox.Show("Zmiany zapisane.");
            }
        }

        private void lOGINTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (lOGINTextBox.Text == "")
            {
                e.Cancel = true;
                errProvider.SetError((Control)sender, "Pole nie może być puste.");
                return;               
            }
            errProvider.SetError((Control)sender, "");         
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.lOGINTextBox.Select();
        }

        private void usersTabPage_Leave(object sender, EventArgs e)
        {
            this.uZYTKOWNICYBindingSource.EndEdit();

            SLOWNIKDataSet.UZYTKOWNICYDataTable changes
            = this.sLOWNIKDataSet.UZYTKOWNICY.GetChanges(
            DataRowState.Added | DataRowState.Modified
            ) as SLOWNIKDataSet.UZYTKOWNICYDataTable;
            
            if (changes != null)
            {
                if (MessageBox.Show("Czy zapisać zmiany?", "", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.uZYTKOWNICYTableAdapter.Update(this.sLOWNIKDataSet.UZYTKOWNICY);
                }
                else
                {
                    this.sLOWNIKDataSet.UZYTKOWNICY.RejectChanges();
                    this.usersTabPage.Select();
                }
            }          
        }       
    }
}