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 NotificationSendMsgGenerator;
using EjadaMQProcessor;

public partial class Business_ApproveClaimsPOS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadStatus();
            loadEvents();
            loadSamaStatuses();
            loadSamaClaimTypes();
            rdPOS_CheckedChanged(sender, e);
            //loadClaims(Helper.gridSize(), 1);
        }
    }

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

    /// <summary>
    /// IJR 
    /// 28-Jan-2012
    /// Add The load events functions to get the events related to POS (2)
    /// </summary>
    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_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 = 2;

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

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

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



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


    private void loadStatus()
    {
        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);
        oracon.Close();

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

        int p = 0;
        for (int i = 0; i < ddlClaimType.Items.Count; i++)
        {
            if (ddlClaimType.Items[i].Value == "003" || ddlClaimType.Items[i].Value == "005" || ddlClaimType.Items[i].Value == "007")
                p = 0;
            else
            {
                ddlClaimType.Items.RemoveAt(i);
                i--;
            }
        }
        ddlClaimType.SelectedValue = "007";
    }

    private void loadATMAcc(string claimID)
    {
        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_APPROVER.get_atm_glaccts";

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

        oracom.Parameters["p_claim_id"].Value = claimID;
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);
        oracon.Close();

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

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //pnlApprove.Visible = false;
        txtClaimID.Text = txtClaimID.Text.Trim();
        txtClientNo.Text = txtClientNo.Text.Trim();
        txtTermID.Text = txtTermID.Text.Trim();
        txtRetailerID.Text = txtRetailerID.Text.Trim();
        txtCardPOS.Text = txtCardPOS.Text.Trim();

        //IJR - 28-1-2012 , Add the original claim ID to search Criteria
        txtOrgClaimID.Text = txtOrgClaimID.Text.Trim();

        if (("".Equals(txtcfdate.Text.Trim()) && txtctdate.Text.Trim().Length > 3) || ("".Equals(txtctdate.Text.Trim()) && txtcfdate.Text.Trim().Length > 3))
        {
            //lblError.Text = "Please Enter both (From and To) Date";
            Notifications.showMessage("Please Enter both (From and To) Date", this, Notifications.errorMessage, false);
            return;
        }
        else if (txtTermID.Text.Length == 0 && txtCardPOS.Text.Length == 0 && txtClaimID.Text.Length == 0)
        {
            //lblError.Text = "please enter at least one of the marked fields";
            Notifications.showMessage("Please enter at least one of the marked fields", this, Notifications.errorMessage, false);
            return;
        }
        else
        {
            loadClaims(Helper.gridSize(), 1);
        }
    }

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

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

        //IRFAN_MOD SAIB_CR 170820 starts
        // oracom.Parameters.Add("c_tran_date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_claim_date_from", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_claim_date_To", OracleType.VarChar).Direction = ParameterDirection.Input;

        //oracom.Parameters.Add("p_claim_Date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_on_off_Us_Flag", OracleType.VarChar).Direction = ParameterDirection.Input;
        // Ends
        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_mer_ch", 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_getclaim_atmpos", OracleType.Cursor).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("p_total_pages", OracleType.Number).Direction = ParameterDirection.Output;

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

        //if (rdATM.Checked)
        //    oracom.Parameters["c_card_number"].Value = txtCardNumber.Text;
        //else
        //    oracom.Parameters["c_card_number"].Value = txtCardPOS.Text;

        if (txtCardPOS.Text.Length > 0)
            oracom.Parameters["c_card_number"].Value = txtCardPOS.Text;
        else
            oracom.Parameters["c_card_number"].Value = OracleString.Null;


        //irfan_mod saib_cr uat 170820 starts

        ////IRFAN_MOD SAIB_CR 170202 starts
        //oracom.Parameters["p_claim_date"].Value = dtClaimDate.date;
        //oracom.Parameters["p_tran_date_from"].Value = dtTranDateFrom.date;
        //oracom.Parameters["p_tran_date_To"].Value = dtTranDateTo.date;

        oracom.Parameters["p_claim_date_from"].Value = pickcfdate.Text;
        oracom.Parameters["p_claim_date_To"].Value = pickctdate.Text;


        if (rdClaimOnUS.Checked == true)
            oracom.Parameters["p_on_off_Us_Flag"].Value = "on-us";
        else
            oracom.Parameters["p_on_off_Us_Flag"].Value = "off-us";

        //IRFAN_MOD ENDS

        oracom.Parameters["c_claim_id"].Value = txtClaimID.Text;
        oracom.Parameters["c_rrn"].Value = txtReference.Text;
        //  oracom.Parameters["p_claim_Date"].Value = dtClaimDate.date;
        oracom.Parameters["c_client_number"].Value = txtClientNo.Text;
        oracom.Parameters["c_retailer_id"].Value = txtRetailerID.Text;
        oracom.Parameters["c_term_id"].Value = txtTermID.Text;
        oracom.Parameters["p_atm_num"].Value = txtATMNo.Text;


        //IJR - 28-1-2012 , Add the original claim ID to search Criteria
        if (txtOrgClaimID.Text.Length > 0)
            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)
            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)
            oracom.Parameters["p_claim_action_status"].Value = ddlSamaStatuses.SelectedValue;
        else
            oracom.Parameters["p_claim_action_status"].Value = OracleString.Null;


        //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;
        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 (txtsfdate.Text.Length > 0)
        {
            string d, m, y;
            string[] values = txtsfdate.Text.Split('-');
            d = values[0].ToString();
            m = values[1].ToString();
            y = values[2].Substring(2).ToString();

            picksfdate.Text = y + m + d;
            oracom.Parameters["p_sama_from_date"].Value = picksfdate.Text;
        }
        else
        {
            oracom.Parameters["p_sama_from_date"].Value = OracleString.Null;
        }

        if (txtstdate.Text.Length > 0)
        {
            string d, m, y;
            string[] values = txtstdate.Text.Split('-');
            d = values[0].ToString();
            m = values[1].ToString();
            y = values[2].Substring(2).ToString();

            pickstdate.Text = y + m + d;
            oracom.Parameters["p_sama_to_date"].Value = pickstdate.Text;
        }
        else
        {
            oracom.Parameters["p_sama_to_date"].Value = OracleString.Null;
        }

        if (ddlClaimType2.SelectedIndex > 0)
            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
        //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";
        //}

        oracom.Parameters["c_product"].Value = "2";

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


        if (rdOnUs.Enabled)
            if (rdOnUs.Checked)
                oracom.Parameters["p_onus"].Value = "0";
            else
                oracom.Parameters["p_onus"].Value = "1";
        else
            oracom.Parameters["p_onus"].Value = "0";


        oracom.Parameters["c_claim_status"].Value = ddlClaimType.SelectedValue;

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

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

        pnlResult.Visible = true;
    }

    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();
            //if (authenticateUser(rowID))
            // Response.Redirect("InvestigateClaim.aspx?clID=" + rowID);

            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "new window",
                           "window.open('InvestigateClaim.aspx?clID=" + rowID + "','Redirect')", true);
            //else
            //lblError.Text = "You are not authorized to investigate this claim";
        }
        else if (e.CommandName.Equals("a"))
        {
            Lbl_CommandName.Text = "a";
            Lbl_CommandArgument.Text = e.CommandArgument.ToString();
            Lbl_URC_Message.Text = "Are you sure you want to Approve ?";

            Notifications.showModal("UR-confirmation-modal-xl", this);
        }
        else if (e.CommandName.Equals("d"))
        {
            Lbl_CommandName.Text = "d";
            Lbl_CommandArgument.Text = e.CommandArgument.ToString();
            Lbl_URC_Message.Text = "Are you sure you want to Reject ?";

            Notifications.showModal("UR-confirmation-modal-xl", this);
        }
        else if (e.CommandName.Equals("r"))
        {
            Lbl_CommandName.Text = "r";
            Lbl_CommandArgument.Text = e.CommandArgument.ToString();
            Lbl_URC_Message.Text = "Are you sure you want to Reset ?";

            Notifications.showModal("UR-confirmation-modal-xl", this);
        }
        else if (e.CommandName.Equals("s"))
        {
            //int index = int.Parse(e.CommandArgument.ToString());
            //string rowID = GridView1.DataKeys[index].Value.ToString();//claim
            //string cardNumber = GridView1.Rows[index].Cells[1].Text;//Card Number                      
            //string days = "", events = "";
            //string claimStatus = GridView1.Rows[index].Cells[14].Text;


            //if (claimStatus.Trim().Equals("003"))
            //{
            //    events = ConfigurationSettings.AppSettings["ApprovedClaimEvent"];
            //    days = ConfigurationSettings.AppSettings["ClaimDays"];
            //}
            //if (claimStatus.Trim().Equals("005"))
            //{
            //    events = ConfigurationSettings.AppSettings["RejectedClaimEvent"];
            //    days = "";
            //}

            //if (!claimStatus.Trim().Equals("003") && !claimStatus.Trim().Equals("005"))
            //    lblError.Text = "Cannot send Notification";
            //else
            //{
            //    string contactNumber = SendNotification(cardNumber, rowID, events, days);
            //    lblError.Text = contactNumber;
            //}


        }
        loadClaims(Helper.gridSize(), 1);
    }

    protected void btn_URConfirm_OK_Click(object sender, EventArgs e)
    {
        if (Lbl_CommandName.Text == "a")
        {
            //int index = int.Parse(e.CommandArgument.ToString());
            int index = int.Parse(Lbl_CommandArgument.Text);
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string product = GridView1.Rows[index].Cells[08].Text;
            string amount = GridView1.Rows[index].Cells[09].Text;
            string approved = GridView1.Rows[index].Cells[5].Text;
            if (!approved.ToLower().Equals("approved claim"))
            {
                if (product == "2")
                {
                    //POS
                    //pnlApprove.Visible = true;
                    ddlATMAcc.Visible = false;
                    //ddlFromAcct.Visible = true;
                    // ddlToAcct.Visible = true;
                    ddlGLtype.Visible = true;
                    //loadClaimAcct(rowID);
                    //txtSugAmt.Visible = true;
                    ViewState["clid"] = rowID;
                    txtSugAmt.Text = amount;
                    this.loadPOSSelectedAcct(rowID);

                    btnSavePost.Focus();
                    //txtSugAmt.ReadOnly = true;
                    //string error = "";
                    //bool saved = Approve(rowID,"2","", ref error);
                    //if (saved)
                    //{
                    //    lblError.Text = "Claim Approved Successful";
                    //}
                    //else
                    //{
                    //    lblError.Text = error;
                    //}

                    lblNarrrFromAcct.Text = "From Account";
                    lblNarrrToAcct.Text = "To Account";
                    lblNarrrFromAcct.Text = "From Account";
                    lblNarrrToAcct.Text = "To Account"; ;
                    lblNarrrFromAcct_2.Text = "2nd Gl From Account";
                    lblNarrrToAcct_2.Text = "2nd Gl To Account";
                    lblNarrrFromAcct_3.Text = "3rd Gl From Account";
                    lblNarrrToAcct_3.Text = "3rd Gl To Account";
                }
                else
                {
                    //pnlApprove.Visible = true;
                    Notifications.showModal("pnlApprove-modal-xl", this);

                    //lblApprove.Text = "ATM cash Account";
                    // ddlFromAcct.Visible = false;
                    //  ddlToAcct.Visible = false;
                    ddlGLtype.Visible = false;
                    ddlATMAcc.Visible = true;
                    //txtSugAmt.Visible = true;
                    loadATMAcc(rowID);
                    ViewState["clid"] = rowID;
                    txtSugAmt.Text = amount;

                    btnSavePost.Focus();
                    //txtSugAmt.ReadOnly = false;
                }
            }
            else
            {
                //lblError.Text = "Claim already approved";
                Notifications.showMessage("Claim already approved", this, Notifications.errorMessage, false);
                return;

                //pnlApprove.Visible = false;
                // btnSavePost.Enabled = false;
            }
        }
        else if (Lbl_CommandName.Text == "d")
        {
            //int index = int.Parse(e.CommandArgument.ToString());
            int index = int.Parse(Lbl_CommandArgument.Text);
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string approved = GridView1.Rows[index].Cells[5].Text;
            if (!approved.ToLower().Equals("declined claim"))
            {
                string error = "";
                bool saved = Decline(rowID, ref error);
                if (saved)
                {
                    //lblError.Text = "Claim Declined Successful";
                    Notifications.showMessage("Claim Declined Successful", this, Notifications.successMessage, false);
                    return;
                }
                else
                {
                    //lblError.Text = error;
                    Notifications.showMessage(error, this, Notifications.errorMessage, false);
                    return;
                }
            }
            else
            {
                //lblError.Text = "claim already declined";
                Notifications.showMessage("claim already declined", this, Notifications.errorMessage, false);
                return;
            }
        }
        else if (Lbl_CommandName.Text == "r")
        {
            //int index = int.Parse(e.CommandArgument.ToString());
            int index = int.Parse(Lbl_CommandArgument.Text);
            string rowID = GridView1.DataKeys[index].Value.ToString();
            string reset = GridView1.Rows[index].Cells[5].Text;
            if (reset.Equals("Pre-Approved Claim"))
            {
                //lblError.Text = "Claim Already has the status 'Pre-Approved Claim'";
                Notifications.showMessage("Claim Already has the status - Pre-Approved Claim", this, Notifications.errorMessage, false);
                return;
            }
            else
            {
                string error = "";
                bool saved = Reset(rowID, ref error);
                if (saved)
                {
                    //lblError.Text = "Claim Reseted Successful";
                    Notifications.showMessage("Claim Reseted Successful", this, Notifications.successMessage, false);
                    return;
                }
                else
                {
                    //lblError.Text = error;
                    Notifications.showMessage(error, this, Notifications.errorMessage, false);
                    return;
                }
            }
        }
    }

            //private string SendNotification(string cardNumber, string claimID, string events, string days)
            //{
            //    string ContactNo;
            //    try
            //    {
            //        string sTimeout = ConfigurationManager.AppSettings["TimeOut"];
            //        string sQMgrName = ConfigurationManager.AppSettings["QMgrName"];
            //        string SendQ = ConfigurationManager.AppSettings["SendNotificatoinReq"];
            //        string RecvQ = ConfigurationManager.AppSettings["SendNotificatoinResp "];
            //        NotificationSendMsgGen NotSendMsgGenObj = new NotificationSendMsgGen();
            //        string strReqMsg = NotSendMsgGenObj.GenReqMsg(txtCardNumber .Text, events, claimID, days);
            //        //string strReqMsg = NotSendMsgGenObj.GenReqMsg("4323260002531152", events, claimID, days);            
            //        string SendMsg = strReqMsg;
            //        EjadaMQProcessorCls EjadaMQProcessorObj = new EjadaMQProcessorCls(sQMgrName, SendQ, RecvQ, txtCardNumber .Text);
            //        //EjadaMQProcessorCls EjadaMQProcessorObj = new EjadaMQProcessorCls(sQMgrName, SendQ, RecvQ, "4323260002531152");
            //        string RecvMsg = EjadaMQProcessorObj.ProcessMessage(SendMsg, sTimeout);

            //        string result = NotSendMsgGenObj.ParseResMsgXMLBased(RecvMsg, out ContactNo);           
            //        // ContactNo = result + "  " + ContactNo;
            //        return ContactNo;
            //    }
            //    catch (Exception ex)
            //    {
            //        ContactNo = ex.Message;
            //        return ContactNo;

            //    }
            //}

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[08].Visible = false;
            e.Row.Cells[09].Visible = false;
            e.Row.Cells[10].Visible = false;
        }
    }

    protected void rdATM_CheckedChanged(object sender, EventArgs e)
    {
        RefNo.Visible = true;
        CardNo.Visible = true;
        ATMNo.Visible = true;
        CEType.Visible = true;
        ClientNo.Visible = false;
        TerminalID.Visible = false;
        RetailerID.Visible = false;
        CardPOSNo.Visible = false;
        RBSType.Visible = false;

        //rowATM.Visible = true;
        //rowATM2.Visible = true;
        //rowPOS.Visible = false;
        //rowPOS2.Visible = false;
        //rowPOS3.Visible = false;
    }

    protected void rdPOS_CheckedChanged(object sender, EventArgs e)
    {
        RefNo.Visible = false;
        CardNo.Visible = false;
        ATMNo.Visible = false;
        CEType.Visible = false;
        ClientNo.Visible = true;
        TerminalID.Visible = true;
        RetailerID.Visible = true;
        CardPOSNo.Visible = true;
        RBSType.Visible = true;

        //rowATM.Visible = false;
        //rowATM2.Visible = false;
        //rowPOS.Visible = true;
        //rowPOS2.Visible = true;
        //rowPOS3.Visible = true;
    }

    private bool Approve(string index, string isATM, ref string error)
    {
        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_APPROVER.apprv_claim_gl";

        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_sugg_amount", OracleType.Number).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_atm_acct", 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;
        if (txtSugAmt.Text.Length == 0)
            txtSugAmt.Text = "0";
        if (isATM == "2")
            oracom.Parameters["p_atm_acct"].Value = OracleString.Null;
        else
            oracom.Parameters["p_atm_acct"].Value = ddlATMAcc.SelectedValue;

        oracom.Parameters["p_sugg_amount"].Value = txtSugAmt.Text;
        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")
            return true;
        else
            return false;
    }

    private bool ApprovePOSCalim(string index, string isATM, ref string error)
    {
        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_APPROVER.approve_POS_claim_gl";

        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_frm_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_to_acct", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_gl_type", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("p_sugg_amount", 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["p_claim_id"].Value = index;

        if (txtSugAmt.Text.Length == 0)
            txtSugAmt.Text = "0";
        //if (isATM == "2")
        //    oracom.Parameters["p_atm_acct"].Value = OracleString.Null;
        //else
        //    oracom.Parameters["p_atm_acct"].Value = ddlATMAcc.SelectedValue;

        oracom.Parameters["p_frm_acct"].Value = lblValFromAcct.Text;
        oracom.Parameters["p_to_acct"].Value = lblValToAcct.Text;
        oracom.Parameters["p_gl_type"].Value = ddlGLtype.SelectedValue;

        oracom.Parameters["p_sugg_amount"].Value = txtSugAmt.Text;
        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")
            return true;
        else
            return false;
    }

    private bool Reset(string index, ref string error)
    {
        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_APPROVER.reset_claim_to_preapprov";

        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")
            return true;
        else
            return false;
    }

    private bool Decline(string index, ref string error)
    {
        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_APPROVER.decline_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")
            return true;
        else
            return false;
    }

    protected void btnSavePost_Click(object sender, EventArgs e)
    {
        string error = "";
        bool saved = false;
        if (ddlATMAcc.Visible)
            saved = Approve(ViewState["clid"].ToString(), "1", ref error);
        else
            saved = ApprovePOSCalim(ViewState["clid"].ToString(), "2", ref error);
        if (saved)
        {
            //lblError.Text = "Claim Approved Successful";
            Notifications.showMessage("Claim Approved Successful", this, Notifications.successMessage, false);
            return;
        }
        else
        {
            //lblError.Text = error;
            Notifications.showMessage(error, this, Notifications.errorMessage, false);
            return;
        }
        //pnlApprove.Visible = false;
        loadClaims(Helper.gridSize(), 1);
    }

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

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

        loadClaims(Helper.gridSize(), 1);
    }

    private void loadPOSSelectedAcct(string claimID)
    {
        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_APPROVER.Get_selected_POS_Acct";

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

        oracom.Parameters["p_claim_id"].Value = claimID;
        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);
        oracon.Close();

        lblValFromAcct.Text = ds.Tables[0].Rows[0]["FROM_ACCT"] as string;
        lblValFromAcct_2.Text = ds.Tables[0].Rows[0]["FROM_ACCT_2"] as string;
        lblValFromAcct_3.Text = ds.Tables[0].Rows[0]["FROM_ACCT_3"] as string;
        lblValToAcct.Text = ds.Tables[0].Rows[0]["TO_ACCT"] as string;
        lblValToAcct_2.Text = ds.Tables[0].Rows[0]["TO_ACCT_2"] as string;
        lblValToAcct_3.Text = ds.Tables[0].Rows[0]["TO_ACCT_3"] as string;

        lbl3rdGlAmt.Text = ds.Tables[0].Rows[0]["tran_chg"].ToString();
        decimal Commission = Convert.ToDecimal(lbl3rdGlAmt.Text);
        decimal VAT = Commission * Helper.Get_VAT_Rate();

        if (lbl3rdGlAmt.Text.Trim().Length > 0)
            lbl2ndGlAmt.Text = (Convert.ToDecimal(txtSugAmt.Text) - Commission - VAT).ToString("0.##");
    }


    protected void btnGenReport_Click(object sender, EventArgs e)
    {
        txtClaimID.Text = txtClaimID.Text.Trim();
        txtClientNo.Text = txtClientNo.Text.Trim();
        txtTermID.Text = txtTermID.Text.Trim();
        txtRetailerID.Text = txtRetailerID.Text.Trim();
        txtCardPOS.Text = txtCardPOS.Text.Trim();
        txtOrgClaimID.Text = txtOrgClaimID.Text.Trim();

        if (ddlClaimType.SelectedValue.ToString() == "301" && ddlSamaStatuses.SelectedValue.ToString() == "1")
        {
            loadClaimsReport();
        }
        else if (txtTermID.Text.Length == 0 && txtCardPOS.Text.Length == 0 && txtClaimID.Text.Length == 0 && txtOrgClaimID.Text.Length == 0)
        {
            //lblError.Text = "please enter at least one of the marked fields";
            Notifications.showMessage("please enter at least one of the marked fields", this, Notifications.errorMessage, false);
            return;
        }
        else
        {
            loadClaimsReport();
        }
    }

    private void loadClaimsReport()
    {

        int p_page_size = 0;
        int p_page_num = 0;
        int p_mer_ch = 0;
        string c_current_uid = "";
        string c_product = "";
        string c_claim_id = "";
        string c_card_number = "";
        string c_rrn = "";
        string p_claim_Date = "";
        string c_client_number = "";
        string c_retailer_id = "";
        string c_term_id = "";
        string c_claim_status = "";
        string p_atm_num = "";
        string p_onus = "";
        string p_lang = "";
        string p_atmType = "";
        string p_sama_from_date = "";
        string p_sama_to_date = "";
        string p_sama_claim_type = "";
        string p_org_claim_id = "";
        string p_EventId = "";
        string p_claim_action_status = "";

        c_claim_id = txtClaimID.Text;
        c_rrn = txtReference.Text;
        //  p_claim_Date = dtClaimDate.date;
        c_client_number = txtClientNo.Text;
        c_retailer_id = txtRetailerID.Text;
        c_term_id = txtTermID.Text;
        p_atm_num = txtATMNo.Text;
        if (txtCardPOS.Text.Length > 0)
            c_card_number = txtCardPOS.Text;
        else
            c_card_number = null;

        c_product = "2";

        if (rdCH.Checked)
            p_mer_ch = 0;
        else
            p_mer_ch = 1;

        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";
        p_atmType = null;

        if (txtsfdate.Text.Length > 0)
        {
            string d, m, y;
            string[] values = txtsfdate.Text.Split('-');
            d = values[0].ToString();
            m = values[1].ToString();
            y = values[2].Substring(2).ToString();

            picksfdate.Text = y + m + d;
            p_sama_from_date = txtsfdate.Text;
        }
        else
        {
            p_sama_from_date = null;
        }

        if (txtstdate.Text.Length > 0)
        {
            string d, m, y;
            string[] values = txtstdate.Text.Split('-');
            d = values[0].ToString();
            m = values[1].ToString();
            y = values[2].Substring(2).ToString();

            picksfdate.Text = y + m + d;
            p_sama_to_date = picksfdate.Text;
        }
        else
        {
            p_sama_to_date = null;
        }

        if (ddlClaimType2.SelectedIndex > 0)
            p_sama_claim_type = ddlClaimType2.SelectedValue;
        else
            p_sama_claim_type = null;
        p_page_size = 0;
        p_page_num = 0;
        p_lang = "EN";
        c_current_uid = Session["uid"].ToString();


        string UrlString = "rpt=507&c_current_uid=" + c_current_uid
                            + "&c_claim_id=" + c_claim_id
                            + "&c_card_number=" + c_card_number
                            + "&c_rrn=" + c_rrn
                            + "&p_claim_Date=" + p_claim_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);
    }
}