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.home.Album
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        protected string[] r ={ "", "" };
        protected string R = string.Empty;
        protected string a = string.Empty;
        protected string cUserID = string.Empty; //当前访问的用户ID
        protected string cUserName = string.Empty; //当前访问的用户名称
        protected string headImage = string.Empty;  //当前访问的用户头像
        protected string title = "相册";
        protected UserInfo CUI = new UserInfo(); //当前用户
        protected string spaceName = "我的空间";
        protected string albumName = "我的相册";
        protected string checkstr = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.GetUserID() == 0)
            {
                checkstr = " onclick=\"javascript:{unlogin();return false}\"";
            }
            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]"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["a"]))
            {
                a = Request.QueryString["a"];
            }
            R = Request.QueryString["r"];
            if (string.IsNullOrEmpty(R))
            {
                if (!string.IsNullOrEmpty(r[0]))
                {
                    R = r[0];
                }
                else
                {
                    R = this.GetUserID().ToString();
                }
            }
            headImage = this.GetHeadImage(Convert.ToInt32(R));
            Home.User u = new User();
            CUI = u.GetUserInfo(Convert.ToInt32(R));
            cUserID = CUI.UserID.ToString();
            cUserName = CUI.Username;

            if (a == "all")
            {
                title = "好友最新相册";
                this.ptitle.InnerText = "好友的最新相册";
                tb1.Attributes["class"] = "";
                tb2.Attributes["class"] = "current";
                tb3.Attributes["class"] = "";
                tb4.Attributes["class"] = "";
                this.MyAlbum.Visible = false;
                this.FriendAlbum.Visible = true;
                friendAlbum();
            }
            else if (a == "userall")
            {
                title = "随便看看";
                this.ptitle.InnerText = "所有人的相册";
                tb1.Attributes["class"] = "";
                tb2.Attributes["class"] = "";
                tb3.Attributes["class"] = "";
                tb4.Attributes["class"] = "current";
                this.MyAlbum.Visible = false;
                this.FriendAlbum.Visible = false;
                AllUserAlbum();
            }
            else
            {
                if (GetUserID() == CUI.UserID)
                {
                    tb1.Attributes["class"] = "current";
                    tb2.Attributes["class"] = "";
                    tb3.Attributes["class"] = "";
                    tb4.Attributes["class"] = "";
                    title = "我的相册";
                    this.ptitle.InnerText = "我的相册";
                    albumName = "我的相册";
                    spaceName = "我的空间";
                    this.UpNewPhoto.Visible = true;
                    this.FriendAlbum.Visible = false;
                    this.MyAlbum.Visible = true;
                    GetMyAlbum();
                }
                else
                {
                    title = " | " + CUI.Username + "的相册";
                    albumName = CUI.Username + "的相册";
                    spaceName = CUI.Username + "的空间";
                    this.UpNewPhoto.Visible = false;
                    this.FriendAlbum.Visible = false;
                    this.MyAlbum.Visible = true;
                    GetMyAlbum();
                }
            }
        }


        /// <summary>
        /// 获取用户名称
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        protected string getUserName(int userID)
        {
            User u = new User();
            UserInfo ui = u.GetUserInfo(userID);
            return ui.Username;
        }
        /// <summary>
        /// 获取用户省份
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        protected string getUserProvince(int userID)
        {
            Dict dc = new Dict();
            User u = new User();
            UserInfo ui = u.GetUserInfo(userID);
            DictAreaInfo dai = dc.Info(ui.Province);
            return dai.Name;
        }

        /// <summary>
        /// 好友相册
        /// </summary>
        /// <returns></returns>
        protected void friendAlbum()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = UtilPage.GetPage("Friend_Album_List", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", this.GetUserID(), TypeCode.Int32));
            string PageUrl = "index.aspx";
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            Home.Album ab = new Home.Album();
            JuSNS.Home.User u = new JuSNS.Home.User();
            if (ReCount > 0)
            {
                this.noAlbum1.Visible = true;
                this.noAlbum1.InnerHtml = ReCount + "个相册<span class=\"pipe\">|</span><a href=\"albumadd.aspx\"" + checkstr + ">创建新相册</a>";
                this.noAlbum2.Visible = false;
            }
            else
            {
                this.noAlbum1.Visible = false;
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UserInfo ui = u.GetUserInfo((int)dt.Rows[i]["UserID"]);
                Dict dc = new Dict();
                DictAreaInfo dai = dc.Info(ui.Province);
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                sb.Append("              <ul class=\"ulno\">\n");
                sb.Append("                <li class=\"listyle\">\n");
                sb.Append("                    <div class=\"photo-album\">\n");
                sb.Append("                        <table class=\"photo-album-table\">\n");
                sb.Append("                        <tr><td class=\"cover-image\">\n");
                sb.Append("                        <p class=\"image\">\n");
                sb.Append("                            <a href=\"AlbumView.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">\n");
                sb.Append("                                <img alt=\"" + dt.Rows[i]["Title"] + "\" src=\"" + this.GetSmallPic(ab.CoverPath((int)dt.Rows[i]["AlbumID"]), 1) + "\" /></a></p>\n");
                sb.Append("                         </td><td style=\"text-align:left\">\n");
                sb.Append("                        <table class=\"info\">\n");
                sb.Append("                            <span class=\"bold\">\n");
                sb.Append("                                <a href=\"AlbumView.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">" + dt.Rows[i]["Title"] + "</a></span>\n");
                sb.Append("                                    <tr>\n");
                sb.Append("                                        <td style=\"text-align:left\">\n");
                sb.Append("                                            <a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\">" + ui.Username + "</a> (" + dai.Name + ")</td>\n");
                sb.Append("                                    </tr>\n");
                sb.Append("                                    <tr>\n");
                sb.Append("                                        <td class=\"time1\">\n");
                sb.Append("                                            更新于 " + JuSNS.Common.Public.getTimeSpan((DateTime)dt.Rows[i]["CreateTime"]) + " 天前<br />\n");
                sb.Append("                                            创建于 " + Convert.ToString(dt.Rows[i]["CreateTime"]) + "</td>\n");
                sb.Append("                                    </tr>\n");
                sb.Append("                        </table>\n");

                //相册照片
                sb.Append(photosOfAlbum((int)dt.Rows[i]["AlbumID"], (int)dt.Rows[i]["UserID"]));
                sb.Append("                             </td></tr></table>");
                sb.Append("                    </div>\n");
                sb.Append("                        </li>\n");
                sb.Append("                    </ul> \n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.DataList1.Visible = true;
            this.DataList1.DataSource = data;
            this.DataList1.DataBind();
            PageUrl += "?a=all";
            if (ReCount > PAGESZ)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }

        /// <summary>
        /// 随便看看
        /// </summary>
        protected void AllUserAlbum()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = UtilPage.GetPage("Friend_Album_List_All", PageIndex, 15, out ReCount, out PgCount, new SqlConditionInfo("@UserID", this.GetUserID(), TypeCode.Int32));
            string PageUrl = "index.aspx";
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            Home.Album ab = new Home.Album();
            JuSNS.Home.User u = new JuSNS.Home.User();
            if (ReCount > 0)
            {
                this.noAlbum1.Visible = true;
                this.noAlbum1.InnerHtml = "显示15个相册<span class=\"pipe\">|</span><a href=\"albumnew.aspx\"" + checkstr + ">创建新相册</a><span class=\"pipe\"> | </span>";
                this.noAlbum2.Visible = false;
            }
            else
            {
                this.noAlbum1.Visible = false;
            }
            int gRowscount = dt.Rows.Count;
            if (gRowscount > 15)
            {
                gRowscount = 15;
            }
            for (int i = 0; i < gRowscount; i++)
            {
                UserInfo ui = u.GetUserInfo((int)dt.Rows[i]["UserID"]);
                Dict dc = new Dict();
                DictAreaInfo dai = dc.Info(ui.Province);
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                sb.Append("              <ul class=\"ulno\">\n");
                sb.Append("                <li class=\"listyle\">\n");
                sb.Append("                    <div class=\"photo-album\">\n");
                sb.Append("                        <table class=\"photo-album-table\">\n");
                sb.Append("                        <tr><td class=\"cover-image\">\n");
                sb.Append("                        <div class=\"image\">\n");
                sb.Append("                            <a href=\"AlbumView.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">\n");
                sb.Append("                                <img alt=\"" + dt.Rows[i]["Title"] + "\" src=\"" + this.GetSmallPic(ab.CoverPath((int)dt.Rows[i]["AlbumID"]), 1) + "\" /></a></div>\n");
                sb.Append("                         </td><td style=\"text-align:left\">\n");
                sb.Append("                        <table class=\"info\">\n");
                sb.Append("                            <span class=\"bold\">\n");
                sb.Append("                                <a href=\"AlbumView.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">" + dt.Rows[i]["Title"] + "</a></span>\n");
                sb.Append("                                    <tr>\n");
                sb.Append("                                        <td style=\"text-align:left\">\n");
                sb.Append("                                            <a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\">" + ui.Username + "</a> (" + dai.Name + ")</td>\n");
                sb.Append("                                    </tr>\n");
                sb.Append("                                    <tr>\n");
                sb.Append("                                        <td class=\"time1\">\n");
                sb.Append("                                            更新于 " + JuSNS.Common.Public.getTimeSpan((DateTime)dt.Rows[i]["CreateTime"]) + " 天前<br />\n");
                sb.Append("                                            创建于 " + Convert.ToString(dt.Rows[i]["CreateTime"]) + "</td>\n");
                sb.Append("                                    </tr>\n");
                sb.Append("                        </table>\n");

                //相册照片
                sb.Append(photosOfAlbum((int)dt.Rows[i]["AlbumID"], (int)dt.Rows[i]["UserID"]));
                sb.Append("                             </td></tr></table>");
                sb.Append("                    </div>\n");
                sb.Append("                        </li>\n");
                sb.Append("                    </ul> \n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.Repeater1.Visible = true;
            this.Repeater1.DataSource = data;
            this.Repeater1.DataBind();
            PageUrl += "?a=userall";
            if (ReCount > PAGESZ)
            {
                this.DivPage2.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage2.Visible = false;
            }
        }
        /// <summary>
        /// 我的相册
        /// </summary>
        /// <returns></returns>
        protected void GetMyAlbum()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = UtilPage.GetPage("My_Album_List", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@UserID", Convert.ToInt32(R), TypeCode.Int32));
            string PageUrl = "Photos.aspx";
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            JuSNS.Home.Album ab = new JuSNS.Home.Album();
            JuSNS.Home.User u = new JuSNS.Home.User();
            if (ReCount > 0)
            {
                this.noAlbum1.Visible = true;
                this.noAlbum1.InnerHtml = ReCount + "个相册<span class=\"pipe\">|</span><a href=\"albumnew.aspx\"" + checkstr + ">创建新相册</a><span class=\"pipe\"> </span><span  class=\"reshow\"><a href=\"../album/headalbum.aspx?r[0]=" + GetUserID() + "&r[1]=0\">我的头像相册</a></span>";
                this.noAlbum2.Visible = false;
            }
            else
            {
                this.noAlbum1.Visible = false;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                sb.Append("                    <ul class=\"ulno\">\n");
                sb.Append("                        <li class=\"listyle\">\n");
                sb.Append("                            <div class=\"photo-album\">\n");
                sb.Append("                             <table class=\"photo-album-table\">\n");
                sb.Append("                               <tr><td class=\"cover-image\">\n");
                sb.Append("                                <p class=\"image\">\n");
                sb.Append("                                    <a href=\"Albumview.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">\n");
                sb.Append("                                        <img alt=\"" + dt.Rows[i]["Title"] + "\" src=\"" + this.GetSmallPic(ab.CoverPath((int)dt.Rows[i]["AlbumID"]), 1) + "\" /></a></p>\n");
                sb.Append("                                </td><td>\n");
                sb.Append("                                <table class=\"info\">\n");
                sb.Append("                                    <tr>\n");
                sb.Append("                                        <td >\n");
                sb.Append("                                            <span class=\"bold\"><a href=\"Albumview.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">" + dt.Rows[i]["Title"] + "</a></span> <span class=\"count\">" + dt.Rows[i]["ImagesCount"] + " 张照片</span></td>\n");
                sb.Append("                                    </tr>\n");
                sb.Append("                                    <tr>\n");
                sb.Append("                                        <td class=\"time1\">\n");
                sb.Append("                                            更新于 " + dt.Rows[i]["CreateTime"] + "<br />\n");
                sb.Append("                                            创建于 " + dt.Rows[i]["CreateTime"] + "</td>\n");
                sb.Append("                                    </tr>\n");
                sb.Append("                                    <tr>\n");
                if (dt.Rows[i]["UserID"].ToString() == this.GetUserID().ToString())
                {
                    sb.Append("                                        <td>\n");
                    sb.Append("                                            <a href=\"PhotoAdd.aspx?r=" + dt.Rows[i]["AlbumID"] + "\" " + checkstr + ">上传照片</a><span class=\"pipe\">|</span><a href=\"Photoedit.aspx?r=" + dt.Rows[i]["AlbumID"] + "\">编辑照片</a><span class=\"pipe\">|</span><a href=\"AlbumEdit.aspx?r=" + dt.Rows[i]["AlbumID"] + "\">编辑相册</a><span\n");
                    sb.Append("                                                class=\"pipe\">|</span><a href=\"AlbumView.aspx?r[0]=" + this.GetUserID() + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">浏览相册</a></td>\n");
                }
                sb.Append("                                    </tr>\n");
                sb.Append("                                </table>\n");
                //相册照片
                sb.Append(photosOfAlbum((int)dt.Rows[i]["AlbumID"], (int)dt.Rows[i]["UserID"]));
                sb.Append("                             </td></tr></table>");
                sb.Append("                            </div>\n");
                sb.Append("                        </li>\n");
                sb.Append("                    </ul> \n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.MyAlbumList.Visible = true;
            this.MyAlbumList.DataSource = data;
            this.MyAlbumList.DataBind();
            PageUrl += "?";
            if (ReCount > PAGESZ)
            {
                this.DivPage1.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage1.Visible = false;
            }
        }

        /// <summary>
        /// 相册中的照片
        /// </summary>
        /// <param name="AlbumID">相册ID</param>
        /// <returns></returns>
        protected string photosOfAlbum(int AlbumID, int UserID)
        {
            StringBuilder sb = new StringBuilder();
            Photo p = new Photo();
            List<PhotoInfo> infolist = p.InfoList(AlbumID, UserID);
            if (infolist.Count > 0)
            {
                sb.Append("<table class=\"photo-recent\">");
                sb.Append("<tr>");
                for (int i = 0; i < infolist.Count; i++)
                {
                    if (i > 4) break;
                    sb.Append("<td><a href=\"photoview.aspx?r[0]=" + infolist[i].PhotoID + "\"><img src=\"" + this.GetSmallPic(infolist[i].FilePath, 0) + "\" /></a></td>");
                }
                sb.Append("<td>&nbsp;<a href=\"albumview.aspx?r[0]=" + UserID + "&r[1]=" + AlbumID + "\">浏览更多 &raquo;</a></td>");
                sb.Append("</tr></table>");
            }
            return sb.ToString();
        }
    }
}
