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;
using JuSNS.UI.Control;
namespace JuSNS.Web.user.Album
{
    public partial class PhotoEdit : JuSNS.UI.Page.UserPage
    {
        protected string R = string.Empty;
        protected string errmsg = string.Empty;
        public string FormContent = string.Empty;
        protected int PhotoCount = 0;
        protected AlbumInfo AbInf = new AlbumInfo();
        protected string PhotoIDS = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            GetIsNotEmail();
            if (Page.IsPostBack)
            {
                SaveEdit();
            }
            string s = Request.QueryString["s"];
            if (s == "1")
            {
                this.isSuc.Visible = true;
            }
            else
            {
                this.isSuc.Visible = false;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["r"]))
            {
                R = Request.QueryString["r"];
            }

            JuSNS.Home.Album ab = new JuSNS.Home.Album();
            if (Convert.ToInt32(R) == 0)
            {
                AbInf.Title = "头像相册";
                AbInf.AlbumID = 0;
                AbInf.Description = "";
                AbInf.UserID = this.UserID;
            }
            else
            {
                AbInf = ab.Info(Convert.ToInt32(R));
            }
            if (this.UserID != AbInf.UserID && !this.IsAdmin())
            {
                this.PageError("不是你的相册，您没有权限修改", Request.RawUrl);
            }
            formContent();
           

        }
        /// <summary>
        /// 设置表单内容
        /// </summary>
        protected void formContent()
        {
            

            int ReCount, PgCount;
            int PageIndex = 1;
            string whatpage = "Album_View";
            if (Convert.ToInt32(R) == 0)
            {
                whatpage = "Album_Head_View";
            }
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt1 = UtilPage.GetPage(whatpage, PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", this.UserID, TypeCode.Int32), new SqlConditionInfo("@AlbumID", Convert.ToInt32(R), TypeCode.Int32));
            string PageUrl = "photoedit.aspx";
            DataTable dt = new DataTable();
            dt.Columns.Add("d1", typeof(string));
            PhotoCount = ReCount;

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                PhotoIDS += dt1.Rows[i]["PhotoID"] + ",";
                DataRow dr = dt.NewRow();
                StringBuilder sb = new StringBuilder();
                string coverChecked = "";
                if (Convert.ToBoolean(dt1.Rows[i]["IsCover"]))
                {
                    coverChecked = "checked";
                }
                string headChecked = "";
                if (this.UserPortrait == dt1.Rows[i]["FilePath"].ToString())
                {
                    headChecked = "checked";
                }
                sb.Append(" <li class=\"nl border-eee margins5 paddings10\">\n");
                sb.Append("     <div class=\"nl w150\">\n");
                if (Convert.ToInt32(dt1.Rows[i]["PhotoType"]) == 1)
                {

                    sb.Append("         <div class=\"cpstr\"><img src=\"" + this.GetSmallPic(dt1.Rows[i]["FilePath"].ToString(), 2) + "\" /></div>\n");
                }
                else
                {
                    sb.Append("         <div class=\"cpstr\"><img src=\"" + Public.GetSmallHeadPic(dt1.Rows[i]["FilePath"], 2) + "\" /></div>\n");
                }
                if (Convert.ToInt32(dt1.Rows[i]["PhotoType"]) == 1)
                {
                    sb.Append("         <div class=\"pstr\"><label for=\"p-cov-" + i + "\"><input type=\"radio\" name=\"coverid\" value=\"" + dt1.Rows[i]["PhotoID"] + "\" id=\"p-cov-" + i + "\" " + coverChecked + "/> 设置为封面</label></div>\n");
                }
                else
                {
                    sb.Append("         <div class=\"pstr\"><label for=\"p-head-" + i + "\"><input type=\"radio\" name=\"headid\" value=\"" + dt1.Rows[i]["PhotoID"] + "\" id=\"p-head-" + i + "\" " + headChecked + " " + headChecked + "/> 设为默认头像</label></div>\n");
                }
                if (Convert.ToInt32(dt1.Rows[i]["PhotoType"]) == 0 && i > 0)
                {
                    sb.Append("         <div class=\"pstr\"><label for=\"p-del-" + i + "\"><input type=\"checkbox\" value=\"" + dt1.Rows[i]["PhotoID"] + "\" name=\"delete[" + dt1.Rows[i]["PhotoID"] + "]\" id=\"p-del-" + i + "\" /> 删除</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[" + dt1.Rows[i]["PhotoID"] + "]\" maxlength=\"300\" class=\"f-text s70 height120\">" + dt1.Rows[i]["Description"] + "</textarea></div>\n");
                sb.Append("     </div>\n");
                sb.Append("     <div class=\"line1\"></div>\n");
                sb.Append(" </li>\n");
                dr["d1"] = sb.ToString();
                dt.Rows.Add(dr);
            }
            if (PhotoIDS != "")
            {
                PhotoIDS = PhotoIDS.Substring(0, PhotoIDS.Length - 1);
            }
            this.formrpt.DataSource = dt;
            this.formrpt.DataBind();
            PageUrl += "?r=" + R ;
            if (ReCount > PAGESZ)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
            dt.Dispose();
            dt1.Dispose();
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        protected void SaveEdit()
        {
            Photo ph = new Photo();
            string coverid = Request.Form["coverid"];
            string headid = Request.Form["headid"];
            string ids = Request.Form["formphotoids"];
            string[] idss = ids.Split(',');
            int AlbumID = 0;
            for (int i = 0; i < idss.Length; i++)
            {
                //删除
                string tempvalue = Request.Form["delete[" + idss[i] + "]"];
                if (tempvalue == idss[i])
                {
                    ph.Del(Convert.ToInt32(idss[i]));
                }
                else
                {
                    //其它操作
                    PhotoInfo phi = ph.Info(Convert.ToInt32(idss[i]));
                    AlbumID = phi.AlbumID;
                    if (phi.UploadUser != this.UserID && !this.IsAdmin())
                    {
                        this.PageError("不是你的相册，你没有权限修改", Request.RawUrl);
                    }
                    phi.Description = Request.Form["caption[" + idss[i] + "]"];
                    ph.Edit(phi);
                    if (coverid == idss[i])
                    {
                        ph.SetCover(Convert.ToInt32(idss[i]), UserID, false);
                    }
                    if (headid == idss[i])
                    {
                        ph.SetCover(Convert.ToInt32(idss[i]), UserID, true);
                        SNSToKenCookie["headpic"] = phi.FilePath;
                    }
                }
            }
            PageRight("图片操作成功。", rooDir + "/home/album/?r=" + this.GetUserID() + "");
            //Response.Redirect("PhotoEdit.aspx?s=1&r=" + AlbumID);
        }
    }
}
