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

namespace LLNet.Tienda.MainForms.Forms
{
    public partial class frmAppGarantias : DevExpress.XtraEditors.XtraForm
    {
        private decimal sku;
        private decimal linea;
        private string EsquemaC;
        
        public frmAppGarantias()
        {
            InitializeComponent();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (GUI.CApp.ValidateAccess("AppGarantias", "Aplicación de Garantías"))
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("DOCUMENTO", Program.FrmMainPos.Order.ToString());
                qp.AddReplaceParam("LINEA", linea.ToString());
                qp.AddReplaceParam("MONTO", gridView1.GetFocusedRowCellValue(colMONTO_RECONOCIMIENTO).ToString());
                qp.AddReplaceParam("USUARIO", GUI.CApp.usuario);
                qp.AddReplaceParam("GARANTIA", gridView1.GetFocusedRowCellValue(colGARANTIA).ToString());
                GUI.CApp.bll.ExecuteSql("tda_p_aplicar_garantia", qp, "PDV");
                Program.FrmMainPos.RefreshData();
                DialogResult = DialogResult.OK;
            }
        }

        public virtual void Init(object[] pArgs)
        {
            if (pArgs != null)
            {
                sku = Convert.ToDecimal(pArgs[0]);
                linea = Convert.ToDecimal(pArgs[1]);
            }
            EsquemaC = Utils.Helpers.ObjToStr(GUI.CApp.GetParam("NDEC"));
        }


        private void frmAppGarantias_Load(object sender, EventArgs e)
        {
        }

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

        private void txtFactura_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                if (Utils.Helpers.ObjToStr(merFactura.PropertiesCollection[0].Value) != string.Empty)
                {
                    qp.AddReplaceParam("SERIE", merFactura.PropertiesCollection[0].Value.ToString());
                    qp.AddReplaceParam("DOCUMENTO", Utils.Helpers.ObjToStr(((DevExpress.XtraEditors.TextEdit)sender).EditValue));
                    //qp.AddReplaceParam("SKU", sku.ToString());
                }

                if (EsquemaC != string.Empty)
                {
                    try
                    {
                        dsGarantias1.Merge(GUI.CApp.bll.getConnTypedDataSet(dsGarantias1.GetType(), dsGarantias1.TDA_V_GARANTIAS_X_FACTURA.TableName, EsquemaC, qp));
                    }
                    catch
                    {
                        dsGarantias1.Merge(GUI.CApp.bll.getTypedDataSet(dsGarantias1.GetType(), dsGarantias1.TDA_V_GARANTIAS_X_FACTURA.TableName, qp));
                    }

                }
                else
                {
                    dsGarantias1.Merge(GUI.CApp.bll.getTypedDataSet(dsGarantias1.GetType(), dsGarantias1.TDA_V_GARANTIAS_X_FACTURA.TableName, qp));
                }

                if (dsGarantias1.TDA_V_GARANTIAS_X_FACTURA.Count == 0)
                {
                    GUI.CApp.ShowMessage("La factura no existe, no contiene el producto o la garantía ya fue aplicada", "Aplicar Garantías",true);
                }
            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            using (Finders.frmFindWarranty fwar = new LLNet.Tienda.MainForms.Finders.frmFindWarranty())
            {
                fwar.ShowDialog();
                vGridControl1.Focus();
                vGridControl1.FocusedRow = merFactura;
                merFactura.PropertiesCollection[0].Value = fwar.Serie;
                merFactura.PropertiesCollection[1].Value = fwar.Factura;
            }
        }

        private void frmAppGarantias_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.F2:
                    btnBuscar_Click(null, null);
                    break;
                case Keys.F12:
                    btnAceptar_Click(null, null);
                    break;
                case Keys.Escape:
                    btnSalir_Click(null, null);
                    break;
            }
        }

    }
}