using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class WTOSYSadmin_Products_BigProductCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        this.databind2();
      
     }
    public void databind2()
    { 
    string isdo = Request.QueryString["do"].ToString().Trim();
    string LBid=Request.QueryString["id"].ToString().Trim();
    SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        this.Panel1.Visible = false;
        this.Panel2.Visible = false;
        this.Panel3.Visible = false;
        this.mslable.Visible = false;
        this.Panel4.Visible = false;
        this.Panel5.Visible = false;
        switch (isdo)
        {
          case "edit":
                this.title.Text="修改类名";
                this.Panel1.Visible = true;
                SqlCommand cmd = new SqlCommand("select * from BigProductCategory where id=" + LBid, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                this.LB.Text = sdr.GetString(1);
                this.sort.Text =Convert.ToString(sdr.GetInt32(2));
                sdr.Close();
                conn.Close();
                break;
          case "addLB":
                string biglb=Request.QueryString["biglb"].ToString();
                this.title.Text = biglb+"--添加子类";
                this.Panel2.Visible = true;


                break;
            case "Del"://删除大类

                SqlCommand smlbcount = new SqlCommand("select count(*) from SmallProductCategory where BigCategoryId="+LBid,conn);
                int count = Convert.ToInt32(smlbcount.ExecuteScalar().ToString());

                if (count > 0)
                {
                    Response.Redirect("/sysadmin/main/errorlei.aspx?err=存在子类,不能删除!&backurl=/sysadmin/products/bigproductcategory.aspx");
                }
                else
                {
                    SqlCommand cmdDel = new SqlCommand("delete BigProductCategory where id=" + LBid, conn);
                    cmdDel.ExecuteNonQuery();
                    Response.Redirect("BigProductCategory.aspx?del=NO");
                }
                

                
                break;
           
            case "Del2"://删除子类
               
                SqlCommand cmdDel2 = new SqlCommand("delete SmallProductCategory where id=" + LBid, conn);
               
                cmdDel2.ExecuteNonQuery();

                int psID = Convert.ToInt32(Request.QueryString["bigid"].ToString().Trim());

                SqlCommand scmdel2 = new SqlCommand("select BigCategory from BigProductCategory where id=" + psID, conn);
                string biglbdel2 = scmdel2.ExecuteScalar().ToString().Trim();
                conn.Close();


                Response.Redirect("productLBedit.aspx?id=" + psID + "&do=zilei&biglb=" + biglbdel2 + "");
                //Response.Redirect("BigProductCategory.aspx");
                conn.Close();
                break;
            case "zilei"://查看子类
                string biglb2 = Request.QueryString["biglb"].ToString();
                string id = Request.QueryString["id"].ToString();
                this.title.Text = biglb2 + "--查看子类";
                this.Panel3.Visible = true;
                SqlDataAdapter sdr2 = new SqlDataAdapter("select * from SmallProductCategory where BigCategoryId=" + id + " order by Sort", conn);
                DataSet ds = new DataSet();
                sdr2.Fill(ds,"cu");   
                this.GridView1.DataSource = ds;
                this.GridView1.DataKeyNames = new string[] { "id" };
                this.GridView1.DataBind();

                if (GridView1.Rows.Count>0)      
                {
                    this.Label2.Text = "当前记录数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.GridView1.PageCount + "</b></font>页显示.";

                }
                if(GridView1.Rows.Count==0)
                {
                    this.mslable.Visible = true;
                }
                
                break;
            case "edit2":
                this.title.Text = "修改子类";
                this.Panel4.Visible = true;
                SqlCommand cmd3 = new SqlCommand("select * from SmallProductCategory where id=" + LBid, conn);
                SqlDataReader sdr3 = cmd3.ExecuteReader();
                sdr3.Read();
                this.lbns.Text = sdr3.GetString(2);
                this.lbsorts.Text = Convert.ToString(sdr3.GetInt32(3));
                sdr3.Close();
                conn.Close();
                break;

            case "chagelb":
                this.title.Text = "转移父类";
                this.Panel5.Visible = true;
                string smlbname=Request.QueryString["smlbname"].ToString().Trim();
                this.Label1.Text = smlbname;
                string biglbid =Request.QueryString["bigid"].ToString().Trim();
                SqlDataAdapter sdrddl = new SqlDataAdapter("select * from BigProductCategory", conn);
                DataSet dsddl = new DataSet();
                sdrddl.Fill(dsddl);
                this.lbddl.DataSource = dsddl;
                this.lbddl.DataTextField = "BigCategory";
                this.lbddl.DataValueField = "id";
                
                this.lbddl.DataBind();
                this.lbddl.SelectedValue = biglbid;
                conn.Close();
                dsddl.Clear();
                break;

        }
      }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string LBid2 = Request.QueryString["id"].ToString().Trim();
        string LB = Request["LB"].ToString().Trim();
        int Sort =Convert.ToInt32(Request["sort"].ToString().Trim());
        
        SqlConnection conn = CDatabase.wtoconn();


        conn.Open();
        Response.Write(LBid2+LB+Sort);
        SqlCommand cmd2 = new SqlCommand("update BigProductCategory  set BigCategory='" + LB + "',Sort="+Sort+" where id=" + LBid2, conn);
        cmd2.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("BigProductCategory.aspx");
        
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        string LBid2 = Request.QueryString["id"].ToString().Trim();
        string LB = Request["slb"].ToString().Trim();
        int Sort = Convert.ToInt32(Request["ssort"].ToString().Trim());
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand cmd2 = new SqlCommand("insert into SmallProductCategory(BigCategoryId,SmallCategory,Sort) values (" + LBid2 + ",'"+LB+"',"+Sort + ")", conn);
        cmd2.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("BigProductCategory.aspx");
    }
    //修改子类
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        string LBid2 = Request.QueryString["id"].ToString().Trim();
        string LB = Request["lbns"].ToString().Trim();
        int Sort = Convert.ToInt32(Request["lbsorts"].ToString().Trim());

        SqlConnection conn = CDatabase.wtoconn();


        conn.Open();
        SqlCommand cmd2 = new SqlCommand("update SmallProductCategory  set SmallCategory='" + LB + "',Sort=" + Sort + " where id=" + LBid2, conn);
        cmd2.ExecuteNonQuery();


        SqlCommand scm4 = new SqlCommand("select BigCategoryId from SmallProductCategory where id=" + LBid2, conn);
        int psID =Convert.ToInt32(scm4.ExecuteScalar().ToString().Trim());

        SqlCommand scm5 = new SqlCommand("select BigCategory from BigProductCategory where id=" + psID, conn);
        string biglb2 =scm5.ExecuteScalar().ToString().Trim();
        conn.Close();


        Response.Redirect("productLBedit.aspx?id="+psID+"&do=zilei&biglb="+biglb2+"");
    }
    //添加子类
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        string LBid2 = Request.QueryString["id"].ToString().Trim();
        string LB = Request["LB2"].ToString().Trim();
        int Sort = Convert.ToInt32(Request["sort2"].ToString().Trim());
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand cmd2 = new SqlCommand("insert into SmallProductCategory(BigCategoryId,SmallCategory,Sort) values (" + LBid2 + ",'" + LB + "'," + Sort + ")", conn);
        cmd2.ExecuteNonQuery();

        SqlCommand scm5 = new SqlCommand("select BigCategory from BigProductCategory where id=" + LBid2, conn);
        string biglb2 = scm5.ExecuteScalar().ToString().Trim();
        conn.Close();


        Response.Redirect("productLBedit.aspx?id=" + LBid2 + "&do=zilei&biglb=" + biglb2 + "");
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        SqlConnection smlbchageconn=CDatabase.wtoconn();
        smlbchageconn.Open();
        string smlbid = Request.QueryString["id"];
        string bigchageid = Request["lbddl"].ToString();
        SqlCommand smlbchage = new SqlCommand("update SmallProductCategory set BigCategoryId=" + bigchageid + " where id=" + smlbid, smlbchageconn);
        smlbchage.ExecuteNonQuery();
        this.lblmsg.Visible = true;
        this.lblmsg.Text = "操作成功";
 
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind2();
    }
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            g++;
        }
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((HyperLink)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }
    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)
            {
                string sqlstr = "delete from SmallProductCategory where id=" + GridView1.DataKeys[i].Value;
                Doadmin.Execsql(sqlstr);
            }
        }
        int psID = Convert.ToInt32(Request.QueryString["id"].ToString().Trim());
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand scmdel2 = new SqlCommand("select BigCategory from BigProductCategory where id=" + psID, conn);
        string biglbdel2 = scmdel2.ExecuteScalar().ToString().Trim();
        conn.Close();
        Response.Redirect("productLBedit.aspx?id=" + psID + "&do=zilei&biglb=" + biglbdel2 + "");
        this.GridView1.DataBind();
    }
}
