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 frmFindWarranty : GUI.FormBases.frmBase
    {
        private string EsquemaC;

        public frmFindWarranty()
        {
            InitializeComponent();
            EsquemaC = Utils.Helpers.ObjToStr(GUI.CApp.GetParam("NDEC"));
            GUI.CApp.TraducirForm(this);
        }

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

        public string Factura
        {
            get 
            {
                if (gridView1.RowCount > 0)
                {
                    return gridView1.GetFocusedRowCellValue(colNO_DOCUMENTO).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();

                //serie like '%&&BUSQUEDA&&%'
                //or     garantia like '%&&BUSQUEDA&&%'
                //or    socio_alterno like '%&&BUSQUEDA&&%'
                //or    nombre like '%&&BUSQUEDA&&%'
                //or    nit like '%&&BUSQUEDA&&%' 
                //or    no_documento like '%&&BUSQUEDA&&%'
                //or    serie like '%&&BUSQUEDA&&%'
                //or    sku_alterno like '%&&BUSQUEDA&&%'
                //or    descripcion like '%&&BUSQUEDA&&%')

                string sColumna = string.Empty;

                switch (cboColumnas.Text)
                {
                    case "No. Serie":
                        sColumna = "SERIE";
                        break;
                    case "Garantía":
                        sColumna = "GARANTIA";
                        break;
                    case "Placa":
                        sColumna = "PLACA";
                        break;
                    case "Serie":
                        sColumna = "SERIE_DOCUMENTO";
                        break;
                    case "Factura":
                        sColumna = "NO_DOCUMENTO";
                        break;
                    case "Cliente":
                        sColumna = "SOCIO_ALTERNO";
                        break;
                    case "NIT":
                        sColumna = "NIT";
                        break;
                    case "Nombre":
                        sColumna = "NOMBRE";
                        break;
                    default:
                        sColumna = "SERIE";
                        break;
                }

                qp.AddReplaceParam("COLUMNA", sColumna);

                if (GUI.CApp.GetParam("HBMM").ToString() == "X")
                {
                    qp.AddReplaceParam("BUSQUEDA", "%" + txtSearch.Text.ToUpper() + "%");
                }
                else
                {
                    qp.AddReplaceParam("BUSQUEDA", "%" + txtSearch.Text + "%");
                }

                dsGarantias1.TDA_V_CONSULTA_GARANTIAS.Clear();

                if (EsquemaC != string.Empty)
                {
                    Utils.Helpers.Merge(GUI.CApp.bll.getConnTypedDataSet(dsGarantias1.GetType(), dsGarantias1.TDA_V_CONSULTA_GARANTIAS.TableName, EsquemaC, qp, "BusquedaGarantias"), dsGarantias1);
                }
                else
                {
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGarantias1.GetType(), dsGarantias1.TDA_V_CONSULTA_GARANTIAS.TableName, qp, "BusquedaGarantias"), dsGarantias1);
                }

                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)
        {
            DialogResult = DialogResult.OK;
        }

        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 frmFindInvoice_KeyDown(object sender, KeyEventArgs e)
        {
            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;
            }

        }
    }
}