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

public partial class OrdersManage_AddOrdersInfo : 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>");
		}
		DropDownList1.Attributes.Add("OnClick", "bind()");
		DropDownList2.Attributes.Add("OnClick", "bind1()");
		if(!IsPostBack)
		{
			this.Inistans();
		}
	}

	/// <summary>
	/// 页面初始化数据
	/// </summary>
	public void Inistans()
	{
		OrdersBll ob = new OrdersBll();
		DataSet ds = ob.GetAllProduct();
		DropDownList1.DataSource = ds.Tables[0];
		DropDownList1.DataTextField = "NAME";
		DropDownList1.DataBind();
	}

	protected void query_Click(object sender, EventArgs e)
	{
		string moblie = Request["TextBox4"].ToString();
		if (moblie == "")
		{
			Response.Write("<script>alert('请输入客户电话!')</script>");
			return;
		}
		if (Tools.validate(moblie))
		{
			Response.Write("<script>alert('手机号中不能有非法字符')</script>");
		}
		OrdersBll ob = new OrdersBll();
		SqlDataReader sr = ob.GetReader(moblie);
		int essclr = ob.GetExcuteScal(moblie);
		if (essclr > 0)
		{
			if (sr.Read())
			{
				this.Clear();
				clientno.Text = sr["id"].ToString();
				name.Text = sr[3].ToString();
				type.Text = sr[1].ToString();
				area.Text = sr[0].ToString();
				mobile.Text = sr["mobile"].ToString();
				address.Text = sr["address"].ToString();
			}
			ob.Close();
		}
		else
		{
			Response.Write("<script>window.open('/SystemManage/AddClientInfo.aspx','','width=450,height=250')</script>");
		}
	}

	protected void Button3_Click(object sender, EventArgs e)
	{
		OrdersBll ob = new OrdersBll();
		if (clientno.Text == "")
		{
			Response.Write("<script>alert('请选择客户!')</script>");
			return;
		}
		if (No.Text == "")
		{
			Response.Write("<script>alert('请选择您要购买的产品!')</script>");
			return;
		}

		int cli_id = Convert.ToInt32(Request["clientno"]);
		int pro_id = Convert.ToInt32(Request["No"]);
		string no = Tools.GetProxySheetNo();
		string status = Constext.Orders_Initialize;
		DateTime dt = DateTime.Now;
		int aount = Convert.ToInt32(Request["Amount"]);
		decimal price = Convert.ToDecimal(Request["price"]);
		string bott = bottle.Text == "" ? "" : bottle.Text;
		int resule = ob.AddOrdersInfo(cli_id, pro_id, no, status, dt, aount, price, bott);
		if (resule > 0)
		{
			this.Clear();
			this.Clert();
			Response.Write("<script>alert('添加成功!');history.back()</script>");
		}
		else
		{
			this.Clear();
			this.Clert();
			Response.Write("<script>alert('操作失败,请重试!');history.back()</script>");
		}
	}

	/// <summary>
	/// 清理数据
	/// </summary>
	public void Clear()
	{
		clientno.Text = "";
		name.Text = "";
		type.Text = "";
		area.Text = "";
		mobile.Text = "";
		address.Text = "";
	}

	public void Clert()
	{
		productName.Text = "";
		No.Text = "";
		price.Text = "";
		bottle.Text = "";
	}

	protected void Button1_Click(object sender, EventArgs e)
	{
		if (productName.Text != "")
		{
			OrdersBll ob = new OrdersBll();
			SqlDataReader reader = ob.GetSingProduct(productName.Text.ToString());
			if (reader.Read())
			{
				No.Text = reader["id"].ToString();
				price.Text = reader["price"].ToString();
			}
		}
	}
}
