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 Spaces;

public partial class Admin_AlbumList : System.Web.UI.Page
{
    string pathFormat = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        rprAlbumList.ItemCreated += new RepeaterItemEventHandler(rprAlbumList_ItemCreated);
        rprAlbumList.ItemCommand += new RepeaterCommandEventHandler(rprAlbumList_ItemCommand);
        //if (!IsPostBack)
        //{
            pathFormat = string.Format("{0}/{1}/{2}/small/{3}", Config.GetConfig().PhotosUrl, User.Identity.Name, "{0}", "{1}");
            BindAlbums();
       // }
    }

    void rprAlbumList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "delete")
        {
            int id = Convert.ToInt32(e.CommandArgument);
            Photos.DelCategory(id);
        }
        BindAlbums();
    }

    void rprAlbumList_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            PhotoCategory  c= e.Item.DataItem as PhotoCategory;
            if (c != null)
            {
                Image icon = e.Item.FindControl("CategoryIcon") as Image;
                if (icon != null)
                {
                    if (c.MainPhotoID > 0)
                        icon.ImageUrl = string.Format(pathFormat, c.ID, c.Image);
                    else
                        icon.ImageUrl = string.Format("{0}/images/NoPhoto.gif", Globals.ApplicationPath);
                }
                HyperLink AlbumName = e.Item.FindControl("AlbumName") as HyperLink;
                if (AlbumName != null)
                {
                    AlbumName.Text = c.Name;
                    AlbumName.NavigateUrl = string.Format("PhotoList.aspx?categoryID={0}",c.ID);
                }
                Literal NumPhotos = e.Item.FindControl("NumPhotos") as Literal;
                if (NumPhotos != null)
                { 
                NumPhotos.Text=string.Format("{0}张",c.NumPhotos);
                }
                Literal Updated = e.Item.FindControl("Updated") as Literal;
                if (Updated != null)
                {
                    Updated.Text = c.Updated.ToString("yy-M-dd hh:mm");
                }
                HyperLink EditCategory = e.Item.FindControl("EditCategory") as HyperLink;
                if (EditCategory != null)
                {
                    EditCategory.NavigateUrl = string.Format("PhotoClassEdit.aspx?ID={0}",c.ID);
                }
                LinkButton lnkDelete = e.Item.FindControl("lnkDelete") as LinkButton;
                if (lnkDelete != null)
                {
                    lnkDelete.CommandArgument = c.ID.ToString();
                    if (c.NumPhotos > 0)
                        lnkDelete.OnClientClick = "alert('请先删除该目录下所有图片。');return false;";
                    else
                        lnkDelete.OnClientClick = "return confirm('是否真的删除该目录？')";
                }
            }
         
        }
    }
    void BindAlbums()
    {
        rprAlbumList.DataSource = Photos.GetCategories(User.Identity.Name);
        rprAlbumList.DataBind();
    }
}
