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>
	/// MessageFrm 的摘要说明。
	/// </summary>
	public class MessageFrm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ToolBar tbrMain;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.GroupBox gbxTop;
		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;
		internal System.Windows.Forms.RichTextBox rtbMessage;
		private System.Windows.Forms.ToolBarButton tbbApprove;
		private System.Windows.Forms.ToolBarButton tbbReject;
		private System.Windows.Forms.ToolBarButton tbbCancel;
		private System.Windows.Forms.Panel pnlFill;
		private System.Windows.Forms.Label lblDate; 

		#region 自定义字段
		/// <summary>
		/// 主窗体引用
		/// </summary>
		internal MainFrm frmMain;
		/// <summary>
		/// 邮件类型:System, Request, Response, mail
		/// </summary>
		private MessageTypeEnum msgType;
		/// <summary>
		/// 同意:1或拒绝:0，未定义为-1
		/// </summary>
		internal UIRequestApprovedEnum approved;

		//Main窗口对象的本地引用
		//书本信息
		private BookInformation m_curBookInfo;
		//用户信息
		private UserInformation m_curUserInfo;
		//书籍封面
		private Image m_curCoverPage;
		//auth服务
		private AuthService m_authSvc;
		//data服务
		private DataService m_dataSvc;
		//ticket
		private string m_authTicket;

		/// <summary>
		/// cache了的ShareInfo
		/// </summary>
		internal DataSetShareInfo m_dsShareInfo ;
		private DataSetMessage m_dsMessage;
		internal System.Windows.Forms.ImageList ilToolbar;

		/// <summary>
		/// 这份Message的id
		/// </summary>
		internal int MessageID;

