using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using LLNet.GUI;
using LLNet.Tienda.MainForms.Forms.MisPuntos.CrystalReport;

namespace LLNet.Tienda.MainForms.Forms.MisPuntos
{
    public partial class frmReporteCBB : Form
    {
        private static String _idListaClubBebe = String.Empty;
        private static String _bebeLista = String.Empty;
        public frmReporteCBB(String _idLista,String _bebe)
        {
            InitializeComponent();
            _idListaClubBebe = _idLista;
            _idListaClubBebe = "'" + _idListaClubBebe + "'";
            _bebeLista = _bebe;
        }

        private void crvDataCBB_Load(object sender, EventArgs e)
        {
            if (frmGestionCbb.Validate(frmGestionCbb._reportesUsuarioFinal))
            {
                Onload(frmGestionCbb._reportesUsuarioFinal);
            }
            else
            {
                CApp.ShowMessage("Ocurrio el problema al obtener el valor de la variable frmGestionCbb._reportesUsuarioFinal", "Club del Bebé", true);
            }
           
            
          
        }

        public void Onload(String _comportamiento)
        {
           
            string queryString = String.Empty;
            
            if (_comportamiento.Equals("2"))
            {
                queryString = "SELECT cbblh.C_SKU \"Codigo\",cbblh.C_DESCRIPCION_ARTICULO \"Descrip\",cbblh.N_CANT_ARTICULO_ORIGI \"Cant\",cbblh.N_CANT_ARTICULO_COMPRA \"CantCompra\",cbblh.N_CANTIDAD_ARTICULO_RESTA \"CantResta\"" +
                                ",cbblh.D_FECHA_REGISTRO \"FechaCompra\",cbblh.C_OBSERVACION \"Comentario\",cbbld.C_NOMBRE_LISTA \"Bebe\",TO_CHAR(cbblh.N_ID_LISTA_DESEO) \"IdListaCBB\",TO_CHAR(cbblh.N_DOCUMENTO) \"Documento\" FROM CBB_DET_LIST_DESEO_HIS cbblh INNER JOIN CBB_LISTAS_DESEO cbbld" +
                                " ON cbblh.N_ID_LISTA_DESEO = cbbld.N_ID_LISTA_DESEO WHERE cbblh.N_ID_LISTA_DESEO = " + _idListaClubBebe + " order by C_SKU,N_DOCUMENTO,N_CANT_ARTICULO_COMPRA DESC";
            }
            else
            {
                queryString = "SELECT cbbl.C_CEDULA \"CedulaAF\",cbbl.C_NOMBRE_LISTA \"Bebe\",cbbl.D_FECHA_GESTACION \"FechaGesta\",cbbl.C_SEXO_BEBE \"Genero\"" +
                                 ",cbbl.C_NOMBRE_MAMA \"Mama\",cbbl.C_CEDULA_MAMA \"CedMama\",cbbdl.C_DESCRIPCION_ARTICULO \"Descrip\",cbbdl.N_CANTIDAD_ARTICULO \"CantRest\"" +
                                 ",cbbdl.N_CANTIDAD_ARTICULO_REAL \"CantOrigi\"" +
                                 ",cbbdl.C_SKU_ALTERNO \"Sku\"" +
                                 " FROM CBB_LISTAS_DESEO cbbl INNER JOIN CBB_DETALLE_LISTA_DESEO cbbdl" +
                                 " ON cbbl.N_ID_LISTA_DESEO = cbbdl.N_ID_LISTA_DESEO WHERE cbbl.N_ID_LISTA_DESEO = " + _idListaClubBebe + " AND cbbl.C_ESTADO = 'A'";
            }
           
          

            string connectionString = GUI.CApp.bll.ConnectionString;



            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                OracleCommand command = connection.CreateCommand();

                command.CommandText = queryString;



                try
                {
                    connection.Open();

                    OracleDataReader reader = command.ExecuteReader();
                    var dataTable = new DataTable();
                    dataTable.Load(reader);
                    reader.Close();
                    connection.Close();
                    command.Dispose();
                    if (dataTable.Rows.Count == 0)
                    {
                        CApp.ShowMessage("La lista seleccionada no tiene articulos", "Club del Bebé", true);
                        this.Close();
                    }
                    else
                    {
                        crpLista crpt = new crpLista();
                        crpListaUF crptuf = new crpListaUF();
                        if (_comportamiento.Equals("2"))
                        {
                            crpt.Database.Tables["Lista"].SetDataSource(dataTable);
                            crvDataCBB.ReportSource = null;
                            crvDataCBB.ReportSource = crpt;
                        }
                        else
                        {
                            crptuf.Database.Tables["ListaUF"].SetDataSource(dataTable);
                            crvDataCBB.ReportSource = null;
                            crvDataCBB.ReportSource = crptuf;
                        }
                       
                      
                    }




                }
                catch (Exception ex)
                {
                    command.Dispose();
                    connection.Close();
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}
