using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication2.Admin
{
	/// <summary>
	/// advusermoneyadd 的摘要说明。
	/// </summary>
	public partial class usermoneyadd : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{ this.Button2.Attributes.Add("onclick","return confirm('确认要"+this.DropDownList1.SelectedItem.Text+"金额！');");
			if(!Page.IsPostBack)
			{
				if(this.Session["USERNAME"]==null||this.Session["Role"]==null||this.Session["USERNAME"].ToString()==""||this.Session["Role"].ToString()=="")
				{
					Response.Redirect("login.aspx");
				}
				else
				{
					string [] role=this.Session["Role"].ToString().Split('#');
					bool exists=false;
					for(int i=0;i<role.Length;i++)
					{
						if(role[i].Equals("2")||role[i].Equals("3")||role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
					{  
						string user=Request.Params["username"].ToString();
						string db=Request.Params["db"].ToString();
						string account=SB.ADOHelper.inputText(this.TextBox3.Text);
						SB.ADOHelper help=new SB.ADOHelper();
						SqlDataReader rd=help.getReader("select * from "+db+" where UserName='"+user+"'");
						if(rd.Read())
						{
							this.Label1.Text=rd["UserName"].ToString();
							this.Label2.Text=rd["id"].ToString();
							this.Label4.Text=rd["Money"].ToString();
				
						}
						else
						{
				
							SB.ADOHelper.alertmsg("没有此用户的数据！",Request.UrlReferrer.ToString());
						}
						rd.Close();
					}
					else
					{
						Response.Redirect("NoAuthor.aspx");
					}
				}
			}			// 在此处放置用户代码以初始化页面
		}
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
		protected void Button2_Click(object sender, System.EventArgs e)
		{
			
			if(this.TextBox3.Text.Trim()!="")
			{
			
				double money=0;
				double total=0;
				string remark="入账";
				string memo=SB.ADOHelper.inputText(this.TextBox4.Text);
				if(this.TextBox3.Text.Trim()!=""){money=Convert.ToDouble(this.TextBox3.Text);}
				if(!this.DropDownList1.Items[0].Selected){total=total-money;}
				else{total=total+money;}
				SB.ADOHelper help=new SB.ADOHelper();
				double advmoney=0.0;
//				if(Request.Params["db"].ToString().Trim()=="AdvHost")
//				{
					SqlDataReader rd=help.getReader("select * from "+Request.Params["db"].ToString()+" where UserName='"+this.Label1.Text.Trim()+"'");
					if(rd.Read())
					{
						advmoney=Convert.ToDouble(rd["Money"].ToString());
						rd.Close();
					}
					else
					{
						rd.Close();
						SB.ADOHelper.alertmsg("用户不存在！","UserSearchadv.aspx");
					}
//				}
				string cmdtext="update "+Request.Params["db"].ToString()+" set Money=Money+"+total+" where UserName='"+this.Label1.Text+"'";
				if(help.conn.State==ConnectionState.Closed)
				{help.conn.Open();}
				SqlCommand cmd=new SqlCommand(cmdtext,help.conn);
				SqlTransaction tran=help.conn.BeginTransaction();
				cmd.Transaction=tran;
				try
				{
					cmd.ExecuteNonQuery();
					if(Request.Params["db"].ToString()=="WebsiteHost")
					{
						cmd.CommandText="insert into FinanceWeb(AffairsType,WebUserName,AdvPeriod,AdvListAdvTitle,ClosebookMoney,RewardMoney,PayTotalMoney,ProcedureMoney,TaxMoney,PayMoney,Surplusamount,Remarks,AddTime) values('3','"+this.Label1.Text+"','"+DateTime.Now.ToShortDateString()+"','',0.0,"+total+",0.0,0.0,0.0,0.0,"+(total+advmoney)+",'"+memo+"','"+DateTime.Now.ToString()+"')";
					}
					else
					{
				            if(total<0)
							{remark="扣款";}
						cmd.CommandText="insert into FinanceAdv(AffairsType,UserName,OperationName,TotalEnter,TotaGoto,Commission,Surplusamount,Remarks,Ip,AddTime) values('"+remark+"','"+this.Label1.Text+"','"+this.Session["USERNAME"].ToString()+"',"+total+",0.0,0.0,"+(total+advmoney)+",'扣除/增加','"+Request.UserHostAddress.ToString()+"','"+DateTime.Now.ToShortDateString()+"')";
					
					}
					cmd.ExecuteNonQuery();
					tran.Commit();	
		        this.Panel1.Visible=false;
				this.Label3.Visible=true;
				}
				catch
				{
					tran.Rollback();
					Response.Write("<script>alert('数据操作发生错误！')</script>");
				}
				finally
				{
					help.conn.Close();
				}
				
			
			}
			else
			{
				Response.Write("<script>alert('不能输入空格!')</script>");
			}
			
		}

		
	}
}
