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;
using System.Data;


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

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

            DataBindRePeat();

        }
    }
    private void DataBindRePeat()
    {
        TextBox4.Text = DateTime.Now.ToShortDateString().ToString();
        int i=0;
        string struer = Session["uid"].ToString();
        string strsql = "select PersonnelName  from tabP_Personnel where pid = '" + struer + "'";
        string strsql1 = "select max(Rkdh)  from tabG_rk01";
        string strsql2 = "select Storageid,Storagename  from tabG_Storage where State = 0";

        SqlCommand com = new SqlCommand(strsql, conn);
        SqlCommand com1 = new SqlCommand(strsql1, conn);
        SqlCommand com2 = new SqlCommand(strsql2, conn);
        conn.Open();

        string strname = ConvertHelper.GetString(com.ExecuteScalar());
        int strrkdh = ConvertHelper.GetInteger(com1.ExecuteScalar());
        SqlDataReader  dr = com2.ExecuteReader();
     

        while (dr.Read())
        {
            this.DropDownList1.Items.Insert(i, new ListItem(dr["Storagename"].ToString(), dr["Storageid"].ToString()));
            i++;
        }
        conn.Close();

        if (strrkdh == 0)
        {
            Textrkdh.Text = "001";
        
        }
        else
            Textrkdh.Text = Convert.ToString(strrkdh+1);
         TextBox3.Text = strname;
         Labid.Text = struer;
         Labdkbs.Text = "0";
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("goodrkcl.aspx");
    }
    protected void click(object sender, EventArgs e)
    {
        
        string strname = txtDemo.Text;
        string strygid = Labid.Text;

        string sql = " select gid from v_tabG_Goods where GoodsName ='" + strname + "'";

        int intleixingid = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
        SqlCommand cmd = new SqlCommand(sql,conn);
        conn.Open();
        string strid = Convert.ToString(cmd.ExecuteScalar());
        conn.Close();

        string sql1,sql2;

        if (Labdkbs.Text == "0")
        {
            sql1 = "insert into tabG_rk01(Storageid,HandlerId,Rkbz)values('" + intleixingid + "','" + strygid + "',0)";

            SqlCommand cmd1 = new SqlCommand(sql1,conn);
            conn.Open();
            cmd1.ExecuteNonQuery();
            conn.Close();
            Labdkbs.Text = "1";
        }
        if (Textwpsl.Text == "")
        {
            Response.Write("<script>alert('物品数量不能为空！')</script>");
            return;

        }

        sql2 = "insert into tabG_rk02(Rkdh,Gid,Rksl)values('" + Textrkdh.Text.ToString() + "','" + strid + "','" + Convert.ToInt32(Textwpsl.Text) + "')";

        SqlCommand cmd2 = new SqlCommand(sql2, conn);
        conn.Open();
        cmd2.ExecuteNonQuery();
        conn.Close();

        List<department> Stus = GetTestData();

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

        txtDemo.Text = "";
            Textwpsl.Text = "";

    }

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

        string strrkdh;
        strrkdh = Textrkdh.Text;
        try
        {
            string sql = "SELECT id,rkdh,Gid,Rksl,price,GoodsName FROM [v_rk02] where rkdh='" + strrkdh + "' ";
            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.rkdh = ConvertHelper.GetInteger(dr["rkdh"]);
                    s.Rksl = ConvertHelper.GetInteger(dr["Rksl"]);
                    s.price = ConvertHelper.GetDecimal(dr["price"]);
                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    stus.Add(s);
                }
            }
        }
        catch (Exception)
        {

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

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

    }
    protected void Button4_Click1(object sender, EventArgs e)
    {
        Response.Write("<script>alert('执行成功！')</script>");
    }

    public class department
    {
        public int Gid { get; set; }
        public int Rksl { get; set; }
        public decimal price { get; set; }
        public string GoodsName { get; set; }
        public int rkdh { get; set; }
     
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strsql;
     
        int str = Convert.ToInt32(e.CommandArgument.ToString());

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

            strsql = "delete from tabG_rk02 where  gid ='" + str + "' and  rkdh='" + Textrkdh.Text +"' ";

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

            conn.Close();

            List<departmen> Stus = GetTestData1();

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


        }

    }

    private List<departmen> GetTestData1()
    {
        List<departmen> stus = new List<departmen>();

        string strrkdh;
        strrkdh = Textrkdh.Text;
        try
        {
            string sql = "SELECT id,rkdh,Gid,Rksl,price,GoodsName FROM [v_rk02] where rkdh='" + strrkdh + "' ";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    departmen s = new departmen();

                    s.Gid = ConvertHelper.GetInteger(dr["Gid"]);
                    s.rkdh = ConvertHelper.GetInteger(dr["rkdh"]);
                    s.Rksl = ConvertHelper.GetInteger(dr["Rksl"]);
                    s.price = ConvertHelper.GetDecimal(dr["price"]);
                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    stus.Add(s);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
        }
        return stus;
    }
    public class departmen
    {
        public int Gid { get; set; }
        public int Rksl { get; set; }
        public decimal price { get; set; }
        public string GoodsName { get; set; }
        public int rkdh { get; set; }

    }
}