using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class index : System.Web.UI.Page
{
    public string uname = string.Empty;
    public string pwd = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        uname = username.Text.Trim();
        pwd = password.Text.Trim();
        try
        {
            int result = DbHelperSQL.GetEtwUser(uname, pwd);
            if (result > 0)  // 总帐户登入判断
            {
                zzhdata("etwuser/update.aspx");
            }
            else
            {
                int chatresult = DbHelperSQL.GetchatUser(uname, pwd);
                if (chatresult > 0) // 子帐户登入判断
                {
                    zidata("etwuser/update.aspx");
                }
                else
                {
                    Response.Write("<script type=text/javascript>alert('用户名或密码错误，请重试！'); location.href='index.aspx'; </script>");
                }
            }
        }
        catch
        {
            Response.Write("<script language=\"javascript\">alert('登录失败！');window.location='index.aspx'</" + "script>");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        uname = username.Text.Trim();
        pwd = password.Text.Trim();
        try
        {
            int result = DbHelperSQL.GetEtwUser(uname, pwd);
            if (result > 0)  // 总帐户登入判断
            {
                zzhdata("etwuser/index.aspx");
            }
            else
            {
                int chatresult = DbHelperSQL.GetchatUser(uname, pwd);
                if (chatresult > 0) // 子帐户登入判断
                {
                    zidata("etwuser/index.aspx");
                }
                else
                {
                    Response.Write("<script type=text/javascript>alert('用户名或密码错误，请重试！'); location.href='index.aspx'; </script>");
                }
            }
        }
        catch
        {
            Response.Write("<script language=\"javascript\">alert('登录失败！');window.location='index.aspx'</" + "script>");
        }
    }
    public void zidata( string url)
    {
        int There = 0;
        etw.etw_IP lead = new etw.etw_IP();
        DataSet ds = DbHelperSQL.Query("select userid from chat_user where username='" + uname + "' and password='" + pwd + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataSet dss = DbHelperSQL.Query("select usernameid,allow from etw_IP where ip='" + Page.Request.UserHostAddress + "'");
            if (dss.Tables[0].Rows.Count > 0)
            {
                if (dss.Tables[0].Rows[0]["usernameid"].ToString() == "0" && dss.Tables[0].Rows[0]["allow"].ToString() == "1")
                {
                    cookuser();
                    this.Response.Redirect(url);
                }
                else
                {
                    for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
                    {
                        if (dss.Tables[0].Rows[i]["usernameid"].ToString() == ds.Tables[0].Rows[0]["userid"].ToString())
                        {
                            There = 1;
                        }
                    }
                    if (There == 1)
                    {
                        cookuser();
                        this.Response.Redirect(url);
                    }
                    else
                    {
                        DataSet ds1 = DbHelperSQL.Query("select id from etw_error where ip='" + Page.Request.UserHostAddress + "' and usernameid='" + ds.Tables[0].Rows[0]["userid"].ToString() + "'");
                        if (ds1.Tables[0].Rows.Count == 0)
                        {
                            etw.etw_error leaderror = new etw.etw_error();
                            leaderror.usernameid = Convert.ToInt32(ds.Tables[0].Rows[0]["userid"].ToString());
                            leaderror.ip = Page.Request.UserHostAddress;
                            leaderror.Add();
                        }
                        this.Response.Redirect("etwuser/Help.aspx");
                    }
                }
            }
            else
            {
                cookuser();
                lead.usernameid = Convert.ToInt32(ds.Tables[0].Rows[0]["userid"].ToString());
                lead.ip = Page.Request.UserHostAddress;
                lead.Add();
                this.Response.Redirect(url);
            }
        }
    }
    public void zzhdata(string url)
    {
        int There = 0;
        etw.etw_IP lead = new etw.etw_IP();
        DataSet ds = DbHelperSQL.Query("select userid from etw_user where username='" + uname + "' and password='" + pwd + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataSet dss = DbHelperSQL.Query("select usernameid from etw_IP where ip='" + Page.Request.UserHostAddress + "'");
            if (dss.Tables[0].Rows.Count > 0)
            {
                if (dss.Tables[0].Rows[0]["usernameid"].ToString() == "0" && dss.Tables[0].Rows[0]["allow"].ToString() == "1")
                {
                    cookadmin();
                    this.Response.Redirect(url);
                }
                else
                {
                    for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
                    {
                        if (dss.Tables[0].Rows[i]["usernameid"].ToString() == ds.Tables[0].Rows[0]["userid"].ToString())
                        {
                            There = 1;
                        }
                    }
                    if (There == 1)
                    {
                        cookadmin();
                        this.Response.Redirect(url);
                    }
                    else
                    {
                        DataSet ds1 = DbHelperSQL.Query("select id from etw_error where ip='" + Page.Request.UserHostAddress + "' and usernameid='" + ds.Tables[0].Rows[0]["userid"].ToString() + "'");
                        if (ds1.Tables[0].Rows.Count == 0)
                        {
                            etw.etw_error leaderror = new etw.etw_error();
                            leaderror.usernameid = Convert.ToInt32(ds.Tables[0].Rows[0]["userid"].ToString());
                            leaderror.ip = Page.Request.UserHostAddress;
                            leaderror.Add();
                        }
                        this.Response.Redirect("etwuser/Help.aspx");
                    }
                }
            }
            else
            {
                cookadmin();
                lead.usernameid = Convert.ToInt32(ds.Tables[0].Rows[0]["userid"].ToString());
                lead.ip = Page.Request.UserHostAddress;
                lead.Add();
                this.Response.Redirect(url);
            }
        }
    }
    public void cookadmin()
    {
        HttpCookie user = new HttpCookie("cookEtwuser", uname);
        user.Values.Add("IsLogin", "ok");
        user.Values.Add("username", uname);
        user.Expires = DateTime.Now.AddHours(12);
        Response.Cookies.Add(user);
        FormsAuthentication.SetAuthCookie(uname, true);
        Response.Cookies["cookChatuser"].Expires = DateTime.Now.AddDays(-5);
    }
    public void cookuser()
    {
        HttpCookie user = new HttpCookie("cookChatuser", uname);
        user.Values.Add("IsLogin", "ok");
        user.Values.Add("username", uname);
        user.Values.Add("account", "0");
        user.Expires = DateTime.Now.AddHours(12);
        Response.Cookies.Add(user);
        FormsAuthentication.SetAuthCookie(uname, true);
        Response.Cookies["cookEtwuser"].Expires = DateTime.Now.AddDays(-5);
    }
}
