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

namespace LLNet.Tienda.MainForms.Forms
{
    public partial class FrmDonacion : LLNet.GUI.FormBases.frmBase
    {
        private char tipdonativ = 'M';
        public decimal monto = 0;
        
        public FrmDonacion()
        {
            InitializeComponent();
            lblmonto.Visible = true;
            tipdonativ = 'M';
            btnvuelto.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            btnMonto.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
            btnMonto.Appearance.BackColor = Color.FromKnownColor(KnownColor.Yellow);
            calcEditMonto.Focus();
        }
        
        private void btnCancelar_Click(object sender, EventArgs e)
        {
            monto = 0;
            DialogResult = DialogResult.Cancel;
        }

        private void btnMonto_Click(object sender, EventArgs e)
        {
            lblmonto.Visible = true;
            tipdonativ = 'M';
            btnvuelto.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            btnMonto.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
            btnMonto.Appearance.BackColor = Color.FromKnownColor(KnownColor.Yellow);
            calcEditMonto.Focus();
        }

        private void btnvuelto_Click(object sender, EventArgs e)
        {
            lblmonto.Visible = false;
            tipdonativ = 'V';
            btnMonto.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            btnvuelto.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
            btnvuelto.Appearance.BackColor = Color.FromKnownColor(KnownColor.Yellow);
            calcEditMonto.Focus();
        }

      
        private void btnAceptar_Click(object sender, EventArgs e)
        {
            //elimina la donacion anterior
            int linea = 0;
            int cont = Program.FrmMainPos.DsPedido.WMS_DOCUMENTOS.Count;
            if (cont > 0)
            {
                string doc = Program.FrmMainPos.DsPedido.WMS_DOCUMENTOS[0].DOCUMENTO.ToString();
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("pDocumento", doc);
                linea = int.Parse(Helpers.Nvl(GUI.CApp.bll.GetSingleValue("LINEA_DONACION", qp, "PDV"), "0").ToString());
                if (linea > 0)
                {
                    Program.FrmMainPos.DeleteOrderLine(linea);
                }
            }
            //Actualiza el monto a donar
            if (tipdonativ == 'M')
            {
                monto = calcEditMonto.Value;
            }
            else
            {

                decimal saldo = Decimal.Parse(Program.FrmMainPos.DsPedido.WMS_DOCUMENTOS[0].TOTAL.ToString());
                monto = calcEditMonto.Value - saldo ;
            }

            this.Close();
        }

        private void Donar()
        {//EKONO.wms_p_donaciones(
       //p_documento NUMBER,
       //p_monto NUMBER,
       //p_usuario VARCHAR2 default USER,
       //p_skuDonacion number

            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("p_documento", Program.FrmMainPos.Order.ToString());
            qp.AddReplaceParam("p_monto", monto.ToString());
            qp.AddReplaceParam("p_usuario", GUI.CApp.usuario);
            qp.AddReplaceParam("p_skuDonacion", "173096686");           
            GUI.CApp.bll.ExecuteSql("Donaciones", qp, "PDV");

            ////DLQG
            //Utils.QueryParams qp2 = new LLNet.Utils.QueryParams();
            //qp2.AddReplaceParam("PDOCUMENTO", Program.FrmMainPos.Order.ToString());
            //if (supervisor == null)
            //    qp2.AddReplaceParam("PUSUARIO", GUI.CApp.usuario);
            //else
            //    qp2.AddReplaceParam("PUSUARIO", supervisor);
            //if (pLinea != null)
            //    qp2.AddReplaceParam("PLINEA", pLinea.ToString());
            //else
            //    qp2.AddReplaceParam("PLINEA", "null");
            //GUI.CApp.bll.ExecuteSql("AutorizaDiscount", qp2, "PDV"); //Bitacora usuario q autoriza Descuento
            ////

            Program.FrmMainPos.RefreshData();

            DialogResult = DialogResult.OK;
        }

        private void FrmDonacion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.None)
            {
                // Verifica el shortcut que presiono el usuario
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        btnCancelar_Click(null, null);
                        break;

                    case Keys.F2:
                        btnMonto_Click(null, null);
                        break;

                    case Keys.F3:
                        btnvuelto_Click(null, null);
                        break;
                    case Keys.F12:
                        btnAceptar_Click(null, null);
                        break;
                }
            }
        }

        private void calcEditMonto_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.None)
            {
                // Verifica el shortcut que presiono el usuario
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                        btnAceptar_Click(null, null);
                        break;
                }
            }
        }

    }
}