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

namespace JuSNS.Web.home.Group
{
    public partial class Creat_Member : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            Response.CacheControl = "no-cache";
            Check();
            LoadInfo();
        }

        /// <summary>
        /// 检测有无权限设置
        /// </summary>
        protected void Check()
        {
            string GroupID = Request.QueryString["GroupID"];
            if (GroupID != null && GroupID != "")
            {
                JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
                JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();
                bool admintf = false;
                bool supertf = false;
                if (usergroup.IsMyGroup(Convert.ToInt32(GroupID), UserID))
                    supertf = true;
                if (groupmember.IsGroupAdmin(UserID, Convert.ToInt32(GroupID)))
                    admintf = true;

                if (!(admintf || supertf))
                {
                    PageError("参数错误!", "Creat_base.aspx");
                }
            }
        }


        /// <summary>
        /// 加载页面初始信息
        /// </summary>
        protected void LoadInfo()
        {
            div_Error.Style.Value = "display:none;";
            div_Suc.Style.Value = "display:none;";
            string GroupID = Request.QueryString["GroupID"];
            string isAdd = Request.QueryString["a"];
            if (GroupID != null && GroupID != "")
            {
                JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
                UserGroupInfo usergroupinfo = new UserGroupInfo();
                usergroupinfo = usergroup.Info(Convert.ToInt32(GroupID));

                H2_GroupName.InnerHtml = usergroupinfo.GroupName;
                Hid_GroupID.Value = GroupID;
                JuSNS.Home.Friend friend = new JuSNS.Home.Friend();
                Dictionary<int, string> dct = friend.GetFriendsForGroup(UserID, Convert.ToInt32(GroupID), "");
                string s_List = "";
                foreach (KeyValuePair<int, string> kvp in dct)
                {
                    s_List += "<div id=\"l" + kvp.Key.ToString() + "\" style=\"height:20px;\"><input type=\"checkbox\" value=\"" + kvp.Key.ToString() + "\" onclick=\"GroupAddMber('" + kvp.Key.ToString() + "','" + kvp.Value + "');\" /> " + kvp.Value + "</div>";
                }
                FriendList.InnerHtml = s_List;
            }
        }

        /// <summary>
        /// 执行发送邀请过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_ServerClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string s_JoinUserID = Request.Form["Hid_FriendID"];
                if (s_JoinUserID != null && s_JoinUserID != "")
                {
                    s_JoinUserID = s_JoinUserID.TrimEnd('|');
                    string[] arruserid = s_JoinUserID.Split('|');
                    for (int i = 0; i < arruserid.Length; i++)
                    {
                        JuSNS.Home.GroupInvite invite = new JuSNS.Home.GroupInvite();
                        GroupInviteInfo inviteinfo = new GroupInviteInfo();
                        inviteinfo.ID = 0;
                        inviteinfo.GroupID = Convert.ToInt32(Hid_GroupID.Value);
                        inviteinfo.JoinUserID = Convert.ToInt32(arruserid[i].ToString());
                        inviteinfo.UserID = UserID;
                        inviteinfo.Postscript = msg.Value;
                        inviteinfo.PostTime = DateTime.Now;
                        inviteinfo.PostIP = JuSNS.Common.Public.GetClientIP();
                        inviteinfo.Response = 0;
                        inviteinfo.RespTime = DateTime.Now;
                        inviteinfo.RespIP = "";
                        inviteinfo.InviteFlag = true;
                        invite.Add(inviteinfo);
                    }
                    LoadInfo();
                    msg.Value = "";
                    ShowSuc("邀请朋友成功!", div_Suc);
                    Hid_FriendID.Value = "";
                }
                else
                {
                    ShowErr("请选择要邀请加入的好友！", div_Error);
                }
            }
        }
    }
}
