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_goodstzmx : PageBase
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strnumber;
            strnumber = Request.QueryString["gid"];

            string strtime2 = DateTime.Now.ToString("yyyy-MM-dd");
            string strtime1 = DateTime.Now.ToString("yyyy-MM");

            strtime1 = strtime1 + "-01";
            TextBox1.Text = strtime1;
            TextBox2.Text = strtime2;
            gid.Text = strnumber;
            DataBindRePeat();

            string sql = "SELECT Quantity FROM [v_tabG_inventory] where gid  = '" + ConvertHelper.GetInteger(gid.Text) + "' ";
             conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            xkc.Text = cmd.ExecuteScalar().ToString();
                 conn.Close();
        }
    }
    private void DataBindRePeat()
    {
        int temp1 = Convert.ToInt32(lblCurrentPage.Text);
        List<department> Stus = GetTestData();
        Stus = Stus.Skip((temp1 - 1) * 15).Take(15).ToList();
        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();

        int count;
        List<department> 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<department> GetTestData()
    {
        string str1, str2;
        DateTime adt1, adt2;
        str1 = TextBox1.Text + " 00:00:00";
        str2 = TextBox2.Text + " 23:59:59";
        adt1 = ConvertHelper.GetDateTime(str1);
        adt2 = ConvertHelper.GetDateTime(str2);

        List<department> stus = new List<department>();
        try
        {
            string sql = "SELECT * FROM [v_tzmx] where date >='" + adt1 + "' and date <= '" + adt2 + "'and gid = '" + ConvertHelper.GetInteger(gid.Text) + "' order by date desc ";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    department s = new department();
                    s.gid = ConvertHelper.GetInteger(dr["gid"]);
                    s.cksl = ConvertHelper.GetInteger(dr["cksl"]);
                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    s.czlx = ConvertHelper.GetInteger(dr["czlx"]);
                    s.Goodprice = ConvertHelper.GetDecimal(dr["Goodprice"]);
                    s.Date = ConvertHelper.GetDateTime(dr["date"]);
                    stus.Add(s);
                }
            }
        }
        catch (Exception)
        {

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

    public class department
    {
        public int gid { get; set; }
        public int cksl { get; set; }
        public string GoodsName { get; set; }
        public int czlx { get; set; }
        public decimal Goodprice { get; set; }
        public DateTime Date { get; set; }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        string url = "goodskccx.aspx?";
        Response.Redirect(url);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {

        List<department> 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<department> 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<department> 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<department> Stus = GetTestData();

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

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
        lblCurrentPage.Text = lblPageCount.Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataBindRePeat();
    }

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        List<department> Stus = GetTestData();
        Stus = Stus.Where(p => p.czlx == 1).ToList();

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        List<department> Stus = GetTestData();
        Stus = Stus.Where(p => p.czlx == 0).ToList();
        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
    }
    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        DataBindRePeat();
    }
}