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.IO;

public partial class Business_ReconResult : System.Web.UI.Page
{


    int currentPage;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadChannels();
            loadChannelEvents();
            pnlControlsChannel.Visible = false;
            pnlControlsMaster.Visible = false;
            pnlResultMaster.Visible = false;
            btnSearch.Enabled = true;
            currentPage = 0;
            this.btn_editTran.Visible = false;
            this.pnl_editB24Tran.Visible = false;
            this.ddl_tranType.Enabled = false;
            if (Request.QueryString["channel"] != null)
            {
                UserControl ucMenu = Page.Master.FindControl("Menu1") as UserControl;
                UserControl ucLogout = Page.Master.FindControl("Logout1") as UserControl;
                ucMenu.Visible = false;
                ucLogout.Visible = false;
                pnlSearch.Enabled = true;
                pnlResultChannel.Visible = true;
                ddlChannels.SelectedValue = Request.QueryString["channel"];
                loadChannelEvents();
                ddlEvents.SelectedValue = Request.QueryString["event"];
                
                //DatePicker1.FormatedDate = Request.QueryString["filedate"].ToString();
                string filedate = Request.QueryString["filedate"].ToString();
                DatePicker1.FormatedDate = Helper.convertDate(filedate);

                ddl_tranType.SelectedIndex = int.Parse(Request.QueryString["eventType"].ToString());
                if (int.Parse(Request.QueryString["Type"].ToString()) == 1)
                {
                    RadioButton1.Checked = true;
                    RadioButton2.Checked = false;
                }
                else
                {
                    RadioButton1.Checked = false;
                    RadioButton2.Checked = true;
                }

                Channel6_ToolTip();

                loadResult(Helper.gridSize(), 1);
            }
            else
            {
                pnlResultChannel.Visible = false;
            }
            
               
            
        }
        lblError.Text = "";
    }

    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_Job_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 loadChannelEvents()
    {
        ddlEvents.Items.Clear();

        int selectedChannelId;

        if (this.ddlChannels.SelectedIndex > 0)
        {
            selectedChannelId = int.Parse(this.ddlChannels.SelectedValue.ToString());
        } 
        else
        {
            selectedChannelId = 0;
        }

        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.VarChar).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);
            oracon.Close();
            ddlEvents.DataSource = ds;
            ddlEvents.DataBind();

            if (selectedChannelId == 6)
            {
                this.ddl_eventName.DataSource = ds;
                this.ddl_eventName.DataBind();
            }
        }
        ddlEvents.Items.Insert(0, "------ Events ------");

    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadChannelEvents();
        btnSearch.Enabled = false;

        this.pnlResultMaster.Visible = false;
        this.pnlResultChannel.Visible = false;
        this.pnl_editB24Tran.Visible = false;
        this.pnlControlsChannel.Visible = false;
        this.pnlControlsMaster.Visible = false;

        int selectedChannelId = int.Parse(this.ddlChannels.SelectedValue.ToString());

        if (selectedChannelId == 6)
        {
            this.ddl_tranType.SelectedIndex = 1;
            this.ddl_tranType.Enabled = false;
        }
        else
        {
            this.ddl_tranType.Enabled = true;
        }
    }

    protected void ddlEvents_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.pnlResultMaster.Visible = false;
        this.pnlResultChannel.Visible = false;
        this.pnl_editB24Tran.Visible = false;
        this.pnlControlsChannel.Visible = false;
        this.pnlControlsMaster.Visible = false;

        Channel6_ToolTip();

        if (ddlEvents.SelectedIndex > 0)
            btnSearch.Enabled = true;
        else
            btnSearch.Enabled = false;
        DatePicker1.date = "";
    }

    public void Channel6_ToolTip()
    {
        if (ddlChannels.SelectedValue == "6")
        {
            Channel_6.Visible = true;

            if (ddlEvents.SelectedValue == "44")
            {
                RadioButton1.ToolTip = "Exist in A12 Not in Switch C6E 002";
                RadioButton2.ToolTip = "Exist in Switch C6E 002 Not in A12";
            }
            else if (ddlEvents.SelectedValue == "45")
            {
                RadioButton1.ToolTip = "Exist in A12 Not in Virgin Txn 003";
                RadioButton2.ToolTip = "Exist in Virgin Txn 003 Not in A12";
            }
        }
        else
        {
            Channel_6.Visible = false;

            RadioButton1.ToolTip = "Exist in Master Not in Switch";
            RadioButton2.ToolTip = "Exist in Switch Not in Master";
        }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DatePicker1.date.Length == 0)
        {
            if (Request.QueryString["channel"] == null)
                lblError.Text = "Please Enter the Date";
            else
            {
                DatePicker1.FormatedDate = Request.QueryString["fileDate"];
                loadResult(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
            }
        }
        else
        {
            pnlResultChannel.Visible = true;
            loadResult(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
            lblError0.Text = ddlPages.SelectedIndex.ToString();
            
            //loadResult(Helper.gridSize(), 1);
            pnlControlsChannel.Visible = false;
        }
    }

    protected void ddlPages1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DatePicker1.date.Length == 0)
        {
            if (Request.QueryString["channel"] == null)
                lblError.Text = "Please Enter the Date";
            else
            {
                DatePicker1.FormatedDate = Request.QueryString["fileDate"];
                loadResult(Helper.gridSize(), int.Parse(ddlPages1.SelectedValue));
            }
        }
        else
        {
            pnlResultMaster.Visible = true;
            loadResult(Helper.gridSize(), int.Parse(ddlPages1.SelectedValue));
            pnlControlsMaster.Visible = false;
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName.Equals("v"))
        {
            this.pnl_editB24Tran.Visible = false;
            this.Div.Visible = false;
            
            int index = int.Parse(e.CommandArgument.ToString());
            string tID = GridView1.DataKeys[index].Value.ToString();
            string type = ViewState["type"].ToString();

            ViewState["tId"] = tID;

            int selectedEventId = int.Parse(this.ddlEvents.SelectedValue.ToString());

            if (type.Equals("C"))
            {
                loadTranDetails(tID);

                if (selectedEventId == 1)
                {
                    this.btn_editTran.Visible = true;
                }
                else
                {
                    this.btn_editTran.Visible = false;
                }
            }
            else
            {
                loadTranDetails1(tID);
            }
        }
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string tID = GridView2.DataKeys[index].Value.ToString();
            string type = ViewState["type"].ToString();
            if (type.Equals("C"))
            {
                loadTranDetails(tID);
            }
            else
            {
                loadTranDetails1(tID);
            }
        }
    }

    private void loadTranDetails(string tID)
    {
        pnlControlsChannel.Visible = true;
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "TRANSACTIONS_SEARCHER.Get_Channel_Tran";

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

        oracom.Parameters["p_ct_code"].Value = tID;

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

        oracon.Close();

        //set the data in the controls
        Label1.Text = ds.Tables[0].Rows[0]["RECORD_DATE"].ToString();
        Label2.Text = ds.Tables[0].Rows[0]["SETTL_DATE"].ToString();

        Label3.Text = ds.Tables[0].Rows[0]["TRAN_DATE"].ToString();
        Label4.Text = ds.Tables[0].Rows[0]["TRAN_TIME"].ToString();
        Label5.Text = ds.Tables[0].Rows[0]["CARD_NUMBER"].ToString();
        Label6.Text = ds.Tables[0].Rows[0]["ISO_RSP"].ToString();
        Label7.Text = ds.Tables[0].Rows[0]["TERM_ID"].ToString();
        Label8.Text = ds.Tables[0].Rows[0]["TRAN_AMOUNT"].ToString();

        Label9.Text = ds.Tables[0].Rows[0]["TRAN_SOURCE"].ToString();
        Label10.Text = ds.Tables[0].Rows[0]["TRAN_DEST"].ToString();
        Label11.Text = ds.Tables[0].Rows[0]["MSG_TYPE"].ToString();
        Label12.Text = ds.Tables[0].Rows[0]["TRACE_ID"].ToString();
        Label13.Text = ds.Tables[0].Rows[0]["FILE_NUMBER"].ToString();
        Label14.Text = ds.Tables[0].Rows[0]["CARD_BRAND"].ToString();
        Label15.Text = ds.Tables[0].Rows[0]["RRN"].ToString();
        Label16.Text = ds.Tables[0].Rows[0]["CARD_EXP_DATE"].ToString();
        Label17.Text = ds.Tables[0].Rows[0]["ACCT_NUMBER"].ToString();
        Label18.Text = ds.Tables[0].Rows[0]["AUTH_CODE"].ToString();
        Label19.Text = ds.Tables[0].Rows[0]["BUSS_CLS"].ToString();
        Label20.Text = ds.Tables[0].Rows[0]["RECON_STATUS"].ToString();
        Label21.Text = ds.Tables[0].Rows[0]["TRAN_CURR"].ToString();
        Label22.Text = ds.Tables[0].Rows[0]["SETTL_CURR"].ToString();
        Label23.Text = ds.Tables[0].Rows[0]["SETTL_AMOUNT"].ToString();
        Label24.Text = ds.Tables[0].Rows[0]["TRAN_CHG"].ToString();
        Label25.Text = ds.Tables[0].Rows[0]["MATCH_RESULT"].ToString();
        Label26.Text = ds.Tables[0].Rows[0]["CHIP_INDEX"].ToString();
        Label27.Text = ds.Tables[0].Rows[0]["channel_name"].ToString();
        Label28.Text = ds.Tables[0].Rows[0]["event_name"].ToString();
        Label29.Text = ds.Tables[0].Rows[0]["PCODE"].ToString();
        Label30.Text = ds.Tables[0].Rows[0]["FINANCIAL_FLAG"].ToString();
        Label31.Text = ds.Tables[0].Rows[0]["PRODUCT"].ToString();
        Label32.Text = ds.Tables[0].Rows[0]["MSG_RESAON_CODE"].ToString();
        Label33.Text = ds.Tables[0].Rows[0]["SETTL_FLAG"].ToString();
        Label82.Text = ds.Tables[0].Rows[0]["CLAIM_ID"].ToString();
        lblGLAcct.Text = ds.Tables[0].Rows[0]["GL_ACCT_NO"].ToString();

        /*06-06-2012 Add the new fields in the Channels_TRAN */
        lblFinValDate.Text = ds.Tables[0].Rows[0]["FINACLE_VALUE_DATE"].ToString();
        lblFinPostDate.Text = ds.Tables[0].Rows[0]["FINACLE_POSTING_DATE"].ToString();
        lblFinPostTime.Text = ds.Tables[0].Rows[0]["FINACLE_POSTING_TIME"].ToString();
        lblMerchantName.Text = ds.Tables[0].Rows[0]["MERCHANT_NAME"].ToString();
    }

    private void loadTranDetails1(string tID)
    {
        pnlControlsMaster.Visible = true;
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "TRANSACTIONS_SEARCHER.Get_Master_Tran";

        oracom.Parameters.Add("p_mt_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_match_grid", OracleType.Cursor).Direction = ParameterDirection.Output;

        oracom.Parameters["p_mt_code"].Value = tID;

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

        oracon.Close();

        //set the data in the controls
        Label34.Text = ds.Tables[0].Rows[0]["RECORD_DATE"].ToString();
        Label35.Text = ds.Tables[0].Rows[0]["SETTL_DATE"].ToString();

        Label36.Text = ds.Tables[0].Rows[0]["TRAN_DATE"].ToString();
        Label37.Text = ds.Tables[0].Rows[0]["TRAN_TIME"].ToString();
        Label38.Text = ds.Tables[0].Rows[0]["CARD_NUMBER"].ToString();
        Label39.Text = ds.Tables[0].Rows[0]["PCODE"].ToString();
        Label40.Text = ds.Tables[0].Rows[0]["FINANCIAL_FLAG"].ToString();
        Label41.Text = ds.Tables[0].Rows[0]["B24_RSP"].ToString();
        Label42.Text = ds.Tables[0].Rows[0]["ISO_RSP"].ToString();
        Label43.Text = ds.Tables[0].Rows[0]["TERM_ID"].ToString();
        Label44.Text = ds.Tables[0].Rows[0]["TRAN_AMOUNT"].ToString();
        Label45.Text = ds.Tables[0].Rows[0]["TRAN_SOURCE"].ToString();
        Label46.Text = ds.Tables[0].Rows[0]["TRAN_DEST"].ToString();
        Label47.Text = ds.Tables[0].Rows[0]["MSG_TYPE"].ToString();
        Label48.Text = ds.Tables[0].Rows[0]["TRACE_ID"].ToString();
        Label49.Text = ds.Tables[0].Rows[0]["FILE_NUMBER"].ToString();
        Label50.Text = ds.Tables[0].Rows[0]["CARD_BRAND"].ToString();
        Label51.Text = ds.Tables[0].Rows[0]["RRN"].ToString();
        Label52.Text = ds.Tables[0].Rows[0]["CARD_EXP_DATE"].ToString();
        Label53.Text = ds.Tables[0].Rows[0]["ACCT_NUMBER"].ToString();
        Label54.Text = ds.Tables[0].Rows[0]["AUTH_CODE"].ToString();
        Label55.Text = ds.Tables[0].Rows[0]["BUSS_CLASS"].ToString();
        Label56.Text = ds.Tables[0].Rows[0]["TRAN_STATUS"].ToString();
        Label57.Text = ds.Tables[0].Rows[0]["TRAN_CURR"].ToString();
        Label58.Text = ds.Tables[0].Rows[0]["SETTL_CURR"].ToString();
        Label59.Text = ds.Tables[0].Rows[0]["SETTL_AMOUNT"].ToString();
        Label60.Text = ds.Tables[0].Rows[0]["TRAN_CHG"].ToString();
        Label61.Text = ds.Tables[0].Rows[0]["RECON_REQ_FLAG"].ToString();
        Label62.Text = ds.Tables[0].Rows[0]["RECON_PERFORMED_FLAG"].ToString();
        Label63.Text = ds.Tables[0].Rows[0]["MATCH_CRIERIA"].ToString();
        Label64.Text = ds.Tables[0].Rows[0]["MATCH_REF1"].ToString();
        Label65.Text = ds.Tables[0].Rows[0]["MATCH_REF2"].ToString();
        Label66.Text = ds.Tables[0].Rows[0]["MATCH_REF3"].ToString();
        Label67.Text = ds.Tables[0].Rows[0]["MATCH_REF4"].ToString();
        Label68.Text = ds.Tables[0].Rows[0]["MATCH_DATE1"].ToString();
        Label69.Text = ds.Tables[0].Rows[0]["MATCH_DATE2"].ToString();
        Label70.Text = ds.Tables[0].Rows[0]["MATCH_DATE3"].ToString();
        Label71.Text = ds.Tables[0].Rows[0]["MATCH_DATE4"].ToString();
        Label72.Text = ds.Tables[0].Rows[0]["CHIP_DATA"].ToString();
        Label73.Text = ds.Tables[0].Rows[0]["NEXT_MATCH"].ToString();
        Label74.Text = ds.Tables[0].Rows[0]["re_code"].ToString();
        Label75.Text = ds.Tables[0].Rows[0]["PRODUCT"].ToString();
        Label76.Text = ds.Tables[0].Rows[0]["ORIG_OSEQ_NUM"].ToString();
        Label77.Text = ds.Tables[0].Rows[0]["ORIG_DATE"].ToString();
        Label78.Text = ds.Tables[0].Rows[0]["ORIG_TIME"].ToString();
        Label79.Text = ds.Tables[0].Rows[0]["AMT_2"].ToString();
        Label80.Text = ds.Tables[0].Rows[0]["AMT_3"].ToString();
        Label81.Text = ds.Tables[0].Rows[0]["AUTH_AMT"].ToString();
        Label82.Text = ds.Tables[0].Rows[0]["CLAIM_ID"].ToString();

        GridView3.DataSource = ds.Tables[1];
        GridView3.DataBind();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlControlsChannel.Visible = false;
    }

    protected void btnCancel1_Click(object sender, EventArgs e)
    {
        pnlControlsMaster.Visible = false;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pnlControlsChannel.Visible = false;
        pnlControlsMaster.Visible = false;
        pnlResultMaster.Visible = false;
        pnlResultChannel.Visible = false;
        this.pnl_editB24Tran.Visible = false;

        if (DatePicker1.date.Length == 0)
        {
            lblError.Text = "Please Enter the Date";
        }
        else
        {
            loadResult(Helper.gridSize(), 1);

            //this.lnk_firstPage.Enabled = false;
            //this.lnk_prePage.Enabled = false;
            //this.lnk_lastPage.Enabled = true;
            //this.lnk_nextPage.Enabled = true;
        }
    }

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

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            if (RadioButton1.Checked)
                oracom.CommandText = "RECON_RESULT_VIEWER.Get_Non_Match_Recon";
            else if (RadioButton2.Checked)
                oracom.CommandText = "RECON_RESULT_VIEWER.Get_Non_Match_Tran";
            else if (RadioButton4.Checked)
                oracom.CommandText = "RECON_RESULT_VIEWER.Get_Match_Tran";
            else if (RadioButton3.Checked)
                oracom.CommandText = "RECON_RESULT_VIEWER.Get_Duplicate";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_recon_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_tran_type", 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.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;


            oracom.Parameters["p_rc_code"].Value = ddlChannels.SelectedValue;
            oracom.Parameters["p_re_code"].Value = ddlEvents.SelectedValue;
            //if (Request.QueryString["fileDate"]!=null)
            //    oracom.Parameters["p_recon_date"].Value = DatePicker1.FormatedDate;
            //else
            oracom.Parameters["p_recon_date"].Value = DatePicker1.date;
            if (ddl_tranType.SelectedIndex > 0)
                oracom.Parameters["p_tran_type"].Value = this.ddl_tranType.SelectedValue;
            else
                oracom.Parameters["p_tran_type"].Value = "ALL";

            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();

            string type = oracom.Parameters["p_output_type"].Value.ToString();
            ViewState["type"] = type;

            if (type.Equals("C"))
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
                pnlResultChannel.Visible = true;
                pnlResultMaster.Visible = false;
            }
            else
            {
                GridView2.DataSource = ds;
                GridView2.DataBind();
                pnlResultMaster.Visible = true;
                pnlResultChannel.Visible = false;
            }

            //this.lbl_firstPage.Text = "Page " + pageNumber + " of " + nop;

            ViewState["currentPage"] = pageNumber;
            ViewState["totalPages"] = nop;
            if (pageNumber == 1)
            {
                ddlPages.Items.Clear();
                ddlPages1.Items.Clear();
                for (int i = 1; i <= nop; i++)
                {
                    if (type.Equals("C"))
                        ddlPages.Items.Add(i.ToString());
                    else
                        ddlPages1.Items.Add(i.ToString());
                }
            }
        }
        catch(Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
    
    protected void ddl_tranType_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.pnlResultMaster.Visible = false;
        this.pnlResultChannel.Visible = false;

        this.pnlControlsMaster.Visible = false;
        this.pnlControlsChannel.Visible = false;
        this.pnl_editB24Tran.Visible = false;
    }
    
    protected void btn_editTran_Click(object sender, EventArgs e)
    {
        this.pnl_editB24Tran.Visible = true;
        this.pnlControlsChannel.Visible = false;

        this.lbl_recordDate.Text = this.Label1.Text;
        this.lbl_settlDate.Text = this.Label2.Text;
        this.lbl_tranDate.Text = this.Label3.Text;
        this.lbl_tranTime.Text = this.Label4.Text;
        this.lbl_cardNumber.Text = this.Label5.Text;
        this.lbl_isoRes.Text = this.Label6.Text;
        this.txt_terminalId.Text = this.Label7.Text;
        this.lbl_tranAmount.Text = this.Label8.Text;
        this.txt_tranSource.Text = this.Label9.Text;
        this.txt_tranDest.Text = this.Label10.Text;
        this.lbl_msgType.Text = this.Label11.Text;
        this.txt_traceId.Text = this.Label12.Text;
        this.lbl_fileNumber.Text = this.Label13.Text;
        this.lbl_cardBrand.Text = this.Label14.Text;
        this.lbl_refNumber.Text = this.Label15.Text;
        this.lbl_cardExpiryDate.Text = this.Label16.Text;
        this.lbl_accountNumber.Text = this.Label17.Text;
        this.txt_authCode.Text = this.Label18.Text;
        this.lbl_businessClass.Text = this.Label19.Text;
        this.lbl_reconStatus.Text = this.Label20.Text;
        this.lbl_tranCurrency.Text = this.Label21.Text;
        this.lbl_settlCurrency.Text = this.Label22.Text;
        this.lbl_settlAmount.Text = this.Label23.Text;
        this.lbl_tranChange.Text = this.Label24.Text;
        this.lbl_matchResult.Text = this.Label25.Text;
        this.lbl_chipIndex.Text = this.Label26.Text;
        this.lbl_channelName.Text = this.Label27.Text;
        this.ddl_eventName.SelectedValue = this.ddlEvents.SelectedValue;
        this.lbl_pCode.Text = this.Label29.Text;
        this.lbl_financialFlag.Text = this.Label30.Text;
        this.lbl_product.Text = this.Label31.Text;
        this.lbl_msgReasonCode.Text = this.Label32.Text;
        this.lbl_settlFlag.Text = this.Label33.Text;
        this.lbl_claimId.Text = this.Label82.Text;
    }
    
    protected void btn_updatePnl_cancel_Click(object sender, EventArgs e)
    {
        this.pnl_editB24Tran.Visible = false;
    }
    
    protected void btn_updatePnl_save_Click(object sender, EventArgs e)
    {
        int ctCode = int.Parse( ViewState["tId"].ToString() );
        string terminalID = this.txt_terminalId.Text;
        string tranSource = this.txt_tranSource.Text;
        string tranDest = this.txt_tranDest.Text;
        string traceId = this.txt_traceId.Text;
        string authCode = this.txt_authCode.Text;
        string eventId = this.ddl_eventName.SelectedValue;

        string cardNumber = this.lbl_cardNumber.Text;
        int messageType = int.Parse( this.lbl_msgType.Text.Substring(0, 3) );
        string tranDate = Helper.formatDate_ToDBFormat( this.lbl_tranDate.Text );
        string tranTime = Helper.formatTime_toDBFormat( this.lbl_tranTime.Text );
        string tranAmount = this.lbl_tranAmount.Text.Replace(",", "") ;
        string refNumber = this.lbl_refNumber.Text;

        int resCode;
        string errorMsg = "";
       
        resCode = this.updateChannelTran(ctCode, terminalID, tranSource, tranDest, traceId,
                                  authCode, eventId, cardNumber, messageType,
                                  tranDate, tranTime, tranAmount, refNumber, ref errorMsg);
        
        if (resCode == 0)
        {
            this.pnl_editB24Tran.Visible = false;
            this.loadResult(Helper.gridSize(), 1);
            lblError.ForeColor = System.Drawing.Color.DarkGreen;
            this.lblError.Text = "The Transaction Updated Successfully.";
            this.loadResult(Helper.gridSize(),  int.Parse(this.lblError0.Text) + 1);
            ddlPages.SelectedIndex = int.Parse(this.lblError0.Text);
        }
        else
        {
            this.lblError.Text = errorMsg;
        }
    }

    protected void img_search_Click(object sender, ImageClickEventArgs e)
    {
        Div.Visible = true;

        string cardNumber = this.lbl_cardNumber.Text;
        string refNumber = this.lbl_refNumber.Text;
        string product = this.lbl_product.Text;
        string tranTime = Helper.formatTime_toDBFormat(this.lbl_tranTime.Text);

        loadTrans(cardNumber, refNumber, product, tranTime);
    }

    protected void btn_close_Click(object sender, EventArgs e)
    {
        Div.Visible = false;
    }

    private void loadTrans(string cardNumber, string refNumber, string product, string tranTime)
    {
        string DBConnection = Helper.dbConnection();

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

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "transactions_searcher.GET_SUGGESTED_MATCHED_TRANS";

        oracom.Parameters.Add("P_CARD_NUMBER", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_REF_NUMBER", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_PRODUCT", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_TRAN_TIME", OracleType.VarChar).Direction = ParameterDirection.Input;

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


        oracom.Parameters["P_CARD_NUMBER"].Value = cardNumber;
        oracom.Parameters["P_REF_NUMBER"].Value = refNumber;
        oracom.Parameters["P_PRODUCT"].Value = product;
        oracom.Parameters["P_TRAN_TIME"].Value = tranTime;

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

        this.grd_trans.DataSource = ds;
        this.grd_trans.DataBind();

        oracon.Close();
    }

    protected void grd_trans_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("s"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            //string cardNo = this.grd_trans.DataKeys[index].Value.ToString();

            this.txt_terminalId.Text = this.grd_trans.Rows[index].Cells[0].Text.ToString();
            this.txt_tranSource.Text = this.grd_trans.Rows[index].Cells[1].Text.ToString();
            this.txt_tranDest.Text = this.grd_trans.Rows[index].Cells[2].Text.ToString();
            this.txt_traceId.Text = this.grd_trans.Rows[index].Cells[3].Text.ToString();
            this.txt_authCode.Text = this.grd_trans.Rows[index].Cells[4].Text.Trim() == "&nbsp;" ? "" : this.grd_trans.Rows[index].Cells[4].Text;

            this.ddl_eventName.SelectedValue = this.grd_trans.Rows[index].Cells[5].Text.ToString();

            Div.Visible = false;
        }
    }

    protected void grd_trans_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grd_trans.PageIndex = e.NewPageIndex;


        string cardNumber = this.lbl_cardNumber.Text;
        string refNumber = this.lbl_refNumber.Text;
        string product = this.lbl_product.Text;
        string tranTime = Helper.formatTime_toDBFormat(this.lbl_tranTime.Text);

        loadTrans(cardNumber, refNumber, product, tranTime);
    }

    private int updateChannelTran(int ctCode, string terminalID, string tranSource, string tranDest, string traceId,
                                  string authCode, string eventId, string cardNumber, int messageType,
                                  string tranDate, string tranTime, string tranAmount, string refNumber, ref string errorMsg)
    {
        int resCode;

        string DBConnection = Helper.dbConnection();
        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

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

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "transactions_searcher.UPDATE_BASE24_TRAN";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;

        oracom.Parameters.Add("p_channel_ct_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_terminal_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_source", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_dest", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_trace_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_auth_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_event_id", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_card_number", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_msg_type", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_time", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_amount", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_ref_number", 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_current_uid"].Value = Session["uid"].ToString();
        oracom.Parameters["p_lang"].Value = "EN";

        oracom.Parameters["p_channel_ct_code"].Value = ctCode;
        oracom.Parameters["p_terminal_id"].Value = terminalID;
        oracom.Parameters["p_tran_source"].Value = tranSource;//
        oracom.Parameters["p_tran_dest"].Value = tranDest;//
        oracom.Parameters["p_trace_id"].Value = traceId;
        oracom.Parameters["p_auth_code"].Value = authCode;
        oracom.Parameters["p_event_id"].Value = eventId;
        oracom.Parameters["p_card_number"].Value = cardNumber;
        oracom.Parameters["p_msg_type"].Value = messageType;
        oracom.Parameters["p_tran_date"].Value = tranDate;
        oracom.Parameters["p_tran_time"].Value = tranTime;
        oracom.Parameters["p_tran_amount"].Value = tranAmount;
        oracom.Parameters["p_ref_number"].Value = refNumber;

        oracom.ExecuteNonQuery();
        oracon.Close();

        resCode = int.Parse(oracom.Parameters["p_res_code"].Value.ToString());
        errorMsg = oracom.Parameters["p_res_msg"].Value.ToString();

        return resCode;
    }

    protected void btn_GenerateRPT_Click(object sender, EventArgs e)
    {
        ExceptionUtility.LogEvent_Errors(0, "", "btn_GenerateRPT_Click", "Start");

        if (ddlChannels.SelectedIndex != 0 && ddlEvents.SelectedIndex != 0)
        {
            if (DatePicker1.date.Length == 0)
            {
                lblError.Text = "Please Enter the Date";
            }
            else
            {
                string p1 = ddlChannels.SelectedValue;
                string p2 = ddlEvents.SelectedValue;
                string p3 = DatePicker1.date;
                string p7 = Convert.ToString(Convert.ToInt32(DatePicker1.date) + 2);
                string p4 = this.ddl_tranType.SelectedValue.ToString();
                string p5 = ddlChannels.SelectedItem.Text;
                string p6 = ddlEvents.SelectedItem.Text;

                if (RadioButton1.Checked)
                {
                    int p8 = 2;
                    if (p1 == "6")
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=1015&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "&p8=" + p8 + "','Report')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=1&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "&p8=" + p8 + "','Report')", true);
                    }  
                }
                else if (RadioButton2.Checked)
                {
                    if (p1 == "6")
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=1016&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "','Report')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=2&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "','Report')", true);
                    }
                }
                else if(RadioButton3.Checked)
                {
                    lblError.Text = "Duplicated in Switch is not generating the report";
                }
                else if (RadioButton4.Checked)
                {
                    int p8 = 1;
                    if (p1 == "6")
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=1015&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "&p8=" + p8 + "','Report')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                        "window.open('../Reports/Default.aspx?rpt=1&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&p6=" + p6 + "&p7=" + p7 + "&p8=" + p8 + "','Report')", true);
                    }
                }
            }
        }
        else
        {
            lblError.Text = "Select Channel and Event";
        }

        ExceptionUtility.LogEvent_Errors(0, "", "btn_GenerateRPT_Click", "End");
    }

    //protected void lnk_prePage_Click(object sender, EventArgs e)
    //{
    //    if (DatePicker1.date.Length == 0)
    //    {
    //        lblError.Text = "please enter a date";
    //    }
    //    else
    //    {
    //        pnlResultChannel.Visible = true;
    //        loadResult(Helper.gridSize(), int.Parse(ViewState["currentPage"].ToString()) - 1);
    //        pnlControlsChannel.Visible = false;

    //        this.lnk_nextPage.Enabled = true;
    //        this.lnk_lastPage.Enabled = true;

    //        if (int.Parse(ViewState["currentPage"].ToString()) == 1)
    //        {
    //            this.lnk_prePage.Enabled = false;
    //            this.lnk_firstPage.Enabled = false;
    //        }
    //    }
    //}
    //protected void lnk_nextPage_Click(object sender, EventArgs e)
    //{
    //    if (DatePicker1.date.Length == 0)
    //    {
    //        lblError.Text = "please enter a date";
    //    }
    //    else
    //    {
    //        pnlResultChannel.Visible = true;
    //        loadResult(Helper.gridSize(), int.Parse(ViewState["currentPage"].ToString()) + 1);
    //        pnlControlsChannel.Visible = false;

    //        this.lnk_prePage.Enabled = true;
    //        this.lnk_firstPage.Enabled = true;

    //        if (int.Parse(ViewState["totalPages"].ToString()) == int.Parse(ViewState["currentPage"].ToString()))
    //        {
    //            this.lnk_nextPage.Enabled = false;
    //            this.lnk_lastPage.Enabled = false;
    //        }
    //        else
    //        {
    //            this.lnk_nextPage.Enabled = true;
    //            this.lnk_lastPage.Enabled = true;
    //        }
    //    }
    //}
}
