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.Common;
using JuSNS.Home;
using JuSNS.Model;
namespace JuSNS.Web.user.Album
{
    public partial class AlbumAdd : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetIsNotEmail();
            if (Page.IsPostBack)
            {
                string a = Input.Filter(Request.QueryString["a"]);
                string r = Input.Filter(Request.QueryString["r"]);
                Response.Redirect("photoAdd.aspx?a="+a+"&r="+r);
                Response.End();
            }
            if (Request.Form.Count>0)
            {
                SaveAlbum();
            }
            else
            {
                setAlbumList();
            }
        }
        /// <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>添加相册失败</li>";
            }
            if (Convert.ToString(Session["ErrMSG"]) != "")
            {
                Session["ErrMSG"] = "<ul>" + Convert.ToString(Session["ErrMSG"]) + "</ul>";
            }
            Response.Redirect("AlbumAdd.aspx");
        }

        /// <summary>
        /// 设置相册下拉列表
        /// </summary>
        protected void setAlbumList()
        {
            JuSNS.Home.Album AB = new JuSNS.Home.Album();
            List<AlbumInfo> InfoList = AB.AlbumList(this.UserID);
            this.r.Items.Clear();
            for(int i=0;i<InfoList.Count;i++)
            {
                ListItem li = new ListItem();
                li.Text=InfoList[i].Title;
                li.Value=Convert.ToString(InfoList[i].AlbumID);
                this.r.Items.Add(li);
            }
        }
    }
}
