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;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Net.Mail;

public partial class PushTransfer : System.Web.UI.Page
{
    decimal refundedamount;
    static  string refrenceno, LoginId;    
    string  BalanceAfterTransaction, MobileNo;
    string IPAddress = HttpContext.Current.Request.UserHostAddress.ToString();
    CommonFunction objcommonfun = new CommonFunction(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AdminEMAILID"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            if (!IsPostBack)
            {
                BindDdlForMdWl();
                BindDdlForDistributorFrmTo();
                BindDdAgent();
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void BindDdlForMdWl()
    {
        SqlParameter[] param = new SqlParameter[] 
        {};
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udpGetWlMd", param);
        ddl_White_Label_MD.DataSource = ds;
        ddl_White_Label_MD.DataTextField = "username";
        ddl_White_Label_MD.DataValueField = "loginid";
        ddl_White_Label_MD.DataBind();

    }

    private void BindDdlForDistributorFrmTo()
    {
        SqlParameter[] param = new SqlParameter[] 
        { };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "uspGetDistributor", param);
        ddl_Distributor.DataSource = ds;
        ddl_Distributor.DataTextField = "username";
        ddl_Distributor.DataValueField = "loginid";
        ddl_Distributor.DataBind();
    }

    private void BindDdAgent()
    {
        SqlParameter[] param = new SqlParameter[] { };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "uspGetAgent", param);
        ddl_Agent.DataSource = ds;
        ddl_Agent.DataTextField = "username";
        ddl_Agent.DataValueField = "loginid";
        ddl_Agent.DataBind();
    }    

