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;
using System.Collections.Generic;

namespace JuSNS.Web.home.Group
{
    public partial class Group_m : JuSNS.UI.Page.UserPage
    {
        protected JuSNS.Model.UserGroupInfo usergroupinfo;
        protected string ruser = string.Empty;
        protected bool isGroupHead = true;
        JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();
        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();
            CheckPublicity();
            getInfo();
        }

        /// <summary>
        /// 检查是否允许被访问
        /// </summary>
        protected void CheckPublicity()
        {
            bool tf = false;
            string s_GroupID = Request.QueryString["GroupID"];
            if (s_GroupID == null || s_GroupID == "")
            { PageError("参数传递错误！", "/home/group"); }

            int GroupID = Convert.ToInt32(s_GroupID);
            Hid_GroupID.Value = s_GroupID;
            JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
            usergroupinfo = usergroup.Info(GroupID);

            tf = usergroup.CheckPublicity(GroupID, UserID, false);
            if (!tf)
            { PageError("您没有访问权限！", "/home/group"); }
        }



        /// <summary>
        /// 加载页面初始信息
        /// </summary>
        protected void getInfo()
        {
            int GroupID = Convert.ToInt32(Hid_GroupID.Value);

            this.H2_GroupName.InnerHtml = usergroupinfo.GroupName;
            this.ptitle.InnerText = usergroupinfo.GroupName;
            this.P_Publicity.InnerHtml = JuSNS.Home.UserGroup.getPublicityName(usergroupinfo.Publicity);
            this.Div_sect.InnerHtml = JuSNS.Common.Input.HtmlEncode(usergroupinfo.Bulletin);



            string s_Op = "";
            bool supertf = false;
            bool admintf = false;
            if (usergroupinfo.UserID == UserID)
                supertf = true;
            if (groupmember.IsGroupAdmin(UserID, GroupID))
                admintf = true;

            if (supertf || admintf)
            {
                s_Op = "  <li class=\"reshow bold\"><a href=\"group.aspx?GroupID=" + GroupID + "#posttopic\">发表话题</a></li>";
                s_Op += "<li><a href=\"Group-m.aspx?GroupID=" + GroupID + "\">讨论区</a></li>";
                s_Op += "<li><a href=\"album.aspx?GroupID=" + GroupID + "\">群组相册</a></li>";
                s_Op += "<li><a href=\"Creat_base.aspx?GroupID=" + GroupID + "&m=1\">设置群组</a></li>";
                s_Op += "<li><a href=\"Creat_member.aspx?GroupID=" + GroupID + "&a=1\">管理成员</a></li>";
                s_Op += "<li><a href=\"member.aspx?GroupID=" + GroupID + "\" title=\"" + usergroupinfo.Members.ToString() + "个成员\">群组成员(" + usergroupinfo.Members.ToString() + ")</a></li>";
                s_Op += "<li><a href=\"Creat_member.aspx?GroupID=" + GroupID + "&a=1\">邀请好友加入</a></li>";
                if (usergroupinfo.Members == 1 || admintf)
                {
                    s_Op += "<li><a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定要退出此群吗？</li></ul>','退出',function (){self.location='?Action=Exit&GroupID=" + GroupID + "';})\">退出此群</a></li>";
                }
            }
            else
            {
                s_Op = "<li><a href=\"Group-m.aspx?GroupID=" + GroupID + "\">讨论区</a></li>";
                s_Op += "<li><a href=\"album.aspx?GroupID=" + GroupID + "\">群组相册</a></li>";
                s_Op += "<li><a href=\"member.aspx?GroupID=" + GroupID + "\" title=\"" + usergroupinfo.Members.ToString() + "个成员\">群组成员(" + usergroupinfo.Members.ToString() + ")</a></li>";
                //判断是否是本群成员，如果不是则显示加入此群
                if (groupmember.IsGroupMember(UserID, GroupID) == false)
                {
                    this.isPost.InnerHtml = "<span class=\"reshow\">对不起，加入此群组才能发表帖子</span> &nbsp; <a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定加入此群吗？</li></ul>','加入',function (){self.location='?Action=Join&GroupID=" + GroupID + "';})\">加入此群</a>";
                    s_Op += "<li><a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定加入此群吗？</li></ul>','加入',function (){self.location='?Action=Join&GroupID=" + GroupID + "';})\">加入此群</a></li>";
                }
                else
                    s_Op += "<li><a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定要退出此群吗？</li></ul>','退出',function (){self.location='?Action=Exit&GroupID=" + GroupID + "';})\">退出此群</a></li>";
            }
            s_Op += "<li><a href=\"group.aspx?GroupID=" + GroupID + "&a=1\">返回群组</a></li>";
            Ul_GroupAdminOp.InnerHtml = s_Op;

            DivPage.Visible = false;
            int r, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            SqlConditionInfo st = new SqlConditionInfo("@GroupID", GroupID, TypeCode.Int32);

            DataTable dt_Discuss = JuSNS.Home.UtilPage.GetPage("GroupDiscuss_aspx", PageIndex, 25, out r, out p, st);
            if (dt_Discuss != null)
            {
                Rpt_DiscussList.DataSource = dt_Discuss;
                Rpt_DiscussList.DataBind();
                dt_Discuss.Clear();
                dt_Discuss.Dispose();
            }
            //判断是否调用分页
            if (r > 25)
            {
                DivPage.Visible = true;
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "Group-m.aspx");
            }
            span_DiscussCount.InnerHtml = r.ToString();
        }



       

        /// <summary>
        /// 判断是否群主
        /// </summary>
        /// <param name="Type">类型</param>
        /// <returns></returns>
        protected string getGradeName(object Type)
        {
            string s_Name = string.Empty;
            if (Type.ToString() == "0")
                s_Name = "<span class=\"reshow\">群主</span>";
            return s_Name;
        }

        /// <summary>
        /// 取得会员姓名
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <returns></returns>
        protected string getUserName(object UserID)
        {
            string s_Name = "&nbsp;";
            if (UserID != DBNull.Value)
            {
                JuSNS.Home.User user = new JuSNS.Home.User();
                UserInfo userinfo = new UserInfo();
                userinfo = user.GetUserInfo(Convert.ToInt32(UserID));
                s_Name = "<a href=\"/user/" + JuSNS.Common.Public.URLWrite("", "r", UserID.ToString()) + "\">" + userinfo.Username + "</a>";
            }
            return s_Name;
        }

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

        /// <summary>
        /// 显示是否置顶
        /// </summary>
        /// <param name="topnum"></param>
        /// <returns></returns>
        protected string getIsTop(object topnum)
        {
            string s_Top = "";
            if (topnum.ToString() == "1")
                s_Top = "<span class=\"reshow\">[置顶]</span>";
            return s_Top;
        }

        /// <summary>
        /// 执行添加过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            GetIsNotEmail();
            bool tf = groupmember.IsGroupMember(UserID, Convert.ToInt32(Hid_GroupID.Value));
            if (!tf)
                PageError("您没有访问权限!不能发表主题!", "index.aspx");

            if (Page.IsValid)
            {
                if (Topic.Value.Trim() == "" || Content.Value.Trim() == "")
                {
                    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();
                groupdisinfo.ID = 0;
                groupdisinfo.GroupID = Convert.ToInt32(Hid_GroupID.Value);
                groupdisinfo.UserID = UserID;
                groupdisinfo.Topic = Topic.Value;
                groupdisinfo.Content = JuSNS.Common.Input.HtmlEncode(Content.Value);
                groupdisinfo.DiscussType = 0;
                groupdisinfo.BelongTopic = 0;
                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;
                groupdisinfo.ReplyTack = "";
                groupdisinfo.SeriesNum = 0;
                groupdisinfo.TopNum = 0;

                if (groupdis.Add(groupdisinfo) == 1)
                {
                    Response.Cookies["AddGroupDisIsSuc"].Value = "true";
                    Response.Redirect("Group-m.aspx?GroupID=" + Hid_GroupID.Value);
                }
                else
                {
                    Topic.Value = "";
                    Content.Value = "";
                    ShowErr("发表失败!", div_Error);
                }
            Finish: ;
            }
        }
    }
}
