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_AddClientInfo : System.Web.UI.Page
{
    private static readonly string sqlcmd1 = "insert into CLIENT(CLIENT_NAME,ClI_ID,CLI_Id2,MOBILE,ADDRESS,MOME,INSERT_DATE_TIME) values(@CLIENT_NAME,@ClI_ID,@CLI_Id2,@MOBILE,@ADDRESS,@MOME,@INSERT_DATE_TIME)";
	private static readonly string sqlcmd2 = "select * from client_type";
	private static readonly string sqlcmd3 = "select * from client_area";

    protected void Page_Load(object sender, EventArgs e)
    {
		DropDownList1.Attributes.Add("OnClick", "bind1()");
		DropDownList2.Attributes.Add("OnClick", "bind2()");

		if (!IsPostBack)
		{
			using (SqlConnection sqlconn = new SqlConnection(Tools.strConn))
			{
				sqlconn.Open();
				SqlDataAdapter sa1 = new SqlDataAdapter(sqlcmd2, sqlconn);
				DataSet ds1 = new DataSet("client_type");
				sa1.Fill(ds1, "client_type");
				DropDownList1.DataSource = ds1.Tables["client_type"];
				DropDownList1.AutoPostBack = true;
				DropDownList1.DataTextField = "NAME";
				DropDownList1.DataBind();

				SqlDataAdapter sa2 = new SqlDataAdapter(sqlcmd3, sqlconn);
				DataSet ds2 = new DataSet("client_area");
				sa2.Fill(ds2, "client_area");
				DropDownList2.DataSource = ds2.Tables["client_area"];
				DropDownList2.AutoPostBack = true;
				DropDownList2.DataTextField = "NAME";
				DropDownList2.DataBind();
			}
		}
    }
    //
	protected void Button1_Click(object sender, EventArgs e)
	{
		string name = Request["name"];
		string type = Request["type"];
		string area = Request["area"];
		string mobile = Request["mobile"];
		string address = Request["address"];
		string mome = Request["mome"];

		string sqlcmd4 = "select id from client_type where name='" + type + "'";
		string sqlcmd5 = "select id from client_area where name='" + area + "'";
		string sqlcmd6 = "select count(*) as ff from dbo.CLIENT where mobile ='"+mobile+"'"; 

		using(SqlConnection sqlconn=new SqlConnection(Tools.strConn))
		{
			try
			{
				sqlconn.Open();
				SqlCommand cmd = new SqlCommand(sqlcmd6, sqlconn);
				int count = Convert.ToInt32(cmd.ExecuteScalar());
				if (count > 0)
				{
					Response.Write("<script>alert('手机号已经存在，请重新输入！');history.back()</script>");
					return;
				}

				cmd.CommandText = sqlcmd4;
				int typeId = Convert.ToInt32(cmd.ExecuteScalar());
				cmd.CommandText = sqlcmd5;
				int areaId = Convert.ToInt32(cmd.ExecuteScalar());

				cmd.CommandText = sqlcmd1;
				cmd.Parameters.Add("@CLIENT_NAME", SqlDbType.VarChar, 50).Value = name;
                cmd.Parameters.Add("@CLI_ID", SqlDbType.Int).Value = areaId;
                cmd.Parameters.Add("@CLI_Id2", SqlDbType.Int).Value = typeId;
				cmd.Parameters.Add("@MOBILE", SqlDbType.VarChar, 50).Value = mobile;
				cmd.Parameters.Add("@ADDRESS", SqlDbType.VarChar, 50).Value = address;
				cmd.Parameters.Add("@MOME", SqlDbType.VarChar, 50).Value = mome;
				cmd.Parameters.Add("@INSERT_DATE_TIME", SqlDbType.DateTime).Value = DateTime.Now;
				cmd.ExecuteNonQuery();

				Response.Write("<script>alert('添加成功');location='javascript:history.go(-1)';window.close()</script>");
			}
			catch
			{
				Response.Write("<script>alert('操作失败');location='AddClientInfo.aspx'</script>");
			}
		}
	}
}
