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 AlbumEdit : JuSNS.UI.Page.UserPage
    {
        protected JuSNS.Model.AlbumInfo ABI = new AlbumInfo();
        protected string R=string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            R=JuSNS.Common.Input.Filter(Request.QueryString["r"]);
            string S = Request.QueryString["s"];
            if (S == "1")
            {
                this.MSG.Visible = true;
                this.MSG.InnerHtml = "<div class=\"msg-succ\">修改相册成功！</div>";
            }
            else
            {
                this.MSG.Visible = false;
            }
            if (string.IsNullOrEmpty(R))
            {
                this.PageError("缺少必要的参数", Request.RawUrl);
            }
            setCurrentAlbumInfo();
            if (this.UserID != ABI.UserID && !this.IsAdmin())
            {
                this.PageError("不是你的相册，你没有权限修改", Request.RawUrl);
            }
            if (Page.IsPostBack)
            {
                if (saveEdit() == 1)
                {
                    Response.Redirect("AlbumEdit.aspx?s=1&r=" + R);
                }
            }
            else
            {
                setSelectList();
            }
        }

        /// <summary>
        /// 设置当前相册信息
        /// </summary>
        protected void setCurrentAlbumInfo()
        {
            JuSNS.Home.Album ab = new JuSNS.Home.Album();
            ABI=ab.Info(Convert.ToInt32(R));
        }

        /// <summary>
        /// 保存相册信息
        /// </summary>
        /// <returns></returns>
        protected int saveEdit()
        {
            string Desc = Request.Form["desc"];
            string Title = Request.Form["title"];
            if (string.IsNullOrEmpty(Title))
            {
                this.MSG.Visible = true;
                this.MSG.InnerHtml = "相册的名称不能为空";
            }
            JuSNS.Model.AlbumInfo abi = ABI;
            JuSNS.Home.Album ab = new JuSNS.Home.Album();
            abi.Description = Input.Filter(Desc);
            abi.Title = Input.Filter(Title);

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


            switch (Request.Form["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;
            }
            return ab.Edit(abi);
        }
        /// <summary>
        /// 设置隐私选项
        /// </summary>
        protected void setSelectList()
        {
            for (int i = 0; i < 4; i++)
            {
                ListItem li;
                switch(i)
                {
                    case 0:
                        li = new ListItem("全站用户可见", "0");
                        this.attr.Items.Add(li);
                        break;
                    case 1:
                        li = new ListItem("相同网络用户及好友可见", "1");
                        this.attr.Items.Add(li);
                        break;
                    case 2:
                        li = new ListItem("仅好友可见", "2");
                        this.attr.Items.Add(li);
                        break;
                    case 3:
                        li = new ListItem("仅自己可见", "3");
                        this.attr.Items.Add(li);
                        break;
                }
            }
            this.attr.SelectedIndex = Convert.ToInt32(ABI.Privacy);
        }
    }
}
