using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class goods_goods : PageBase
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {

            GridViewBin();

        }
    }
    public void GridViewBin()
    {

        int temp1 = Convert.ToInt32(lblCurrentPage.Text);

        List<Goos> Stus = GetTestData();

        Stus = Stus.Skip((temp1 - 1) * 15).Take(15).ToList();
        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();

        int count;
        List<Goos> Stu = GetTestData();
        count = ConvertHelper.GetInteger(Stu.Count());

        int j = count / 15;
        int i = count % 15;


        if (j >= 1)
        {
            if (i > 0)
            {
                j = j + 1;
            }

        }
        else
        {
            j = 1;

        }

        lblPageCount.Text = j.ToString();


    }

    private List<Goos> GetTestData()
    {
        List<Goos> stus = new List<Goos>();

        try
        {
            string sql = "SELECT * FROM [tabG_Goods] ";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    Goos s = new Goos();
                    s.Gid = ConvertHelper.GetInteger(dr["Gid"]);
                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    s.Goodprice = ConvertHelper.GetDecimal(dr["Goodprice"]);
                    s.CreateTime = ConvertHelper.GetDateTime(dr["CreateTime"]);
                    s.State = ConvertHelper.GetInteger(dr["State"]);

                    stus.Add(s);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
        }
        return stus;
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strsql;

        string str = e.CommandArgument.ToString();

        int strid = Convert.ToInt32((e.Item.FindControl("HiddenField1") as HiddenField).Value);


        if (e.CommandName == "Delete")
        {

            if (strid == 1)
            {
                strsql = "update tabG_Goods set State=0 where Gid='" + Convert.ToInt32(str) + "'";
            }
            else
            {
                strsql = "update tabG_Goods set State=1 where Gid='" + Convert.ToInt32(str) + "'";
            }

            conn.Open();
            SqlCommand mycmd = new SqlCommand(strsql, conn);
            mycmd.ExecuteNonQuery();
            mycmd.Dispose();
            conn.Close();
            Response.Write("<script>alert('执行成功！')</script>");

            GridViewBin();
        }

        if (e.CommandName == "updateUser")
        {
            string url;
            url = "updategoods.aspx?name=" + str;
            Response.Redirect(url);

        }
    }



    protected void Button2_Click(object sender, EventArgs e)
    {
        int temp = Convert.ToInt32(this.lblCurrentPage.Text);

        if (temp < Convert.ToInt32(lblPageCount.Text))
        {
            List<Goos> Stus = GetTestData();
            this.lblCurrentPage.Text = (++temp).ToString();
            Stus = Stus.Skip((temp - 1) * 15).Take(15).ToList();

            this.Repeater1.DataSource = Stus;
            this.Repeater1.DataBind();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }



    public class Goos
    {
        public int Gid { get; set; }
        public string GoodsName { get; set; }
        public decimal Goodprice { get; set; }
        public DateTime CreateTime { get; set; }
        public int State { get; set; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        int temp = Convert.ToInt32(this.lblCurrentPage.Text);

        if (temp > 1)
        {
            List<Goos> Stus = GetTestData();

            this.lblCurrentPage.Text = (--temp).ToString();

            Stus = Stus.Skip((temp - 1) * 15).Take(15).ToList();

            this.Repeater1.DataSource = Stus;
            this.Repeater1.DataBind();
        }
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string strname = Textname.Text;

        int temp1 = Convert.ToInt32(lblCurrentPage.Text);

        List<Goos> Stus = GetTestDatas();

        Stus = Stus.Skip((temp1 - 1) * 15).Take(15).ToList();

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();

        string strsql = "select count(*) from tabG_Goods where GoodsName like  '%" + strname + "%'";

        SqlCommand cmd = new SqlCommand(strsql, conn);

        int count;
        conn.Open();

        count = Convert.ToInt32(cmd.ExecuteScalar());
        conn.Close();
        int j = count / 15;
        int i = count % 15;
        if (j >= 1)
        {
            if (i > 0)
            {
                j = j + 1;
            }

        }
        else
        {
            j = 1;

        }

        lblPageCount.Text = j.ToString();

    }
    private List<Goos> GetTestDatas()
    {
        List<Goos> stus = new List<Goos>();

        try
        {
            string strname = Textname.Text;
            string sql = "SELECT * FROM [tabG_Goods] where GoodsName like  '%" + strname + "%'";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    Goos s = new Goos();
                    s.Gid = Convert.ToInt32(dr["Gid"]);
                    s.GoodsName = dr["GoodsName"].ToString();
                    s.Goodprice = Convert.ToDecimal(dr["Goodprice"]);
                    s.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
                    s.State = Convert.ToInt32(dr["State"]);

                    stus.Add(s);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
        }
        return stus;
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("newfoods.aspx");

    }
}
