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

namespace LLNet.Tienda.MainForms.Finders
{
    public partial class frmFindSku : GUI.FormBases.frmBase
    {
        public frmFindSku()
        {
            InitializeComponent();

            GUI.CApp.TraducirForm(this);
        }

        public string SkuAlterno
        {
            get 
            {
                if (gridView1.RowCount > 0)
                {
                    return gridView1.GetFocusedRowCellValue(colSKU_ALTERNO).ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        private void frmFindSku_Load(object sender, EventArgs e)
        {
            gridView1.BestFitColumns();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Enter)
            {
                Cursor oldCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                if (GUI.CApp.GetParam("HBMM").ToString() == "X")
                {
                    qp.AddReplaceParam("BUSQUEDA", "%" + Utils.Helpers.Nvl(txtSearch.EditValue,string.Empty).ToString().ToUpper() + "%");
                }
                else
                {
                    qp.AddReplaceParam("BUSQUEDA", "%" + Utils.Helpers.Nvl(txtSearch.EditValue, string.Empty).ToString() + "%");
                }
                dsPDV1.TDA_V_PRODUCTOS.Clear();
                Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsPDV1.GetType(), dsPDV1.TDA_V_PRODUCTOS.TableName, qp, "BusquedaProductos"), dsPDV1);

                gridView1.BestFitColumns();
                gridControl1.Focus();
                Cursor.Current = oldCursor;
            }

        }

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

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0)
                DialogResult = DialogResult.OK;
            else
                GUI.CApp.ShowMessage("No ha seleccionado ningún producto", "Busqueda de Productos", true);
            
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DialogResult = DialogResult.OK;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void frmFindSku_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Alt)
            {
                switch (e.KeyCode)
                {
                    case Keys.C:
                        btnConsultaP_Click(null, null);
                        break;
                }
            }
            else
            {
                switch (e.KeyCode)
                {
                    case Keys.F12:
                        DialogResult = DialogResult.OK;
                        break;
                    case Keys.Escape:
                        DialogResult = DialogResult.Cancel;
                        break;

                    case Keys.F2:
                        txtSearch.Focus();
                        txtSearch.SelectAll();
                        break;
                }
            }
        }

        private void btnConsultaP_Click(object sender, EventArgs e)
        {

            if (this.gridView1.RowCount > 0)
            {
                string Sku = this.gridView1.GetFocusedRowCellValue(colSKU_ALTERNO).ToString();
                MainForms.frmPricesConsole fp = new LLNet.Tienda.MainForms.frmPricesConsole();
                fp.Init(new object[] { 1, Sku });
                fp.ShowDialog(this);
            }
                  
        }

        private void gridControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
                txtSearch.Focus(); 
        }
    }
}