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 POS_ClaimsATM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadStatus();
            loadEvents();
            loadSamaStatuses();
            loadSamaClaimTypes();
            rdATM_CheckedChanged(sender, e);
            //loadClaims(Helper.gridSize(), 1);
            ExceptionUtility.LogException(0, "", "42", "Page_Load", "");
        }
        lblError.Text = "";
    }

    protected void rdMADAType_CheckedChanged(object sender, EventArgs e)
    {
        loadStatus();
        loadEvents();
        loadSamaClaimTypes();
        rdATM_CheckedChanged(sender, e);
        loadSamaStatuses();
        lblError.Text = "";
        ExceptionUtility.LogException(0, "", "42", "rdMADAType_CheckedChanged", "");
    }

    protected void rdGCCNETType_CheckedChanged(object sender, EventArgs e)
    {

        loadStatus();
        loadEvents();
        loadSamaClaimTypes();
        rdATM_CheckedChanged(sender, e);
        loadSamaStatuses();
        ExceptionUtility.LogException(0, "", "42", "rdGCCNETType_CheckedChanged", "");
    }

    /// <summary>
    /// IJR 
    /// 28-Jan-2012
    /// Add The load events functions to get the events related to ATM (1)
    /// </summary>
    private void loadEvents()
    {
        try
        {
            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_events";

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

            oracom.Parameters["p_product"].Value = 1;

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

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

            oracon.Close();
            ddlEvents.Items.Insert(0, "------ Events ------");
            //ExceptionUtility.LogException("Success", "LOOKUP_GETTER.get_events", "loadEvents", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.get_events","", "loadEvents", "");
     
        }
    }

    /// <summary>
    /// IJR
    /// 30-Jan-2012
    /// Load Sama Statuses
    /// </summary>
    private void loadSamaStatuses()
    {
        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 = "poslookup_getter.get_claim_samastatus";

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

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

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


            ddlSamaStatuses.Items.Insert(0, "------ All ------");
            oracon.Close();
           // ExceptionUtility.LogException("Success", "poslookup_getter.get_claim_samastatus", "loadSamaStatuses", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "poslookup_getter.get_claim_samastatus","", "loadSamaStatuses", "");

        }
    }

    /// <summary>
    /// IJR
    /// 18-Feb-2012
    /// Load Sama claim types
    /// </summary>
    private void loadSamaClaimTypes()
    {
        try
        {
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            if (rdGCCNETType.Checked)
            {
                oracom.CommandText = "Poslookup_Getter.GET_GCC_CLAIM_TYPES";
                oracom.Parameters.Add("p_product", OracleType.Number).Direction = ParameterDirection.Input;
                oracom.Parameters["p_product"].Value = 1;
            }
            else
            {
                oracom.CommandText = "lookup_getter.GET_SAMA_CLAIM_TYPES";
            }
            //oracom.CommandText = "lookup_getter.GET_SAMA_CLAIM_TYPES";

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

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

            ddlClaimType2.DataSource = ds;
            ddlClaimType2.DataBind();
            ddlClaimType2.Items.Insert(0, "------ All ------");
            oracon.Close();
            //ExceptionUtility.LogException("Success", "lookup_getter.GET_SAMA_CLAIM_TYPES", "loadSamaClaimTypes", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "lookup_getter.GET_SAMA_CLAIM_TYPES","", "loadSamaClaimTypes", "");

        }
    }
    
    private void loadStatus()
    {
        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 = "poslookup_getter.get_claim_status";

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

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

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


            ddlClaimType.Items.Insert(0, "------ All ------");
            oracon.Close();
            //ExceptionUtility.LogException("Success", "poslookup_getter.get_claim_status", "loadStatus", "");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "poslookup_getter.get_claim_status","", "loadStatus", "");

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddClaim.aspx?p=1");
        ExceptionUtility.LogException(0, "", "42.230", "btnAdd_Click", "");
    }

    protected void btnAdd1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddMClaim1.aspx");
        ExceptionUtility.LogException(0, "", "42.425", "btnAdd_Click", "");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        txtClaimID.Text = txtClaimID.Text.Trim();
        txtReference.Text = txtReference.Text.Trim();
        txtCardNumber.Text = txtCardNumber.Text.Trim();
        txtATMNo.Text = txtATMNo.Text.Trim();
        //IJR - 28-1-2012 , Add the original claim ID to search Criteria
        txtOrgClaimID.Text = txtOrgClaimID.Text.Trim();

        if (ddlClaimType.SelectedValue.ToString() == "301" && ddlSamaStatuses.SelectedValue.ToString() == "1" )
        {
            ExceptionUtility.LogException(0, "", "42.226", "btnSearch_Click", "");
                loadClaims(Helper.gridSize(), 1);
                
        }
        else
            if (txtATMNo.Text.Length == 0 && txtCardNumber.Text.Length == 0
                && txtClaimID.Text.Length == 0 && dtTranDate.date.Length == 0
              && txtOrgClaimID.Text.Length == 0)
            {
                lblError.Text = "please enter at least one of the marked fields";
                ExceptionUtility.LogException(9, "", "42.226", "btnSearch_Click", "");
            }
            else
            {
                ExceptionUtility.LogException(0, "", "42.226", "btnSearch_Click", "");
                loadClaims(Helper.gridSize(), 1);
                
            }
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadClaims(Helper.gridSize(), int.Parse(ddlPages.SelectedValue));
        ExceptionUtility.LogException(0, "", "", "ddlPages_SelectedIndexChanged", "");
    }

    private void loadClaims(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 = "claim_process.get_atmposclaim_info";

            oracom.Parameters.Add("c_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_claim_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_card_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_rrn", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_tran_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_product", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_client_number", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_retailer_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_term_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("c_claim_status", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_atm_num", OracleType.VarChar).Direction = ParameterDirection.Input;
            //oracom.Parameters.Add("p_onus", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_onus", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_mer_ch", OracleType.Number).Direction = ParameterDirection.Input;

            //IJR - 28-1-2012 , Add the original claim ID to search Criteria
            oracom.Parameters.Add("p_org_claim_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            //IJR - 28-1-2012 , Add the Event ID to search Criteria
            oracom.Parameters.Add("p_EventId", OracleType.VarChar).Direction = ParameterDirection.Input;
            //IJR - 30-1-2012 , edd claim action status to search Criteria
            oracom.Parameters.Add("p_claim_action_status", 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_getclaim_atmpos", OracleType.Cursor).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;


            

            if (txtClaimID.Text.Length > 0)
            {
                oracom.Parameters["c_claim_id"].Value = txtClaimID.Text;
                parameters = "c_claim_id-" + txtClaimID.Text+":";
            }
            else
            {
                oracom.Parameters["c_claim_id"].Value = OracleString.Null;
                // oracom.Parameters["c_claim_id"].Value = txtClaimID.Text;

            }
            if (txtReference.Text.Length > 0)
            {
                parameters = parameters + "c_rrn-" + txtReference.Text+":";
                oracom.Parameters["c_rrn"].Value = txtReference.Text;
            }
            else
                oracom.Parameters["c_rrn"].Value = OracleString.Null;
            //oracom.Parameters["c_rrn"].Value = OracleString.Null;

            oracom.Parameters["c_tran_date"].Value = dtTranDate.date;
            if (dtTranDate.date.Length > 0)
                parameters = parameters + "c_tran_date-" + dtTranDate.date + ":";
            if (txtClientNo.Text.Length > 0)
            {
                parameters = parameters + "c_client_number-" + txtClientNo.Text + ":";
                oracom.Parameters["c_client_number"].Value = txtClientNo.Text;
            }
            else
                oracom.Parameters["c_client_number"].Value = OracleString.Null;
            //oracom.Parameters["c_client_number"].Value = txtClientNo.Text;

            if (txtRetailerID.Text.Length > 0)
            {
                parameters = parameters + "c_retailer_id-" + txtRetailerID.Text + ":";
                oracom.Parameters["c_retailer_id"].Value = txtRetailerID.Text;
            }
            else
                oracom.Parameters["c_retailer_id"].Value = OracleString.Null;
            //oracom.Parameters["c_retailer_id"].Value = txtRetailerID.Text;


            if (txtTermID.Text.Length > 0)
            {
                parameters = parameters + "c_term_id" + txtTermID.Text + ":";
                oracom.Parameters["c_term_id"].Value = txtTermID.Text;
            }
            else
                oracom.Parameters["c_term_id"].Value = OracleString.Null;
            //oracom.Parameters["c_term_id"].Value = txtTermID.Text;


            if (txtATMNo.Text.Length > 0)
            {
                parameters = parameters + "p_atm_num-" + txtATMNo.Text + ":";
                oracom.Parameters["p_atm_num"].Value = txtATMNo.Text;
            }
            else
                oracom.Parameters["p_atm_num"].Value = OracleString.Null;
            //oracom.Parameters["p_atm_num"].Value = txtATMNo.Text;

            //IJR - 31-1-2012 , On US OF Us Handling

            oracom.Parameters["c_product"].Value = "1";
            parameters = parameters + "c_product-1"+":";
            oracom.Parameters["p_mer_ch"].Value = OracleNumber.Null;




            //if (ddlOnOf.Enabled == false)
            //{
            //    oracom.Parameters["p_onus"].Value = "0";
            //}
            //else
            //{
            //    if(ddlOnOf.SelectedIndex > 0)
            //        oracom.Parameters["p_onus"].Value = ddlOnOf.SelectedValue;
            //    else
            //        oracom.Parameters["p_onus"].Value = "0";//??
            //}

            if (txtCardNumber.Text.Length > 0)
            {
                parameters = parameters + "c_card_number-" + Helper.Mask_CardNumber(txtCardNumber.Text)  + ":";
                oracom.Parameters["c_card_number"].Value = txtCardNumber.Text;
            }
            else
                oracom.Parameters["c_card_number"].Value = OracleString.Null;


            ////oracom.Parameters["c_card_number"].Value = txtCardPOS.Text;
            //if (rdATM.Checked)
            //    oracom.Parameters["c_card_number"].Value = txtCardNumber.Text;
            //else
            //    oracom.Parameters["c_card_number"].Value = txtCardPOS.Text;
            //if (rdATM.Checked)
            //{
            //    oracom.Parameters["c_product"].Value = "1";
            //    oracom.Parameters["p_mer_ch"].Value = OracleNumber.Null;

            //    if (rdOnUs.Checked)
            //        oracom.Parameters["p_onus"].Value = "1";
            //    else
            //        oracom.Parameters["p_onus"].Value = "0";
            //}
            //else
            //{
            //    oracom.Parameters["c_product"].Value = "2";
            //    oracom.Parameters["p_onus"].Value = OracleNumber.Null;

            //    if (rdCH.Checked)
            //        oracom.Parameters["p_mer_ch"].Value = "0";
            //    else
            //        oracom.Parameters["p_mer_ch"].Value = "1";
            //}

            if (ddlClaimType.SelectedIndex > 0)
            {
                parameters = parameters + "c_claim_status-" + ddlClaimType.SelectedItem.Text + ":";
                oracom.Parameters["c_claim_status"].Value = ddlClaimType.SelectedValue;
            }
            else
                oracom.Parameters["c_claim_status"].Value = OracleString.Null;

            //IJR - 28-1-2012 , Add the original claim ID to search Criteria
            if (txtOrgClaimID.Text.Length > 0)
            {
                parameters = parameters + "p_org_claim_id-" + txtOrgClaimID.Text + ":";
                oracom.Parameters["p_org_claim_id"].Value = txtOrgClaimID.Text;
            }
            else
                oracom.Parameters["p_org_claim_id"].Value = OracleString.Null;
            // oracom.Parameters["p_org_claim_id"].Value = txtOrgClaimID.Text;
            //IJR - 28-1-2012 , Add the Event ID to search Criteria
            if (ddlEvents.SelectedIndex > 0)
            {
                parameters = parameters + "p_EventId" + ddlEvents.SelectedItem.Text + ":";
                oracom.Parameters["p_EventId"].Value = ddlEvents.SelectedValue;
            }
            else
                oracom.Parameters["p_EventId"].Value = OracleString.Null;

            ////IJR - 30-1-2012 , edd claim action status to search Criteria
            if (ddlSamaStatuses.Enabled == false)
                oracom.Parameters["p_claim_action_status"].Value = OracleString.Null;
            else
                if (ddlSamaStatuses.SelectedIndex > 0)
                {
                    parameters = parameters + "p_claim_action_status" + ddlSamaStatuses.SelectedItem.Text + ":";
                    oracom.Parameters["p_claim_action_status"].Value = ddlSamaStatuses.SelectedValue;
                }
                else
                    oracom.Parameters["p_claim_action_status"].Value = OracleString.Null;


            if (rdOnUs.Enabled)
                if (rdOnUs.Checked)
                {
                    parameters = parameters + "p_onus-C" + ":";
                    oracom.Parameters["p_onus"].Value = "C";
                }
                else
                {
                    parameters = parameters + "p_onus-S" + ":";
                    oracom.Parameters["p_onus"].Value = "S";
                }
            else
            {
                parameters = parameters + "p_onus-C" + ":";
                oracom.Parameters["p_onus"].Value = "C";
            }


            //Start --> 18-2-2012 IJR: Add new parameter to define the ATM Type (CW or DEP)
            // and add the sama from/to date and the sma claim type
            oracom.Parameters.Add("p_atmType", OracleType.VarChar).Direction = ParameterDirection.Input;
            if (ddlATM_Type.SelectedIndex > 0)
            {
                parameters = parameters + "p_atmType-" + ddlATM_Type.SelectedItem.Text + ":";
                oracom.Parameters["p_atmType"].Value = ddlATM_Type.SelectedValue;
            }
            else
            {
                oracom.Parameters["p_atmType"].Value = OracleString.Null;
            }

            oracom.Parameters.Add("p_sama_from_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_sama_to_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_sama_claim_type", OracleType.VarChar).Direction = ParameterDirection.Input;

            if (dtSamaFromDate.date.Length > 0)
            {
                parameters = parameters + "p_sama_from_date-" + dtSamaFromDate.date + ":";
                oracom.Parameters["p_sama_from_date"].Value = dtSamaFromDate.date;
            }
            else
                oracom.Parameters["p_sama_from_date"].Value = OracleString.Null;

            if (dtSameToDate.date.Length > 0)
            {
                parameters = parameters + "p_sama_to_date-" + dtSameToDate.date + ":";
                oracom.Parameters["p_sama_to_date"].Value = dtSameToDate.date;
            }
            else
                oracom.Parameters["p_sama_to_date"].Value = OracleString.Null;


            if (ddlClaimType2.SelectedIndex > 0)
            {
                parameters = parameters + "p_sama_claim_type-" + ddlClaimType2.SelectedItem.Text + ":";
                oracom.Parameters["p_sama_claim_type"].Value = ddlClaimType2.SelectedValue;
            }
            else
                oracom.Parameters["p_sama_claim_type"].Value = OracleString.Null;

            //END --> 18-2-2012 IJR: Add new parameter to define the ATM Type (CW or DEP)
            // and add the sama from/to date and the sma claim type

            oracom.Parameters["p_page_size"].Value = pageSize;
            oracom.Parameters["p_page_num"].Value = pageNumber;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["c_current_uid"].Value = Session["uid"].ToString();

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            rdMADAType.Checked = false;
            rdGCCNETType.Checked = false;
            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());
                }
            }
             
            //parameters = parameters.Substring(parameters.Length-1);
            if (currentMethod.Contains("btnSearch_Click"))
                ExceptionUtility.LogException(0, "claim_process.get_atmposclaim_info", "226.1", "loadClaims", parameters );
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(0, "claim_process.get_atmposclaim_info", "226.1", "loadClaims", parameters);
            GridView1.DataSource = null ;
            GridView1.DataBind();
        }
    }

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

            //By Ibrahim Al-Ashqar
            //For ANB Bank, because in it we are not using the departments concept
            //if (authenticateUser(rowID))

            ExceptionUtility.LogException(0, "", "42.227", "GridView1_RowCommand.Investigate", "");//c_claim_id: " + txtClaimID.Text + ", c_rrn: " + txtReference.Text + ", c_tran_date: " + dtTranDate.date + ", c_client_number: " + txtClientNo.Text + ", c_retailer_id: " + txtRetailerID.Text + ", c_term_id: " + txtTermID.Text + ", p_atm_num: " + txtATMNo.Text + ", c_product: 1, c_card_number: " + txtCardNumber.Text + ", c_claim_status: " + ddlClaimType.SelectedValue + ", p_org_claim_id: " + txtOrgClaimID.Text + ", p_EventId: " + ddlEvents.SelectedValue + ", p_claim_action_status: " + ddlSamaStatuses.SelectedValue + ",  p_atmType:" + ddlATM_Type.SelectedValue + ", p_sama_from_date: " + dtSamaFromDate.date + ", p_sama_to_date: " + dtSameToDate.date + ",  p_sama_claim_type: " + ddlClaimType2.SelectedValue);
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                            "window.open('InvestigateClaim.aspx?clID=" + rowID +"','Redirect')", true);