    protected void ddl_White_Label_MD_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddl_White_Label_MD.SelectedItem.Text!="select")
        {
            lbl_UserId.Text = ddl_White_Label_MD.SelectedItem.Text;
            LoginId = ddl_White_Label_MD.SelectedValue.ToString();
            GetNameAndBalance(lbl_UserId.Text);
            ddl_Distributor.SelectedItem.Text = "select";
            ddl_Agent.SelectedItem.Text = "select";
        }
    }

    protected void ddl_Distributor_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_Distributor.SelectedItem.Text != "select")
        {
            lbl_UserId.Text = ddl_Distributor.SelectedItem.Text;
            LoginId = ddl_Distributor.SelectedValue.ToString();
            GetNameAndBalance(lbl_UserId.Text);
            ddl_White_Label_MD.SelectedItem.Text = "select";
            ddl_Agent.SelectedItem.Text = "select";
        }
    }

    protected void ddl_Agent_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_Agent.SelectedItem.Text != "select")
        {
            lbl_UserId.Text = ddl_Agent.SelectedItem.Text;
            LoginId = ddl_Agent.SelectedValue.ToString();
            GetNameAndBalance(lbl_UserId.Text);
            ddl_Distributor.SelectedItem.Text = "select";
            ddl_White_Label_MD.SelectedItem.Text = "select";
        }
    }

    public void GetNameAndBalance(string Userid)
    {
        try
        {
            SqlParameter[] GetAgentDetail = new SqlParameter[]
            {
                new SqlParameter ("@Username",Userid),
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributerAgentDetail", GetAgentDetail);
            if (dr.Read())
            {
                lbl_Balance.Text = Convert.ToString(dr["balance"]);
                lbl_Name.Text = Convert.ToString(dr["Firstname"]);
            }
        }
        catch (Exception ex)
        {
           //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
            throw (ex);
        }
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            PushBalabce();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void PushBalabce()
    {
        refrenceno = TID_GEN();       

        if (ddl_type.SelectedItem.Text == "Roll Back")
        {
            try
            {
                refundedamount = Convert.ToDecimal(txt_amount.Text.Trim());
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@LOGINID",LoginId),
                    new SqlParameter("@refundedamount",refundedamount),
                    new SqlParameter("@ref_no_refunded",refrenceno),
                    new SqlParameter("@remark",txt_Remark.Text.Trim()),
                    new SqlParameter("@IPAddress",IPAddress),
                    new SqlParameter("@ErrorMessage",SqlDbType.Int)
                };
                param[5].Direction = ParameterDirection.Output;
                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_rollback_amount_new", param);
                int result = Convert.ToInt32(param[5].Value);
                if (result == 1)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
                }
                else
                {
                    GetMobileNo(LoginId);
                    BalanceAfterTransaction = objcommonfun.userdetails(LoginId);
                    string sms = "An amount of Rs. " + refundedamount + " RollBack into your account on " + Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).ToString("dd-MMM-yyyy") + " at " + DateTime.Now.ToString("HH:mm:ss") + " against txn id " + refrenceno + " Your Current Balance is " + BalanceAfterTransaction + "";
                    objcommonfun.SendSMSByAdmin(MobileNo, sms, refrenceno, LoginId);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Amount Has been Deducted Successfully');"), true); 
                    txt_amount.Text = "";
                    txt_Remark.Text = "";
                    ddl_White_Label_MD.SelectedIndex =0;
                    ddl_Distributor.SelectedIndex = 0;
                    ddl_Agent.SelectedIndex = 0;
                    ddl_type.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
            }
        }
        if (ddl_type.SelectedItem.Text == "Credit")
        {
            try
            {
                refundedamount = Convert.ToDecimal(txt_amount.Text.Trim());
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@LOGINID",LoginId),
                    new SqlParameter("@creditamount",refundedamount),
                    new SqlParameter("@ref_no_refunded",refrenceno),
                    new SqlParameter("@remark",txt_Remark.Text.Trim()),
                    new SqlParameter("@IPAddress",IPAddress),
                    new SqlParameter("@ErrorMessage",SqlDbType.Int)
                };
                param[5].Direction = ParameterDirection.Output;
                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_givecredit_amount_new", param);
                int result = Convert.ToInt32(param[5].Value);
                if (result == 1)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
                }
                else
                {
                    GetMobileNo(LoginId);
                    BalanceAfterTransaction = objcommonfun.userdetails(LoginId);
                    string sms = "An amount of Rs. " + refundedamount + " credited into your account on " + Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).ToString("dd-MMM-yyyy") + " at " + DateTime.Now.ToString("HH:mm:ss") + " against txn id " + refrenceno + " Your Current Balance is " + BalanceAfterTransaction + "";
                    objcommonfun.SendSMSByAdmin(MobileNo, sms, refrenceno, LoginId);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Amount Has been Credited into your Account');"), true);
                    txt_amount.Text = "";
                    txt_Remark.Text = "";
                    ddl_White_Label_MD.SelectedIndex = 0;
                    ddl_Distributor.SelectedIndex = 0;
                    ddl_Agent.SelectedIndex = 0;
                    ddl_type.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem.Please try again');"), true);
            }
        }

    }

    public void GetMobileNo(string Loginid)
    {
        try
        {
            SqlParameter[] loginid = new SqlParameter[]
            {
                new SqlParameter ("@Loginid",Loginid)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Udp_GetRegisteredMobileNo", loginid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                MobileNo = ds.Tables[0].Rows[0]["MobileNo"].ToString();
            }
        }
        catch (Exception ex)
        {

        }
    }

    public static string TID_GEN()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 16).Replace("a", "1");
        new_tid = new_tid.Substring(0, 16).Replace("b", "2");
        new_tid = new_tid.Substring(0, 16).Replace("c", "3");
        new_tid = new_tid.Substring(0, 16).Replace("d", "4");
        new_tid = new_tid.Substring(0, 16).Replace("e", "5");
        new_tid = new_tid.Substring(0, 16).Replace("f", "6");
        new_tid = new_tid.Substring(0, 16).Replace("g", "14");
        new_tid = new_tid.Substring(0, 16).Replace("h", "8");
        new_tid = new_tid.Substring(0, 16).Replace("i", "9");
        new_tid = new_tid.Substring(0, 16).Replace("j", "0");
        new_tid = new_tid.Substring(0, 16).Replace("k", "1");
        new_tid = new_tid.Substring(0, 16).Replace("l", "2");
        new_tid = new_tid.Substring(0, 16).Replace("m", "3");
        new_tid = new_tid.Substring(0, 16).Replace("n", "4");
        new_tid = new_tid.Substring(0, 16).Replace("o", "5");
        new_tid = new_tid.Substring(0, 16).Replace("p", "0");
        new_tid = new_tid.Substring(0, 16).Replace("q", "9");
        new_tid = new_tid.Substring(0, 16).Replace("r", "8");
        new_tid = new_tid.Substring(0, 16).Replace("s", "14");
        new_tid = new_tid.Substring(0, 16).Replace("t", "6");
        new_tid = new_tid.Substring(0, 16).Replace("u", "5");
        new_tid = new_tid.Substring(0, 16).Replace("v", "4");
        new_tid = new_tid.Substring(0, 16).Replace("w", "3");
        new_tid = new_tid.Substring(0, 16).Replace("x", "2");
        new_tid = new_tid.Substring(0, 16).Replace("y", "1");
        new_tid = new_tid.Substring(0, 16).Replace("z", "0");
        return new_tid;
    }
}
