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.Home;
using JuSNS.Model;
using JuSNS.UI.Control;

namespace JuSNS.Web.home.mood
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        public string sList = string.Empty;
        public bool isMy = false;
        public string userName = string.Empty;
        protected string checkstr = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["Option"] == "Delete")
            {
                string ID = Request.Form["ID"];
                Mood md = new Mood();
                if (md.DeleteMini(this.GetUserID(), Convert.ToInt32(ID)) > 0)
                {
                    Response.Write("1");
                    Response.End();
                }
            }
            else
            {
                string r = Request.QueryString["r"];
                string p = Request.QueryString["p"];
                if (p == "1")
                {
                    this.DivMsg.Visible = true;
                    this.DivMsg.InnerHtml = "添加成功！";
                }
                if (r == "my") { isMy = true; } else { isMy = false; }
                sList = "<select name=\"mType\" id=\"mType\">";
                string opitem = JuSNS.Config.UiConfig.MiniType;
                string tmpItem = string.Empty;
                sList += "<option value=\"\">选择类型</option>";
                if (opitem.IndexOf(",") > -1)
                {
                    string[] opitemARR = opitem.Split(',');
                    for (int i = 0; i < opitemARR.Length; i++)
                    {
                        sList += "<option value=\"" + opitemARR[i] + "\">" + opitemARR[i] + "</option>";
                    }
                }
                sList += "</select>";

                this.DivMsg.Visible = false;
                if (Request.Form["Action"] == "Submit")
                {
                    if (AddNewMini() > 0)
                    {
                        HttpCookie cookiesu = new HttpCookie("miniblog");
                        cookiesu["recent"] = "true";
                        Response.AppendCookie(cookiesu);
                        Response.Redirect("../mood/?p=1&r=my");
                    }
                }
                HttpCookie cookie = Request.Cookies["miniblog"];
                if (cookie != null && cookie["recent"] == "true")
                {
                    this.DivMsg.Visible = true;
                    cookie.Expires = DateTime.Now.AddMilliseconds(0.1);
                    Response.AppendCookie(cookie);
                }
                BindData();
                DataBind();
            }
        }
        /// <summary>
        /// 分页绑定数据
        /// </summary>
        protected void BindData()
        {
            int ReCount = 0;
            int PgCount = 0;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            
            DataTable dt = null;
            string mid = Request.QueryString["mid"];
            string r = Request.QueryString["r"];
            if (JuSNS.Common.Input.IsInteger(mid))
            {
                dt = UtilPage.GetPage("mood_id", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32), new SqlConditionInfo("@MID", Convert.ToInt32(mid), TypeCode.Int32));
            }
            else
            {
                if (r != string.Empty && r != null)
                {
                    if (JuSNS.Common.Input.IsInteger(r))
                    {
                        if (!this.PrivacyAvailable(EnumPrivacyType.MiniBlog, GetUserID(), Convert.ToInt32(r)))
                        {
                            PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
                        }

                        JuSNS.Home.User us = new User();
                        JuSNS.Model.UserInfo mdl = new UserInfo();
                        mdl = us.GetUserInfo(Convert.ToInt32(r));
                        userName = mdl.Username + "的";
                        this.fDiv.Attributes.Add("class", "current");
                        this.myDiv.Attributes.Add("class", "");
                        this.allDiv.Attributes.Add("class", "");
                        dt = UtilPage.GetPage("mood_my", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", Convert.ToInt32(r), TypeCode.Int32), new SqlConditionInfo("@MID", Convert.ToInt32(mid), TypeCode.Int32));
                    }
                    else
                    {
                        if (r == "my")
                        {
                            if (this.GetUserID() == 0)
                            {
                                PageError("需要登录才能查看！", rooDir + "/?urls=" + Request.Url + "", true);
                            }
                            this.fDiv.Attributes.Add("class", "");
                            this.myDiv.Attributes.Add("class", "current");
                            this.allDiv.Attributes.Add("class", "");
                            dt = UtilPage.GetPage("mood_my", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32), new SqlConditionInfo("@MID", Convert.ToInt32(mid), TypeCode.Int32));
                        }
                        else if (r == "all")
                        {
                            this.fDiv.Attributes.Add("class", "");
                            this.myDiv.Attributes.Add("class", "");
                            this.allDiv.Attributes.Add("class", "current");
                            dt = UtilPage.GetPage("mood_all", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32), new SqlConditionInfo("@MID", Convert.ToInt32(mid), TypeCode.Int32));
                        }
                        else
                        {
                            PageError("错误的参数", "javascript:history.back();");
                        }
                    }
                }
                else
                {
                    this.fDiv.Attributes.Add("class", "current");
                    this.myDiv.Attributes.Add("class", "");
                    this.allDiv.Attributes.Add("class", "");
                    dt = UtilPage.GetPage("mood_friend", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32));
                }
            }
            if (this.GetUserID() == 0)
            {
                this.fDiv.InnerHtml = "<a href=\"../mood/\" onclick=\"javascript:{unlogin();return false}\">好友的心情</a>";
                this.myDiv.InnerHtml = "<a href=\"../mood/?r=my\" onclick=\"javascript:{unlogin();return false}\">我的心情</a>";
                this.PostMoodButton.Value = "请登录后发布";
                this.PostMoodButton.Disabled = true;
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string kk = dt.Rows[i]["Content"].ToString();
            }
            this.RptData.DataSource = dt;
            this.RptData.DataBind();
            this.LtrNum.Text = ReCount.ToString();
            if (ReCount > PAGESZ)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, "/home/mood/?r=" + r + "");
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }

        /// <summary>
        /// 新增心情日记
        /// </summary>
        protected int AddNewMini()
        {
            GetIsNotEmail();
            string content = JuSNS.Common.Input.loseExceptImg(Request.Form["TxtContent"]);
            string conStr = JuSNS.Common.Input.FilterHTML(Request.Form["TxtContent"]);
            if (Request.Form["TxtContent"] == null || Request.Form["TxtContent"] == string.Empty)
            {
                this.DivMsg1.Visible = true;
                this.DivMsg1.InnerHtml = "心情日记必须填写";
                return 0;
            }

            if (Request.Form["TxtContent"].Length > 250)
            {
                this.DivMsg1.Visible = true;
                this.DivMsg1.InnerHtml = "字数不能超过250个字符";
                return 0;
            }
            #region 过滤关键字
            string res = JuSNS.Common.Public.CheckKey(conStr, true);
            if (res != "")
            {
                content = content.Replace(res, "***");
            }
            #endregion
            JuSNS.Home.Mood md = new JuSNS.Home.Mood();
            string MType = string.Empty;
            if (Request.Form["mType"] != null && Request.Form["mType"] != string.Empty)
            {
                MType = Request.Form["mType"];
            }
            return md.SaveMini(GetUserID(), content, MType);
        }

        /// <summary>
        /// 显示删除
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        protected string ShowDel(object uid, object mid)
        {
            if (GetUserID() == (int)uid)
                return "<span class=\"delete\"><a href=\"javascript:void(0);\" onclick=\"Confirm(this,'确定删除心情日记吗？','删除心情日记',function(){Delete(" + mid + ")});\">删除</a></span>";
            else
                return string.Empty;
        }

        protected string ShowType(object MType)
        {
            string listStr = string.Empty;
            if (MType != null)
            {
                if (MType.ToString() != string.Empty && MType.ToString() != null)
                {
                    listStr = "<span class=\"minitype\">[" + MType.ToString() + "]</span> ";
                }
            }
            return listStr;
        }

        protected int ShowComment(object mid)
        {
            JuSNS.Home.Mood mini = new JuSNS.Home.Mood();
            return mini.GetMiniCount(Convert.ToInt32(mid.ToString()));
        }
    }
}
