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.Space
{
    public partial class UserHeadPhoto : JuSNS.UI.Page.UserPage
    {
        JuSNS.Home.Space sp = new JuSNS.Home.Space();
        protected string availExtension = "JPG|GIF|PNG|JPEG|BMP";
        protected int availFileLength = 1024 * 5 * 100;
        protected int suctag = 0; //成功标志
        protected string HeadImage = string.Empty;
        protected string HeadImage2 = string.Empty;
        protected string errmsg = string.Empty; //错误信息
        public static string GetTiShiDivStr = string.Empty;
        public string path = string.Empty;
        public bool isHead = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.Expires = 0;
            HeadImage = this.GetHeadImage(this.UserID, 1);
            HeadImage2 =  this.GetHeadImage(this.UserID, 2);
            if (!string.IsNullOrEmpty(this.GetHeadPath(this.UserID)))
            {
                path = this.GetHeadPath(this.UserID);
            }
            else
            {
                isHead = false;
            }
            string f = Request.QueryString["f"];
            string h = Request.QueryString["h"];
            string err = Request.QueryString["errmsg"];
            string succ = Request.QueryString["sucmsg"];
            if (h == "1")
            {
                GetTiShiDivStr = "<div class=\"msg-warn\">" + Request.QueryString["Err"] + " 现在开始：<a href=\"userHeadPhoto.aspx\">上传头像</a>，也可以使用<a href=\"" + rooDir + "/home/space/UserHead-Camera.aspx?r=" + this.UserID + "\">摄像头拍摄头像</a>。</div>";
            }
            else
            {
                if (!string.IsNullOrEmpty(err))
                {
                    GetTiShiDivStr = "<div class=\"msg-error\">" + err + "</div>";
                }
                else if (!string.IsNullOrEmpty(succ))
                {
                    GetTiShiDivStr = "<div class=\"msg-succ\">" + succ + "</div>";
                }
                else
                {
                    GetTiShiDivStr = string.Empty;
                }
            }
            if (Page.IsPostBack)
            {
                upfile();
                if (!string.IsNullOrEmpty(errmsg))
                {
                    Response.Redirect("UserHeadPhoto.aspx?f=" + f + "&errmsg=" + Server.UrlEncode("<ul>" + errmsg + "</ul>"));
                }
                else
                {
                    Response.Redirect("UserHeadPhoto1.aspx");
                }
            }
        }

        /// <summary>
        /// 上传图片函数
        /// </summary>
        protected void upfile()
        {
            if (string.IsNullOrEmpty(Request.Form["picture"]))
            {
                GetTiShiDivStr = "<div class=\"msg-error\">请选择图片。</div>";
            }
            PicUpload picup = new HeadUpload();
            picup.Start();
            //照片信息入库
            PhotoInfo phi = new PhotoInfo();
            phi.AlbumID = 0;
            phi.Comments = 0;
            phi.Description = "";
            phi.FilePath = picup.OutFileName;
            SNSToKenCookie["headpic"] = picup.OutFileName; //写入cookie
            Session["headpic"] = picup.OutFileName;
            phi.FileSize = picup.LastContentLength;
            phi.Height = picup.LastHeight;
            phi.Width = picup.LastWidth;
            phi.IsCover = false;
            phi.IsLock = EnumIsLock.UnLock;
            phi.PhotoType = 0;
            phi.PostIP = Request.UserHostAddress;
            phi.PostTime = DateTime.Now;
            phi.State = 0;
            phi.UploadUser = this.UserID;
            phi.Views = 0;
            Photo ph = new Photo();
            ph.Add(phi);
        }        
    }
}
