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.Home;
using JuSNS.Model;
using System.Collections.Generic;
using JuSNS.Common;

namespace JuSNS.Web.home.Album
{
    public partial class GroupAlbumNew : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                SaveAlbum();
            }
            setGroup();
        }


        protected void setGroup()
        {
            int GroupID = 0;
            try
            {
                GroupID = int.Parse(Request.QueryString["groupid"]);
            }
            catch { }
            if (GroupID == 0)
            {
                PageError("参数传递错误!", "javascript:history();");
            }
            UserGroup ug = new UserGroup();
            if (!ug.IsMyGroup(GroupID, this.UserID))
            {
                //this.addtip.InnerHtml = "<div  class=\"msg-error\">您不是群组成员，不能发布群相册。<a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定加入此群吗？</li></ul>','加入',function (){self.location='../group/group.aspx?Action=Join&GroupID=" + GroupID + "';})\">加入此群</a></div>";
                this.addtip.InnerHtml = "<div  class=\"msg-error\">您不是群组创建者，不能发布群相册。</div>";
                this.addform.Visible = false;
                return;
            }
            this.addform.Visible = true;
            this.addtip.Visible = false;
            List<UserGroupInfo> grouplist = ug.GetUserGroup(this.UserID);
            for (int i = 0; i < grouplist.Count; i++)
            {
                ListItem li=new ListItem(grouplist[i].GroupName,grouplist[i].GroupID.ToString());
                if (grouplist[i].GroupID == GroupID)
                    li.Selected = true;
                this.GroupID.Items.Add(li);
            }
        }
        /// <summary>
        /// 保存相册
        /// </summary>
        protected void SaveAlbum()
        {
            string title = Input.Filter(Request.Form["title"]);
            string desc = Input.Filter(Request.Form["desc"]);
            string attr = Input.Filter(Request.Form["attr"]);
            string groupid = Input.Filter(Request.Form["groupid"]);
            if (title == "")
            {
                Session["ErrMSG"] = "<li>相册的名称不能为空";
            }

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

            int GroupID = 0;
            try
            {
                GroupID = int.Parse(groupid);
            }
            catch { }
            JuSNS.Home.Album ab = new JuSNS.Home.Album();
            AlbumInfo abi = new AlbumInfo();
            abi.Title = title;
            abi.UserID = this.UserID;
            abi.Description = desc;
            abi.ImagesCount = 0;
            abi.CreateTime = DateTime.Now;
            abi.GroupID = GroupID;
            switch (attr)
            {
                case "0":
                    abi.Privacy = EnumPrivacy.ForWholeSite;
                    break;
                case "1":
                    abi.Privacy = EnumPrivacy.ForNetWorkAndFriends;
                    break;
                case "2":
                    abi.Privacy = EnumPrivacy.ForFriends;
                    break;
                case "3":
                    abi.Privacy = EnumPrivacy.ForOwner;
                    break;
                default:
                    abi.Privacy = EnumPrivacy.ForWholeSite;
                    break;
            }
            if (ab.Add(abi) >0)
            {
                Session.Remove("ErrMSG");
                Response.Redirect("PhotoAdd.aspx?r=" + ab.LastAlbumID(this.UserID));
            }
            else
            {
                Session["ErrMSG"] = "<li>添加相册失败";
            }
            if (Convert.ToString(Session["ErrMSG"]) != "")
            {
                Session["ErrMSG"] = "<ul>" + Convert.ToString(Session["ErrMSG"]) + "</ul>";
            }
            Response.Redirect("GroupAlbumNew.aspx?groupid=" + groupid);
        }
    }
}
