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

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string strnumber;
            strnumber = Request.QueryString["ckdh"];
            Labrkdh.Text = strnumber;
            DataBindRePeat();

        }
    }


    private void DataBindRePeat()
    {
        int temp1 = Convert.ToInt32(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>();

        string strrkdh;
        strrkdh = Labrkdh.Text;

        try
        {
            string sql = "SELECT * 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;

    }
    public class goods
    {
        public int Gid { get; set; }
        public int cksl { get; set; }
        public decimal goodprice { get; set; }
        public string GoodsName { get; set; }
        public int ckdh { 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 Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("goodssqrk.aspx");
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strrksl;
        TextBox text;

        text = Repeater1.Items[e.Item.ItemIndex].FindControl("Textrksl") as TextBox;
        strrksl = text.Text;

        int strgid = ConvertHelper.GetInteger(e.CommandArgument.ToString());
        string sql;
        string strrkdh = Labrkdh.Text;

        strrksl = strrksl.Trim();

        int rksl = ConvertHelper.GetInteger(strrksl);

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

            sql = "delete from tabG_ck02 where ckdh='" + strrkdh + "' and gid='" + strgid + "' ";

            SqlCommand cmd1 = new SqlCommand(sql, conn);
            conn.Open();
            cmd1.ExecuteNonQuery();
            conn.Close();


        }

        if (e.CommandName == "update")
        {
            if (rksl == 0)
            {
                Response.Write("<script>alert('物品数量不能为空！')</script>");
                return;
            }

            sql = "update tabG_ck02 set cksl='" + rksl + "' where ckdh='" + strrkdh + "' and gid='" + strgid + "' ";
            SqlCommand cmd1 = new SqlCommand(sql, conn);
            conn.Open();
            cmd1.ExecuteNonQuery();
            conn.Close();


        }
        List<goods> Stus = GetTestData();

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


    }
    protected void Button9_Click(object sender, EventArgs e)
    {


        string strrkdh = Labrkdh.Text;
        string sql = "SELECT *  FROM [v_ck02] where ckdh='" + strrkdh + "' ";
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        int intgid, intrksl;
        decimal decpriece;
        string strinsert, str2;
        List<goods> stus = new List<goods>();
        string strkcsl;
        int goodQuantity, intsysl, inthclx;
        inthclx = 1;
        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);
            }
        }

        for (int i = 0; i < stus.Count(); i++)
        {
            intgid = stus[i].Gid;
            intrksl = stus[i].cksl;
            decpriece = stus[i].goodprice;

            strkcsl = "select Quantity from tabG_inventory where gid ='" + intgid + "'";

            SqlCommand cmd3 = new SqlCommand(strkcsl, conn);
            goodQuantity = ConvertHelper.GetInteger(cmd3.ExecuteScalar().ToString());

            intsysl = goodQuantity - intrksl;

            if (intsysl < 0)
            {
                Response.Write("<script>alert('物品数量小于出库数量，请重新输入出库数量！')</script>");

                return;
            }



        }

        for (int i = 0; i < stus.Count(); i++)
        {
            intgid = stus[i].Gid;
            intrksl = stus[i].cksl;
            decpriece = stus[i].goodprice;

            strkcsl = "select Quantity,hclx from v_tabG_inventory where gid ='" + intgid + "'";
            SqlCommand cmd3 = new SqlCommand(strkcsl, conn);

            goodQuantity = ConvertHelper.GetInteger(cmd3.ExecuteScalar().ToString());

            SqlDataReader dr = cmd3.ExecuteReader();


            while (dr.Read())
            {
                goodQuantity = ConvertHelper.GetInteger(dr["Quantity"].ToString());
                inthclx = ConvertHelper.GetInteger(dr["hclx"].ToString());
            }
            dr.Close();
            conn.Close();
            conn.Open();
            if (inthclx == 1)
            {
                intsysl = goodQuantity - intrksl;

                strinsert = "update tabG_inventory  set Quantity = '" + intsysl + "' where  Gid= '" + intgid + "'";
                SqlCommand cmd1 = new SqlCommand(strinsert, conn);
                cmd1.ExecuteScalar();
            }

        }

        str2 = " update tabG_ck01 set ckbz = 1 where ckdh='" + Convert.ToInt32(strrkdh) + "'";
        SqlCommand md2 = new SqlCommand(str2, conn);
        md2.ExecuteScalar();
        conn.Close();

        Response.Redirect("goodsckcl.aspx");
    }
}