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_FLS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ExceptionUtility.LogException(0, "", "11", "Page_Load", "");
            loadChannels();
        }
    }

    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 loadDates(int channelID)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            if (channelID == 3)
                oracom.CommandText = "FILE_LOADING_SUMMARY.Get_Master_Dates";
            else
                oracom.CommandText = "FILE_LOADING_SUMMARY.Get_Channel_Dates";

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

            oracom.Parameters["p_channel"].Value = channelID;

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

            ddlDates.DataSource = ds;
            ddlDates.DataBind();
            oracon.Close();
            ddlDates.Items.Insert(0, "------ Dates ------");
            ExceptionUtility.LogException(0, "FILE_LOADING_SUMMARY.Get_Channel_Dates,FILE_LOADING_SUMMARY.Get_Master_Dates", "2.1", "loadDates", "channelID: " + channelID);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "FILE_LOADING_SUMMARY.Get_Channel_Dates,FILE_LOADING_SUMMARY.Get_Master_Dates","2.1", "loadDates", "channelID: " + channelID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadFiles(int channelID, string date)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            if (channelID == 3)
                oracom.CommandText = "FILE_LOADING_SUMMARY.Get_Master_Files";
            else
                oracom.CommandText = "FILE_LOADING_SUMMARY.Get_Channel_Files";

            oracom.Parameters.Add("p_channel", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_files", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_channel"].Value = channelID;
            oracom.Parameters["p_date"].Value = date;

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

            ddlFile.DataSource = ds;
            ddlFile.DataBind();
            oracon.Close();
            ddlFile.Items.Insert(0, "------ Files ------");
            ExceptionUtility.LogException(0, "FILE_LOADING_SUMMARY.Get_Master_Files,FILE_LOADING_SUMMARY.Get_Channel_Files", "3.1", "loadFiles", "channelID: " + channelID);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "FILE_LOADING_SUMMARY.Get_Master_Files,FILE_LOADING_SUMMARY.Get_Channel_Files","3.1", "loadFiles", "channelID: " + channelID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult(int channelID, string fileID)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            if (channelID == 3)
                oracom.CommandText = "FILE_LOADING_SUMMARY.Get_Master_File_Summary";
            else
                oracom.CommandText = "FILE_LOADING_SUMMARY.Get_Channel_File_Summary";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_file_id", OracleType.VarChar).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["p_file_id"].Value = fileID;
            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);
            oracon.Close();

            lblstime.Text = ds.Tables[0].Rows[0]["proc_start_time"].ToString();
            lblet.Text = ds.Tables[0].Rows[0]["proc_end_time"].ToString();
            lblstatus.Text = ds.Tables[0].Rows[0]["processing_status"].ToString();
            lblloaded.Text = ds.Tables[0].Rows[0]["records_loaded"].ToString();
            lblrejected.Text = ds.Tables[0].Rows[0]["records_rejected"].ToString();
            lbltotal.Text = ds.Tables[0].Rows[0]["total_records"].ToString();
            lblRecDate.Text = ds.Tables[0].Rows[0]["record_Date"].ToString();
            lblFileNumber.Text = ds.Tables[0].Rows[0]["file_number"].ToString();
            ExceptionUtility.LogException(0, "FILE_LOADING_SUMMARY.Get_Master_File_Summary", "12.1", "loadResult", "channelID: " + channelID + ", fileID: " + fileID);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "FILE_LOADING_SUMMARY.Get_Master_File_Summary", "12.1","loadResult", "channelID: " + channelID + ", fileID: " + fileID);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlChannels.SelectedIndex != 0)
        {
            ExceptionUtility.LogException(0, "", "2", "ddlChannels_SelectedIndexChanged", ddlChannels .SelectedItem .Text );
            loadDates(int.Parse(ddlChannels.SelectedValue));
            datesrow.Visible = true;
            filerow.Visible = false;
            searchrow.Visible = false;
        }
        else
        {
            datesrow.Visible = false;
            filerow.Visible = false;
            searchrow.Visible = false;
        }
        pnlResult.Visible = false;
    }

    protected void ddlDates_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDates.SelectedIndex != 0)
        {
            loadFiles(int.Parse(ddlChannels.SelectedValue), ddlDates.SelectedValue.Replace("-",""));
            filerow.Visible = true;
            searchrow.Visible = false;
        }
        else
        {
            filerow.Visible = false;
            searchrow.Visible = false;
        }
        pnlResult.Visible = false;
    }

    protected void ddlFile_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlFile.SelectedIndex != 0)
        {
            searchrow.Visible = true;
        }
        else
        {
            searchrow.Visible = false;
        }
        pnlResult.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "11.12", "Button1_Click", "");
        pnlResult.Visible = true;
        loadResult(int.Parse(ddlChannels.SelectedValue), ddlFile.SelectedValue);
        changestatus();
    }

    private void changestatus()
    {
        string status = lblstatus.Text;
        if (status.Equals("001"))
            lblstatus.Text = "In Process";
        else
            lblstatus.Text = "Completed";
    }
}
