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;
using System.Data.SqlClient;

public partial class SystemManage_AddNewEmployee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["username"]) == "")
        {
            Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = Request["name"];
        string tel = Request["tel"];
        string mobile = Request["mobile"];
        string address = Request["address"];
        if (Tools.validate(name))
        {
            Response.Write("<script>alert('用户名中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(tel))
        {
            Response.Write("<script>alert('固定电话中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(mobile))
        {
            Response.Write("<script>alert('手机号中不能含有非法字符');history.back()</script>");
            return;
        }
        if (Tools.validate(address))
        {
            Response.Write("<script>alert('地址中不能含有非法字符');history.back()</script>");
            return;
        }
        string sqlcmd1 = "select count(*) as ff from EMPLOYEE where name='" + name + "'";
        using(SqlConnection sqlconn=new SqlConnection(Tools.strConn))
        {
            sqlconn.Open();
            SqlCommand cmd = new SqlCommand(sqlcmd1, sqlconn);
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            if (count > 0)
            {
				Response.Write("<script>alert('此员工姓名已经存在，请重新输入！');history.back()</script>");
                return;
            }
            string sqlcmd2 = "insert into EMPLOYEE (name,tel,mobile,address)values(@name,@tel,@mobile,@address)";
            try
            {
                cmd.CommandText = sqlcmd2;
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = name ;
                cmd.Parameters.Add("@tel", SqlDbType.VarChar, 50).Value = tel;
                cmd.Parameters.Add("@mobile", SqlDbType.VarChar, 50).Value = mobile;
                cmd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = address;
                cmd.ExecuteNonQuery();
                Response.Write("<script>alert('员工添加成功');location='EmployeeManage.aspx'</script>");
            }
            catch
            {
                Response.Write("<script>alert('操作失败');location='AddNewEmployee.aspx'</script>");
            }
        }
    }
}
