using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OracleClient;

public partial class Business_GLS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ExceptionUtility.LogException(0, "", "13", "Page_Load", "");
            loadChannels();
            loadEvents();
            
        }
    }

    private void loadChannels()
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "LOOKUP_GETTER.Get_Recon_Channels";

            oracom.Parameters.Add("p_recon_channels", OracleType.Cursor).Direction = ParameterDirection.Output;

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ddlChannels.DataSource = ds;
            ddlChannels.DataBind();

            ddlChannels.Items.Insert(0, "------ Channels ------");
            oracon.Close();
            string dgn = ConfigurationSettings.AppSettings["DGN"];
            ddlChannels.Items.Remove(ddlChannels.Items.FindByValue(dgn));
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Channels","1.1", "loadChannels", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadEvents()
    {
        try
        {
            ddlEvents.Items.Clear();
            if (ddlChannels.SelectedIndex > 0)
            {
                string DBConnection = Helper.dbConnection();

                OracleConnection oracon = new OracleConnection(DBConnection);
                oracon.Open();

                OracleCommand oracom = new OracleCommand();
                oracom.Connection = oracon;

                oracom.CommandType = CommandType.StoredProcedure;
                oracom.CommandText = "LOOKUP_GETTER.Get_Recon_Channel_Events";

                oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_recon_events", OracleType.Cursor).Direction = ParameterDirection.Output;

                oracom.Parameters["p_rc_code"].Value = ddlChannels.SelectedValue; ;

                OracleDataAdapter da = new OracleDataAdapter(oracom);
                DataSet ds = new DataSet();
                da.Fill(ds);

                ddlEvents.DataSource = ds;
                ddlEvents.DataBind();

                oracon.Close();
            }
            ddlEvents.Items.Insert(0, "------ Events ------");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Channel_Events","1.2", "loadEvents", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult(string channelID, string eventID, string date, int pageSize, int pageNumber)
    {
        string parameters = "";
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            if (ddlChannels.SelectedValue == "3")
                oracom.CommandText = "GL_SUMMARY_INQUIRER.Search_GL_Summary_B24";
            else if (ddlChannels.SelectedValue == "6")
                oracom.CommandText = "GL_SUMMARY_INQUIRER.Search_GL_Summary_Bancs";
            else
            {
                oracom.CommandText = "GL_SUMMARY_INQUIRER.Search_GL_Summary";
                oracom.Parameters.Add("P_OUT_IN_FLAG", OracleType.VarChar).Direction = ParameterDirection.Input;
            }


            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            if (ddlChannels.SelectedValue != "3")
                oracom.Parameters.Add("p_channel", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_settle_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            
            oracom.Parameters.Add("p_page_size", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_re_code"].IsNullable = true;

            if (ddlChannels.SelectedValue != "3")
            {
                if (channelID.Length != 0)
                {
                    parameters = "p_channel-" + channelID;
                    oracom.Parameters["p_channel"].Value = channelID;
                }
                else
                    oracom.Parameters["p_channel"].Value = OracleNumber.Null;
            }
            if (eventID.Length != 0)
            {
                parameters = parameters + ":p_re_code-" + eventID;
                oracom.Parameters["p_re_code"].Value = eventID;
            }
            else
                oracom.Parameters["p_re_code"].Value = OracleNumber.Null;

            if (ddlChannels.SelectedValue != "3" && ddlChannels.SelectedValue != "6")
            if (rdIn.Checked)
            {
                parameters = parameters + ":P_OUT_IN_FLAG-I";
                oracom.Parameters["P_OUT_IN_FLAG"].Value = "I";
            }
            else
            {
                parameters = parameters + ":P_OUT_IN_FLAG-)";
                oracom.Parameters["P_OUT_IN_FLAG"].Value = "O";
            }

            oracom.Parameters["p_settle_date"].Value = date;
            parameters = parameters + ":p_settle_date-" + date; 
            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            int nop = int.Parse(oracom.Parameters["p_total_pages"].Value.ToString());
            oracon.Close();

            GridView1.DataSource = ds;
            GridView1.DataBind();
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    ddlPages.Items.Add(i.ToString());
                }
            }
            ExceptionUtility.LogException(0, "GL_SUMMARY_INQUIRER.Search_GL_Summary_B24,GL_SUMMARY_INQUIRER.Search_GL_Summary_Bancs,GL_SUMMARY_INQUIRER.Search_GL_Summary", "9.1", "loadResult", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "GL_SUMMARY_INQUIRER.Search_GL_Summary_B24,GL_SUMMARY_INQUIRER.Search_GL_Summary_Bancs,GL_SUMMARY_INQUIRER.Search_GL_Summary","9.1", "loadResult",parameters );// "channelID: "+channelID+", eventID: "+eventID +", date: "+date +", pageSize: "+pageSize +", pageNumber: "+pageNumber );
            GridView1.DataSource = null;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        string channelID = "", EventID = "";
        if (ddlChannels.SelectedIndex > 0)
            channelID = ddlChannels.SelectedValue;
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        pnlResult.Visible = true;
        ExceptionUtility.LogException(0, "", "13.9", "Button1_Click", "");
        loadResult(channelID, EventID, DatePicker1.date, Helper.gridSize(), 1);
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        string channelID = "", EventID = "";
        if (ddlChannels.SelectedIndex > 0)
            channelID = ddlChannels.SelectedValue;
        if (ddlEvents.SelectedIndex > 0)
            EventID = ddlEvents.SelectedValue;
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", ddlPages .SelectedItem .Text );
        loadResult(channelID, EventID, DatePicker1.date, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "", "ddlChannels_SelectedIndexChanged", ddlChannels .SelectedItem .Text );
        loadEvents();
        if (ddlChannels.SelectedValue == "19")
        {
            rdIn.Visible = true;
            rdOut.Visible = true;
        }
        else
        {
            rdIn.Visible = false;
            rdOut.Visible = false;
        }
    }
}
