using System;
using System.Windows.Forms;

namespace Baza_Reklam
{
    public partial class AddClient : Form
    {
        private readonly int _clientId;
        private REKLAMADataSet.KLIENCIRow _client;

        protected REKLAMADataSet.KLIENCIRow Client
        {
            get { return _client; }
        }

        public AddClient(BindingSource bindingSource)
        {
            InitializeComponent();
            
            clientsBindingSource = bindingSource;
        }

        public AddClient(int clientId, BindingSource bindingSource)
        {
            InitializeComponent();
            
            _clientId = clientId;
            clientsBindingSource = bindingSource;
        }

        private void AddClient_Load(object sender, EventArgs e)
        {
            LoadTitleList();

            agenciTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            clientsAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            agenciTableAdapter.Fill(sLOWNIKDataSet.AGENCI);

            if (_clientId != 0)
            {
                clientsAdapter.FillByCustomerId(rEKLAMADataSet.KLIENCI, _clientId);
                _client = (REKLAMADataSet.KLIENCIRow)rEKLAMADataSet.KLIENCI.Rows[0];
            }
            else
            {
                _client = rEKLAMADataSet.KLIENCI.NewKLIENCIRow();
                _client.Last_Modify = DateTime.Now;
                _client.data = DateTime.Now;
                _client.UserName = User.getUser().Login;
                _client.AgentExpo = User.getUser().Login;
                rEKLAMADataSet.KLIENCI.AddKLIENCIRow(_client);
                clientsBindingSource.MoveLast();
            }
                
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //TODO: refresh parent clients grid
            //ClientsForm.getClientsForm().
            clientsBindingSource.EndEdit();
            clientsAdapter.Update(rEKLAMADataSet.KLIENCI);
            MessageBox.Show("Klient dodany: " + _client.CustomerID);
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            rEKLAMADataSet.KLIENCI.RejectChanges();
            Close();
        }

        private void btnCopyAddress_Click(object sender, EventArgs e)
        {
            string[] adres = new string[4];

            adres[0] = txtName1.Text;
            adres[1] = txtAddress.Text;
            adres[2] = txtPostCode.Text + " " + txtCity.Text;
            adres[3] = cbState.Text + " " + cbCountry.Text;

            txtInvoiceAddress.Lines = adres;
            txtCorespondenceAddress.Lines = adres;
        }

        private void LoadTitleList()
        {
            SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter titleListAdapter = new SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter();
            titleListAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            lbTitles.DataSource = titleListAdapter.GetData();
            lbTitles.DisplayMember = "SYMB";
            lbTitles.ValueMember = "SYMB";
        }
    }
}