using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
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.Common;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Config;
using JuSNS.UI.Control;

namespace JuSNS.Web.home.note
{
    public partial class index : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                JuSNS.Home.Notepad pd = new JuSNS.Home.Notepad();
                string r = Request.QueryString["r"];
                string ID = Request.QueryString["Id"];
                this.TXTime.Text = DateTime.Now.ToShortDateString();
                if (r == "write")
                {
                    this.ShowWriteNote.Visible = true;
                    this.showListNote.Visible = false;
                    this.MyNote.Attributes.Add("class", "");
                    this.WriteNote.Attributes.Add("class", "current");
                }
                else if (r == "del")
                {
                    if (pd.DelNote(UserID, Convert.ToInt32(ID)) > 0)
                    {
                        ShowRight("删除成功");
                    }
                    else
                    {
                        ShowError("删除失败");
                    }
                    this.ShowWriteNote.Visible = false;
                    this.showListNote.Visible = false;
                    this.MyNote.Attributes.Add("class", "current");
                    this.WriteNote.Attributes.Add("class", "");
                }
                else
                {
                    this.ShowWriteNote.Visible = false;
                    this.showListNote.Visible = true;
                    this.MyNote.Attributes.Add("class", "current");
                    this.WriteNote.Attributes.Add("class", "");
                }
                if (JuSNS.Common.Input.IsInteger(ID))
                {
                    this.WriteNote.InnerHtml = "<a href=\"javascript:;\">修改事件</a>";
                    IDataReader dr = pd.GetContent(UserID, Convert.ToInt32(ID));
                    if (dr.Read())
                    {
                        if (UserID.ToString() != dr["UserID"].ToString())
                        {
                            ShowError("你没权限查看别人的事件！");
                        }
                        this.title.Text = dr["title"].ToString();
                        this.Content.Text = dr["Content"].ToString();
                        this.TXTime.Text = DateTime.Parse(dr["TXTime"].ToString()).ToShortDateString();
                    }
                    dr.Close();
                }
                else
                {
                    myNote();
                }
            }
        }

        protected void myNote()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            DataTable dt = UtilPage.GetPage("NotePad_aspx", PageIndex, 50, out ReCount, out PgCount, new SqlConditionInfo("@UserID", UserID, TypeCode.Int32));
            string PageUrl = "../note";
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            JuSNS.Home.Notepad np = new JuSNS.Home.Notepad();
            JuSNS.Home.User u = new JuSNS.Home.User();
            if (ReCount > 0)
            {
                this.noNote1.Visible = true;
                this.noNote1.InnerHtml = ReCount + "个事件<span class=\"pipe\">|</span><a href=\"../note/?r=write\">创建新事件</a>";
            }
            else
            {
                this.noNote1.Visible = false;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                string reshowStr = string.Empty;
                string titleStr = string.Empty;
                string TTXTime = DateTime.Parse(dt.Rows[i]["TXTime"].ToString()).ToShortDateString();
                string NTXTime = DateTime.Now.ToShortDateString();
                TimeSpan ts = DateTime.Parse(TTXTime).Subtract(DateTime.Parse(NTXTime));
                //TimeSpan ts1 = DateTime.Parse(NTXTime).s(DateTime.Parse(TTXTime));

                if (ts.Days <= Convert.ToInt32(JuSNS.Config.UiConfig.noteDate) && ts.Days>=0)
                {
                    reshowStr = " reshow bold";
                    titleStr = "title=\"您要做的事件！\"";
                }
                else
                {
                    reshowStr = string.Empty;
                }
                sb.Append("<div class=\"name nl " + reshowStr + "\" " + titleStr + "><a href=\"../note?r=write&Id=" + dt.Rows[i]["ID"].ToString() + "\">" + dt.Rows[i]["title"].ToString() + "</a></div>\n");
                sb.Append("<div class=\"contents nl " + reshowStr + "\" " + titleStr + "><span id=\"s_" + dt.Rows[i]["ID"].ToString() + "\"><a href=\"javascript:showNoteContent('" + dt.Rows[i]["ID"].ToString() + "')\"><img src=\"../../template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/site/note.gif\" title=\"点击查看详情\" align=\"absmiddle\" /></a></span></div>\n");
                sb.Append("<div class=\"txtime nl " + reshowStr + "\" " + titleStr + ">" + DateTime.Parse(dt.Rows[i]["TXTime"].ToString()).ToString("yyyy-MM-dd") + "</div>\n");
                sb.Append("<div class=\"posttime nl " + reshowStr + "\" " + titleStr + ">" + JuSNS.Common.Public.getTimeEXSpan(DateTime.Parse(dt.Rows[i]["LastPostTime"].ToString())) + "</div>\n");
                sb.Append("<div class=\"del nl " + reshowStr + "\" " + titleStr + "><a href=\"../note/?r=del&Id=" + dt.Rows[i]["ID"].ToString() + "\" onclick=\"{if(confirm('确定要删除吗？')){return true;}return false;}\">删除</a></div>\n");
                sb.Append("<div id=\"c_" + dt.Rows[i]["ID"].ToString() + "\" style=\"display:block;\" class=\"cbbg nl\" " + titleStr + ">" + dt.Rows[i]["Content"].ToString() + "</div>\n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.MyNoteList.Visible = true;
            this.MyNoteList.DataSource = data;
            this.MyNoteList.DataBind();
            PageUrl += "?";
            if (ReCount > 50)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GetIsNotEmail();
            if (Page.IsValid)
            {
                int NoteID = 0;
                int nflg = 0;
                if (JuSNS.Common.Input.IsInteger(Request.QueryString["Id"]))
                {
                    NoteID = Convert.ToInt32(Request.QueryString["Id"]);
                    nflg = 1;
                }
                int nUserID = UserID;
                string nTitle = this.title.Text;
                string nContent = this.Content.Text;
                if (nTitle.Length < 2)
                {
                    ShowError("请正确填写标题");
                    return;
                }
                if (nContent.Length < 3)
                {
                    ShowError("请正确填写事件内容");
                    return;
                }
                if (!JuSNS.Common.Input.IsDate(this.TXTime.Text))
                {
                    ShowError("请填写有效的时间！格式：2008/10/1");
                    return;
                }
                DateTime nTXTime = DateTime.Parse(this.TXTime.Text);
                JuSNS.Home.Notepad np = new JuSNS.Home.Notepad();
                if (np.SaveNote(nUserID, nTitle, nContent, nTXTime, nflg, NoteID) > 0)
                {
                    if (nflg == 0)
                    {
                        ShowRight("添加新事件成功！");
                    }
                    else
                    {
                        ShowRight("修改事件成功！");
                    }
                }
                else
                {
                    ShowError("发生错误！");
                }
            }
        }

        protected void ShowError(string MSG)
        {
            this.msg_error.Visible = true;
            this.msg_success.Visible = false;
            this.msg_error.InnerHtml = MSG;
        }
        protected void ShowRight(string MSG)
        {
            this.msg_error.Visible = false;
            this.msg_success.Visible = true;
            this.msg_success.InnerHtml = MSG;
        }
    }
}
