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.Forms
{
    public partial class frmPagosAnticipados : LLNet.GUI.FormBases.frmBase
    {
        public frmPagosAnticipados()
        {
            InitializeComponent();

            GUI.CApp.TraducirForm(this);
        }

        private decimal? socio;
        private string oldNit;

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

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

        public override void Init(object[] pArgs)
        {
            if (Program.FrmMainPos != null)
            {
                Program.FrmMainPos.DsPedido.Clear();
                Program.FrmMainPos.DsVale.Clear();
            }
            
            base.Init(pArgs);
            dsPDV1.TDA_V_FACTURAS_CREDITO.Clear();
            beNIT.EditValue = null;
            beCliente.EditValue = null;
            txtMonto.Properties.ReadOnly = true;
            txtMonto.EditValue = 0.00;
            beNIT.Focus();
        }

        private void getInvoices()
        {
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam(Utils.QueryParams.PredefinedTags.AllToolbarsTag, " AND SOCIO = '" + socio + "'");
            //qp.AddReplaceParam("SOCIO", socio.ToString());
            dsPDV1.TDA_V_FACTURAS_CREDITO.Clear();
            Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsPDV1.GetType(), dsPDV1.TDA_V_FACTURAS_CREDITO.TableName, qp), dsPDV1);
            
            decimal total = 0;
            foreach (Model.PDV.DsPDV.TDA_V_FACTURAS_CREDITORow row in dsPDV1.TDA_V_FACTURAS_CREDITO)
            {
                total += row.SALDO;
            }
            
            if (total > 0)
            {
                GUI.CApp.ShowMessage("El cliente tiene facturas al crédito pendientes de pago", "Pagos Anticipados",true);
                this.beCliente.Text = "";
                socio = null;
            }

            txtMonto.EditValue = 0.00;
        }

        private void beNIT_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Tienda.MainForms.Finders.frmFindCustomer fc = new LLNet.Tienda.MainForms.Finders.frmFindCustomer();
            fc.Init(null);
            fc.ShowDialog(this);

            if (fc.DialogResult == DialogResult.OK)
            {
                socio = Convert.ToDecimal(fc.Socio);
                beNIT.EditValue = fc.NIT;
                beCliente.EditValue = fc.Nombre;
                txtMonto.Properties.ReadOnly = false;
                oldNit = fc.NIT;
                getInvoices();
                txtMonto.Focus();
            }
        }

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

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

        private MainForms.Forms.frmPayments pmt;

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (socio != null)
            {
                if (Convert.ToDecimal(txtMonto.EditValue) != 0)
                {
                    if (pmt == null)
                        pmt = new LLNet.Tienda.MainForms.Forms.frmPayments();
                    pmt.Init(new object[] { socio, txtMonto.EditValue, true });
                    pmt.ShowDialog(this);
                    Init(null);
                    beNIT.Focus();
                }
                else
                {
                    GUI.CApp.ShowMessage("Ingrese el monto a pagar", "Pagos Anticipados", true);
                }
            }
            else
            {
                GUI.CApp.ShowMessage("Seleccione un cliente para continuar", "Pagos Anticipados", true);
            }

        }

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

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

        private void beNIT_KeyDown(object sender, KeyEventArgs e)
        {
            
            switch (e.KeyCode)                
            {
                case Keys.Enter:
                    Model.PDV.DsPDV.WMS_SOCIOSRow cust = GetCustomer(Utils.Helpers.ObjToStr(((DevExpress.XtraEditors.ButtonEdit)sender).EditValue));
                    if (cust != null)
                    {
                        socio = cust.SOCIO;
                        beNIT.EditValue = cust.NIT;
                        beCliente.EditValue = cust.NOMBRE;
                        txtMonto.Properties.ReadOnly = false;
                        oldNit = cust.NIT;
                        getInvoices();
                        txtMonto.Focus();
                    }
                    else
                    {
                        oldNit = null;
                        socio = 0;
                        beCliente.EditValue = null;
                        txtMonto.Properties.ReadOnly = true;
                        dsPDV1.TDA_V_FACTURAS_CREDITO.Clear();
                        beCliente.EditValue = "Cliente no existe";
                        beNIT.SelectAll();
                        beNIT.Focus();
                    }
                    break;
            }
        }


        public Model.PDV.DsPDV.WMS_SOCIOSRow GetCustomer(string NIT)
        {
            Utils.QueryParams qp = new Utils.QueryParams();

            if (NIT == oldNit && dsPDV1.WMS_SOCIOS.Count > 0 && NIT != string.Empty)
                return dsPDV1.WMS_SOCIOS[0];
            dsPDV1.WMS_SOCIOS.Clear();

            // Si no ingresaron codigo devuelve el consumidor final
            if ((NIT == null) || (NIT == ""))
            {
                return null;
            }
            else
            {
                qp.AddReplaceParam("NIT", NIT);

                // Busca el NIT ingresado
                Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsPDV1.GetType(), dsPDV1.WMS_SOCIOS.TableName, qp, "CustomerByNIT"), dsPDV1);
                if (dsPDV1.WMS_SOCIOS.Rows.Count == 0)
                {
                    return null;
                }
            }
            oldNit = NIT;

            return dsPDV1.WMS_SOCIOS[0];
        }
        
        private void btnBuscar_Click(object sender, EventArgs e)
        {
            beNIT_ButtonClick(null, null);
        }

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

        private void frmPagosAnticipados_Shown_1(object sender, EventArgs e)
        {
            layoutControl1.Focus();
            layoutControl1.FocusHelper.FocusFirst();
        }

        private void txtMonto_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnAceptar_Click(null, null);
            }
        }
    }
}