//            Response.Redirect("InvestigateClaim.aspx?clID=" + rowID);
            
            //else
            //  lblError.Text = "You are not authorized to investigate this claim";
        }
        else if (e.CommandName.Equals("d"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string claimStatus = GridView1.Rows[index].Cells[7].Text;
            if (!claimStatus.Equals("Posted Claim"))
            {
                string error = "";
                ExceptionUtility.LogException(0, "", "42.228", "GridView1_RowCommand.Delete", "");
                bool saved = Delete(rowID, ref error);
                if (saved)
                {

                    lblError.Text = "Claim Inactivated Successful";
                }
                else
                {
                   
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "Reset Failed";
                        ExceptionUtility.LogException(9, "", "42.228", "GridView1_RowCommand.Delete", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            else
            {
                lblError.Text = "Posted Claim Can not be Inactivated";
            }
        }
        else if (e.CommandName.Equals("r"))
        {
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string error = "";
            ExceptionUtility.LogException(0, "", "42.366", "GridView1_RowCommand.Reset", "");
            bool saved = Reset(rowID, ref error);
            if (saved)
            {
                
                lblError.Text = "Claim Reset Successful";
            }
            else
            {
                
                if (Helper.OracleError(error))
                {
                    lblError.Text = "Reset Failed";
                    ExceptionUtility.LogException(0, "", "42.366", "GridView1_RowCommand.Reset", error);
                }
                else
                    lblError.Text = error;
            }
        }
        loadClaims(Helper.gridSize(), 1);
    }

    private bool Delete(string index, ref string error)
    {
        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 = "claim_process.inactive_claim_info";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_claim_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["p_claim_id"].Value = index;

            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")
            {
                ExceptionUtility.LogException(0, "claim_process.inactive_claim_info", "228.1", "Delete", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "claim_process.inactive_claim_info", "228.1", "Delete", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "claim_process.inactive_claim_info", "228.1", "Delete", "index: " + index);
            return false;
        }
    }
    private bool Reset(string index, ref string error)
    {
        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 = "claim_process.resetclm_to_orgclmstatus";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_claim_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["p_claim_id"].Value = index;

            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")
            {
                ExceptionUtility.LogException(0, "claim_process.resetclm_to_orgclmstatus","366.1", "Reset", "index: " + index);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "claim_process.resetclm_to_orgclmstatus", "366.1", "Reset", "index: " + index);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "claim_process.resetclm_to_orgclmstatus", "366.1", "Reset", "index: " + index);
            return false;
        }
    }

    protected void rdATM_CheckedChanged(object sender, EventArgs e)
    {
        rowATM.Visible = true;
        rowATM2.Visible = true;
        rowATM3.Visible = true;
        rowPOS.Visible = false;
        rowPOS2.Visible = false;
        rowPOS3.Visible = false;
        btnAdd1.Visible = false;

    }

    protected void rdPOS_CheckedChanged(object sender, EventArgs e)
    {
        rowATM.Visible = false;
        rowATM2.Visible = false;
        rowATM3.Visible = false;
        rowPOS.Visible = true;
        rowPOS2.Visible = true;
        rowPOS3.Visible = true;
        btnAdd1.Visible = true;
    }

    private bool authenticateUser(string claimID)
    {
        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 = "CLAIM_SECURITY.get_auth_user_claim";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_claim_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_user_id", 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_out_auth_user", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_claim_id"].Value = claimID;
            oracom.Parameters["p_user_id"].Value = Session["uid"].ToString();

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

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_out_auth_user"].Value.ToString();
            if (a == "1")
            {
                ExceptionUtility.LogException(0, "CLAIM_SECURITY.get_auth_user_claim","", "authenticateUser", "claimID: " + claimID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "CLAIM_SECURITY.get_auth_user_claim","", "authenticateUser", "claimID: " + claimID);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "CLAIM_SECURITY.get_auth_user_claim","", "authenticateUser", "claimID: " + claimID );
            return false;
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ddlClaimType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlClaimType.SelectedValue == "301")
        {
            ddlSamaStatuses.Enabled = true;
            rdOnUs.Enabled = true;
            rdOther.Enabled = true;
            
        }
        else
        {
            ddlSamaStatuses.Enabled = false;
            rdOnUs.Enabled = false;
            rdOther.Enabled = false;
        }
        ExceptionUtility.LogException(0, "", "", "ddlClaimType_SelectedIndexChanged", "");
    }
    protected void ddlEvents_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlEvents.SelectedValue == "4"
            || ddlEvents.SelectedValue == "10"
            || ddlEvents.SelectedValue == "12"
            || ddlEvents.SelectedValue == "13"
            || ddlEvents.SelectedValue == "111"
            || ddlEvents.SelectedValue == "112"
            || ddlEvents.SelectedValue == "113"
            || ddlEvents.SelectedValue == "114"
            || ddlEvents.SelectedValue == "115")
            ddlATM_Type.Enabled = true;
        else
            ddlATM_Type.Enabled = false;
        ExceptionUtility.LogException(0, "", "", "ddlClaimType_SelectedIndexChanged", "");
    }
    protected void ddlATM_Type_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    
    protected void btnGenReport_Click(object sender, EventArgs e)
    {
        txtClaimID.Text = txtClaimID.Text.Trim();
        txtReference.Text = txtReference.Text.Trim();
        txtCardNumber.Text = txtCardNumber.Text.Trim();
        txtATMNo.Text = txtATMNo.Text.Trim();
        //IJR - 28-1-2012 , Add the original claim ID to search Criteria
        txtOrgClaimID.Text = txtOrgClaimID.Text.Trim();

        if (ddlClaimType.SelectedValue.ToString() == "301" && ddlSamaStatuses.SelectedValue.ToString() == "1")
        {
            ExceptionUtility.LogException(0, "", "42.465", "btnGenReport_Click", "");
            loadClaimsReport();
        }
        else
            if (txtATMNo.Text.Length == 0 && txtCardNumber.Text.Length == 0
                && txtClaimID.Text.Length == 0 && dtTranDate.date.Length == 0
              && txtOrgClaimID.Text.Length == 0)
            {
                ExceptionUtility.LogException(9, "", "42.465", "btnGenReport_Click", "");
                lblError.Text = "please enter at least one of the marked fields";
            }
            else
            {
                ExceptionUtility.LogException(9, "", "42.465", "btnGenReport_Click", "");
                loadClaimsReport();
            }
    }



    private void loadClaimsReport()
    {
        string c_current_uid = "";
        string c_claim_id = "";
        string c_card_number = "";
        string c_rrn = "";
        string c_tran_date = "";
        string c_product = "";
        string c_client_number = "";
        string c_retailer_id = "";
        string c_term_id = "";
        string c_claim_status = "";
        string p_atm_num = "";
        string p_onus = "";
        int p_mer_ch = -1;
        string p_org_claim_id = "";
        string p_EventId = "";
        string p_claim_action_status = "";
        string p_atmType = "";
        string p_sama_from_date  = "";
        string p_sama_to_date  = "";
        string p_sama_claim_type = "";

        int p_page_size = 0;
        int p_page_num = 0;
        string p_lang = "EN";

        if (txtClaimID.Text.Length > 0)
            c_claim_id = txtClaimID.Text;
        else
            c_claim_id = null;

        if (txtReference.Text.Length > 0)
            c_rrn = txtReference.Text;
        else
            c_rrn = null;

        c_tran_date = dtTranDate.date;

        if (txtClientNo.Text.Length > 0)
            c_client_number = txtClientNo.Text;
        else
            c_client_number = null;

        if (txtRetailerID.Text.Length > 0)
            c_retailer_id = txtRetailerID.Text;
        else
            c_retailer_id =null;

        if (txtTermID.Text.Length > 0)
            c_term_id = txtTermID.Text;
        else
            c_term_id = null;


        if (txtATMNo.Text.Length > 0)
            p_atm_num = txtATMNo.Text;
        else
            p_atm_num = null;

        c_product = "1";
        //p_mer_ch = null;

        if (txtCardNumber.Text.Length > 0)
           c_card_number = txtCardNumber.Text;
        else
            c_card_number = null;

        if (ddlClaimType.SelectedIndex > 0)
           c_claim_status= ddlClaimType.SelectedValue;
        else
            c_claim_status = null;

        if (txtOrgClaimID.Text.Length > 0)
            p_org_claim_id = txtOrgClaimID.Text;
        else
            p_org_claim_id = null;
       

        if (ddlEvents.SelectedIndex > 0)
            p_EventId  = ddlEvents.SelectedValue;
        else
            p_EventId = null;

        if (ddlSamaStatuses.Enabled == false)
            p_claim_action_status = null;
        else
            if (ddlSamaStatuses.SelectedIndex > 0)
                p_claim_action_status  = ddlSamaStatuses.SelectedValue;
            else
                p_claim_action_status = null;


        if (rdOnUs.Enabled)
            if (rdOnUs.Checked)
                p_onus = "C";
            else
                 p_onus = "S";
        else
             p_onus = "C";


        if (ddlATM_Type.SelectedIndex > 0)
        {
            p_atmType = ddlATM_Type.SelectedValue;
        }
        else
        {
            p_atmType = null;
        }        

        if (dtSamaFromDate.date.Length > 0)
            p_sama_from_date = dtSamaFromDate.date;
        else
            p_sama_from_date = null;

        if (dtSameToDate.date.Length > 0)
            p_sama_to_date = dtSameToDate.date;
        else
            p_sama_to_date = null;


        if (ddlClaimType2.SelectedIndex > 0)
            p_sama_claim_type = ddlClaimType2.SelectedValue;
        else
            p_sama_claim_type = null;

        c_current_uid = Session["uid"].ToString();
        
        string UrlString = "rpt=506&c_current_uid="+c_current_uid
                            +"&c_claim_id="+c_claim_id
                            +"&c_card_number="+c_card_number
                            +"&c_rrn="+c_rrn
                            +"&c_tran_date="+c_tran_date
                            +"&c_product="+c_product
                            +"&c_client_number="+c_client_number
                            +"&c_retailer_id="+c_retailer_id
                            +"&c_term_id="+c_term_id
                            +"&c_claim_status="+c_claim_status
                            +"&p_atm_num="+p_atm_num
                            +"&p_onus="+p_onus
                            +"&p_mer_ch="+p_mer_ch
                            +"&p_org_claim_id="+p_org_claim_id
                            +"&p_EventId="+p_EventId
                            +"&p_claim_action_status="+p_claim_action_status
                            +"&p_atmType="+p_atmType
                            +"&p_sama_from_date="+p_sama_from_date
                            +"&p_sama_to_date="+p_sama_to_date
                            +"&p_sama_claim_type="+p_sama_claim_type
                            +"&p_page_size="+p_page_size
                            +"&p_page_num="+p_page_num
                            +"&p_lang="+p_lang;

        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
            "window.open('../Reports/Default.aspx?" + UrlString + "','Report')", true);
    }





}
