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=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {
            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.Panel3.Visible = false;
    this.Panel4.Visible = false;
    this.Panel5.Visible = false;
    this.Panel6.Visible = false;
    this.Panel7.Visible = false;
    this.Panel8.Visible = false;
  
     
     switch (isdo)
        {
         case "addlb":
             SqlCommand attconn = new SqlCommand("select AttributeFL from SmallProductAttribute where id="+LBid, conn);
             string attDoid = attconn.ExecuteScalar().ToString().Trim();
            
             if (attDoid == "1")
             {
                 this.Panel4.Visible = true;
                 SqlDataAdapter sdratt = new SqlDataAdapter("select * from SmallProductAttribute where AttributeFL=1 and attvalue='novalue'", conn);
                 DataSet dsatt = new DataSet();
                 sdratt.Fill(dsatt);
                 this.attlbbind.DataSource = dsatt;
                 this.attlbbind.DataTextField="SmallAttribute";
                 this.attlbbind.DataValueField="id";
                 this.attlbbind.DataBind();
                
                 
             }
             else
             {
                 this.Panel3.Visible = true;
                 SqlDataAdapter sdratt2 = new SqlDataAdapter("select * from SmallProductAttribute where AttributeFL=2 and attvalue='novalue'", conn);
                 DataSet dsatt2 = new DataSet();
                 sdratt2.Fill(dsatt2);
                 this.attlbind2.DataSource = dsatt2;
                 this.attlbind2.DataTextField = "SmallAttribute";
                 this.attlbind2.DataValueField = "id";
                 this.attlbind2.DataBind();
             }



             this.title.Text = "添加属性配件值";

             break;


          case "edit"://修改大类
                this.title.Text="修改类名";
                this.Panel6.Visible = true;
              //绑定大类下拉列表
              SqlDataAdapter sdreditbig = new SqlDataAdapter("select * from BigProductAttribute",conn);
                DataSet databig = new DataSet();
                sdreditbig.Fill(databig);
                this.ddllb.DataSource = databig;
                this.ddllb.DataTextField = "BigAttribute";
                this.ddllb.DataValueField = "id";
                this.ddllb.DataBind();
                //取值
                string id = Request.QueryString["id"].ToString().Trim();
                string SmallAttributeEdit = Request.QueryString["SmallAttribute"].ToString().Trim();
                string BigAttributeId = Request.QueryString["BigAttributeId"].ToString().Trim();
                string BigAttributeNameEdit = Request.QueryString["BigAttributeName"].ToString().Trim();
                string AttributeFL = Request.QueryString["AttributeFL"].ToString().Trim();
                string Sort = Request.QueryString["Sort"].ToString().Trim();
                //绑定大类
                this.ddllb.SelectedValue = BigAttributeId;
                this.LBName.Text = SmallAttributeEdit;
                this.ddlsmfl.SelectedValue = AttributeFL;
                this.SortEdit.Text = Sort;
                
              break;
          case "Editzilei"://修改小类

              
             
              string felei = Request.QueryString["AttributeFL"].ToString().Trim();
              string smallAttribute = Request.QueryString["SmallAttribute"].ToString().Trim();
              this.title.Text = "修改<font color=red>"+smallAttribute + "</font>属性";
              SqlCommand editsm = new SqlCommand();
             //根据ID取出值
              editsm.CommandText = "select attvalue,Stock,Price,Weight from SmallProductAttribute where id=" + LBid;
              editsm.Connection = conn;
              SqlDataReader sdredit = editsm.ExecuteReader();
              sdredit.Read();
              string attvalueEdit = sdredit.GetString(0).ToString().Trim();
              string attvalueEdit2 = Convert.ToString(sdredit.GetInt32(1)).ToString().Trim();
              
              decimal money = Convert.ToDecimal(sdredit.GetDecimal(2));

              string attvalueEdit4 = sdredit.GetString(3).ToString().Trim();
              if (felei == "1")
              {
                  this.Panel7.Visible = true;
                  this.Label1.Text = smallAttribute;
                  this.TextBox2.Text=attvalueEdit;
              }
              else
              {
                  this.Panel8.Visible = true;
                  this.sspj.Text = smallAttribute;
                  this.TextBox3.Text = attvalueEdit;
                  this.TextBox4.Text = attvalueEdit4;
                  this.TextBox5.Text = money.ToString();
                  this.TextBox6.Text = attvalueEdit2;
              }


             break;


 
            case "Del"://删除大类
                string SmallAttribute = Request.QueryString["SmallAttribute"].ToString().Trim();
                SqlCommand smlbcount = new SqlCommand("select count(*) from SmallProductAttribute where SmallAttribute='" + SmallAttribute + "'", conn);
                int count = Convert.ToInt32(smlbcount.ExecuteScalar().ToString());

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

                }
                

                
                break;        
         case "zilei":
           
             this.Panel5.Visible = true;
             string SmallAttribute2 = Request.QueryString["SmallAttribute"].ToString().Trim();
             this.title.Text = "<font color=red>"+SmallAttribute2 + "</font>查看值";
             SqlDataAdapter zileisdr = new SqlDataAdapter("select * from SmallProductAttribute where SmallAttribute='" + SmallAttribute2 + "' and attvalue<>'novalue' order by Sort", conn);
             DataSet zileids = new DataSet();
             zileisdr.Fill(zileids,"cu");
             this.zileiattribute.DataSource = zileids;
             this.zileiattribute.DataKeyNames = new string[] { "id" };
             this.zileiattribute.DataBind();
             if (zileiattribute.Rows.Count == 0)
             {
                 this.t1.Visible = false;
                 this.mslable.Visible = true;
             }
             Label aa;
             this.Label2.Text = "当前记录数为<font color='red'><b>" + zileids.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.zileiattribute.PageCount + "</b></font>页显示.";

             for (int i = 0; i <= zileiattribute.Rows.Count - 1; i++)
             {
                 DataRowView mydrv = zileids.Tables["cu"].DefaultView[i];
                 if (Convert.ToString(mydrv["AttributeFL"]).Trim() == "1")
                 {
                     aa = (Label)zileiattribute.Rows[i].FindControl("lb");
                     aa.Text = "属性";
                 }
                 if (Convert.ToString(mydrv["AttributeFL"]).Trim() == "2")
                 {
                     aa = (Label)zileiattribute.Rows[i].FindControl("lb");
                     aa.Text = "配件";
                 }
             }
             zileisdr.Dispose();
             break;



         case "Delzilei":
             string sm = Request.QueryString["SmallAttribute"].ToString().Trim();
             SqlCommand smD = new SqlCommand("delete from SmallProductAttribute where id="+LBid,conn);
             smD.ExecuteNonQuery();
             SqlCommand sM = new SqlCommand("select id from SmallProductAttribute where SmallAttribute='" + sm + "' and attvalue='novalue'", conn);
             string iddd = sM.ExecuteScalar().ToString().Trim();
             Response.Redirect("ProductAttributeSmallEdit.aspx?id="+iddd+"&do=zilei&SmallAttribute="+sm);
             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 BigProductAttribute  set BigAttribute='" + LB + "',Sort=" + Sort + " where id=" + LBid2, conn);
        cmd2.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("ProductAttribute.aspx");
        
    }

    //添加属性
    protected void LinkButton2_Click(object sender, EventArgs e)
    {

        string id = this.attlbbind.SelectedValue.ToString().Trim();//子类ID
        string attvalue = Request["attvalue"].ToString().Trim();
        //通过上述ID找到其他值
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand combigname = new SqlCommand();
        combigname.CommandText = "select * from SmallProductAttribute where id=" + id;
        combigname.Connection=conn;
        SqlDataReader sdratt2 = combigname.ExecuteReader();
        sdratt2.Read();
        string SmallAttribute=sdratt2.GetString(1).Trim();
        string BigAttributeId = Convert.ToString(sdratt2.GetInt32(2)).Trim();
        string BigAttributeName = sdratt2.GetString(3).Trim();
        string AttributeFL = Convert.ToString(sdratt2.GetInt32(4)).Trim();
        int Sort = sdratt2.GetInt32(10);
        
        Sort = Sort + 1;
        sdratt2.Close();
        //添加
        SqlCommand cmd2 = new SqlCommand("insert into SmallProductAttribute(SmallAttribute,BigAttributeId,BigAttributeName,AttributeFL,attvalue,Sort) values ('" + SmallAttribute + "'," + BigAttributeId + ",'" + BigAttributeName + "'," + AttributeFL + ",'" + attvalue + "'," + Sort + ")", conn);
        cmd2.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("ProductAttributeSmall.aspx");
    
    }
    //添加配件
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        string id = this.attlbind2.SelectedValue.ToString().Trim();//子类ID
        string attvalue = Request["attvalue2"].ToString().Trim();
        string Weight = Request["Weight"].ToString().Trim();
        string Price = Request["price"].ToString().Trim();
        string Stock = Request["Stock"].ToString().Trim();
        Response.Write(id);
        //通过上述ID找到其他值
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand combigname = new SqlCommand();
        combigname.CommandText = "select * from SmallProductAttribute where id=" + id;
        combigname.Connection = conn;
        SqlDataReader sdratt2 = combigname.ExecuteReader();
        sdratt2.Read();
        string SmallAttribute = sdratt2.GetString(1).Trim();
        string BigAttributeId = Convert.ToString(sdratt2.GetInt32(2)).Trim();
        string BigAttributeName = sdratt2.GetString(3).Trim();
        string AttributeFL = Convert.ToString(sdratt2.GetInt32(4)).Trim();
        int Sort = sdratt2.GetInt32(10);

        Sort = Sort + 1;
        sdratt2.Close();
        //添加
        SqlCommand cmd2 = new SqlCommand("insert into SmallProductAttribute(SmallAttribute,BigAttributeId,BigAttributeName,AttributeFL,attvalue,Stock,Price,Weight,Sort) values ('" + SmallAttribute + "'," + BigAttributeId + ",'" + BigAttributeName + "'," + AttributeFL + ",'" + attvalue + "'," + Stock + "," + Price + ",'" + Weight + "'," + Sort + ")", conn);
        cmd2.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("ProductAttributeSmall.aspx");

    }

    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        //取修改值
        string ddllb = this.ddllb.SelectedValue.ToString().Trim();
        string LBName = Request["LBName"].ToString().Trim();
        string ddlsmfl = this.ddlsmfl.SelectedValue.ToString().Trim();
        string SortEdit = Request["SortEdit"].ToString().Trim();
        string SmallAttributeEdit = Request.QueryString["SmallAttribute"].ToString().Trim();


        //通过ddllb大类的ID找出大类表中BigProductAttribute中大类名称
       SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand updateAtt = new SqlCommand("select BigAttribute from BigProductAttribute where id=" + ddllb, conn);
        string bigAttribute = updateAtt.ExecuteScalar().ToString().Trim();
 
        
        if (ddlsmfl == "1")
        {
            SqlCommand cmd3 = new SqlCommand("update SmallProductAttribute set SmallAttribute='" + LBName + "',BigAttributeId=" + ddllb + ",BigAttributeName='" + bigAttribute + "',AttributeFL=" + ddlsmfl + ",Price=0,Weight=0,Stock=100 where SmallAttribute='" + SmallAttributeEdit + "'", conn);


            cmd3.ExecuteNonQuery();
            conn.Close();
  
        }

        else
        {
            SqlCommand cmd3 = new SqlCommand("update SmallProductAttribute set SmallAttribute='" + LBName + "',BigAttributeId=" + ddllb + ",BigAttributeName='" + bigAttribute + "',AttributeFL=" + ddlsmfl + " where SmallAttribute='" + SmallAttributeEdit + "'", conn);


            cmd3.ExecuteNonQuery();
            conn.Close();
           
        }
        Response.Redirect("ProductAttributeSmall.aspx");
    }

    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        string idD = Request.QueryString["id"].ToString().Trim();
        string updatasxvalue = Request["TextBox2"].ToString().Trim();

        SqlCommand updatesx = new SqlCommand("update SmallProductAttribute set attvalue='" + updatasxvalue+ "' where id="+idD,conn);
        updatesx.ExecuteNonQuery();

        string sm2 = Request["SmallAttribute"].ToString().Trim();

        SqlCommand sM2 = new SqlCommand("select id from SmallProductAttribute where SmallAttribute='" + sm2 + "' and attvalue<>'novalue'", conn);
        string iddd2 = sM2.ExecuteScalar().ToString().Trim();
        Response.Redirect("ProductAttributeSmallEdit.aspx?id=" + iddd2 + "&do=zilei&SmallAttribute=" + sm2);
        conn.Close();

    }
    protected void LinkButton9_Click(object sender, EventArgs e)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        string idD2 = Request.QueryString["id"].ToString().Trim();
        string namE= Request["TextBox3"].ToString().Trim();
        string weighT = Request["TextBox4"].ToString().Trim();
        string pricE = Request["TextBox5"].ToString().Trim();
        decimal pricE2 = Convert.ToDecimal(pricE);
        string stocK = Request["TextBox6"].ToString().Trim();



        SqlCommand updatesx2 = new SqlCommand("update SmallProductAttribute set attvalue='" + namE + "',Price=" + pricE2 + ",Stock=" + stocK + "  where id=" + idD2, conn);
        updatesx2.ExecuteNonQuery();

        string sm22 = Request["SmallAttribute"].ToString().Trim();

        SqlCommand sM22 = new SqlCommand("select id from SmallProductAttribute where SmallAttribute='" + sm22 + "' and attvalue<>'novalue'", conn);
        string iddd22 = sM22.ExecuteScalar().ToString().Trim();
        Response.Redirect("ProductAttributeSmallEdit.aspx?id=" + iddd22 + "&do=zilei&SmallAttribute=" + sm22);
        conn.Close();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        zileiattribute.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 < zileiattribute.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 <= zileiattribute.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)zileiattribute.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void LinkButton12_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= zileiattribute.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)zileiattribute.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)
            {
                string sqlstr = "delete from SmallProductAttribute where id=" + zileiattribute.DataKeys[i].Value;
                Doadmin.Execsql(sqlstr);
            }
        }
        this.databind2();
    }
    protected void LinkButton11_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= zileiattribute.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)zileiattribute.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }

}
