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.Home;

namespace JuSNS.Web.user.blog
{
    public partial class edit : JuSNS.UI.Page.UserPage
    {
        public string   isEdit = "<a href=\"\">写日志</a>";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetIsNotEmail();
                string isHeadPic = this.GetHeadPath(this.UserID);

                if (string.IsNullOrEmpty(isHeadPic))
                {
                    string errorStr = "您需要上传了头像后，才能 <b>发表、修改博客日志</b>！";
                    if (JuSNS.Config.UiConfig.isUpHeadPic == "1" && !JuSNS.Common.Public.GetHeadLimite(3))
                    {
                        Response.Redirect(rooDir + "/home/space/userheadphoto.aspx?h=1&Err=" + JuSNS.Common.Input.URLEncode(errorStr) + "");
                    }
                }

                this.HidBlogId.Value = "0";
                this.DivError.Visible = false;
                this.DivDS.Visible = false;
                ListItem it0 = new ListItem("相同网络用户及好友可见", ((int)EnumPrivacy.ForNetWorkAndFriends).ToString());
                this.DdlPrvcy.Items.Add(it0);
                ListItem it1 = new ListItem("全站用户可见", ((int)EnumPrivacy.ForWholeSite).ToString());
                it1.Selected = true;
                this.DdlPrvcy.Items.Add(it1);
                ListItem it2 = new ListItem("仅好友可见", ((int)EnumPrivacy.ForFriends).ToString());
                this.DdlPrvcy.Items.Add(it2);
                ListItem it3 = new ListItem("仅自己可见", ((int)EnumPrivacy.ForOwner).ToString());
                this.DdlPrvcy.Items.Add(it3);
                this.LtrCaption.Text = "发表新博客日志";
                string calid = Request.QueryString["calid"]; //日程id
                if (!string.IsNullOrEmpty(calid))
                {
                    this.calid.Value = calid;
                }
                if (JuSNS.Common.Input.IsInteger(Request.QueryString["r"]))
                {
                    isEdit = "<a href=\"\">编辑日志</a>";
                    ptitle.InnerHtml = "编辑日志";
                    int id = int.Parse(Request.QueryString["r"]);
                    this.HidBlogId.Value = id.ToString();
                    this.LtrCaption.Text = "编辑博客日志";
                    bool isdraft = false;
                    if (Request.QueryString["a"] != null && Request.QueryString["a"].ToLower() == "draft")
                    {
                        isdraft = true;
                        this.HidIsDraft.Value = "1";
                    }
                    else
                    {
                        this.BtnDraft.Visible = false;
                    }
                    JuSNS.Home.Blog bl = new JuSNS.Home.Blog();
                    BlogInfo info = bl.GetBlog(id, isdraft);
                    if (info.UserID != UserID)
                    {
                        PageError("没有权限编辑该日志", "javascript:history.back();", false);
                    }
                    this.TxtTitle.Text = info.Title;
                    this.TxtContent.Value = info.Content;
                    this.DdlPrvcy.SelectedValue = ((int)info.Privacy).ToString();
                    //this.BtnDraft.Visible = false;
                }
                else
                {
                    this.BtnDel.Visible = false;
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            Save(false);
        }

        protected void BtnDraft_Click(object sender, EventArgs e)
        {
            Save(true);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="isDraft"></param>
        protected void Save(bool isDraft)
        {
            string title = this.TxtTitle.Text;
            //string content = JuSNS.Common.Input.loseExceptImg(this.TxtContent.Value);
            string content = this.TxtContent.Value;
            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content))
            {
                this.DivError.Visible = true;
                return;
            }

            content = JuSNS.Common.Input.loseIframe(content);
            content = JuSNS.Common.Input.loseScript(content);

            #region 过滤关键字
            string res = JuSNS.Common.Public.CheckKey(content, true);
            if (res != "")
            {
                content = content.Replace(res,"***");
            }
            #endregion

            BlogInfo info = new BlogInfo();
            info.UserID = UserID;
            info.Title = title;
            info.Content = content;
            info.ID = int.Parse(this.HidBlogId.Value);
            JuSNS.Home.Blog bl = new JuSNS.Home.Blog();
            if (info.ID > 0)
            {
                if (isDraft)
                {
                    bl.Edit(info, 1);
                }
                else
                {
                    int n = 0;
                    if (this.HidIsDraft.Value == "1")
                    {
                        n = bl.Edit(info, 2);
                    }
                    else
                    {
                        bl.Edit(info, 0);
                        n = info.ID;
                    }
                    PageRight("修改日志成功！", rooDir + "/blog/b" + n + ".aspx");
                }
            }
            else
            {
                int newid = bl.Deliver(info, isDraft);                
                this.HidBlogId.Value = newid.ToString();
                if (!isDraft)
                {
                    JuSNS.Home.User us = new JuSNS.Home.User();
                    us.updateinte(UserID, 2, 0, 0,"发表日志");
                    PageRight("发表日志成功！", rooDir + "/blog/b" + newid + ".aspx");
                    //Response.Redirect("" + rooDir + "/blog/b" + newid + ".aspx");
                }
            }
            if (isDraft)
            {
                this.DivDS.Visible = true;
            }
        }

        protected void BtnDel_Click(object sender, EventArgs e)
        {
            int id = int.Parse(this.HidBlogId.Value);
            JuSNS.Home.Blog bl = new JuSNS.Home.Blog();
            if (this.HidIsDraft.Value == "1")
            {
                bl.DelDraft(UserID, id);
                Response.Redirect("draft.aspx");
            }
            else
            {
                bl.DelBlog(UserID, id);
                User us = new User();
                us.updateinte(UserID, 2, 0, 1, "删除日志");
                //Response.Redirect("../blog/?r=my");
                PageRight("删除日志成功！", rooDir + "/home/blog/?r=my");
            }
        }

    }
}
