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
{
    public string rflag;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadChannels();
        }
    }

    private void loadChannels()
    {
        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();
    }

    private void loadDates(int channelID)
    {
        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 || channelID == 13)
            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 ------");
    }

    private void loadFiles(int channelID, string date)
    {
        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 || channelID == 13)
            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 ------");
    }

    private void loadResult(int channelID, string fileID)
    {
        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 || channelID == 13)
            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();
    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlChannels.SelectedIndex != 0)
        {
            loadDates(int.Parse(ddlChannels.SelectedValue));

            datesrow.Visible = true;
            filerow.Visible = false;
            btnSearch.Visible = false;
            btnRevert.Visible = false;
        }
        else
        {
            datesrow.Visible = false;
            filerow.Visible = false;
            btnSearch.Visible = false;
            btnRevert.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;
            btnSearch.Visible = false;
            btnRevert.Visible = false;
        }
        else
        {
            filerow.Visible = false;
            btnSearch.Visible = false;
            btnRevert.Visible = false;
        }
        pnlResult.Visible = false;
    }

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

        

        pnlResult.Visible = false;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pnlResult.Visible = true;
        loadResult(int.Parse(ddlChannels.SelectedValue), ddlFile.SelectedValue);
        changestatus();

        rflag = GetRevertFlag(ddlChannels.SelectedValue);
        if (rflag == "Y")
        {
            btnRevert.Visible = true;
        }
        else
        {
            btnRevert.Visible = false;
        }
    }

    public string GetRevertFlag(string rc_code)
    {
        DataTable dtRes = new DataTable();
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);

            OracleCommand oraCom = new OracleCommand();
            oraCom.CommandText = "select * from recon_channel where rc_code ='" + rc_code + "'";
            oraCom.Connection = oracon;
            OracleDataAdapter da = new OracleDataAdapter(oraCom);

            da.Fill(dtRes);
            if (dtRes.Rows.Count > 0)
            {
                return dtRes.Rows[0]["revert_flag"] as string;
            }  
            else return "N";
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex);
            lbl_error.Text = ex.Message;
            return "EXCEPTION";
        }
    }

    protected void btnRevert_Click(object sender, EventArgs e)
    {
        try
        {
            string error = "";
            bool saved = RevertFile(ref error);
            if (saved)
            {
                lblstatus.Text = "File " + lblFileNumber.Text + " Reverted Successfully..";
                pnlResult.Visible = false;
            }
            else
            {
                lblstatus.Text = error;
                pnlResult.Visible = true;
            }
        }
        catch(Exception ex)
        {
            lbl_error.Text = ex.Message;
        }
    }

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

    private bool RevertFile(ref string error)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "FILE_LOADING_SUMMARY.Revert_Process";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_channel", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_file_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_file_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_file_number", 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["p_file_id"].Value = ddlFile.SelectedValue;
        oracom.Parameters["p_file_number"].Value = lblFileNumber.Text;
        oracom.Parameters["p_file_date"].Value = ddlDates.SelectedValue;

        oracom.Parameters["p_lang"].Value = "EN";
        oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

        oracom.ExecuteNonQuery();
        oracon.Close();
        string a = oracom.Parameters["p_res_code"].Value.ToString();
        error = oracom.Parameters["p_res_msg"].Value.ToString();
        
        if (a == "0")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
