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;

namespace JuSNS.Web.home.Group
{
    public partial class Creat_base : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetIsNotEmail();
            DataBind();
            Response.CacheControl = "no-cache";
            string isHeadPic = this.GetHeadPath(this.UserID);
            if (string.IsNullOrEmpty(isHeadPic))
            {
                string errorStr = "您需要上传了头像后，才能 <b>创建群组</b>！";
                if (JuSNS.Config.UiConfig.isUpHeadPic == "1" && !JuSNS.Common.Public.GetHeadLimite(2))
                {
                    Response.Redirect(rooDir + "/home/space/userheadphoto.aspx?h=1&Err=" + JuSNS.Common.Input.URLEncode(errorStr) + "");
                }
            }

            Check();
            if (!Page.IsPostBack)
            {
                getInfo();
            }
        }

        /// <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)
                {
                    if (admintf)
                    {
                        Txt_GroupName.Disabled = true;
                        Txt_Bulletin.Disabled = true;
                        Sel_NetWork.Disabled = true;
                        Rad_Publicity.Enabled = true;
                        Submit1.Disabled = true;
                    }
                }
                else
                {
                    PageError("你不是管理员，不能设置群组信息!", "Creat_base.aspx");
                }
            }
        }

        /// <summary>
        /// 加载页面初始信息
        /// </summary>
        protected void getInfo()
        {
            JuSNS.Home.User user = new JuSNS.Home.User();
            JuSNS.Model.UserInfo userinfo = new JuSNS.Model.UserInfo();
            JuSNS.Home.Dict dict = new JuSNS.Home.Dict();
            JuSNS.Model.DictAreaInfo dictinfo = new JuSNS.Model.DictAreaInfo(0, 0, "");
            userinfo = user.GetUserInfo(UserID);
            dictinfo = dict.Info(userinfo.Province);
            ListItem itm = new ListItem();
            itm.Text = dictinfo.Name;
            itm.Value = userinfo.Province.ToString();
            Sel_NetWork.Items.Add(itm);

            string GroupID = Request.QueryString["GroupID"];
            if (GroupID != null && GroupID != "")
            {
                ptitle.InnerText = "修改群组";
                Submit1.Value = "保存配置";
                JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
                JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();

                UserGroupInfo usergroupinfo = new UserGroupInfo();
                usergroupinfo = usergroup.Info(Convert.ToInt32(GroupID));
                Txt_GroupName.Value = usergroupinfo.GroupName;
                Txt_Bulletin.Value = usergroupinfo.Bulletin;
                Sel_NetWork.Value = usergroupinfo.NetWork.ToString();
                Rad_Publicity.Text = Convert.ToInt32(usergroupinfo.Publicity).ToString();
                Hid_GroupID.Value = GroupID;

            }
        }

        /// <summary>
        /// 执行添加群组过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Submit1_ServerClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool tf = true;
                bool isedit = false;
                if (Txt_GroupName.Value.Trim() == "")
                {
                    tf = false;
                    ShowErr("群组名称不能为空", div_Error);
                    goto Finish;
                }

                #region 过滤关键字
                string res = JuSNS.Common.Public.CheckKey(this.Txt_Bulletin.Value.Trim(), true);
                if (res != "")
                {
                    this.Txt_Bulletin.Value = this.Txt_Bulletin.Value.Replace(res, "***");
                }
                #endregion

                if (tf)
                {
                    JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
                    JuSNS.Model.UserGroupInfo usergroupinfo = new JuSNS.Model.UserGroupInfo();

                    if (Hid_GroupID.Value != null && Hid_GroupID.Value != "")
                        isedit = true;

                    if (isedit)
                        usergroupinfo.GroupID = Convert.ToInt32(Hid_GroupID.Value);
                    else
                        usergroupinfo.GroupID = 0;

                    usergroupinfo.GroupName = Txt_GroupName.Value;
                    usergroupinfo.UserID = UserID;
                    usergroupinfo.Members = 1;
                    usergroupinfo.Bulletin = Txt_Bulletin.Value;
                    usergroupinfo.NetWork = Convert.ToInt32(Sel_NetWork.Value);
                    usergroupinfo.Publicity = (EnumGroupPrivacy)Convert.ToInt32(Rad_Publicity.Text);
                    usergroupinfo.Portrait = "";
                    usergroupinfo.State = 0;
                    usergroupinfo.PostTime = DateTime.Now;
                    usergroupinfo.PostIP = JuSNS.Common.Public.GetClientIP();

                    if (isedit == false)
                    {
                        int GroupID = usergroup.Add(usergroupinfo);
                        if (GroupID > 0)
                            Response.Cookies["AddGroupIsScu"].Value = "true";
                        Response.Redirect("Creat_pic.aspx?a=0&GroupID=" + GroupID);
                    }
                    else
                    {
                        if (usergroup.EditInfo(usergroupinfo) == 1)
                            Response.Cookies["EditGroupIsScu"].Value = "true";
                        Response.Redirect("Creat_pic.aspx?a=1&GroupID=" + Hid_GroupID.Value);
                    }
                }
            Finish: ;
            }
        }
    }
}
