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_newckcl : 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(ckdh)  from tabG_ck01";

        string strsql2 = "select pid,PersonnelName  from tabP_Personnel where State = 1";

        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.DropDownList2.Items.Insert(i, new ListItem(dr["PersonnelName"].ToString(), dr["pid"].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";
    }

    public class goods
    {
        public int Gid { get; set; }
        public int cksl { get; set; }
        public string GoodsName { get; set; }
        public int ckdh { get; set; }
        public decimal Goodprice { get; set; }
    }
    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 = ConvertHelper.GetInteger(DropDownList1.SelectedValue.ToString());
        int ygid;
        ygid = 0;
        if (intleixingid == 1)
        {
            ygid = ConvertHelper.GetInteger(DropDownList2.SelectedValue.ToString());
        }
             if (intleixingid == 0)
        {
            Response.Write("<script>alert('请选择出库方式！')</script>");
            return;

        }

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

        string sql1, sql2;

        if (strid == "")
        {
            Response.Write("<script>alert('请重新输入物品！')</script>");
            return;
        }
        if (Labdkbs.Text == "0")
        {
           // sql1 = "insert into tabG_ck01(Storageid,HandlerId,Rkbz)values('" + intleixingid + "','" + strygid + "',0)";

            sql1 = "insert into tabG_ck01(Storageid,HandlerId,ckygid)values('" + intleixingid + "','" + strygid + "','" + ygid + "')";

            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_ck02(Ckdh,Gid,cksl)values('" + Textrkdh.Text.ToString() + "','" + strid + "','" + ConvertHelper.GetInteger(Textwpsl.Text) + "')";
    

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

        List<goods> Stus = GetTestData();

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

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


    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("goodsckcl.aspx");
    }
    private List<goods> GetTestData()
    {
        List<goods> stus = new List<goods>();

        string strrkdh;
        strrkdh = Textrkdh.Text;
        try
        {
            string sql = "SELECT id,ckdh,Gid,cksl,Goodprice,GoodsName FROM [v_ck02] where ckdh='" + strrkdh + "' ";
            conn.Open();

            SqlCommand cmd = new SqlCommand(sql, conn);

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    goods s = new goods();

                    s.Gid = ConvertHelper.GetInteger(dr["Gid"]);

                    s.ckdh = ConvertHelper.GetInteger(dr["ckdh"]);
                    s.cksl = ConvertHelper.GetInteger(dr["cksl"]);
                    s.Goodprice = ConvertHelper.GetDecimal(dr["Goodprice"]);

                    s.GoodsName = ConvertHelper.GetString(dr["GoodsName"]);
                    stus.Add(s);

                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
        }
        return stus;
    }
    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_ck02 where  gid ='" + str + "' and  ckdh='" + Textrkdh.Text + "' ";

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

            conn.Close();

            List<goods> Stus = GetTestData();

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


        }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strid = DropDownList1.SelectedValue.ToString();
       
     
        if (strid == "1")
        {
            DropDownList2.Visible = true;

        }
       
            if (strid == "2")
            {
                DropDownList2.Visible = false;

            }

            if (strid == "3")
            {
                DropDownList2.Visible = false;

            }
            if (strid == "" || strid.Length == 0)
            {
                DropDownList2.Visible = false;

            }
     
    }
}