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.IO;
using Xiaoxiao.Upload;
using System.Collections.Generic;
using Spaces.Controls;
public partial class Manage_EditUserConfig : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
        if (!IsPostBack)
            BindConfig();
    }
    void BindConfig()
    {
        UserConfig uc = Cacher.GetUserConfig(Account);
        this.txtTitle.Text = uc.Title;
        LnkAccount.Text = uc.Account;
        BindGroup(uc.GroupID);
        List<Spaces.Theme> tList = Globals.GetThemes().ThemeList;
        dropThemes.Items.Clear();
        BindSpaceLimits();
        foreach (Spaces.Theme t in tList)
        {
            ListItem li = new ListItem(t.Title,t.Name);
            if (t.Name == uc.Theme)
                li.Selected = true;
            dropThemes.Items.Add(li);
        }

        if (uc.PictureID > 0)
        {
            Picture.ImageUrl = Globals.GetSiteUrls().DownloadPage(uc.Account, uc.PictureID);
        }
        else
        {
            Picture.Visible = false;
        }
        Description.Text = uc.Description;
        if (buttonUpdate != null)
        {
            UploadHelper uploadHelper = new UploadHelper();
            //如需要显示进度条
            uploadHelper.RegisterProgressBar(buttonUpdate, false);

        }


    }

    private void BindSpaceLimits()
    {
        UserDirectory ud = Directorys.GetUserRootDirectory(Account);
        if (ud.Password != string.Empty)
        {
            txtFolderPassword.Text = Utils.DecryptDir(ud.Password);
            dropSelectShare.SelectedValue = "0";
        }
        else
        {
            dropSelectShare.SelectedValue = "1";
            foreach (ListItem li in dropLimits.Items)
            {
                li.Selected = false;
                if (li.Value == ud.Limits)
                    li.Selected = true;
            }
        }
    }

    private void BindGroup(int p)
    {
        dropGroups .Items.Clear();
        string sql = "SELECT  GroupID,GroupName FROM User_Group";
        DataView dv =Spaces.SpacesDataProvider.Instance().GetDataView(sql);
        
        if (dv != null && dv.Count>0)
        {
            foreach (DataRowView dr in dv)
            {
                ListItem li = new ListItem(dr["GroupName"].ToString(), dr["GroupID"].ToString());
                if (li.Value == p.ToString())
                    li.Selected = true;
     
                dropGroups.Items.Add(li);
            }
        }
    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
        UserConfig uc = Cacher.GetUserConfig(Account);
        uc.Title = this.txtTitle.Text;
        uc.Description = Description.Text;
        uc.GroupID = int.Parse(dropGroups.SelectedValue);
        /*
        UploadFile uploadFile = UploadHelper.GetUploadFile("file1");
        if (chkUpdatePicture.Checked)
        {
            UserFile uf = new UserFile();
            uf.DirectoryID = uc.RootDirID;
            uf.Created = DateTime.Now;
            uf.Attribute = FileAttribute.System;
            uf.Description = "首页形像照片";
            uf.FileSize = 0;//fudPicture.PostedFile.ContentLength;
            uf.Hits = 0;
            uf.Price = 0;

            string path = Config.GetConfig().UploadFilesPath;
            path = Path.Combine(path, uc.Account);
            if (!path.EndsWith("\\"))
            {
                path = string.Format("{0}\\", path);
            }
            //  if (fudPicture.PostedFile == null)
            //    return;
            string fileName = Path.GetFileName(uploadFile.FileName);
            uf.SourceName = uf.Name = fileName;
            uc.PictureID = Files.CreateFile(uf);
            uploadFile.SaveAs(Path.Combine(path, uf.SourceName));
        }
        */
        if (uc.Layout == null)
        {

            Theme the = Globals.GetThemes().FindTheme(uc.Theme);
            UserLayout ul = new UserLayout();
            ul.Theme = the.Name;
            ul.Style = the.Style;

            foreach (ThemeRow tr in the.Rows)
            {
                ul.Rows.Add(tr);
            }

            uc.Layout = ul;
        }
        Users.UpdateUserConfig(uc);

        //更改U盘
        UserDirectory ud = Directorys.GetUserRootDirectory(Account);
        if (dropSelectShare.SelectedValue == "0")
        {
            ud.Password = Utils.EncryptDir(txtFolderPassword.Text.Trim());
        }
        else
        {
            ud.Limits = dropLimits.SelectedValue;
            ud.Password = string.Empty;
        }
        Directorys.UpdateDirectory(ud);

        BindConfig();
     
        Message.Text = Msg.GetMsg("您已成功更改了页面设置！");
    }
    public string SaveImg(HttpPostedFile pf, string path)
    {
        int filelength = pf.ContentLength;
        if (filelength == 0)
        {
            return string.Empty;
        }
        string endname = string.Empty;

        //当保存目录不存在则创建目录

        switch (pf.ContentType.Trim())
        {
            case "image/gif":
                endname = ".gif";
                break;
            case "image/jpeg":
            case "image/pjpeg":
                endname = ".jpg";
                break;
            case "image/bmp":
                endname = ".bmp";
                break;
            case "image/x-png":
                endname = ".png";
                break;
            default:
                endname = "";
                break;


        }

        byte[] myData = new Byte[pf.ContentLength];
        pf.InputStream.Read(myData, 0, pf.ContentLength);

        string sFilename = System.IO.Path.GetFileName(pf.FileName);
        int file_append = 0;

        //如果文件名存在，在文件名后加一
        while (System.IO.File.Exists(pf + sFilename))
        {
            file_append++;
            sFilename = System.IO.Path.GetFileNameWithoutExtension(pf.FileName)
                + file_append.ToString() + endname;
        }


        System.IO.FileStream newFile = new System.IO.FileStream(path + sFilename, System.IO.FileMode.Create);
        newFile.Write(myData, 0, myData.Length);
        newFile.Close();
        return sFilename;



    }
    string Account
    {
        get { return Request.QueryString["Account"]; }
    }
}
