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;
using System.Diagnostics;

public partial class Business_EJSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlControls.Visible = false;
            ExceptionUtility.LogException(0, "", "9", "Page_Load", "");
            loadMatchStatus();
            lbTermNotFoundlError.Visible = false;
        }
    }

    private void loadMatchStatus()
    {
        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_EJ_TXN_Status";

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

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

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

            ddlMatch.Items.Insert(0, new ListItem("------ Match Status ------", "-1"));
            //ddlMatch.Items.Insert( 1, new ListItem("New","-2") );

            oracon.Close();
            //ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_EJ_TXN_Status", "loadMatchStatus", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_EJ_TXN_Status","", "loadMatchStatus", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadResult(int pageSize, int pageNumber)
    {
        string parameters = "";
        try
        {
            string currentMethod = "";
            StackTrace stackTrace = new StackTrace();
            currentMethod = stackTrace.GetFrame(1).GetMethod().Name;
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "ej_recon_searcher.get_ej_txn";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_card_no", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_tran_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_trace_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_proc_flg", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_status_cd", 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_atm_num"].Value = txtTerminal.Text.ToUpper();
            if (txtTerminal.Text.Length > 0)
                parameters = "p_atm_num-" + txtTerminal.Text;
            oracom.Parameters["p_card_no"].Value = txtCardNumber.Text;
            if (txtCardNumber.Text.Length > 0)
                parameters = parameters + ":p_card_no-" + Helper.Mask_CardNumber(txtCardNumber.Text);
            oracom.Parameters["p_tran_date"].Value = DatePicker1.date;
            if (DatePicker1.date.Length > 0)
                parameters = parameters + ":p_tran_date-" + DatePicker1.date;
            oracom.Parameters["p_trace_id"].Value = txtTraceID.Text;
            if (txtTraceID.Text.Length > 0)
                parameters = parameters + ":p_trace_id-" + txtTraceID.Text;
            if (ddlMatch.SelectedValue.Equals("-1"))
            {
                oracom.Parameters["p_proc_flg"].Value = OracleString.Null;
                oracom.Parameters["p_status_cd"].Value = OracleString.Null;
            }
            else if (ddlMatch.SelectedValue.Equals("-2"))
            {
                oracom.Parameters["p_proc_flg"].Value = "N";
                parameters = parameters + ":p_proce_flg-N-";
                oracom.Parameters["p_status_cd"].Value = OracleString.Null;
            }
            else
            {
                oracom.Parameters["p_proc_flg"].Value = "Y";
                oracom.Parameters["p_status_cd"].Value = ddlMatch.SelectedValue;
                parameters = parameters + "p_proc_flg-Y:p_status_cd-" + ddlMatch.SelectedItem.Text;
            }

            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());
                }
            }
            if (currentMethod.Contains("Button1_Click"))
                 ExceptionUtility.LogException(0, "ej_recon_searcher.get_ej_txn", "38.2", "loadResult", parameters);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ej_recon_searcher.get_ej_txn","38.2", "loadResult",parameters  );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private bool CheckATMExist()
    {
        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;
            oracom.CommandText = "ej_recon_searcher.get_ATM";

            oracom.Parameters.Add("p_atm_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_out_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_atm_num"].Value = txtTerminal.Text.ToUpper();
            if (txtTerminal.Text.Length > 0)
                parameters = "p_atm_num-" + txtTerminal.Text.ToUpper(); 
            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            string result = oracom.Parameters["p_out_msg"].Value.ToString();
            oracon.Close();

            if (result == "False")
            {
                ExceptionUtility.LogException(9, "ej_recon_searcher.get_ATM", "38.1", "CheckATMExist", parameters );
                return false;
            }
            else
            {
                ExceptionUtility.LogException(0, "ej_recon_searcher.get_ATM", "38.1", "CheckATMExist", parameters);
                return true;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "ej_recon_searcher.get_ATM", "38.1", "CheckATMExist", parameters);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false; 
        }
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        lbTermNotFoundlError.Visible = false;
        if (txtTerminal.Text != "")
        {
            
            if (CheckATMExist())
            {
                ExceptionUtility.LogException(0, "", "9.38", "Button1_Click", "");
                loadResult(Helper.gridSize(), 1);
                pnlResult.Visible = true;
                pnlControls.Visible = false;
            }
            else
            {
                ExceptionUtility.LogException(9, "", "9.38", "Button1_Click", "");
                lbTermNotFoundlError.Visible = true;
                lbTermNotFoundlError.Text = "ATM Not Found ";
            }
        }
        else
        {
            loadResult(Helper.gridSize(), 1);
            pnlResult.Visible = true;
            pnlControls.Visible = false;
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", ddlPages .SelectedItem .Text );
        loadResult(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        pnlResult.Visible = true;
        pnlControls.Visible = false;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            ExceptionUtility.LogException(0, "", "9.38", "GridView1_RowCommand.view", "");
            int index = int.Parse(e.CommandArgument.ToString());
            Label1.Text = GridView1.Rows[index].Cells[0].Text;
            Label2.Text = GridView1.Rows[index].Cells[9].Text;
            Label3.Text = GridView1.Rows[index].Cells[10].Text;
            Label4.Text = GridView1.Rows[index].Cells[1].Text;
            Label5.Text = GridView1.Rows[index].Cells[2].Text;
            Label6.Text = GridView1.Rows[index].Cells[3].Text;
            Label7.Text = GridView1.Rows[index].Cells[13].Text;
            Label8.Text = GridView1.Rows[index].Cells[4].Text;
            Label9.Text = GridView1.Rows[index].Cells[5].Text;
            Label11.Text = GridView1.Rows[index].Cells[6].Text;
            Label12.Text = GridView1.Rows[index].Cells[7].Text;
            Label13.Text = GridView1.Rows[index].Cells[8].Text;
            Label14.Text = GridView1.Rows[index].Cells[11].Text;
            /*Label10.Text = GridView1.Rows[index].Cells[14].Text;
            Label15.Text = GridView1.Rows[index].Cells[15].Text;
            Label16.Text = GridView1.Rows[index].Cells[16].Text;
            Label17.Text = GridView1.Rows[index].Cells[17].Text;*/

            LblDenom.Text = GridView1.Rows[index].Cells[21].Text; // Denomination
            //load the message

            string termid = GridView1.Rows[index].Cells[6].Text;
            string trace = GridView1.Rows[index].Cells[9].Text;
            string date = GridView1.Rows[index].Cells[18].Text;
            string status = GridView1.Rows[index].Cells[19].Text;
            loadStatus(termid, trace, date, status);
            pnlControls.Visible = true;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 10; i <= 19; i++)
                e.Row.Cells[i].Visible = false;

            e.Row.Cells[21].Visible = false; // Denomination
        }
       
    }

    private void loadStatus(string termid, string trace, string date, string status)
    {
        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 = "ej_recon_searcher.Get_Status_Messages";

            oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_trace", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_tran_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_status_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_status_msgs", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_term_id"].Value = termid;
            oracom.Parameters["p_trace"].Value = trace;
            oracom.Parameters["p_tran_date"].Value = date;
            oracom.Parameters["p_status_code"].Value = status;

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

            oracon.Close();

            GridView2.DataSource = ds;
            GridView2.DataBind();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "","38.1", "loadStatus", "termid: " + termid + ", trace: " + trace + ", date: " + date + ", status: " + status);
            GridView2.DataSource = null;
            GridView2.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
}
