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.Group
{
    public partial class Topic : JuSNS.UI.Page.UserPage
    {
        bool isre = true;
        protected string ruser = string.Empty;
        protected bool isGroupHead = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            string isHeadPic = this.GetHeadPath(this.UserID);
            if (string.IsNullOrEmpty(isHeadPic))
            {
                if (JuSNS.Config.UiConfig.isUpHeadPic == "1" && !JuSNS.Common.Public.GetHeadLimite(2))
                {
                    isGroupHead = false;
                }
            }
            ruser = UserID.ToString();
            Response.CacheControl = "no-cache";
            CheckPublicity();
            StartLoad();
        }

        /// <summary>
        /// 检查是否允许被访问,并且加载页面初始信息
        /// </summary>
        protected void CheckPublicity()
        {
            bool tf = false;
            bool tftop = false;
            string s_DisID = Request.QueryString["DisID"];
            if (string.IsNullOrEmpty(s_DisID))
            {
                PageError("参数传递错误!", "" + rooDir + "/home/group");
            }
            DisID.Value = s_DisID;
            Div_JoinGroup.Visible = false;
            JuSNS.Home.Group groupdis = new JuSNS.Home.Group();
            JuSNS.Model.GroupDiscussInfo groupdisinfo = new JuSNS.Model.GroupDiscussInfo();
            groupdisinfo = groupdis.Info(Convert.ToInt32(s_DisID));

            JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
            UserGroupInfo usergroupinfo = usergroup.Info(groupdisinfo.GroupID);
            GroupID.Value = groupdisinfo.GroupID.ToString();
            tf = usergroup.CheckPublicity(groupdisinfo.GroupID, UserID, true);
            if (!tf)
            {
                PageError("您没有访问权限!", "" + rooDir + "/home/group");
            }
            this.Span_GroupName.InnerHtml = usergroupinfo.GroupName;
            this.A_Return.HRef = "group.aspx?GroupID=" + groupdisinfo.GroupID;
            //A_A_Return1.HRef = "group.aspx?GroupID=" + groupdisinfo.GroupID;
            this.A_ReturnTop.HRef = "Group-m.aspx?GroupID=" + groupdisinfo.GroupID;
            this.A_ReturnTop1.HRef = "Group-m.aspx?GroupID=" + groupdisinfo.GroupID;
            this.Img_GroupIMG.Src = this.GetGroupHead(usergroupinfo.Portrait);
            this.Img_GroupIMG.Alt = usergroupinfo.GroupName;
            this.P_Count.InnerHtml = groupdisinfo.Replys.ToString();

            JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();
            if (!groupmember.IsGroupMember(UserID, groupdisinfo.GroupID))
            {
                this.Div_NewTopic.Visible = false;
                this.Div_JoinGroup.Visible = true;
                isre = false;
                this.but_Join.Attributes.Add("onclick", "self.location='group.aspx?Action=Join&GroupID=" + GroupID.Value + "'");
            }
            if (groupdisinfo.TopNum == 1)
            {
                this.H_TopicName.InnerHtml = "置顶：" + groupdisinfo.Topic;
                tftop = true;
            }
            else
            {
                this.H_TopicName.InnerHtml = groupdisinfo.Topic;
            }
            this.ptitle.InnerText = groupdisinfo.Topic;
            if (groupmember.IsGroupAdmin(UserID, usergroupinfo.GroupID) || usergroupinfo.UserID == UserID)
            {
                string s_Op = "";
                if (tftop)
                {
                    s_Op = "<li><a href=\"javascript:void(0);\" rev=\"ShowMsg\" deltype=\"UnTopTopic\" userid=\"" + UserID + "\" tempid=\"" + s_DisID + "\" rel=\"untop\">取消置顶</a></li>";
                }
                else
                {
                    s_Op = "<li><a href=\"javascript:void(0);\" rev=\"ShowMsg\" deltype=\"TopTopic\" userid=\"" + UserID + "\" tempid=\"" + s_DisID + "\" rel=\"top\">置顶主题</a></li><li><a href=\"javascript:void(0);\" rev=\"ShowMsg\" deltype=\"DelTopTopic\" userid=\"" + UserID + "\" tempid=\"" + s_DisID + "\" rel=\"del\">删除主题</a></li>";

                }
                this.Div_OpTopic.InnerHtml = s_Op;
            }
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        protected void StartLoad()
        {
            DivPage.Visible = false;
            int r, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            SqlConditionInfo st = new SqlConditionInfo("@ID", DisID.Value, TypeCode.Int32);
            DataTable dt = JuSNS.Home.UtilPage.GetPage("GroupDiscussInfo_aspx", PageIndex, 20, out r, out p, st);
            if (dt != null)
            {
                Rpt_ReList.DataSource = dt;
                Rpt_ReList.DataBind();
                dt.Clear();
                dt.Dispose();
            }
            //判断是否调用分页
            if (r > 20)
            {
                DivPage.Visible = true;
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "topic.aspx?DisID=" + DisID.Value);
            }
        }

        

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

        /// <summary>
        /// 取得楼数
        /// </summary>
        /// <param name="SeriesNum">当前楼数</param>
        /// <returns></returns>
        protected string getSeriesNum(object SeriesNum)
        {
            int i = Convert.ToInt32(SeriesNum) + 1;
            return i.ToString();
        }

        /// <summary>
        /// 取得最后编辑时间
        /// </summary>
        /// <param name="edittime"></param>
        /// <returns></returns>
        protected string getLastEditTime(object edittime)
        {
            string s_Time = string.Empty;
            if (edittime != DBNull.Value)
                s_Time = "<p class=\"time\">最后编辑于" + JuSNS.Common.Public.getTimeSpan(Convert.ToDateTime(edittime)) + "</p>";
            return s_Time;
        }

        /// <summary>
        /// 取得回复附加内容
        /// </summary>
        /// <param name="ReplyTack"></param>
        protected string getReplay(object ReplyTack)
        {
            string s_ReplyTack = string.Empty;
            if (ReplyTack != DBNull.Value)
            {
                s_ReplyTack = "<div class=\"tip\">" + ReplyTack.ToString() + "</div>";
            }
            return s_ReplyTack;
        }

        /// <summary>
        /// 取得是否能编辑
        /// </summary>
        /// <param name="UID">用户编号</param>
        /// <param name="ID">主题编号</param>
        /// <returns></returns>
        protected string getEdit(object UID, object ID)
        {
            string s_Edit = string.Empty;
            if (UserID == Convert.ToInt32(UID))
                s_Edit = "&nbsp;&nbsp;<a href=\"javascript:void(0);\" rev=\"EditDis\" tempid=\"" + ID.ToString() + "\" rel=\"edit\" >编辑</a></span>";
            return s_Edit;
        }

        /// <summary>
        /// 取得删除
        /// </summary>
        /// <param name="SeriesNum">回复楼数</param>
        /// <param name="ID">回复编号</param>
        /// <returns></returns>
        protected string getDel(object SeriesNum, object ID, object UID)
        {
            string s_Del = string.Empty;
            if ((Convert.ToInt32(SeriesNum) > 0) && (Convert.ToInt32(UID) == UserID))
                s_Del = "<span class=\"delete\"><a href=\"javascript:void(0);\" rev=\"ShowMsg\" deltype=\"DelReplys\" tempid=\"" + ID.ToString() + "\"  userid=\"" + UserID + "\"  rel=\"del\">删除</a></span>";
            return s_Del;
        }

        protected string getContent(object ID, object GroupID, object UID, object Content, object PostTime,
            object LastModifyTime, object ReplyTack, object SeriesNum, object State, object UserName, object FilePath, object Name)
        {
            string s_Content = "";
            if (Convert.ToInt32(State) != 3)
            {
                string s_reply = "";
                if (isre)
                    s_reply = "<a href=\"#c-post\" " +
                              "onclick=\"javascript:ReplyDis" +
                              "(" + ID.ToString() + ",'" + UserName + "'," + getSeriesNum(SeriesNum) + ");\">回复</a>";

                s_Content = "<div class=\"cpstr s100 nl\">" +
                                    "<div class=\"cpstr s99 nl\" id=\"Div_" + ID.ToString() + "\">" +
                                    "<div class=\"nl w60 h4image\">" +
                                    "<a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", UID.ToString()) + "\">" +
                                    "<img src=\"" + this.GetHeadImage(UID,1) + "\" alt=\"" + UserName + "\" style=\"height:48px;width:48px;\" /></a></div>" +
                                    "<div class=\"nr w660\">" +
                                     "<div class=\"nl s100 hdtitle\">" +
                                    "<span class=\"nl\"><span class=\"author\">" +
                                    "<a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", UID.ToString()) + "\">" + UserName.ToString() + "</a>" +
                                    "(" + Name.ToString() + ")</span> <span class=\"time\">" +
                                    "" + getTime(PostTime) + "</span> " +
                                    "<span>" + s_reply +
                                    "" + getEdit(UID, ID) + "" +
                                    "</span>" +
                                    "</span> " +
                                    "<span class=\"nr\">" + getDel(SeriesNum, ID, UID) + "" + getSeriesNum(SeriesNum) + " 楼</span>" +
                                    "<input id=\"HidID" + ID.ToString() + "\" type=\"hidden\" /></div>" +
                                    "<div class=\"spline\"></div><div class=\"content w660 nl\">" +
                                    "" + getReplay(ReplyTack) + "" +
                                    "<div id=\"Div_Content" + ID.ToString() + "\">" +
                                    "<div class=\"c333\">" + JuSNS.Common.Input.Htmls(JuSNS.Common.Input.HtmlDecode(Content.ToString())) + "</div>" +
                                    "" + getLastEditTime(LastModifyTime) + "</div></div></div>" +
                                    "</div></div><div class=\"line1\"></div>";
            }
            else
            {
                s_Content = "<div class=\"cpstr nl\">" +
                            "<div class=\"nr reshow\">" + getSeriesNum(SeriesNum) + " 楼的回复已被删除</div></div><div class=\"spline\"></div>";
            }
            return s_Content;
        }

        /// <summary>
        /// 执行回复过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            GetIsNotEmail();
            JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();
            bool tf = groupmember.IsGroupMember(UserID, Convert.ToInt32(GroupID.Value));
            if (!tf)
                PageError("您没有访问权限!不能回复主题!", rooDir+"/home/group");

            if (Page.IsValid)
            {
                if (Content.Value.Trim() == string.Empty)
                {
                    ShowErr("内容不能为空", div_Error);
                    goto Finish;
                }

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

                JuSNS.Home.Group groupdis = new JuSNS.Home.Group();
                JuSNS.Model.GroupDiscussInfo groupdisinfo = new JuSNS.Model.GroupDiscussInfo();
                JuSNS.Model.GroupDiscussInfo groupdisinfo2 = new JuSNS.Model.GroupDiscussInfo();
                groupdisinfo2 = groupdis.Info(Convert.ToInt32(DisID.Value));

                groupdisinfo.ID = 0;
                groupdisinfo.GroupID = Convert.ToInt32(GroupID.Value);
                groupdisinfo.UserID = UserID;
                groupdisinfo.Topic = string.Empty;
                groupdisinfo.Content = JuSNS.Common.Input.HtmlEncode(Content.Value);
                groupdisinfo.DiscussType = 1;
                groupdisinfo.BelongTopic = Convert.ToInt32(DisID.Value);
                groupdisinfo.PostTime = DateTime.Now;
                groupdisinfo.PostIP = JuSNS.Common.Public.GetClientIP();
                groupdisinfo.LastPostTime = DateTime.Now;
                groupdisinfo.LastPostIP = JuSNS.Common.Public.GetClientIP();
                groupdisinfo.LastPoster = 0;
                groupdisinfo.Replys = 0;
                groupdisinfo.State = 0;
                groupdisinfo.LastModifyTime = DateTime.Now;
                groupdisinfo.LastModifyIP = JuSNS.Common.Public.GetClientIP();
                groupdisinfo.ReplyID = 0;
                string s_IsReply = Request.Form["IsReply"];
                if (s_IsReply != "0")
                    groupdisinfo.ReplyTack = "回复 " + s_IsReply + " 楼";
                else
                    groupdisinfo.ReplyTack = "";
                groupdisinfo.SeriesNum = groupdisinfo2.Replys + 1;
                groupdisinfo.TopNum = 0;
                
                if (groupdis.Replay(groupdisinfo) >0)
                {
                    div_Error.Visible = false;
                    ShowSuc("回复成功！", div_Suc);
                    StartLoad();
                    Content.Value = string.Empty;
                }
                else
                {
                    ShowErr("回复失败！", div_Error);
                    Content.Value = string.Empty;
                }
            Finish: ;
            }
        }
    }
}
