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.IO;
using System.Text;
using System.Net.Mail;
using System.Net.Sockets;
using System.Xml;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections.Specialized;


public partial class AdminLogin : System.Web.UI.Page
{
    int randomNumber;
    string WebsiteUrl;
    string bookPageTitle, bookKeywords, description, sno;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            login_admin();
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }
    public void login_admin()
    {
        if (txtEmail.Text != "" && txtPwd.Text != "")
        {
            try
            {
                SqlParameter[] check_login_details = new SqlParameter[]
                {
                    new SqlParameter("@EMAILID",txtEmail.Text.Trim()),
                    new SqlParameter("@Password",txtPwd.Text.Trim()),
                    new SqlParameter("@ErrorResult",SqlDbType.Int),
                };
                check_login_details[2].Direction = ParameterDirection.Output;
                SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_CHECK_USERLOGIN_DETAILS_ADMIN", check_login_details);
                int loginvariable = Convert.ToInt32(check_login_details[2].Value);
                if (loginvariable == 0)
                {
                    if (dr.Read())
                    {
                        Session["AdminEMAILID"] = dr["EMAILID"];
                        Session["PASSWORD"] = txtPwd.Text.Trim();
                        Response.Redirect("../Admin/TransactionAnalysis.aspx", false);
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Either username, password is wrong !!!');"), true);
                    }
                }
            }
            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 btnSendPwd_Click(object sender, EventArgs e)
    {
        try
        {
            Random rand = new Random((int)DateTime.Now.Ticks);
            randomNumber = rand.Next(100000, 999999);
            SqlParameter[] SetPwd = new SqlParameter[]
        {
            new SqlParameter("@pwd", randomNumber),
            new SqlParameter("@emailid", txtEmail.Text.Trim())
        };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "sp_SetPassword", SetPwd);
            if (ds.Tables[0].Rows.Count != 0)
            {
                ViewState["status"] = ds.Tables[0].Rows[0]["statusPwd"];
            }
            if (Convert.ToString(ViewState["status"]).Equals("1"))
            {
                string[] mailData = new string[5];
                mailData[0] = mailmsgfailstatus();
                mailData[1] =txtEmail.Text.Trim();
                mailData[2] = "";
                mailData[3] = "egenrecharge@gmail.com";
                mailData[4] = "Venus Utility Services Super Admin – OTP";
                CommonFunction.SendMailmsg(mailData[0], mailData[1], mailData[2], mailData[3], mailData[4]);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('OTP has been send on  " + txtEmail.Text.Trim() + ". Please use the same for login.');"), true);
                trPwd.Visible = true;
               // btnSendPwd.Visible = false;
                btnLogin.Visible = true;
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('Unauthorized E-mail Id. Please login with Registered E-mail Id only.');"), true);
                trEmail.Visible = true;
                trPwd.Visible = false;
                //btnSendPwd.Visible = true;
                btnLogin.Visible = false;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
        }    
    }

    private string mailmsgfailstatus()
    {
        StringBuilder strHTML = new StringBuilder();
        strHTML.Append("<table width=100%  style='font-family:Trebuchet MS'>");
        strHTML.Append("<tr><td width=100% colspan=3 align=Left >Dear Admin User,</td></tr>");
        strHTML.Append("<tr><td width=100% colspan=3 align=Left ></td></tr>");
        strHTML.Append("<tr><td width=100% colspan=3 align=Left >Please Use " + randomNumber + " for login your Admin Account.</td></tr>");
        strHTML.Append("<tr><td width=100% colspan=3 align=Left ></td></tr>");
        strHTML.Append("<tr><td width=100% colspan=3 align=Left >Regards,</td></tr>");
        strHTML.Append("<tr><td width=100% colspan=3 align=Left >Services Team</td></tr>");
        strHTML.Append("</table>");
        return strHTML.ToString();
    }
}