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

public partial class Manage_FindUser : ManagePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rprSelectionList.ItemCommand += new RepeaterCommandEventHandler(rprSelectionList_ItemCommand);
        if (!IsPostBack)
            InitTime();
    }
    void rprSelectionList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "delete")
        {
            string account = e.CommandArgument.ToString();
            Users.DelUser(account);
        }

        rprSelectionList.DataBind();
    }
    private void InitTime()
    {
        for (int i = 2006; i < 2010; i++)
        {
            dropSYear.Items.Add(new ListItem(i.ToString()));
            dropEYear.Items.Add(new ListItem(i.ToString()));
        }
        for (int i = 1; i < 13; i++)
        {
            dropSMonth.Items.Add(new ListItem(i.ToString()));
            dropEMonth.Items.Add(new ListItem(i.ToString()));
        }
        for (int i = 1; i < 32; i++)
        {
            dropSDay.Items.Add(new ListItem(i.ToString()));
            dropEDay.Items.Add(new ListItem(i.ToString()));
        }
    }
    protected void btnFindByTime_Click(object sender, EventArgs e)
    {
        string msg = string.Empty;
        DateTime st=GetDateTime(dropSYear.SelectedValue,dropSMonth.SelectedValue,dropSDay.SelectedValue,out msg);
        if(msg!=string.Empty)
        {
         PageMsg.Text=Msg.GetMsg(msg);
            return ;
        }
        DateTime et = GetDateTime(dropEYear.SelectedValue, dropEMonth.SelectedValue, dropEDay.SelectedValue, out  msg);
        if (msg != string.Empty)
        {
            PageMsg.Text = Msg.GetMsg(msg);
            return;
        }
        string sql = "select  u.Account,c.Title,u.Created,u.Active,c.Theme,u.DateLastLogin from Users u inner join User_UserConfig c on (u.Account=c.Account) where u.Created>'{0}' and u.Created<'{1}' order by u.Created desc";
        DataView dv = SpacesDataProvider.Instance().GetDataView(string.Format(sql, st.ToString(),et.ToString()));
        this.rprSelectionList.DataSource = dv;
        rprSelectionList.DataBind();
    }
    DateTime GetDateTime(string y, string m, string d,out string msg)
    {
        try
        {
            DateTime dt = new DateTime(int.Parse(y), int.Parse(m), int.Parse(d));
            msg = string.Empty;
            return dt;
        }
        catch(Exception x){
            msg = x.Message;
            return DateTime.MinValue;
        }
       
    }
    protected void btnFindByIP_Click(object sender, EventArgs e)
    {
        string ip=txtIPAddress.Text;
        if(ip.Trim()==string.Empty)return;
        string sql = "select  u.Account,c.Title,u.Created,u.Active,c.Theme,u.DateLastLogin from Users u inner join User_UserConfig c on (u.Account=c.Account) where u.regIP like  '{0}%' or u.LastIP Like '{0}%'";
        DataView dv = SpacesDataProvider.Instance().GetDataView(string.Format(sql, ip));
        this.rprSelectionList.DataSource = dv;
        rprSelectionList.DataBind();
    }
    protected void PostPaged_PageChanged(object src, Xiaoxiao.WebUI.PageChangedEventArgs e)
    {

    }
    protected void btnFindByEmail_Click(object sender, EventArgs e)
    {
        string email = txtEmail.Text;
        if (email.Trim() == string.Empty) return;
        string sql = string.Format("select  u.Account,c.Title,u.Created,u.Active,c.Theme,u.DateLastLogin from Users u inner join User_UserConfig c on (u.Account=c.Account) where u.Email =  '{0}'", email);
        DataView dv = SpacesDataProvider.Instance().GetDataView(sql);
        this.rprSelectionList.DataSource = dv;
        rprSelectionList.DataBind();
    }
    protected void btnFindByNickName_Click(object sender, EventArgs e)
    {
        string nickName = txtNickName.Text;
        if (nickName.Trim() == string.Empty) return;
        string sql = string.Format("select  u.Account,c.Title,u.Created,u.Active,c.Theme,u.DateLastLogin from Users u inner join User_UserConfig c on (u.Account=c.Account) where u.NickName =  '{0}'", nickName);
        DataView dv = SpacesDataProvider.Instance().GetDataView(sql);
        this.rprSelectionList.DataSource = dv;
        rprSelectionList.DataBind();
    }
}