//		public string MessageTitle
//		{
//			get
//			{
//				return this.lblTitle.Text;
//			}
//		}

		#endregion

		#region 窗体创建与销毁
		public MessageFrm(MainFrm mainFrm, int id)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			this.frmMain = mainFrm;
			this.approved = UIRequestApprovedEnum.UNDEFINED;

			//设置message的类型
			string str = this.frmMain.DsMessage.MESSAGE_INFO.FindByID(id).MessageType;
			if(str.ToUpper()=="REQUEST")
			{
				this.msgType = MessageTypeEnum.REQUEST;
			}
			else if(str.ToUpper()=="RESPONSE")
			{
				this.msgType = MessageTypeEnum.RESPONSE;
			}
			else if(str.ToUpper()=="SYSTEM")
			{
				this.msgType = MessageTypeEnum.SYSTEM;
			}
			else//"MAIL"
			{
				this.msgType = MessageTypeEnum.MAIL;
			}

			//按照msgType设置tbbReply是否可用
			if(this.msgType==MessageTypeEnum.REQUEST/*且未读*/)
			{
				this.tbbApprove.Visible = true;
				this.tbbReject.Visible = true;
			}

			this.m_curBookInfo = mainFrm.CurrentBookInfo;
			this.m_curCoverPage = mainFrm.CurrentCoverPage;
			this.m_curUserInfo = mainFrm.CurrentUserInfo;

			this.m_authSvc = mainFrm.m_authSvc;
			this.m_dataSvc = mainFrm.m_dataSvc;

			this.m_dsShareInfo = null;//indicate that we need to retrieve data

			this.m_authTicket = mainFrm.m_authTicket;

			this.m_dsMessage = mainFrm.DsMessage;		
			
			this.MessageID = id;

			//将message的信息邦定到各个控件中
			//this.rtbMessage.Text = "你好，我想借您的书。。。";
			DataSetMessage.MESSAGE_INFORow row = this.m_dsMessage.MESSAGE_INFO.FindByID(this.frmMain.CurrentSelectedID);

			this.lblFrom.Text = row["Sender"] as string;
			this.lblTo.Text = row["Receiver"] as string;
			this.lblTitle.Text = row["Title"] as string;
			this.lblDate.Text = ((DateTime)row["SendDate"]).ToLongDateString();

			this.rtbMessage.Text = row["Body"] as string;

		}

		/// <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(MessageFrm));
			this.tbrMain = new System.Windows.Forms.ToolBar();
			this.tbbApprove = new System.Windows.Forms.ToolBarButton();
			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.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.lblDate = new System.Windows.Forms.Label();
			this.ilToolbar = new System.Windows.Forms.ImageList(this.components);
			this.pnlFill.SuspendLayout();
			this.gbxTop.SuspendLayout();
			this.SuspendLayout();
			// 
			// 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 = 5;
			this.tbrMain.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
			this.tbrMain.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.tbrMain_ButtonClick);
			// 
			// tbbApprove
			// 
			this.tbbApprove.ImageIndex = 0;
			this.tbbApprove.Text = "同意";
			this.tbbApprove.ToolTipText = "回复并协商接洽时间和地点";
			this.tbbApprove.Visible = false;
			// 
			// tbbReject
			// 
			this.tbbReject.ImageIndex = 1;
			this.tbbReject.Text = "拒绝";
			this.tbbReject.ToolTipText = "发送拒绝消息";
			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 = 6;
			// 
			// rtbMessage
			// 
			this.rtbMessage.Dock = System.Windows.Forms.DockStyle.Fill;
			this.rtbMessage.Location = new System.Drawing.Point(4, 76);
			this.rtbMessage.Name = "rtbMessage";
			this.rtbMessage.ReadOnly = true;
			this.rtbMessage.Size = new System.Drawing.Size(624, 362);
			this.rtbMessage.TabIndex = 3;
			this.rtbMessage.Text = "";
			// 
			// gbxTop
			// 
			this.gbxTop.Controls.Add(this.label2);
			this.gbxTop.Controls.Add(this.lblFrom);
			this.gbxTop.Controls.Add(this.lblTo);
			this.gbxTop.Controls.Add(this.lblTitle);
			this.gbxTop.Controls.Add(this.label9);
			this.gbxTop.Controls.Add(this.label8);
			this.gbxTop.Controls.Add(this.label1);
			this.gbxTop.Controls.Add(this.lblDate);
			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, 76);
			this.gbxTop.TabIndex = 2;
			this.gbxTop.TabStop = false;
			// 
			// 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, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(60, 17);
			this.label2.TabIndex = 48;
			this.label2.Text = "标题:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblFrom
			// 
			this.lblFrom.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblFrom.Location = new System.Drawing.Point(72, 16);
			this.lblFrom.Name = "lblFrom";
			this.lblFrom.Size = new System.Drawing.Size(184, 16);
			this.lblFrom.TabIndex = 47;
			this.lblFrom.Text = "lblFrom";
			this.lblFrom.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblTo
			// 
			this.lblTo.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblTo.Location = new System.Drawing.Point(72, 32);
			this.lblTo.Name = "lblTo";
			this.lblTo.Size = new System.Drawing.Size(184, 16);
			this.lblTo.TabIndex = 46;
			this.lblTo.Text = "lblTo";
			this.lblTo.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblTitle
			// 
			this.lblTitle.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblTitle.Location = new System.Drawing.Point(72, 48);
			this.lblTitle.Name = "lblTitle";
			this.lblTitle.Size = new System.Drawing.Size(520, 16);
			this.lblTitle.TabIndex = 45;
			this.lblTitle.Text = "lblTitle";
			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, 32);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(60, 17);
			this.label9.TabIndex = 43;
			this.label9.Text = "收件人:";
			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, 16);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(48, 17);
			this.label8.TabIndex = 42;
			this.label8.Text = "发件人:";
			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, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 17);
			this.label1.TabIndex = 49;
			this.label1.Text = "发送时间:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblDate
			// 
			this.lblDate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.lblDate.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblDate.Location = new System.Drawing.Point(408, 16);
			this.lblDate.Name = "lblDate";
			this.lblDate.Size = new System.Drawing.Size(184, 16);
			this.lblDate.TabIndex = 44;
			this.lblDate.Text = "lblDate";
			this.lblDate.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// 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;
			// 
			// MessageFrm
			// 
			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 = "MessageFrm";
			this.Text = "MessageFrm";
			this.pnlFill.ResumeLayout(false);
			this.gbxTop.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		#endregion

		private void tbrMain_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch(this.tbrMain.Buttons.IndexOf(e.Button))
			{
				case 0://同意
					this.approved = UIRequestApprovedEnum.APPROVED;
					MsgEditFrm form1 = new MsgEditFrm(this);
					form1.Show();
					break;
				case 1://拒绝
					this.approved = UIRequestApprovedEnum.NOTAPPROVED;
					MsgEditFrm form2 = new MsgEditFrm(this);
					form2.Show();
					break;
				case 2://关闭
					DataSetMessage.MESSAGE_INFORow row = this.m_dsMessage.MESSAGE_INFO.FindByID(this.MessageID);
					row.HasRead = 1;
					this.Close();
					break;
			}
		}
	}
}
