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.Threading;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;


public partial class admin_paymoneylog : System.Web.UI.Page
{
    private static string cmd_str;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!Page.IsPostBack)
            {
                if (this.Session["USERNAME"] == null || this.Session["Role"] == null || this.Session["USERNAME"].ToString() == "" || this.Session["Role"].ToString() == "")
                {
                    Response.Redirect("login.aspx");
                }
                else
                { 
                    string[] role = this.Session["Role"].ToString().Split('#');
                    bool exists = false;
                    for (int i = 0; i < role.Length; i++)
                    {
                        if (role[i].Equals("8") || role[i].Equals("0"))
                        {
                            exists = true; break;
                        }
                    }
                    if (exists)
                    {
                        this.date.Value = DateTime.Now.ToShortDateString();
                        cmd_str = " where convert(smalldatetime,Convert(varchar(10),AddTime))='" +date.Value+"'";
                        Bind(1);
                    }
                    else
                    { Response.Redirect("NoAuthor.aspx"); }
                }
            }

        }

    }
    public string GetTitle(string id, string table, string title)
    {
        string titles = "无";
        SB.ADOHelper help = new SB.ADOHelper();
        if (help.conn.State == ConnectionState.Closed)
        {
            help.conn.Open();
        }
        SqlCommand cmd = new SqlCommand("select " + title + " from " + table + " where id='" + id + "'", help.conn);
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.Read())
        {
            titles = rd[title].ToString();
        }

        rd.Close();
        help.conn.Close();
        return titles;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string username;
        if (SB.ADOHelper.inputText(TextBox1.Text).Trim() == "")
        { username = " is not null "; }
        else { username = "='" + SB.ADOHelper.inputText(TextBox1.Text) + "'"; }
        string datetime;
        if (date.Value.Trim() != "")
        {
            try
            {
                datetime = "='" + Convert.ToDateTime(SB.ADOHelper.inputText(date.Value.ToString())).ToShortDateString() + "'";
            }
            catch
            { Response.Write("<script>alert('输入的日期无格式不正确！')</script>"); return; }
        }
        else
        {
            datetime = " is not null ";
        }
        cmd_str = "where SystemUser" + username + " and convert(smalldatetime,Convert(varchar(10),AddTime))" + datetime+" and Event "+DropDownList1.SelectedValue;
        Bind(1);
        if (DataGrid1.Items.Count > 0)
        { Label1.Visible = false; }
        else { Label1.Visible = true; }
    }
    private void Bind(int record)
    {
        int maxrecord = 20;
        int recordcount;
        int page;
        int startrecord = (record - 1) * maxrecord;
        string connstr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        SqlConnection conn = new SqlConnection(connstr);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from paymoneylog "+cmd_str+" order by id desc", conn);
        DataSet dataset = new DataSet();
        da.Fill(dataset, startrecord, maxrecord, "websit");
        this.DataGrid1.DataSource = dataset;
        this.DataGrid1.DataBind();
        SqlCommand cmd = new SqlCommand("select count(*) from paymoneylog " + cmd_str, conn);
        recordcount = (int)cmd.ExecuteScalar();
        conn.Close();
        page = ((recordcount % maxrecord) == 0) ? (recordcount / maxrecord) : (recordcount / maxrecord + 1);
        this.Label2.Text =record.ToString();
        if (this.DataGrid1.Items.Count > 0)
        {
            this.Label3.Text = page.ToString();
            this.Label4.Text = recordcount.ToString();
            this.DropDownList2.Items.Clear();
            for (int i = 1; i <= int.Parse(this.Label3.Text); i++)
            {
                this.DropDownList2.Items.Add(i.ToString());
            }
            this.DropDownList2.SelectedIndex = int.Parse(this.Label2.Text) - 1;

        }
        else
        {
            this.Label3.Text = "0";
            this.Label4.Text = "0";
            this.DropDownList2.Items.Clear();
        }


        this.LinkButton1.Enabled = record <= 1 ? false : true;
        this.LinkButton2.Enabled = record <= 1 ? false : true;
        this.LinkButton3.Enabled = record < page ? true : false;
        this.LinkButton4.Enabled = record < page ? true : false;


    }

    protected void gopage(object sender, System.EventArgs e)
    {
        int prepage = Convert.ToInt32(this.Label2.Text.ToString());

        int nextpage = Convert.ToInt32(this.Label3.Text.ToString());
        string cmdname = ((LinkButton)sender).CommandArgument.ToString();
        switch (cmdname)
        {
            case "pre":
                if (prepage > 1)
                    prepage--; break;
            case "next":
                if (prepage < nextpage)
                    prepage++; break;
            case "frist": prepage = 1; break;
            case "end": prepage = nextpage; break;
        }

        Bind(prepage);
    }

  
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind(Convert.ToInt32(DropDownList2.SelectedItem.Text));
    }
 
}
