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 list : JuSNS.UI.Page.BasePage
    {
        public string title = "好友的群组";
        public string r = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            r = Request.QueryString["r"];
            if (r == "all")
            {
                title = "随便看看";
                this.alllist.Attributes.Add("class", "current");
                this.flist.Attributes.Add("class", "");
            }
            else
            {
                this.alllist.Attributes.Add("class", "");
                this.flist.Attributes.Add("class", "current");
            }
            DataBind();
            LoadList();
        }


        protected string getGroupPic(object path)
        {
            string PicStr = string.Empty;
            if (path != null)
            {
                if (path.ToString() == "../../template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/000.jpg")
                {
                    PicStr = "../../template/" + JuSNS.Config.UiConfig.SkinStyle + path.ToString();
                }
                else
                {
                    PicStr = path.ToString();
                }
            }
            return PicStr;
        }


        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            DivPage.Visible = false;
            DivResult.Visible = false;
            bool tf = false;
            int or, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            string GroupName = Request.QueryString["GroupName"];
            this.myKey.Value = GroupName;

            DataTable dt = null;
            SqlConditionInfo st=null;
            if (!string.IsNullOrEmpty(GroupName))
            {
                SqlConditionInfo[] sts = new SqlConditionInfo[2];
                sts[0] = new SqlConditionInfo("@GroupName", GroupName, TypeCode.String);
                sts[1] = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                sts[0].Blur = 3;
                if (r == "all")
                {
                    dt = JuSNS.Home.UtilPage.GetPage("GroupList_Key_aspx", PageIndex, 20, out or, out p, sts);
                }
                else
                {
                    dt = JuSNS.Home.UtilPage.GetPage("GroupList_Key_Friend_aspx", PageIndex, 20, out or, out p, sts);
                }
            }
            else
            {
                st = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                if (r == "all")
                {
                    dt = JuSNS.Home.UtilPage.GetPage("GroupList_aspx", PageIndex, PAGESZ, out or, out p, st);
                }
                else
                {
                    if (this.GetUserID() == 0)
                    {
                        PageError("请登录后查看！", rooDir + "/?urls=" + Request.Url, true);
                    }
                    dt = JuSNS.Home.UtilPage.GetPage("GroupList_Friend_aspx", PageIndex, PAGESZ, out or, out p, st);
                }
            }
            if (dt != null)
            {
                Rpt_GroupList.DataSource = dt;
                Rpt_GroupList.DataBind();
                dt.Clear();
                dt.Dispose();
            }
            if (GroupName != null && GroupName.Trim() != "")
            {
                tf = true;
                P_Result.InnerHtml = or + " 个含有<b class=\"reshow\"> " + GroupName + " </b>的群组 </a>";

            }
            else
            {
                P_Result.InnerHtml = or + "个群组";
            }
            if (or == 0 && tf)
            {
                DivResult.Visible = true;
                DivResult.InnerHtml = "没有找到含有“ <strong class=\"reshow\">" + GroupName + "</strong> ”的群组";
            }
            //判断是否调用分页
            if (or > PAGESZ)
            {
                DivPage.Visible = true;
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "list.aspx?GroupName=" + this.myKey.Value);
            }
        }

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

        /// <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), GetUserID()))
            {
                isSuper = true;
            }
            if (groupmember.IsGroupAdmin(GetUserID(), Convert.ToInt32(GroupID)))
            {
                isAdmin = true;
            }
            if (isSuper || isAdmin)
            {
                s_Op = "<li><a href=\"Creat_base.aspx?GroupID=" + GroupID.ToString() + "&m=1\"><span  class=\"paddings5\">设置群组</span></a></li><li><a href=\"out.aspx?r=" + GroupID.ToString() + "&userid=" + GetUserID().ToString() + "\"><span  class=\"paddings5\">退出此群</span></a></li>";
            }
            return s_Op;
        }
    }
}
