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;

namespace JuSNS.Web.Inc.Ajax
{
    public partial class GroupMemberList : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            getFriendList();
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        protected void getFriendList()
        {
            string GroupID = Request.QueryString["GroupID"];
            string Grade = Request.QueryString["Grade"];

            if (GroupID == null || Grade == null || GroupID == "" || Grade == "")
            {
                Response.Write("<li>参数传递错误!</li>");
                Response.End();
            }

            string s_List = "";
            string s_Gname = "";
            JuSNS.Home.GroupMember gmember = new JuSNS.Home.GroupMember();
            DataTable dt = gmember.GroupMemberGradeList(Convert.ToInt32(Grade), Convert.ToInt32(GroupID));

            JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
            bool admintf = false;
            bool supertf = false;
            if (usergroup.IsMyGroup(Convert.ToInt32(GroupID), UserID))
                supertf = true;
            if (gmember.IsGroupAdmin(UserID, Convert.ToInt32(GroupID)))
                admintf = true;

            if (dt != null && dt.Rows.Count > 0)
            {
                string curPage = Request.QueryString["page"];    //当前页码
                int pageSize = 10, page = 0;                     //每页显示数

                if (curPage == "" || curPage == null || curPage == string.Empty) { page = 1; }
                else
                {
                    try { page = int.Parse(curPage); }
                    catch (Exception e)
                    {
                        page = 0;
                    }
                }
                int i, j;
                int Cnt = dt.Rows.Count;

                int pageCount = Cnt / pageSize;
                if (Cnt % pageSize != 0) { pageCount++; }
                if (page > pageCount) { page = pageCount; }
                if (page < 1) { page = 1; }

                for (i = (page - 1) * pageSize, j = 1; i < Cnt && j <= pageSize; i++, j++)
                {
                    if (Grade != "1")
                    {
                        if (supertf)
                        {
                            s_Gname = "<a href=\"javascript:void(0);\" " +
                                      "onclick=\"javascript:Confirm(this,'" +
                                      "<ul><li>确定从这个群组里删除 " + dt.Rows[i]["UserName"].ToString() + " 吗？</li></ul>'," +
                                      "'确定'," +
                                      "function()" +
                                      "{GroupStartOp('DelGroupMem'," + GroupID + "," + dt.Rows[i]["UserID"].ToString() + ")})\">删除</a>" +
                                      " | " +
                                      "<a href=\"javascript:void(0);\" " +
                                      "onclick=\"javascript:Confirm(this," +
                                      "'<ul><li>将 " + dt.Rows[i]["UserName"].ToString() + " 升级为管理员吗？</li></ul>'," +
                                      "'确定'," +
                                      "function(){GroupStartOp('SetGroupAdmin'," + GroupID + "," + dt.Rows[i]["UserID"].ToString() + ")})\">" +
                                      "设为管理员</a>";
                            if (dt.Rows[i]["Grade"].ToString() == "0")
                                s_Gname = "群主";
                            if (dt.Rows[i]["Grade"].ToString() == "1")
                                s_Gname = "<a href=\"javascript:void(0)\" " +
                                          "onclick=\"Confirm(this," +
                                          "'<ul><li>确定从这个群组里删除 " + dt.Rows[i]["UserName"].ToString() + " 吗？</li></ul>'," +
                                          "'确定'," +
                                          "function()" +
                                          "{GroupStartOp('DelGroupMem'," + GroupID + "," + dt.Rows[i]["UserID"].ToString() + ")})\">删除</a>" +
                                          " | " +
                                          "<a href=\"javascript:void(0);\" " +
                                          "onclick=\"javascript:Confirm(this," +
                                          "'<ul><li>取消 " + dt.Rows[i]["UserName"].ToString() + " 的管理员身份吗？</li></ul>'," +
                                          "'确定'," +
                                          "function()" +
                                          "{GroupStartOp('DelGroupAdmin'," + GroupID + "," + dt.Rows[i]["UserID"].ToString() + ")})\">" +
                                          "删除管理员</a>";
                        }
                        if (admintf)
                        {
                            s_Gname = "<a href=\"javascript:void(0);\" " +
                                      "onclick=\"javascript:Confirm(this,'" +
                                      "<ul><li>确定从这个群组里删除 " + dt.Rows[i]["UserName"].ToString() + " 吗？</li></ul>'," +
                                      "'确定'," +
                                      "function()" +
                                      "{GroupStartOp('DelGroupMem'," + GroupID + "," + dt.Rows[i]["UserID"].ToString() + ")})\">删除</a>";
                            if (dt.Rows[i]["Grade"].ToString() == "0")
                                s_Gname = "群主";
                            if (dt.Rows[i]["Grade"].ToString() == "1")
                                s_Gname = "管理员";
                            if (Convert.ToInt32(dt.Rows[i]["UserID"].ToString()) == UserID)
                                s_Gname = "<a href=\"javascript:void(0);\" " +
                                          "onclick=\"javascript:Confirm(this,'" +
                                          "<ul><li>确定辞职吗？</li></ul>'," +
                                          "'确定'," +
                                          "function()" +
                                          "{GroupStartOp('QuitGroup'," + GroupID + "," + dt.Rows[i]["UserID"].ToString() + ")})\">辞职</a>";
                        }
                        s_List += "<li><div style=\"float:left; width:260px; clear:left;\">" +
                                  "<a href=\""+rooDir+"/user/?r=" + dt.Rows[i]["UserID"].ToString() + "\">" +
                                  "" + dt.Rows[i]["UserName"].ToString() + " </a>" +
                                  "(" + dt.Rows[i]["Name"].ToString() + ")</div>" +
                                  "<div style=\"float:left;\">" + s_Gname + "</div></li>";
                    }
                    else
                    {
                        s_List += "<li><div style=\"float:left; width:260px;clear:left;\">" +
                            "<a href=\""+rooDir+"/user/?r=" + dt.Rows[i]["JoinUserID"].ToString() + "\">" +
                            "" + dt.Rows[i]["UserName"].ToString() + " </a>" +
                            "(" + dt.Rows[i]["Name"].ToString() + ")</div>" +
                            "<div style=\"float:left;\"></div></li>";
                    }
                }

                if (dt.Rows.Count > 10)
                {
                    string urlstr = "共" + Cnt.ToString() + "个成员,共" + pageCount.ToString() + "页,当前第" + page.ToString() + "页   ";
                    urlstr += "<a href=\"javascript:getGroupMemPage(1," + Grade + "," + GroupID + ");\">首页</a> ";
                    if ((page - 1) < 1)
                        urlstr += " <a href=\"javascript:getGroupMemPage(1," + Grade + "," + GroupID + ");\">上一页</a> ";
                    else
                        urlstr += " <a href=\"javascript:getGroupMemPage(" + (page - 1) + "," + Grade + "," + GroupID + ");\">上一页</a> ";
                    if ((page + 1) < pageCount)
                        urlstr += " <a href=\"javascript:getGroupMemPage(" + (page + 1) + "," + Grade + "," + GroupID + ");\">下一页</a> ";
                    else
                        urlstr += " <a href=\"javascript:getGroupMemPage(" + pageCount + "," + Grade + "," + GroupID + ");\" >下一页</a> ";
                    urlstr += " <a href=\"javascript:getGroupMemPage(" + pageCount + "," + Grade + "," + GroupID + ");\">尾页</a> ";
                    s_List += "<li><div>" + urlstr + "</div></li>";
                }
                dt.Clear(); dt.Dispose();
            }
            Response.Write(s_List);
            Response.End();
        }
    }
}
