using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
//www.51aspx.com
public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lbtnLogin_Click(object sender, EventArgs e)//登录事件
    {
        string username = txtUserName.Text.Trim();
        string pwd = txtPwd.Text.Trim();
        DataSet ds = SqlHelper.GetDataSet("SELECT Pid,WorkId,LoginName,Pwd,IdCardNum,FamilyAddress,WorkEmail,WorkQQ,PersonalEmail,PersonalQQ,ContactAddress,CallPhone,UrgetRelationName,UrgetRelationRelation,UrgetRelationPhone,ApproveState,State FROM dbo.tabP_Personnel WHERE LoginName='" + username + "'", "constring");
        if (ds.Tables[0].Rows.Count > 0)//判断用户名是否正确
        {
            if (ConvertHelper.GetString(ds.Tables[0].Rows[0]["Pwd"]).Trim() == pwd)//判断密码是否正确
            {
                //if (Convert.ToInt32(ds.Tables[0].Rows[0]["WorkId"]) != 1)//不是管理员的用户登录
                //{
                //    //判断身份证号，工作Email,工作QQ,个人Email,个人QQ,个人家庭住址，联系地址，手机号，应急人姓名，与本人关系，应急人电话是否为空，如果其中一个为空则提示信息不完整，请完整后登录
                //    if (Convert.ToString(ds.Tables[0].Rows[0]["IdCardNum"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["WorkEmail"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["WorkQQ"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["PersonalEmail"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["PersonalQQ"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["FamilyAddress"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["ContactAddress"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["CallPhone"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["UrgetRelationName"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["UrgetRelationRelation"]) == "" || Convert.ToString(ds.Tables[0].Rows[0]["UrgetRelationPhone"]) == "")
                //    {
                //        Session["RegisterUid"] = Convert.ToString(ds.Tables[0].Rows[0]["ID"]);//为补充完整信息的时候判断显示的用户信息和要登录的用户是否一致
                //        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>alert('您的信息不完整，请补充完整后登录！');window.location.href('personal/register.aspx.aspx?userIds=" + ds.Tables[0].Rows[0]["ID"] + "')</script>");
                //        //Response.Write("<script>alert('您的信息不完整，请补充完整后登录！')</script>");
                //        //Response.Redirect("User/UserInfo.aspx?userIds=" + ds.Tables[0].Rows[0]["ID"] + "");//跳转到用户信息页，让其用户补充完整信息
                //    }
                //    else
                //    {
                //        if (Convert.ToInt32(ds.Tables[0].Rows[0]["ApproveState"]) == 1)//判断是否通过审批； 1-已审批，0-未审批 ；已审批的用户才能登录
                //        {
                //            try
                //            {
                //                Operation.Operations("UPDATE dbo.tabUser SET LastLoginTime='" + System.DateTime.Now + "' WHERE ID=" + ds.Tables[0].Rows[0]["ID"] + "");//修改最后登录时间
                //                Session["uid"] = Convert.ToString(ds.Tables[0].Rows[0]["ID"]);//存Session["uid"]
                //                //Response.Redirect("FirstPage.aspx");
                //                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>window.location.href('FirstPage.aspx')</script>");
                //            }
                //            catch
                //            {
                //            }
                //        }
                //        else
                //        {
                //            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>alert('对不起，您的账号未通过管理员审批，请审批过后再登录！')</script>");
                //        }
                //    }
                //}
                //else//是管理员的用户登录
                //{
                if (ConvertHelper.GetInteger(ds.Tables[0].Rows[0]["ApproveState"]) == 0)//判断是否通过审批； 0-已审批，1-未审批 ；已审批的用户才能登录
                {
                    if (ConvertHelper.GetInteger(ds.Tables[0].Rows[0]["State"]) == 1)//未停用
                    {
                        // Operation.Operations("UPDATE dbo.tabUser SET LastLoginTime='" + System.DateTime.Now + "' WHERE ID=" + ds.Tables[0].Rows[0]["ID"] + "");//修改最后登录时间
                        Session["uid"] = ConvertHelper.GetInteger(ds.Tables[0].Rows[0]["Pid"]);//存Session["uid"]-员工id
                        //Session.Timeout = 60;//一个小时
                        //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>window.location.href('b2c/index.aspx');</script>");
                        Response.Redirect("b2c/index.aspx");
                    }
                    else//已停用
                    {
                        ScriptHelper.AlertMessage(this.Page,"您的账号已停用，如需恢复请联系管理员！");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>alert('对不起，您的账号未通过管理员审批，请审批过后再登录！')</script>");
                }
                //}
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script lanuage='javascript' defer>alert('您输入的密码错误，请重新输入！')</script>");
            }
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>alert('该用户不存在，请确认后再重新输入！')</script>");
        }
    }
    protected void lbtnRegister_Click(object sender, EventArgs e)//注册事件
    {
        Response.Redirect("personal/register.aspx");
    }
}