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.Dialogs.Offers
{
    public partial class dlgOfferNx1 : LLNet.GUI.FormBases.frmBase
    {
        #region Constructor

        public dlgOfferNx1()
        {
            InitializeComponent();

            GUI.CApp.TraducirForm(this);
        }

        #endregion

        private decimal linea;
        private decimal oferta;
        private decimal cantidad;
        private bool aceptarMixtos;
        private int cntX1;
        private string skuAlterno;

        public override void Init(object[] pArgs)
        {
            base.Init(pArgs);
            linea = Convert.ToDecimal(pArgs[0]);
            oferta = Convert.ToDecimal(pArgs[1]);
            skuAlterno = pArgs[2].ToString();
            cantidad = Convert.ToDecimal(pArgs[3]);

            btnUltimo.Text = string.Format(btnUltimo.Text, skuAlterno);
            
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("OFERTA", oferta.ToString());
            DataSet ds = GUI.CApp.bll.getDataSet("PDV", "GetOfferNx1", qp);
            DataRow drOferta = ds.Tables[0].Rows[0];
            cntX1 = Convert.ToInt32(drOferta["TIPO"]);
            aceptarMixtos = (drOferta["NO_ACEPTAR_MIXTOS"].ToString() != "X");

            if (!aceptarMixtos)
            {
                btnBuscar.Enabled = false;
            }

            qp.Clear();
            qp.AddReplaceParam("DOCUMENTO", Program.FrmMainPos.Order.ToString());
            qp.AddReplaceParam("LINEA", linea.ToString());

            dsPDV1.OfertadosNx1.Clear();
            Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsPDV1.GetType(), dsPDV1.OfertadosNx1.TableName, qp), dsPDV1);

        }

        #region Eventos

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("OFERTA", oferta.ToString());
            qp.AddReplaceParam("DOCUMENTO", Program.FrmMainPos.Order.ToString());
            qp.AddReplaceParam("LINEA", linea.ToString());
            qp.AddReplaceParam("OFERTADOS", (cntX1 > 4 && cntX1 < 12 ? cntX1 - 3 : 1).ToString());  //GUSTAVO 7
            //GUI.CApp.bll.ExecuteSql("ActualizarDescuentoNx1", qp, "PDV");
            GUI.CApp.bll.ExecuteSql("WMS_P_ActualizarDescuentoNx1", qp, "PDV");
            //GUI.CApp.bll.ExecuteSql("ActualizarImpuestoNx1", qp, "PDV");
            GUI.CApp.bll.ExecuteSql("WMS_P_ActualizarImpuestoNx1", qp, "PDV");
            Program.FrmMainPos.RefreshData();
            DialogResult = DialogResult.Cancel;
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("OFERTA", oferta.ToString());
            qp.AddReplaceParam("DOCUMENTO", Program.FrmMainPos.Order.ToString());
            qp.AddReplaceParam("LINEA", linea.ToString());
            qp.AddReplaceParam("OFERTADOS", (cntX1 > 4 && cntX1 < 12 ? cntX1 - 3 : 1).ToString());  //GUSTAVO 7
            //GUI.CApp.bll.ExecuteSql("ActualizarDescuentoNx1", qp, "PDV");
            GUI.CApp.bll.ExecuteSql("WMS_P_ActualizarDescuentoNx1", qp, "PDV");
            //GUI.CApp.bll.ExecuteSql("ActualizarImpuestoNx1", qp, "PDV");
            GUI.CApp.bll.ExecuteSql("WMS_P_ActualizarImpuestoNx1", qp, "PDV");
            Program.FrmMainPos.RefreshData();
            DialogResult = DialogResult.OK;

        }

        private void dlgOfferNx1_Load(object sender, EventArgs e)
        {
            vGridControl1.FocusedRow = rowSku;
            vGridControl1.Focus();
        }

        #endregion

        private void dlgOfferNx1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape :
                    btnCancelar_Click(null, null);
                    break;
                case Keys.F12 :
                    btnAceptar_Click(null, null);
                    break;
                case Keys.F2 :
                    break;
                case Keys.F3 :
                    btnUltimo_Click(null, null);
                    break;
            }
        }

        private void repSku_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!aceptarMixtos && ((DevExpress.XtraEditors.ButtonEdit)sender).Text != skuAlterno)
                {
                    GUI.CApp.ShowMessage(GUI.CApp.TraducirPalabra("La oferta solo permite ingresar el producto ") + skuAlterno, GUI.CApp.TraducirPalabra("Ofertas N x 1"),false);
                    vGridControl1.FocusedRow = rowSku;
                    vGridControl1.Focus();
                    ((DevExpress.XtraEditors.ButtonEdit)sender).Text = "";
                    vGridControl1.ShowEditor();

                }
                else
                    InsertLineNx1(sender);
            }
        }

        private void InsertLineNx1(object sender)
        {
            if (cntX1 > 4 && cntX1 < 12) //GUSTAVO 7
            {
                if (cntX1 - 2 == dsPDV1.OfertadosNx1.Count)
                {
                    GUI.CApp.ShowMessage("No puede seguir agregando más productos a la oferta", "Ofertas N x 1", true);
                    if (sender != null)
                        ((DevExpress.XtraEditors.ButtonEdit)sender).Text = "";
                    else
                        rowSku.Properties.Value = "";

                    return;
                }
            }

            if (cntX1 == dsPDV1.OfertadosNx1.Count)
            {
                GUI.CApp.ShowMessage("No puede seguir agregando más productos a la oferta", "Ofertas N x 1", true);
            }
            else
            {
                string sku;
                if (sender != null)
                    sku = ((DevExpress.XtraEditors.ButtonEdit)sender).Text;
                else
                    sku = skuAlterno;

                if (Program.FrmMainPos.pos.ctlAddSkus1.InsertOrderLine(sku,
                    cantidad, GUI.CApp.usuario, linea, oferta))
                {
                    Utils.QueryParams qp = new LLNet.Utils.QueryParams();

                    qp.AddReplaceParam("DOCUMENTO", Program.FrmMainPos.Order.ToString());
                    qp.AddReplaceParam("LINEA", linea.ToString());

                    dsPDV1.OfertadosNx1.Clear();
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsPDV1.GetType(), dsPDV1.OfertadosNx1.TableName, qp), dsPDV1);
                }
                else
                {
                    GUI.CApp.ShowMessage("El producto no existe o no es un producto ofertado", "Ofertas N x 1", true);
                    vGridControl1.FocusedRow = rowSku;
                    vGridControl1.Focus();
                    vGridControl1.ShowEditor();
                }
            }
            if (sender != null)
                ((DevExpress.XtraEditors.ButtonEdit)sender).Text = "";
            else
                rowSku.Properties.Value = "";
        }

        private void btnUltimo_Click(object sender, EventArgs e)
        {
            InsertLineNx1(null);
            vGridControl1.FocusedRow = rowSku;
            vGridControl1.Focus();
        }

    }
}

