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

namespace JuSNS.Web.user.Album
{
    public partial class sPhotoEdit : JuSNS.UI.Page.UserPage
    {
        protected string R = string.Empty;
        protected string errmsg = string.Empty;
        public string FormContent = string.Empty;
        protected int PhotoCount = 0;
        protected PhotoInfo Phi = new PhotoInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["r"]))
            {
                R = Request.QueryString["r"];
            }

            JuSNS.Home.Photo ph = new JuSNS.Home.Photo();
            Phi = ph.Info(Convert.ToInt32(R));

            string action = Request.QueryString["action"];
            if (action == "sethead")
            {
                if (Phi.PhotoType == 0 && Phi.UploadUser == UserID)
                {
                    ph.SetCover(Convert.ToInt32(R), UserID, true);
                    SNSToKenCookie["headpic"] = GetHeadPath(UserID);
                    Session["successMSG"] = "设置头像成功";
                    Response.Redirect("photoview.aspx?r[0]=" + R);
                }
            }

            formContent();

            if (Request.Form.Count > 0)
            {
                SaveEdit();
            }
        }
        /// <summary>
        /// 设置表单内容
        /// </summary>
        protected void formContent()
        {
            StringBuilder sb = new StringBuilder();
            string coverChecked = "";
            if (Phi.IsCover)
            {
                coverChecked = "checked";
            }
            sb.Append("<ul>\n");
            sb.Append(" <li class=\"nl border-eee margins5 paddings10\">\n");
            sb.Append("     <div class=\"nl\">\n");
            if (Phi.PhotoType == 1)
            {
                sb.Append("         <div><img src=\"" + this.GetSmallPic(Phi.FilePath, 2) + "\"  style=\"padding:2px;border:1px solid #eeeeee;\" /></div>\n");
            }
            else
            {
                sb.Append("         <div><img src=\"" + Public.GetSmallHeadPic(Phi.FilePath, 2) + "\" style=\"padding:2px;border:1px solid #eeeeee;\" /></div>\n");
            }
            sb.Append("         <div><label for=\"p-cov-1\"><input type=\"checkbox\" name=\"coverid\" value=\"" + Phi.PhotoID + "\" id=\"p-cov-1\" " + coverChecked + "/> 设置为封面</label></div>\n");
            sb.Append("     </div>");
            sb.Append("     <div class=\"nr w530\">\n");
            sb.Append("         <div class=\"cpstr\">描述：</div>\n");
            sb.Append("         <div class=\"cpstr\"><textarea name=\"caption[" + Phi.PhotoID + "]\" maxlength=\"500\"  class=\"f-text s70 height120\">" + Phi.Description + "</textarea></div>\n");
            if (Phi.PhotoType == 1)
            {
                sb.Append("         <div class=\"pstr\">相册：" + albumselect("ab[" + Phi.PhotoID + "]", Phi.AlbumID) + "</div>\n");
            }
            sb.Append("     </div>\n");
            sb.Append(" </li>\n");
            sb.Append("</ul>\n");
            sb.Append("<div class=\"pstr\">\n");
            sb.Append(" <input type=\"submit\" class=\"f-button\" value=\"保存编辑\" />\n");
            sb.Append(" <input type=\"button\" class=\"f-reset\" value=\"返回照片\" onclick=\"window.location='PhotoView.aspx?r[0]=" + Phi.PhotoID + "&r[1]=" + Phi.AlbumID + "'\"/>\n");
            sb.Append("</div>");
            FormContent = sb.ToString();
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        protected void SaveEdit()
        {
            string coverid = Request.Form["coverid"];
            string abid = Request.Form["ab[" + Phi.PhotoID + "]"];
            PhotoInfo phi = Phi;
            JuSNS.Home.Photo ph = new JuSNS.Home.Photo();
            phi.Description = Request.Form["caption[" + Phi.PhotoID + "]"];
            if (coverid == Convert.ToString(Phi.PhotoID))
            {
                phi.IsCover = true;
                if (Phi.PhotoType == 0)
                {
                    ph.SetCover(Phi.PhotoID, UserID, true);
                    SNSToKenCookie["headpic"] = GetHeadPath(UserID);
                }
                else
                {
                    ph.SetCover(Phi.PhotoID, UserID, false);
                }
            }
            try
            {
                phi.AlbumID = int.Parse(abid);
            }
            catch
            {
                phi.AlbumID = 0;
            }
            if (ph.Edit(phi) == 1)
            {
                Session["successMSG"] = "编辑成功";
            }
            else
            {
                Session["successMSG"] = "编辑失败";
            }
            Response.Redirect("Photoview.aspx?r[0]=" + phi.PhotoID + "&r[1]=" + phi.AlbumID);
        }
        /// <summary>
        /// 相册选择
        /// </summary>
        /// <param name="name">相册名称</param>
        /// <param name="currentid">当前相册ID</param>
        /// <returns>select</returns>
        protected string albumselect(string name, int currentid)
        {
            JuSNS.Home.Album AB = new JuSNS.Home.Album();
            List<AlbumInfo> InfoList = AB.AlbumList(this.UserID);
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name=\"" + name + "\">");
            for (int i = 0; i < InfoList.Count; i++)
            {
                string secd = "";
                if (InfoList[i].AlbumID == currentid)
                {
                    secd = "selected";
                }
                sb.Append("<option value=\"" + InfoList[i].AlbumID + "\" " + secd + ">" + InfoList[i].Title + "</option>");
            }
            sb.Append("</select>");
            return sb.ToString();
        }
    }
}
