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 JuSNS.Model;
using JuSNS.UI.Control;
using JuSNS.Common;
namespace JuSNS.Web.home.mail
{
    public partial class mail : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            LoadList();
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            DivPage.Visible = false;
            string s_LetterID = Request.QueryString["mid"];
            int r, p, PageIndex = 0, LetterID = Convert.ToInt32(s_LetterID);
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            SqlConditionInfo st = new SqlConditionInfo("@TopicID", LetterID, TypeCode.Int32);

            DataTable dt = JuSNS.Home.UtilPage.GetPage("Mailinfo_aspx", PageIndex, PAGESZ, out r, out p, st);
            if (dt != null)
            {
                Rpt_Mailinfo.DataSource = dt;
                Rpt_Mailinfo.DataBind();
                dt.Clear();
                dt.Dispose();
            }
            //判断是否调用分页
            if (r > PAGESZ)
            {
                DivPage.Visible = true;
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "Mailinfo.aspx");
            }
            JuSNS.Home.Mail letter = new JuSNS.Home.Mail();
            JuSNS.Model.Mailinfo Mailinfo = new JuSNS.Model.Mailinfo();
            Mailinfo = letter.Info(LetterID);
            H_Title.InnerHtml = Mailinfo.Title;

            Hid_Receiver.Value = Mailinfo.Sender.ToString();
            Hid_LetterID.Value = LetterID.ToString();

            letter.Read(LetterID, UserID);
        }

        

        /// <summary>
        /// 取得时间
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns></returns>
        protected string getTime(object time)
        {
            return JuSNS.Common.Public.getTimeSpan(Convert.ToDateTime(time));
        }

        /// <summary>
        /// 添加回复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool tf = true;
                if (Hid_LetterID.Value.Trim() == "")
                {
                    tf = false;
                    ShowErr("参数传递错误", div_Error);
                    goto Finish;
                }
                if (Txt_Content.Text.Trim() == "")
                {
                    tf = false;
                    ShowErr("请填写内容", div_Error);
                    goto Finish;
                }
                if (tf)
                {
                    JuSNS.Home.Mail Mail = new JuSNS.Home.Mail();
                    JuSNS.Model.Mailinfo Mailinfo = new JuSNS.Model.Mailinfo();
                    Mailinfo.ID = 0;
                    Mailinfo.Sender = UserID;
                    Mailinfo.Receiver = Convert.ToInt32(Hid_Receiver.Value);
                    Mailinfo.Title = "";
                    Mailinfo.Content = Txt_Content.Text;
                    Mailinfo.PostTime = DateTime.Now;
                    Mailinfo.PostIP = JuSNS.Common.Public.GetClientIP();
                    Mailinfo.TopicID = Convert.ToInt32(Hid_LetterID.Value);
                    Mailinfo.IsReply = false;
                    Mailinfo.LtType = 0;
                    Mailinfo.RelativeID = 0;
                    Mailinfo.IsRead = 0;

                    if (Mail.Reply(Mailinfo) >0)
                    {
                        Response.Cookies["ReLetterIsScu"].Value = "true";
                        ShowSuc("回复成功!", div_Suc);
                        Txt_Content.Text = "";
                        LoadList();
                    }
                }
            Finish: ;
            }
        }

    }
}
