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 System.Collections.Generic;
using Spaces;
using System.IO;

public partial class Admin_PhotoList : System.Web.UI.Page
{
    int categoryID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            categoryID = Convert.ToInt32(Request.QueryString["categoryID"]);
        }
        catch
        {
            categoryID = -1;
        }
        if (categoryID < 1)
            categoryID = -1;
        rprPhotoList.ItemCommand += new RepeaterCommandEventHandler(rprPhotoList_ItemCommand);
         
            BindPhotos();
    }

    void rprPhotoList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "delete")
        {
            string account = User.Identity.Name;
            int pid = Convert.ToInt32(e.CommandArgument);
           Photo p= Photos.GetPhoto(pid);
           try
           { //删除老图片
               string sImgPath = string.Format("{0}\\{1}\\{2}\\small\\{3}", Config.GetConfig().PhotosPath, account, p.CategoryID, p.SmallPic);
               string bImgPath = string.Format("{0}\\{1}\\{2}\\{3}", Config.GetConfig().PhotosPath, account, p.CategoryID, p.Image);
               File.Delete(sImgPath);
               File.Delete(bImgPath);

           }
           catch
           {

           }
            Photos.DelPhoto(pid);
        }
        if (e.CommandName.ToLower() == "setmain")
        {
            int pid = Convert.ToInt32(e.CommandArgument);
            Photo p = Photos.GetPhoto(pid);
            PhotoCategory pc = Photos.GetCategory(p.CategoryID);
            pc.MainPhotoID = p.PhotoID;
            Photos.UpdateCategory(pc);
          
        }
        BindPhotos();
    }

    void BindPhotos()
    {
        string account=User.Identity.Name;
        List<Photo> photos = Photos.GetPhotos(account, categoryID);
        rprPhotoList.DataSource = photos;
        rprPhotoList.DataBind();
    }
    
}
