using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hukpiWinApp
{
    public partial class SearchDepotForm : BaseForm
    {
        private decimal PartnerId;
        private bool isSearchWithPartnerId = false;
        
        public SearchDepotForm()
        {
            InitializeComponent();

            this.Text = "Felrakóhely keresése";
        }

        public SearchDepotForm(decimal PartnerId)
        {
            InitializeComponent();

            this.PartnerId = PartnerId;
            this.isSearchWithPartnerId = true;

            this.Text = "Felrakóhely keresése";

        }


        private void buttonSearchStart_Click(object sender, EventArgs e)
        {
            if (isSearchWithPartnerId)
            {
                FillDataGridWithSearchResult(this.PartnerId, "%" + textBoxSearchDepotCode.Text + "%", "%" + textBoxSearchAddress.Text + "%");
            }
            else
            {
                FillDataGridWithSearchResult("%" + textBoxSearchDepotCode.Text + "%", "%" + textBoxSearchAddress.Text + "%");
            }
        }

        private void FillDataGridWithSearchResult(decimal PartnerId, string DepotCode, string DepotAddress)
        {
            try
            {
                this.depotDataViewTableAdapter.FillBySearchWithPartnerId(this.hukpiDatabaseDataSet.DepotDataView,DepotAddress,DepotCode,this.PartnerId);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void FillDataGridWithSearchResult(string DepotCode, string DepotAddress)
        {
            try
            {
                this.depotDataViewTableAdapter.FillBySearch(this.hukpiDatabaseDataSet.DepotDataView, DepotAddress, DepotCode);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void dataGridViewSearchResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DepotForm depotForm = new DepotForm(Convert.ToInt64(dataGridViewSearchResult.SelectedRows[0].Cells[0].Value));
            ((MainForm)this.MdiParent).openNewMDIChildForm(depotForm);
        }

        public override void refreshData()
        {
            buttonSearchStart_Click(null, null);
        }

        private void clearAll()
        {
            ((MainForm)MdiParent).openNewMDIChildForm(new SearchDepotForm());
            this.Dispose();
        }

        private void buttonSearchNew_Click(object sender, EventArgs e)
        {
            clearAll();
        }
    }
}
