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

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            string strtime2 = DateTime.Now.ToString("yyyy-MM-dd");
            string strtime1 = DateTime.Now.ToString("yyyy-MM");
            int i = 0;
            strtime1 = strtime1 + "-01";
            TextBox1.Text = strtime1;
            TextBox2.Text = strtime2;

            string strsql2 = "select pid,PersonnelName  from tabP_Personnel where State = 1";
            SqlCommand com2 = new SqlCommand(strsql2, conn);
            conn.Open();
            SqlDataReader dr = com2.ExecuteReader();
            while (dr.Read())
            {
                this.DropDownList1.Items.Insert(i, new ListItem(dr["PersonnelName"].ToString(), dr["pid"].ToString()));
                i++;
            }
            conn.Close();

            DataBindRePeat();
        }
    }
    private void DataBindRePeat()
    {

        List<goods> Stus = GetTestData();

        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();

    }
    public class goods
    {
        public int nbdbid { get; set; }
        public int dbrid { get; set; }
        public int dbsl { get; set; }
        public int dbbz { get; set; }
        public int jsrid { get; set; }
        public string GoodsName { get; set; }
        public string PersonnelName { get; set; }
        public DateTime date { get; set; }
        public decimal Goodprice { get; set; }

    }
    private List<goods> GetTestData()
    {
        string struer = Session["uid"].ToString();
        List<goods> stus = new List<goods>();
        try
        {
            string sql = "select *  from v_nbdb where dbrid = '" + struer + "' 	order by date desc  ";
            conn.Open();

            SqlCommand cmd = new SqlCommand(sql, conn);

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    goods s = new goods();
                    s.nbdbid = ConvertHelper.GetInteger(dr["nbdbid"]);
                    s.dbrid = ConvertHelper.GetInteger(dr["dbrid"]);
                    s.dbbz = ConvertHelper.GetInteger(dr["dbbz"]);
                    s.dbsl = ConvertHelper.GetInteger(dr["dbsl"]);
                    s.jsrid = ConvertHelper.GetInteger(dr["jsrid"]);
                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    s.PersonnelName = ConvertHelper.GetString(dr["PersonnelName"]);
                    s.date = ConvertHelper.GetDateTime(dr["date"]);
                    s.Goodprice = ConvertHelper.GetDecimal(dr["Goodprice"]);
                    stus.Add(s);

                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
        }
        return stus;
    }
    protected void Btncx_Click(object sender, EventArgs e)
    {
        DataBindRePeat();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string struer = Session["uid"].ToString();
        string strname = txtDemo.Text;
        int strygid = ConvertHelper.GetInteger(DropDownList1.SelectedValue);
        string strdbsl = TextBox4.Text;
        int intdbsl = ConvertHelper.GetInteger(strdbsl);
        string sql = " select gid from v_tabG_Goods where GoodsName ='" + strname + "'";
        if (strname == "")
        {
            Response.Write("<script>alert('物品不能为空！')</script>");
            return;
        }
        if (intdbsl == 0)
        {
            Response.Write("<script>alert('调拨数量不能为空！')</script>");
            return;
        }
        if (strygid == 0)
        {
            Response.Write("<script>alert('请输入物品接收人！')</script>");
            return;
        }

        SqlCommand cmd = new SqlCommand(sql, conn);
        conn.Open();
        string strid = Convert.ToString(cmd.ExecuteScalar());

        string strinsert = "insert into tabG_Nbdb(dbrid,jsrid,wpid,dbsl)values('" + struer + "','" + strygid + "','" + strid + "','" + intdbsl + "')";
        SqlCommand cmd1 = new SqlCommand(strinsert, conn);
        cmd1.ExecuteNonQuery();
        conn.Close();
        DataBindRePeat();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strsql;

        int str = ConvertHelper.GetInteger(e.CommandArgument.ToString());
        string strid = (e.Item.FindControl("HiddenField1") as HiddenField).Value;

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

            strsql = "update tabG_Nbdb set dbbz = 1 where nbdbid = '" + str + "'";
            conn.Open();
            SqlCommand cmd = new SqlCommand(strsql,conn);
            cmd.ExecuteScalar();
            conn.Close();
            List<goods> Stus = GetTestData();
            this.Repeater1.DataSource = Stus;
            this.Repeater1.DataBind();


        }
    }
    protected void Btncx_Click1(object sender, EventArgs e)
    {
        DataBindRePeat();
    }
    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;
    }
}