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_EJSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlControls.Visible = false;
            loadMatchStatus();
            lbTermNotFoundlError.Visible = false;
            DatePicker1.date = DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy");
            DatePicker2.date = DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy");
            loadETT();
        }
        rdOther.Attributes.Add("OnClick", "javascript:ClearTextbox();");
        rdOnUs.Attributes.Add("OnClick", "javascript:UnClearTextbox();");
    }

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

    private void loadResult(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;
        oracom.CommandText = "ej_recon_searcher.get_ej_txn";

        oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_ETT_ID", 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_from_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_to_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_account_number", 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;

        if (txtTerminal.Text.Trim().Length != 0)
            oracom.Parameters["p_atm_num"].Value = txtTerminal.Text.Trim ().ToUpper();
        else
            oracom.Parameters["p_atm_num"].Value = OracleString.Null;
        oracom.Parameters["p_card_no"].Value = txtCardNumber.Text.Trim();
        oracom.Parameters["p_from_date"].Value = DatePicker1.date;
        oracom.Parameters["p_to_date"].Value = DatePicker2.date;
        oracom.Parameters["p_trace_id"].Value = txtTraceID.Text.Trim();

        oracom.Parameters["p_ETT_ID"].Value =  Convert.ToInt32(ddlETT.SelectedValue);

        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";
            oracom.Parameters["p_status_cd"].Value = OracleString.Null;
        }
        else
        {
            oracom.Parameters["p_proc_flg"].Value = "Y";
            oracom.Parameters["p_status_cd"].Value = ddlMatch.SelectedValue;
        }
        if(txtAccountNo .Text .Trim().Length >0)
            oracom.Parameters["p_account_number"].Value = txtAccountNo .Text.Trim() ;
        else
            oracom.Parameters["p_account_number"].Value =OracleString .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());
            }
        }
    }

    private bool CheckATMExist()
    {
        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();
        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")
        {
            return false;
        }
        else
        {
            return true;
        }
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (DatePicker1.date.Length != 0 && DatePicker2.date.Length !=0)
            {
                //if (txtTerminal.Text != "" && CheckATMExist())
                //{
                //    loadResult(Helper.gridSize(), 1);
                //    pnlResult.Visible = true;
                //    pnlControls.Visible = false;
                //}
                //else
                //{
                //    lbTermNotFoundlError.Visible = true;
                //    lbTermNotFoundlError.Text = "ATM Not Found ";
                //    pnlResult.Visible = false;
                //    //return;
                //}
                loadResult(Helper.gridSize(), 1);
                pnlResult.Visible = true;
                pnlControls.Visible = false;

            }
            else
            {
                pnlResult.Visible = false;
                pnlControls.Visible = false;
            }

        }
        else
        {
            pnlResult.Visible = false;
            pnlControls.Visible = false;
        }
     
    }

    private void loadETT()
    {

        ddlETT.DataSource = Lookup.lkp_Get_ETT();
        ddlETT.DataBind();
        ddlETT.Items.Insert(0, new ListItem("------ All Tran. Type ------","0"));
       
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        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"))
        {
            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;
            //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;
    }

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

    private void loadStatus(string termid, string trace, string date, string status)
    {
        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();
    }
    protected void cvAccountNo_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (rdOnUs.Checked)
            args.IsValid = txtAccountNo.Text.Trim().Length > 0;
    }
    protected void rdOther_CheckedChanged(object sender, EventArgs e)
    {
        txtAccountNo.Text = "";
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtTerminal.Text = "";
        DatePicker1.date = "";
        DatePicker2.date = "";
        ddlMatch.SelectedIndex = -1;
        txtCardNumber.Text = "";
        txtTraceID.Text = "";
        rdOnUs.Checked = true;
        txtAccountNo.Text = "";
        pnlResult.Visible = false;
        pnlControls.Visible = false;

    }
}
