using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

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

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            DataBindRePeat();

        }

    }
    private void DataBindRePeat()
    {

        int temp1 =ConvertHelper.GetInteger(lblCurrentPage.Text);

        List<goods> Stus = GetTestData();

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

        int count;
        List<goods> 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<goods> GetTestData()
    {
        List<goods> stus = new List<goods>();
        int inti;
        inti = 1;
        try
        {
            string sql = "SELECT * FROM [v_tabG_inventory] where Quantity > 0 or hclx = 0 ";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    goods s = new goods();
                    s.id = inti;
                    s.Gid = ConvertHelper.GetInteger(dr["Gid"]);
                    s.Quantity = ConvertHelper.GetInteger(dr["Quantity"]);
                    s.price = ConvertHelper.GetDecimal(dr["price"]);
                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    s.State = ConvertHelper.GetInteger(dr["State"]);
                    s.Inventoryid = ConvertHelper.GetInteger(dr["Inventoryid"]);
                    stus.Add(s);
                    inti++;
                }
            }
        }
        catch (Exception)
        {

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

    }
    public class goods
    {
        public int id { get; set; }
        public int Gid { get; set; }
        public int Quantity { get; set; }
        public decimal price { get; set; }
        public string GoodsName { get; set; }
        public int State { get; set; }

        public int Inventoryid { get; set; }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {

        List<goods> Stus = GetTestData();

        Stus = Stus.Skip(0).Take(15).ToList();

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
        lblCurrentPage.Text = "1";
    }
    protected void Button6_Click(object sender, EventArgs e)
    {

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

        if (temp > 1)
        {
            List<goods> 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 Button7_Click(object sender, EventArgs e)
    {
        int temp = Convert.ToInt32(this.lblCurrentPage.Text);

        if (temp < Convert.ToInt32(lblPageCount.Text))
        {
            List<goods> 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 Button8_Click(object sender, EventArgs e)
    {
        int intendpage = Convert.ToInt32(lblPageCount.Text);

        List<goods> Stus = GetTestData();

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

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
        lblCurrentPage.Text = lblPageCount.Text;
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        int strState =ConvertHelper.GetInteger(e.CommandArgument.ToString());
        int strInventoryid = ConvertHelper.GetInteger((e.Item.FindControl("HiddenField1") as HiddenField).Value);
        int strid = ConvertHelper.GetInteger((e.Item.FindControl("HiddenField2") as HiddenField).Value);
        string sql;
        if (e.CommandName == "Delete")
        {
            if (strState == 0)
            {
                sql = "update tabG_inventory set State=1 where Inventoryid ='" + strInventoryid + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            else
            {
                sql = "update tabG_inventory set State=0 where Inventoryid ='" + strInventoryid + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();  
            }
        }

        if (e.CommandName == "cktz")
        {
             
                  string url;
                  url = "goodstzmx.aspx?gid=" + ConvertHelper.GetString(strid);
            Response.Redirect(url);


        }
        DataBindRePeat();
    }


}