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 my : JuSNS.UI.Page.UserPage
    {
        public new string UserName = "我";
        public string JoinStr = "我";
        public string CreatStr = "我";
        public string a = string.Empty;
        public string r = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            a = Request.QueryString["a"];
            r = Request.QueryString["r"];

            if (!this.PrivacyAvailable(EnumPrivacyType.Group, GetUserID(), Convert.ToInt32(r)))
            {
                PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
            }

            if ((string.IsNullOrEmpty(a)) || string.IsNullOrEmpty(r))
            {
                PageError("缺少必要的参数", "/home/group");
            }
            if (!JuSNS.Common.Input.IsInteger(r))
            {
                PageError("错误的参数", "javascript:history.back();");
            }
            if (a == "join")
            {
                this.myJoin.Attributes.Add("class", "current");
                this.myCreat.Attributes.Add("class", "");
                if (r == UserID.ToString())
                {
                    JoinStr = "我加入的";
                    CreatStr = "我创建的";
                }
                else
                {
                    JuSNS.Home.User us = new JuSNS.Home.User();
                    UserInfo ui = new UserInfo();
                    ui = us.GetUserInfo(Convert.ToInt32(r));
                    JoinStr = ui.Username + "加入的";
                    CreatStr = ui.Username + "创建的";
                }
            }
            else if (a == "creat")
            {
                this.myJoin.Attributes.Add("class", "");
                this.myCreat.Attributes.Add("class", "current");
                if (r == UserID.ToString())
                {
                    JoinStr = "我加入的";
                    CreatStr = "我创建的";
                }
                else
                {
                    JuSNS.Home.User usc = new JuSNS.Home.User();
                    UserInfo uic = new UserInfo();
                    uic = usc.GetUserInfo(Convert.ToInt32(r));
                    JoinStr = uic.Username + "加入的";
                    CreatStr = uic.Username + "创建的";
                }
            }
            DataBind();
            LoadList();
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            DivPage.Visible = false;
            int or, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            int TmpUser = Convert.ToInt32(r);
            DataTable dt = null;
            SqlConditionInfo st = new SqlConditionInfo("@UserID", TmpUser, TypeCode.Int32);
            if (a == "join")
            {
                dt = JuSNS.Home.UtilPage.GetPage("Group_My_aspx", PageIndex, PAGESZ, out or, out p, st);
            }
            else
            {
                dt = JuSNS.Home.UtilPage.GetPage("Group_Creat_aspx", PageIndex, PAGESZ, out or, out p, st);
            }
            if (dt != null)
            {
                Rpt_MyGroup.DataSource = dt;
                Rpt_MyGroup.DataBind();
                dt.Clear();
                dt.Dispose();
            }
            //判断是否调用分页
            if (or > PAGESZ)
            {
                DivPage.Visible = true;
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "my.aspx");
            }
            JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
            span_MyGroupCount.InnerHtml = usergroup.Count(TmpUser, a).ToString();
        }

        /// <summary>
        /// 取得是否有权限操作群组
        /// </summary>
        /// <param name="GroupID">群编号</param>
        /// <returns></returns>
        protected string getOp(object GroupID)
        {
            string s_Op = string.Empty;
            JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
            JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();
            bool isAdmin = false;
            bool isSuper = false;
            if (usergroup.IsMyGroup(Convert.ToInt32(GroupID), UserID))
            {
                isSuper = true;
            }
            if (groupmember.IsGroupAdmin(UserID, Convert.ToInt32(GroupID)))
            {
                isAdmin = true;
            }
            if (isSuper || isAdmin)
            {
                string sW = Request.QueryString["a"];
                if (sW == "creat")
                {
                    sW = "确定退出吗？您是此群群主。退出后将解散群！";
                }
                else
                {
                    sW = "确定要退出此群吗？";
                }
                s_Op = "<li><a href=\"Creat_base.aspx?GroupID=" + GroupID.ToString() + "&m=1\"><span>设置群</span></a></li><li><a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>" + sW + "</li></ul>','退出',function (){self.location='out.aspx?GroupID=" + GroupID.ToString() + "';})\"><span>退出群</span></a></li>";
            }
            return s_Op;
        }

        /// <summary>
        /// 取得公告
        /// </summary>
        /// <param name="Bulletin">公告</param>
        /// <returns></returns>
        protected string getBulletin(object Bulletin)
        {
            string s_Bulletin = "";
            if (Bulletin != null && Bulletin.ToString() != "")
                s_Bulletin = "<span class=\"reshow\">公告：</span>" + JuSNS.Common.Input.GetSubString(Bulletin.ToString(), 400);
            return s_Bulletin;
        }                    
    }
}
