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

namespace JuSNS.Web.home.Group
{
    public partial class Album : JuSNS.UI.Page.UserPage
    {
        protected int GroupID; //当前群组ID
        protected int sUserID;
        protected void Page_Load(object sender, EventArgs e)
        {
            sUserID = this.UserID;
            CheckPublicity();
            GroupAlbum();
        }

        /// <summary>
        /// 检查是否允许被访问
        /// </summary>
        protected void CheckPublicity()
        {
            bool tf = false;

            string s_GroupID = Request.QueryString["GroupID"];
            if (s_GroupID == null || s_GroupID == "")
                PageError("参数传递错误!", "/home/group");

            int GroupID = Convert.ToInt32(s_GroupID);
            this.GroupID = GroupID;
            JuSNS.Home.UserGroup usergroup = new JuSNS.Home.UserGroup();
            UserGroupInfo usergroupinfo = usergroup.Info(GroupID);

            tf = usergroup.CheckPublicity(GroupID, UserID, true);
            if (!tf)
                PageError("您没有访问权限!", "/home/group");

            Span_GroupName.InnerHtml = usergroupinfo.GroupName;
            A_Return.HRef = "group.aspx?GroupID=" + s_GroupID;
            //A_A_Return1.HRef = "group.aspx?GroupID=" + s_GroupID;
            A_newAlbum.HRef = "../album/groupalbumnew.aspx?groupid=" + s_GroupID;
            //Img_GroupIMG.Src = usergroupinfo.Portrait;
            //Img_GroupIMG.Alt = usergroupinfo.GroupName;


        }

        /// <summary>
        /// 群组相册
        /// </summary>
        /// <returns></returns>
        protected void GroupAlbum()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = UtilPage.GetPage("Group_Album_List", PageIndex, PAGESZ, out ReCount, out PgCount, new SqlConditionInfo("@GroupID", this.GroupID, TypeCode.Int32));
            string PageUrl = "Photos.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();
            this.P_Count.InnerText = ReCount.ToString();
            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=\"/home/album/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=\"/home/album/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?r=" + dt.Rows[i]["UserID"] + "\">" + 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"]) + " 天前\n");
                sb.Append("                                            &nbsp; 创建于 " + Convert.ToString(dt.Rows[i]["CreateTime"]) + "<br />\n");
                sb.Append("                                            <a href=\"/home/album/PhotoAdd.aspx?r=" + dt.Rows[i]["AlbumID"] + "\">上传照片</a><span class=\"pike\">|</span><a href=\"/home/album/AlbumEdit.aspx?r=" + dt.Rows[i]["AlbumID"] + "\">编辑相册</a><span class=\"pike\">|</span><a href=\"/home/album/AlbumView.aspx?r[0]=" + dt.Rows[i]["UserID"] + "&r[1]=" + dt.Rows[i]["AlbumID"] + "\">浏览相册</a>");
                sb.Append("                                            </td>");
                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>
        /// <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 > 3) break;
                    sb.Append("<td><a href=\"/home/album/photoview.aspx?r[0]=" + infolist[i].PhotoID + "\"><img src=\"" + this.GetSmallPic(infolist[i].FilePath, 0) + "\" /></a></td>");
                }
                sb.Append("<td><a href=\"/home/album/albumview.aspx?r[0]=" + UserID + "&r[1]=" + AlbumID + "\">浏览更多 &raquo;</a></td>");
                sb.Append("</tr></table>");
            }
            return sb.ToString();
        }
    }
}
