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;
using System.Collections.Generic;
using Xiaoxiao.Upload;
using System.IO;
public partial class Admin_UploadPhoto : System.Web.UI.Page
{
    int photoID=-1;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            photoID = Convert.ToInt32(Request.QueryString["photoID"]);
        }
        catch
        {
            photoID = -1;
        }
       
        if (!IsPostBack )
        {
            BindDrop();
            if (photoID > 0)
            BindPage();
        }
    }
    void BindPage()
    {
        Photo p = Photos.GetPhoto(photoID);
        txtName.Text = p.Title;
        txtTags.Text = Utils.TagsToString(p.Tags);
        foreach (ListItem l in dropAlbumList.Items)
        { 
            l.Selected=false;
            if (l.Value == p.CategoryID.ToString())
                l.Selected = true;
        }
        txtDesc.Text = p.Detail;
        string url   = string.Format("{0}/{1}/{2}/small/{3}", Config.GetConfig().PhotosUrl, User.Identity.Name, "{0}", "{1}");
        imgPhoto.ImageUrl = string.Format(url,p.CategoryID,p.SmallPic);
    }
    private void BindDrop()
    {
       //--请选择--
        string account = User.Identity.Name;
        dropAlbumList.Items.Clear();
        dropAlbumList.Items.Add(new ListItem("--请选择--","-1"));
        List<PhotoCategory> categories = Photos.GetCategories(account);
        foreach (PhotoCategory c in categories)
        {
            ListItem li = new ListItem(c.Name,c.ID.ToString());
            dropAlbumList.Items.Add(li);
        }
  
        if (photoID>0)
        {
            txtOpt.Text = "编辑图片:";
        
            photoBtn.Text = "更新";
            dropAlbumList.Enabled = false;
        }
        else
        {
            txtOpt.Text = "上传图片:";
            photoBtn.Text = "添加";
           
        }
    }
    protected void photoBtn_Click(object sender, EventArgs e)
    {
        if (photoID > 0)
            UpdatePhoto();
        else
            AddPhoto();

    }
    void UpdatePhoto()
    {
        string account = User.Identity.Name;
        string name = txtName.Text;
        string tags = txtTags.Text;
        string desc = txtDesc.Text;
        int categoryID = int.Parse(dropAlbumList.SelectedValue);
        if (categoryID == -1)
            return;
      

        Photo p = Photos.GetPhoto(photoID);
        p.CategoryID = categoryID;
    
        p.Detail = desc;
      
        
    
        p.Tags = Utils.GetTags(tags);
        p.Title = name;
        if (chkUpdate.Checked)//当用户选择更新图片
        {
            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
            {

            }
            UploadFile uploadFile = UploadHelper.GetUploadFile("file1");
            string path = Config.GetConfig().PhotosPath;
            path = Path.Combine(path, account);
            path = Path.Combine(path, categoryID.ToString());
            if (!path.EndsWith("\\"))
            {
                path = string.Format("{0}\\", path);
            }
          
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            string fileName = Path.GetFileName(uploadFile.FileName);
            string ext = Path.GetExtension(uploadFile.FileName); 
            if (!CheckExt(ext))//检查后缀名
            {
                Response.Redirect("AdminNav.aspx");
                return;
            }
            uploadFile.SaveAs(Path.Combine(path, fileName));
            PictureStruct ps = Utils.SendSmallImage(Path.Combine(path, fileName), 800, 800);
            string smallPath = string.Format("{0}\\small\\", path);

            if (!Directory.Exists(smallPath))
                Directory.CreateDirectory(smallPath);
            string smallPic = Utils.GetThumbnailImage(Path.Combine(path, ps.Name), smallPath, 160, 160);

            p.Height = ps.Height;
            p.Width = ps.Width;
            p.Image = ps.Name;
            p.Size = ps.Size;
            p.SmallPic = smallPic;
        }
        Photos.UpdatePhoto(p);
        Response.Redirect("PhotoList.aspx?categoryID=" + p.CategoryID);
    }
    bool CheckExt(string ext)
    { 
        bool chk=false;
        switch (ext.ToLower())
        { 
            case ".jpg":
                chk = true;
                break;
            case ".jpeg":
                chk = true;
                break;
            case ".gif":
                chk = true;
                break;
            case ".bmp":
                chk = true;
                break;
            case ".tif":
                chk = true;
                break;
            case ".png":
                chk = true;
                break;
        }
        return chk;
    }
    void AddPhoto()
    {
        string account = User.Identity.Name;
        string name = txtName.Text;
        string tags = txtTags.Text;
        string desc = txtDesc.Text;
        int categoryID = int.Parse(dropAlbumList.SelectedValue);
        if (categoryID == -1)
            return;
        UploadFile uploadFile = UploadHelper.GetUploadFile("file1");
        string path = Config.GetConfig().PhotosPath;
        path = Path.Combine(path, account);
        path = Path.Combine(path, categoryID.ToString());
        if (!path.EndsWith("\\"))
        {
            path = string.Format("{0}\\", path);
        }
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        string fileName = Path.GetFileName(uploadFile.FileName);
        string ext = Path.GetExtension(uploadFile.FileName);
        if (!CheckExt(ext))//检查后缀名
        {
            Response.Redirect("AdminNav.aspx");
            return;
        }
        uploadFile.SaveAs(Path.Combine(path, fileName));
        PictureStruct ps = Utils.SendSmallImage(Path.Combine(path, fileName), 800, 800);
        string smallPath = string.Format("{0}\\small\\", path);

        if (!Directory.Exists(smallPath))
            Directory.CreateDirectory(smallPath);
        string smallPic = Utils.GetThumbnailImage(Path.Combine(path, ps.Name), smallPath, 160, 160);

        Photo p = new Photo();
        p.CategoryID = categoryID;
        p.Created = DateTime.Now;
        p.Detail = desc;
        p.Height = ps.Height;
        p.Width = ps.Width;
        p.Image = ps.Name;
        p.IsCover = false;
        p.Size = ps.Size;
        p.SmallPic = smallPic;
        p.Tags = Utils.GetTags(tags);
        p.Title = name;

        Photos.CreatePhoto(p);
        Response.Redirect("PhotoList.aspx?categoryID="+categoryID);
    }
    public int PhotoID
    {
        get { return photoID; }
    }
}
