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 PendingTransaction : System.Web.UI.Page
{
    CommonFunction objcommonfun = new CommonFunction();
    string BalanceAfterTransaction, LoginID, sms, MobileNo, ReqAmount, AG_MobilNo, ServiceType;
    string vendor,OperatorId;
    string ErrorMessage = string.Empty;
    com.ezypayindia.ws.network RechargeEzpayObj = new com.ezypayindia.ws.network();
    WBMobile_Noverification _ObjWBMNverify = new WBMobile_Noverification();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                fill.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            getdetails();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlVendor.SelectedItem.Text.Equals("eGenApi"))
            {
                ViewState["vendor"] = "eGenApi";
            }
            else if (ddlVendor.SelectedItem.Text.Equals("RechServices"))
            {
                ViewState["vendor"] = "RechServices";
            }
            //else if(ddlVendor.SelectedItem.Text.Equals("InstaPay"))
            //{
            //    ViewState["vendor"] = "InstaPay";
            //}
            //else if (ddlVendor.SelectedItem.Text.Equals("ITVision"))
            //{
            //    ViewState["vendor"] = "ITVision";
            //}
            else if (ddlVendor.SelectedItem.Text.Equals("All Pending"))
            {
                ViewState["vendor"] = "All Pending";
            }
            getdetails();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }

    protected void btnsucess_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).Checked)
                {
                    flag = false;
                    string ServiceName = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][0]);
                    string RefNo = Convert.ToString(GridView1.DataKeys[gvdr.RowIndex][1]);
                    TextBox txt_commission = (TextBox)gvdr.FindControl("txtCommission");
                    SqlParameter[] param_del = new SqlParameter[]
                    {
                        new SqlParameter("@refrenceno",RefNo),
                        new SqlParameter("@lblServiceName",ServiceName)                        
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_ChangeStatusToSuccess", param_del);

                    GetTxnDetail(RefNo);
                    BalanceAfterTransaction = objcommonfun.userdetails(LoginID);
                    if (ServiceType == "MOBILE")
                    {
                        sms = "Final Status: TID- " + RefNo + " Mobile No " + MobileNo + " Amount " + ReqAmount + " status updated from Pending to Success Current Balance is " + BalanceAfterTransaction + "";
                    }
                    else if (ServiceType == "DTH")
                    {
                        sms = "Final Status: TID- " + RefNo + " DTH No " + MobileNo + " Amount " + ReqAmount + " status updated from Pending to Success Current Balance is " + BalanceAfterTransaction + "";
                    }
                    else
                    {
                        sms = "Final Status: TID- " + RefNo + " Data Card No " + MobileNo + " Amount " + ReqAmount + " status updated from Pending to Success Current Balance is " + BalanceAfterTransaction + "";
                    }
                    objcommonfun.SendSMSByAdmin(AG_MobilNo, sms, RefNo, LoginID);
                }                
                //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Pending Transaction status has been changed to Success');"), true);                    
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any check box');"), true);
            }
            else
            {
                getdetails();
                //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Record Approved Successfully');"), true);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Pending Transaction status has been changed to Success');"), 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);
        }
    }

    public void GetTxnDetail(string refrencenumber)
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@RefrenceNo",refrencenumber)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetTxnDetail", get_user_details);
            if (ds.Tables[0].Rows.Count > 0)
            {
                LoginID = ds.Tables[0].Rows[0]["COURENTLEVEL"].ToString();
                MobileNo = ds.Tables[0].Rows[0]["MobileDTHNumber"].ToString();
                ReqAmount = ds.Tables[0].Rows[0]["Amount"].ToString();
                AG_MobilNo = ds.Tables[0].Rows[0]["MOBILENO"].ToString();
                ServiceType = ds.Tables[0].Rows[0]["Type"].ToString();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical error, Please try after some time.');"), true);
        }
    }
    protected void btnRefund_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).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 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)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_UpdateStatus_PendingToRefund", param_del);

                    GetTxnDetail(RefNo);
                    BalanceAfterTransaction = objcommonfun.userdetails(LoginID);
                    if (ServiceType == "MOBILE")
                    {
                        sms = "Final Status: TID- " + RefNo + " Mobile No " + MobileNo + " Amount " + ReqAmount + " status updated from Pending to Refunded Current Balance is " + BalanceAfterTransaction + "";
                    }
                    else if (ServiceType == "DTH")
                    {
                        sms = "Final Status: TID- " + RefNo + " DTH No " + MobileNo + " Amount " + ReqAmount + " status updated from Pending to Refunded Current Balance is " + BalanceAfterTransaction + "";
                    }
                    else
                    {
                        sms = "Final Status: TID- " + RefNo + " Data Card No " + MobileNo + " Amount " + ReqAmount + " status updated from Pending to Refunded Current Balance is " + BalanceAfterTransaction + "";
                    }
                    objcommonfun.SendSMSByAdmin(AG_MobilNo, sms, RefNo, LoginID);
                }
            }
            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);
                getdetails();                  
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical error, Please try after some time.');"), true);
        }
    }

    protected void btnGetStatus_Click(object sender, EventArgs e)
    {
        try
        {
            bool flag = true;
            foreach (GridViewRow gvdr in this.GridView1.Rows)
            {
                if (((CheckBox)gvdr.FindControl("chkbox")).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 OperatorMob = Convert.ToString(gvdr);
                    TextBox txt_commission = (TextBox)gvdr.FindControl("txtCommission");
                    //InstaPay(RefNo, Agentid, RequestAmount);
                }
            }
            if (flag)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Please select any checkbox.');"), true);
            }
            else
            {
                getdetails();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Transaction Refunded 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);
        }
    }

    private void getdetails()
    {
        try
        {
            SqlParameter[] param = new SqlParameter[] 
            {
                new SqlParameter("@vendor",ViewState["vendor"])
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetPendingTxnAccVendor", param);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
                fill.Visible = true;
            }
            else
            {
                GridView1.Visible = false;
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('No Pending Transaction exists in the Data Base');"), true);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
        }
    }

    
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
