using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_RefundTxns : System.Web.UI.Page
{
    SqlDataReader dr = null;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnGetTxn_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtTxnId.Text != "" || txtMobNo.Text != "")
            {
                Getdata();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please search with Transaction Id Or Mobile No.');"), true);
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    private void Getdata()
    {
        try
        {
            SqlParameter[] transctionid = new SqlParameter[] 
                {
                    new SqlParameter("@TxnId",txtTxnId.Text.Trim()),
                    new SqlParameter("@MobileNo",txtMobNo.Text.Trim())
                };

            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetTxnAccMobileNoOrTxnId", transctionid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }

    protected void btnSuccess_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked)
                {
                    flag = false;
                    string ServiceName = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][0]);
                    string RefNo = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][1]);
                    string WLID = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][4]);
                    TextBox txt_commission = (TextBox)gvdr.FindControl("txtCommission");
                    SqlParameter[] param_del = new SqlParameter[]
                    {
                        new SqlParameter("@refrenceno",RefNo),
                        new SqlParameter("@lblServiceName",ServiceName),
                        new SqlParameter("@WLID",WLID)
                    };                                                                                      
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_ChangeStatusToSuccessForVB", param_del);
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Pending Transaction status has been changed to Success');"), true);
                }
                Getdata();
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any check box');"), true);
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);

            }
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical error, Please try after some time.');"), true);
            throw (ex);

        }        
    }

    protected void btnRefund_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chk1")).Checked)
                {
                    flag = false;
                    string RefNo = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][1]);
                    string RequestAmount = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][2]);
                    string Agentid = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][3]);
                    string WLID = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][4]);
                    string OperatorMob = Convert.ToString(gvdr);
                    TextBox txt_commission = (TextBox)gvdr.FindControl("txtCommission");
                    SqlParameter[] param_del = new SqlParameter[]
                    {
                        new SqlParameter("@refrenceno",RefNo),
                        new SqlParameter("@AgentID",Agentid),
                        new SqlParameter("@REQUEST_AMOUNT",RequestAmount),
                        new SqlParameter("@WLID",WLID),
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_UpdateStatus_Refund_BillPayment", param_del);
                }
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any check box.');"), true);
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Transaction Refund Successfully.');"), true);
                Getdata();                 
            }
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical error, Please try after some time.');"), true);
            throw (ex);
        }       
    }
}
