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_TSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadChannels();
            loadEvents();
            loadBankBins();
            pnlControls.Visible = false;
            DatePicker2.date = DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy");
            DatePicker1.date = DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy");
        }
    }

    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_2";

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

        ddlChannels.Items.RemoveAt(3);
    }

    private void loadEvents()
    {
        ddlEvents.Items.Clear();
        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_Events";

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

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

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

        ddlEvents.Items.Insert(0, "------ Events ------");
        oracon.Close();
    }

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

        ddlEvents.Items.Insert(0, "------ Events ------");
        oracon.Close();
    }

    private void loadBankBins()
    {
        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_Bank_Bin";

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

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

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

        ddlIssuer.Items.Insert(0, "------ Issuer ------");

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

        ddlAcquirer.Items.Insert(0, "------ Acquirer ------");

        oracon.Close();

    }

    private void loadResult(string channelID, string eventID, string Card, string reference, string datefrom, string dateto, int pageSize, int pageNumber)
    {
        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 = "TRANSACTIONS_SEARCHER.Search_Master_Tran";
        else
            oracom.CommandText = "TRANSACTIONS_SEARCHER.Search_Channels_Tran";

        if (channelID != "3")
        {
            oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;

        }
        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_card_no", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_date_from", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_tran_date_to", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_rrn", OracleType.VarChar).Direction = ParameterDirection.Input;
       
        oracom.Parameters.Add("p_source", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_dest", OracleType.Number).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;


        if (channelID != "3")
        {
            if (channelID.Length != 0)
                oracom.Parameters["p_rc_code"].Value = channelID;
            else
                oracom.Parameters["p_rc_code"].Value = OracleNumber.Null;

            oracom.Parameters["p_term_id"].Value = txtATM.Text;
        }

        if (eventID.Length != 0)
            oracom.Parameters["p_re_code"].Value = eventID;
        else
            oracom.Parameters["p_re_code"].Value = OracleNumber.Null;
        oracom.Parameters["p_card_no"].Value = Card;
        oracom.Parameters["p_tran_date_from"].Value = datefrom;
        oracom.Parameters["p_tran_date_to"].Value = dateto;
        oracom.Parameters["p_rrn"].Value = reference;

        if (ddlIssuer.SelectedIndex > 0)
            oracom.Parameters["p_dest"].Value = ddlIssuer.SelectedValue;
        else
            oracom.Parameters["p_dest"].Value = OracleNumber.Null;
        if (ddlAcquirer.SelectedIndex > 0)
            oracom.Parameters["p_source"].Value = ddlAcquirer.SelectedValue;
        else
            oracom.Parameters["p_source"].Value = OracleNumber.Null;

        //if(ddlIssuer.SelectedIndex > 0)
        //    oracom.Parameters["p_source"].Value = ddlIssuer.SelectedValue;
        //else
        //    oracom.Parameters["p_source"].Value = OracleNumber.Null;
        //if(ddlAcquirer.SelectedIndex > 0)
        //    oracom.Parameters["p_dest"].Value = ddlAcquirer.SelectedValue;
        //else
        //    oracom.Parameters["p_dest"].Value = OracleNumber.Null;



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

    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;

        //set the dates right
        if (DatePicker1.date.Length == 0 && DatePicker2.date.Length != 0)
            DatePicker1.date = DatePicker2.FormatedDate;
        if (DatePicker2.date.Length == 0 && DatePicker1.date.Length != 0)
            DatePicker2.date = DatePicker1.FormatedDate;
        if (DatePicker1.date.Length > 0 && DatePicker2.date.Length > 0)
        {
            loadResult(channelID, EventID, txtCardNumber.Text.Trim(), txtReferenceNumber.Text.Trim(), DatePicker1.date, DatePicker2.date, Helper.gridSize(), 1);
            pnlControls.Visible = false;
        }
        else
        {
            lbl_error.Visible = true;
            lbl_error.Text = "Please enter From Date and To Date";
        }
    }

    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;
        loadResult(channelID, EventID, txtCardNumber.Text, txtReferenceNumber.Text, DatePicker1.date, DatePicker2.date, Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        pnlControls.Visible = false;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string tID = GridView1.DataKeys[index].Value.ToString();
            loadTranDetails(tID);
        }
    }

    private void loadTranDetails(string tID)
    {
        pnlControls.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();
        Label34.Text = ds.Tables[0].Rows[0]["CLAIM_ID"].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();

    }

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

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        pnlResult.Visible = false;
        if (ddlChannels.SelectedIndex == 0)
        {
            loadEvents();
        }
        else
        {
            loadChannelEvents();
        }
    }

    protected void ddlEvents_SelectedIndexChanged(object sender, EventArgs e)
    {
        pnlControls.Visible = false;
        pnlResult.Visible = false;
        if (ddlEvents.SelectedIndex != 0)
        {
            int selectedEventId = int.Parse(this.ddlEvents.SelectedValue.ToString());

            string eventType = Helper.getEventType(selectedEventId);

            if (eventType.Equals("OUS"))
            {
                this.ddlAcquirer.SelectedValue = Constants.BANK_ID;
                this.ddlIssuer.SelectedValue = Constants.BANK_ID;

                this.ddlIssuer.Enabled = false;
                this.ddlAcquirer.Enabled = false;
            }
            else if (eventType.Equals("ACQ"))
            {
                this.ddlAcquirer.SelectedValue = Constants.BANK_ID;
                this.ddlAcquirer.Enabled = false;

                this.ddlIssuer.SelectedIndex = 0;
                this.ddlIssuer.Enabled = true;
            }
            else if (eventType.Equals("ISS"))
            {
                this.ddlIssuer.SelectedValue = Constants.BANK_ID;
                this.ddlIssuer.Enabled = false;

                this.ddlAcquirer.SelectedIndex = 0;
                this.ddlAcquirer.Enabled = true;
            }
            else
            {
                this.ddlAcquirer.SelectedIndex = 0;
                this.ddlAcquirer.Enabled = true;

                this.ddlIssuer.SelectedIndex = 0;
                this.ddlIssuer.Enabled = true;
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddlChannels.SelectedIndex = -1;
        txtCardNumber.Text = "";
        DatePicker1.date = "";
        DatePicker2.date = "";
        ddlIssuer.SelectedIndex = -1;
        ddlEvents.SelectedIndex = -1;
        txtReferenceNumber.Text = "";
        ddlAcquirer.SelectedIndex = -1;
        pnlResult.Visible = false;
        pnlControls.Visible = false;
    }
}
