using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using BookCommunityClient.AuthWS;
using BookCommunityClient.DataWS;

using System.Data;

namespace BookCommunityClient
{
	/// <summary>
	/// MsgEditFrm 的摘要说明。
	/// </summary>
	public class MsgEditFrm : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.ImageList ilToolbar;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label lblFrom;
		private System.Windows.Forms.Label lblTo;
		private System.Windows.Forms.Label lblTitle;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ToolBar tbrMain;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ToolBarButton tbbCancel;
		private System.Windows.Forms.GroupBox gbxTop;
		private System.Windows.Forms.TextBox tbxTitle;
		private System.Windows.Forms.TextBox tbxTo;
		private System.Windows.Forms.TextBox tbxFrom;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.RichTextBox rtbMessage;
		private System.Windows.Forms.Panel pnlFill;
		private System.Windows.Forms.ToolBarButton tbbApprove;
		private System.Windows.Forms.ToolBarButton tbbReject;

		#region 自定义字段
		/// <summary>
		/// 打开本窗口的窗口
		private MessageFrm frmMessage;
		/// </summary>
		#endregion

		#region 窗体创建与销毁
		public MsgEditFrm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}
		public MsgEditFrm(MessageFrm form)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			this.frmMessage = form;

			
			//根据frmMessage中的信息，邦定各个控件

			//添加原文引用，显示相应的工具按钮
			if(this.frmMessage.approved==UIRequestApprovedEnum.APPROVED)
			{
				this.rtbMessage.Text = "我同意您的借阅请求。\r\n";
				this.tbbApprove.Visible = true;
			}
			else if(this.frmMessage.approved == UIRequestApprovedEnum.NOTAPPROVED)
			{
				this.rtbMessage.Text = "对不起，我暂时无法同意您的借阅请求。\r\n";
				this.tbbReject.Visible = true;
			}

			this.rtbMessage.Text += "\r\n-----邮件原件-----\r\n"
					+ form.rtbMessage.Text;

			DataSetMessage.MESSAGE_INFORow row = this.frmMessage.frmMain.DsMessage.MESSAGE_INFO.FindByID(this.frmMessage.MessageID);
			this.tbxTitle.Text = "Re: "+row.Title;
			this.tbxTo.Text = row["Sender"] as string;
			this.tbxFrom.Text = row["Receiver"] as string;

			//聚焦到rtbMessage
			this.rtbMessage.Select(0,0);
			this.rtbMessage.Focus();
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MsgEditFrm));
			this.ilToolbar = new System.Windows.Forms.ImageList(this.components);
			this.label2 = new System.Windows.Forms.Label();
			this.lblFrom = new System.Windows.Forms.Label();
			this.lblTo = new System.Windows.Forms.Label();
			this.lblTitle = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.tbbApprove = new System.Windows.Forms.ToolBarButton();
			this.tbrMain = new System.Windows.Forms.ToolBar();
			this.tbbReject = new System.Windows.Forms.ToolBarButton();
			this.tbbCancel = new System.Windows.Forms.ToolBarButton();
			this.pnlFill = new System.Windows.Forms.Panel();
			this.rtbMessage = new System.Windows.Forms.RichTextBox();
			this.gbxTop = new System.Windows.Forms.GroupBox();
			this.tbxTitle = new System.Windows.Forms.TextBox();
			this.tbxTo = new System.Windows.Forms.TextBox();
			this.tbxFrom = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.pnlFill.SuspendLayout();
			this.gbxTop.SuspendLayout();
			this.SuspendLayout();
			// 
			// ilToolbar
			// 
			this.ilToolbar.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
			this.ilToolbar.ImageSize = new System.Drawing.Size(24, 24);
			this.ilToolbar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilToolbar.ImageStream")));
			this.ilToolbar.TransparentColor = System.Drawing.Color.White;
			// 
			// label2
			// 
			this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(60, 17);
			this.label2.TabIndex = 41;
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblFrom
			// 
			this.lblFrom.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblFrom.Location = new System.Drawing.Point(72, 8);
			this.lblFrom.Name = "lblFrom";
			this.lblFrom.Size = new System.Drawing.Size(184, 16);
			this.lblFrom.TabIndex = 40;
			this.lblFrom.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblTo
			// 
			this.lblTo.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblTo.Location = new System.Drawing.Point(72, 24);
			this.lblTo.Name = "lblTo";
			this.lblTo.Size = new System.Drawing.Size(184, 16);
			this.lblTo.TabIndex = 39;
			this.lblTo.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblTitle
			// 
			this.lblTitle.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblTitle.Location = new System.Drawing.Point(72, 40);
			this.lblTitle.Name = "lblTitle";
			this.lblTitle.Size = new System.Drawing.Size(184, 16);
			this.lblTitle.TabIndex = 38;
			this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label9
			// 
			this.label9.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label9.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label9.Location = new System.Drawing.Point(8, 24);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(60, 17);
			this.label9.TabIndex = 37;
			this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label8
			// 
			this.label8.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label8.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label8.Location = new System.Drawing.Point(8, 8);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(48, 17);
			this.label8.TabIndex = 36;
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label1
			// 
			this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label1.Location = new System.Drawing.Point(328, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 17);
			this.label1.TabIndex = 41;
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label3
			// 
			this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.label3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label3.Location = new System.Drawing.Point(408, 8);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(184, 16);
			this.label3.TabIndex = 38;
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// tbbApprove
			// 
			this.tbbApprove.ImageIndex = 0;
			this.tbbApprove.Text = "发送同意信";
			this.tbbApprove.Visible = false;
			// 
			// tbrMain
			// 
			this.tbrMain.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.tbrMain.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					   this.tbbApprove,
																					   this.tbbReject,
																					   this.tbbCancel});
			this.tbrMain.ButtonSize = new System.Drawing.Size(53, 22);
			this.tbrMain.DropDownArrows = true;
			this.tbrMain.ImageList = this.ilToolbar;
			this.tbrMain.Location = new System.Drawing.Point(0, 0);
			this.tbrMain.Name = "tbrMain";
			this.tbrMain.ShowToolTips = true;
			this.tbrMain.Size = new System.Drawing.Size(632, 36);
			this.tbrMain.TabIndex = 7;
			this.tbrMain.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
			this.tbrMain.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.tbrMain_ButtonClick);
			// 
			// tbbReject
			// 
			this.tbbReject.ImageIndex = 1;
			this.tbbReject.Text = "发送拒绝信";
			this.tbbReject.Visible = false;
			// 
			// tbbCancel
			// 
			this.tbbCancel.ImageIndex = 2;
			this.tbbCancel.Text = "关闭";
			// 
			// pnlFill
			// 
			this.pnlFill.Controls.Add(this.rtbMessage);
			this.pnlFill.Controls.Add(this.gbxTop);
			this.pnlFill.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlFill.DockPadding.Bottom = 4;
			this.pnlFill.DockPadding.Left = 4;
			this.pnlFill.DockPadding.Right = 4;
			this.pnlFill.Location = new System.Drawing.Point(0, 36);
			this.pnlFill.Name = "pnlFill";
			this.pnlFill.Size = new System.Drawing.Size(632, 442);
			this.pnlFill.TabIndex = 9;
			// 
			// rtbMessage
			// 
			this.rtbMessage.Dock = System.Windows.Forms.DockStyle.Fill;
			this.rtbMessage.Location = new System.Drawing.Point(4, 96);
			this.rtbMessage.MaxLength = 3500;
			this.rtbMessage.Name = "rtbMessage";
			this.rtbMessage.Size = new System.Drawing.Size(624, 342);
			this.rtbMessage.TabIndex = 3;
			this.rtbMessage.Text = "";
			// 
			// gbxTop
			// 
			this.gbxTop.Controls.Add(this.tbxTitle);
			this.gbxTop.Controls.Add(this.tbxTo);
			this.gbxTop.Controls.Add(this.tbxFrom);
			this.gbxTop.Controls.Add(this.label4);
			this.gbxTop.Controls.Add(this.label10);
			this.gbxTop.Controls.Add(this.label11);
			this.gbxTop.Dock = System.Windows.Forms.DockStyle.Top;
			this.gbxTop.Location = new System.Drawing.Point(4, 0);
			this.gbxTop.Name = "gbxTop";
			this.gbxTop.Size = new System.Drawing.Size(624, 96);
			this.gbxTop.TabIndex = 2;
			this.gbxTop.TabStop = false;
			// 
			// tbxTitle
			// 
			this.tbxTitle.BackColor = System.Drawing.SystemColors.Window;
			this.tbxTitle.Enabled = false;
			this.tbxTitle.Location = new System.Drawing.Point(96, 65);
			this.tbxTitle.Name = "tbxTitle";
			this.tbxTitle.ReadOnly = true;
			this.tbxTitle.Size = new System.Drawing.Size(520, 21);
			this.tbxTitle.TabIndex = 50;
			this.tbxTitle.Text = "tbxTitle";
			// 
			// tbxTo
			// 
			this.tbxTo.BackColor = System.Drawing.SystemColors.Window;
			this.tbxTo.Enabled = false;
			this.tbxTo.Location = new System.Drawing.Point(96, 41);
			this.tbxTo.Name = "tbxTo";
			this.tbxTo.ReadOnly = true;
			this.tbxTo.Size = new System.Drawing.Size(520, 21);
			this.tbxTo.TabIndex = 49;
			this.tbxTo.Text = "tbxTo";
			// 
			// tbxFrom
			// 
			this.tbxFrom.BackColor = System.Drawing.SystemColors.Window;
			this.tbxFrom.Enabled = false;
			this.tbxFrom.Location = new System.Drawing.Point(96, 15);
			this.tbxFrom.Name = "tbxFrom";
			this.tbxFrom.ReadOnly = true;
			this.tbxFrom.Size = new System.Drawing.Size(520, 21);
			this.tbxFrom.TabIndex = 48;
			this.tbxFrom.Text = "tbxFrom";
			// 
			// label4
			// 
			this.label4.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label4.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label4.Location = new System.Drawing.Point(8, 67);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 17);
			this.label4.TabIndex = 47;
			this.label4.Text = "标题:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label10
			// 
			this.label10.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label10.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label10.Location = new System.Drawing.Point(8, 43);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 17);
			this.label10.TabIndex = 46;
			this.label10.Text = "收件人:";
			this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label11
			// 
			this.label11.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label11.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label11.Location = new System.Drawing.Point(8, 19);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(72, 17);
			this.label11.TabIndex = 45;
			this.label11.Text = "发件人";
			this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// MsgEditFrm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(216)), ((System.Byte)(228)), ((System.Byte)(248)));
			this.ClientSize = new System.Drawing.Size(632, 478);
			this.Controls.Add(this.pnlFill);
			this.Controls.Add(this.tbrMain);
			this.Name = "MsgEditFrm";
			this.Text = "MsgEditFrm";
			this.pnlFill.ResumeLayout(false);
			this.gbxTop.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#endregion

		private void tbrMain_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			try
			{
				DataSetMessage.MESSAGE_INFORow row = this.frmMessage.frmMain.DsMessage.MESSAGE_INFO.FindByID(this.frmMessage.MessageID);
				switch(this.tbrMain.Buttons.IndexOf(e.Button))
				{
					case 0://发送同意
					{
						//在相应的SHARE_INFO中status设为QUEUE
						DataSetShareInfo.SHARE_INFORow rowShareInfo = this.frmMessage.frmMain.DsShareInfo.SHARE_INFO.FindByID(row.ShareInfo);
						rowShareInfo.Status = Enum.GetName(typeof(ShareInfoStatusTypeEnum),ShareInfoStatusTypeEnum.QUEUE);

						//发送邮件
						//投递回复函
						DataSetMessage.MESSAGE_INFORow rowToSend = this.frmMessage.frmMain.DsMessage.MESSAGE_INFO.NewMESSAGE_INFORow();
						rowToSend.Body = this.rtbMessage.Text;
						rowToSend.MessageFrom = row.MessageTo;
						rowToSend.MessageTo = row.MessageFrom;
						rowToSend.MessageType = Enum.GetName(typeof(MessageTypeEnum),MessageTypeEnum.RESPONSE);
						rowToSend.Title = this.tbxTitle.Text;
						rowToSend.HasRead = 0;
						rowToSend.SendDate = DateTime.Today;

						this.frmMessage.frmMain.DsMessage.MESSAGE_INFO.AddMESSAGE_INFORow(rowToSend);

						//设置当前行为已读
						row.HasRead = 1;

						//关闭窗口
						this.Close();
						this.frmMessage.Close();
						break;
					}
					case 1://发送拒绝
					{
						//在相应的SHARE_INFO中status设为REJECT
						DataSetShareInfo.SHARE_INFORow rowShareInfo = this.frmMessage.frmMain.DsShareInfo.SHARE_INFO.FindByID(row.ShareInfo);
						rowShareInfo.Status = Enum.GetName(typeof(ShareInfoStatusTypeEnum),ShareInfoStatusTypeEnum.REJECT);

						//发送邮件
						//投递回复函
						DataSetMessage.MESSAGE_INFORow rowToSend = this.frmMessage.frmMain.DsMessage.MESSAGE_INFO.NewMESSAGE_INFORow();
						rowToSend.Body = this.rtbMessage.Text;
						rowToSend.MessageFrom = row.MessageTo;
						rowToSend.MessageTo = row.MessageFrom;
						rowToSend.MessageType = Enum.GetName(typeof(MessageTypeEnum),MessageTypeEnum.RESPONSE);
						rowToSend.Title = this.tbxTitle.Text;
						rowToSend.HasRead = 0;
						rowToSend.SendDate = DateTime.Today;

						this.frmMessage.frmMain.DsMessage.MESSAGE_INFO.AddMESSAGE_INFORow(rowToSend);

						//设置当前行为已读
						row.HasRead = 1;

						//关闭窗口
						this.Close();
						this.frmMessage.Close();
						break;
					}
					case 2://关闭
						//关闭窗口
						this.Close();
						break;

				}
			}
			catch(Exception ex)
			{
				string str = ex.Message;
			}

		}

	}
}
