using System;
using System.Drawing;
using System.IO;
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;
using JuSNS.Config;
using JuSNS.Common.PicUpload;


namespace JuSNS.Web.user.Album
{
    public partial class photoAdd : JuSNS.UI.Page.UserPage
    {
        protected string availExtension = "JPG|GIF|PNG";
        protected int availFileLength = 1024 * 2 * 1000;
        protected string R = "";
        protected string[] r ={ "", "" };
        protected string errmsg="";
        protected JuSNS.Model.AlbumInfo ABI = new AlbumInfo();  //当前相册
        protected void Page_Load(object sender, EventArgs e)
        {
            GetIsNotEmail();
            if (!string.IsNullOrEmpty(Request.QueryString["r"]))
            {
                R = Request.QueryString["r"];
            }
            else
            {
                this.PageError("缺少必要的参数", Request.RawUrl);
            }
            string isHeadPic = this.GetHeadPath(this.UserID);
            if (string.IsNullOrEmpty(isHeadPic))
            {
                string errorStr = "您需要上传了头像后，才能 <b>上传相片</b>！";
                if (JuSNS.Config.UiConfig.isUpHeadPic == "1" && !JuSNS.Common.Public.GetHeadLimite(1))
                {
                    Response.Redirect(rooDir + "/home/space/userheadphoto.aspx?h=1&Err=" + JuSNS.Common.Input.URLEncode(errorStr) + "");
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["r[0]"]))
            {
                r[0] = Request.QueryString["r[0]"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["r[0]"]))
            {
                r[1] = Request.QueryString["r[1]"];
            }

            setCurrentAlbumInfo();
            GroupMember gm = new GroupMember();
            if (this.UserID != ABI.UserID)
            {
                if (!gm.IsGroupMember(this.UserID, ABI.GroupID))
                {
                    this.PageError("不是你的相册，你没有权限增加照片", Request.RawUrl);
                }
            }
            if (Page.IsPostBack)
            {
                upfile();
                if (errmsg != "")
                {
                    Session.Remove("ErrMSG"); 
                    Session["ErrMSG"] = "<ul>" + errmsg + "</ul>";
                    Response.Redirect("photoAdd.aspx?r=" + R);
                }
                else
                {
                    Session.Remove("ErrMSG");
                    if (ABI.GroupID == 0)
                    {
                        Response.Redirect("photoedit.aspx?r=" + R);
                    }
                    else
                    {
                        Response.Redirect("Albumview.aspx?r[1]="+ABI.AlbumID+"");
                    }
                }
            }
        }

        /// <summary>
        /// 上传图片函数
        /// </summary>
        protected void upfile()
        {
            PicUpload picup = new PhotoUpload();
            picup.Start();
            if (picup.SuccessNum < 1)
            {
                errmsg = "上传图片不成功！";
                return;
            }
            for (int i = 0; i < picup.SuccessNum; i++)
            {
                //照片信息入库
                PhotoInfo phi = new PhotoInfo();
                phi.AlbumID = Convert.ToInt32(R);
                phi.Comments = 0;
                phi.Description = "";
                phi.FilePath = picup.FileName[i];
                phi.FileSize = picup.ContentLength[i];
                phi.Height = picup.Height[i];
                phi.Width = picup.Width[i];
                phi.IsCover = false;
                phi.IsLock = EnumIsLock.UnLock;
                phi.PhotoType = 1;
                phi.PostIP = Common.Public.GetClientIP() ;
                phi.PostTime = DateTime.Now;
                phi.State = 0;
                phi.UploadUser = this.UserID;
                phi.Views = 0;
                Photo ph = new Photo();
                ph.Add(phi);
            }
        }      

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