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_ProductAttribute : System.Web.UI.Page
{
    public string sqlwhere="";
    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton2.Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {
            SqlConnection conn = CDatabase.wtoconn();
            conn.Open();
            SqlCommand smdlb;
            SqlDataAdapter sdalb;
            DataSet dslb;
            DataTable tb;
            DataRow dr;
            SqlDataReader sdrlb;
            tb = new DataTable();
            tb.Columns.Add("id", typeof(int));
            tb.Columns.Add("BigCategory", typeof(string));
            tb.Columns.Add("sort", typeof(int));
            smdlb = new SqlCommand("select * from BigProductCategory where id<>10000034 order by Sort", conn);
            dr = tb.NewRow();
            dr["id"] = 0;
            dr["BigCategory"] = "请选择大类产品";
            dr["sort"] = 0;
            tb.Rows.Add(dr);
            sdrlb = smdlb.ExecuteReader();
            while (sdrlb.Read())
            {
                dr = tb.NewRow();
                dr["id"] = sdrlb["id"].ToString();
                dr["BigCategory"] = sdrlb["BigCategory"].ToString();
                dr["sort"] = sdrlb["sort"].ToString();
                tb.Rows.Add(dr);
            }
            sdrlb.Close();
            Doadmin.ddldatabind2(BigProduct, tb, "BigCategory", "id");
            if (BigProduct.SelectedValue.ToString() == "0")
            {
                tb = new DataTable();
                tb.Columns.Add("id", typeof(int));
                tb.Columns.Add("SmallCategory", typeof(string));
                tb.Columns.Add("sort", typeof(string));
                dr = tb.NewRow();
                dr["id"] = 0;
                dr["SmallCategory"] = "请选择小类产品";
                dr["sort"] = 0;
                tb.Rows.Add(dr);
                Doadmin.ddldatabind2(smalllb, tb, "SmallCategory", "id");
            }
            conn.Close();
            #region 快捷搜索
                switch (this.sql.SelectedValue.Trim())
                {
                    case "allpro":
                        databind("high");
                        break;
                    case "all":
                        sqlwhere = "all";
                        databind("all");
                        break;
                    case "new":
                        sqlwhere = "new";
                        databind("new");
                        break;
                    case "tj":
                        sqlwhere = "tj";
                        databind("tj");
                        break;
                    case "tjt":
                        sqlwhere = "tjt";
                        databind("tjt");
                        break;
                    case "hot":
                        sqlwhere = "hot";
                        databind("hot");
                        break;
                    case "fping":
                        sqlwhere = "fping";
                        databind("fping");
                        break;
                    case "nofp":
                        sqlwhere = "nofp";
                        databind("nofp");
                        break;
                }
                #endregion
        }
        if (Request.QueryString["id"] != null & Request.QueryString["doing"] != null)
        {
            string doing = Request.QueryString["doing"];
            string sql1 = "";
            string zt = Request.QueryString["zt"].ToString().Trim();
            switch (doing)
            {
                case "isfp":
                    if (zt == "y")
                    {
                        sql1 = "update ProductInfo set ISPublished='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update ProductInfo set ISPublished='y' where id=" + Request.QueryString["id"];
                    }

                    break;
                case "isnew":
                    if (zt == "y")
                    {
                        sql1 = "update ProductInfo set isnew='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update ProductInfo set isnew='y' where id=" + Request.QueryString["id"];
                    }

                    break;
                case "ishot":
                    if (zt == "y")
                    {
                        sql1 = "update ProductInfo set ishot='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update ProductInfo set ishot='y' where id=" + Request.QueryString["id"];
                    }
                    break;
                case "isT":
                    if (zt == "y")
                    {
                        sql1 = "update ProductInfo set IsRecommended='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update ProductInfo set IsRecommended='y' where id=" + Request.QueryString["id"];
                    }
                    break;
                case "isTprice":
                    if (zt == "y")
                    {
                        sql1 = "update ProductInfo set IsSpecials='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update ProductInfo set IsSpecials='y' where id=" + Request.QueryString["id"];
                    }
                    break;
                case "zd":
                    sql1 = "update ProductInfo set AddDate='"+DateTime.Now.ToString()+"' where id=" + Request.QueryString["id"];
                    break;

            }
            Doadmin.Execsql(sql1);
            #region 快捷搜索
            switch (this.sql.SelectedValue.Trim())
            {
                case "allpro":
                    databind("high");
                    break;
                case "all":
                    sqlwhere = "all";
                    databind("all");
                    break;
                case "new":
                    sqlwhere = "new";
                    databind("new");
                    break;
                case "tj":
                    sqlwhere = "tj";
                    databind("tj");
                    break;
                case "tjt":
                    sqlwhere = "tjt";
                    databind("tjt");
                    break;
                case "hot":
                    sqlwhere = "hot";
                    databind("hot");
                    break;
                case "fping":
                    sqlwhere = "fping";
                    databind("fping");
                    break;
                case "nofp":
                    sqlwhere = "nofp";
                    databind("nofp");
                    break;
            }
            #endregion
            }
    }
   public void databind(string whe)
    {
        string sqlstr="";
        switch (whe)
        { 
            case "all":
                sqlstr = "select * from ProductInfo order by AddDate desc";
                break;
            case "new":
                sqlstr = "select * from ProductInfo where IsNew='y' order by AddDate desc";
                break;
            case "tj":
                sqlstr = "select * from ProductInfo where IsSpecials='y'  order by AddDate desc";
                break;
            case "tjt":
                sqlstr = "select * from ProductInfo where IsRecommended='y'  order by AddDate desc";
                break;
            case "hot":
                sqlstr = "select * from ProductInfo where IsHot='y'  order by AddDate desc";
                break;
            case "fping":
                sqlstr = "select * from ProductInfo where ISPublished='y'  order by AddDate desc";
                break;
            case "nofp":
                sqlstr = "select * from ProductInfo where ISPublished='n'  order by AddDate desc";
                break;
            case "high":
                if (this.BigProduct.SelectedValue.Trim().ToString()!="0")
                    sqlstr = "select * from  ProductInfo where BigProductID=" + this.BigProduct.SelectedValue.Trim() + " and SmallProductId=" + this.smalllb.SelectedValue.Trim() + " and Products like '%" + this.pro.Text.Trim() + "%' order by AddDate desc";
                else
                sqlstr = "select * from  ProductInfo where Products like '%" + this.pro.Text.Trim() + "%' order by AddDate desc";
                break;
            case "highuo":
                sqlstr = "select * from  ProductInfo where ProductsNo like '%" + this.huohao.Text.Trim() + "%' order by AddDate desc";
                break;
        }
        LinkButton isfb,isnew, ishot, isT, isTprice;
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sdr=new SqlDataAdapter(sqlstr,conn);
        DataSet ds=new DataSet();
        sdr.Fill(ds,"cu");
        this.Productmag.DataSource = ds;
        this.Productmag.DataKeyNames = new string[] {"id"};
        this.Productmag.DataBind();
        this.Label1.Text = "当前商品数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.Productmag.PageCount + "</b></font>页显示.当前为<font color='red'><b>"+Convert.ToString(Convert.ToInt32(this.Productmag.PageIndex.ToString())+1)+"</b></font>页";
        if (Productmag.Rows.Count == 0)
        {
            this.mslable.Visible = true;
        }
        else
        {
            this.mslable.Visible = false;
        }
       //图片显示[功能图片]
        SqlCommand smd2;
        SqlDataReader sdr2;
        Image img;
        string txt="";
        for (int i = 0; i <= Productmag.Rows.Count - 1; i++)
        {
            DataRowView mydrv = ds.Tables["cu"].DefaultView[i];

            smd2 = new SqlCommand("select ISPublished,IsNew,IsHot,IsRecommended,IsSpecials,ThumbnailsPic from ProductInfo where id=" + Productmag.DataKeys[i].Value, conn);
            sdr2 = smd2.ExecuteReader();
            sdr2.Read();
            img = (Image)Productmag.Rows[i].FindControl("imgpic");
            img.ImageUrl = "~/upfile/" + sdr2["ThumbnailsPic"];
            txt = "/upfile/" + sdr2["ThumbnailsPic"];
            img.Height = 30;
            img.Width = 50;
            
            img.Attributes.Add("onmousemove", "ab('" + txt + "')");
            img.Attributes.Add("onmouseout", "ac()");
            //是否发布
            if (sdr2.GetString(0)== "y")
            {
                isfb = (LinkButton)Productmag.Rows[i].FindControl("isfp");
                isfb.Text = "√";
                isfb.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=y&doing=isfp";
            }
            if (sdr2.GetString(0) == "n")
            {
                isfb = (LinkButton)Productmag.Rows[i].FindControl("isfp");
                isfb.Text = "×";
                isfb.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=isfp";
            }
            //最新
            if (sdr2.GetString(1) == "y")
            {
                isnew = (LinkButton)Productmag.Rows[i].FindControl("isnew");
                isnew.Text = "√";
                isnew.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=y&doing=isnew";
            }
            if (sdr2.GetString(1) == "n")
            {
                isnew = (LinkButton)Productmag.Rows[i].FindControl("isnew");
                isnew.Text = "×";
                isnew.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=isnew";
            }
            //最热
            if (sdr2.GetString(2) == "y")
            {
                ishot = (LinkButton)Productmag.Rows[i].FindControl("ishot");
                ishot.Text = "√";
                ishot.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=y&doing=ishot";
            }
            if (sdr2.GetString(2) == "n")
            {
                ishot = (LinkButton)Productmag.Rows[i].FindControl("ishot");
                ishot.Text = "×";
                ishot.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=ishot";
            }

            //推荐
            if (sdr2.GetString(3) == "y")
            {
                isT = (LinkButton)Productmag.Rows[i].FindControl("isT");
                isT.Text = "√";
                isT.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=y&doing=isT";
            }
            if (sdr2.GetString(3) == "n")
            {
                isT = (LinkButton)Productmag.Rows[i].FindControl("isT");
                isT.Text = "×";
                isT.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=isT";
            }
            //特价
            if (sdr2.GetString(4) == "y")
            {
                isTprice = (LinkButton)Productmag.Rows[i].FindControl("isTprice");
                isTprice.Text = "√";
                isTprice.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=y&doing=isTprice";

            }
            if (sdr2.GetString(4) == "n")
            {
                isTprice = (LinkButton)Productmag.Rows[i].FindControl("isTprice");
                isTprice.Text = "×";
                isTprice.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=isTprice";
            }

            isTprice = (LinkButton)Productmag.Rows[i].FindControl("LinkButton4");
            isTprice.Text = "置顶";
            isTprice.PostBackUrl = "ProductsList.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=zd";
            sdr2.Close();

        }
       conn.Close();
   }
    protected void Productmag_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < Productmag.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[11].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }

    }
    protected void Productmag_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Productmag.PageIndex = e.NewPageIndex;
        #region 快捷搜索
        switch (this.sql.SelectedValue.Trim())
        {
            case "allpro":
                databind("high");
                break;
            case "all":
                sqlwhere = "all";
                databind("all");
                break;
            case "new":
                sqlwhere = "new";
                databind("new");
                break;
            case "tj":
                sqlwhere = "tj";
                databind("tj");
                break;
            case "tjt":
                sqlwhere = "tjt";
                databind("tjt");
                break;
            case "hot":
                sqlwhere = "hot";
                databind("hot");
                break;
            case "fping":
                sqlwhere = "fping";
                databind("fping");
                break;
            case "nofp":
                sqlwhere = "nofp";
                databind("nofp");
                break;
        }
        #endregion
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= Productmag.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)Productmag.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= Productmag.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)Productmag.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= Productmag.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)Productmag.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)
            {
                string sqlstr = "delete from ProductInfo where id=" + Productmag.DataKeys[i].Value;
                Doadmin.Execsql(sqlstr);
            }
        }
        #region 快捷搜索
        switch (this.sql.SelectedValue.Trim())
        {
            case "allpro":
                databind("high");
                break; ;
            case "all":
                sqlwhere = "all";
                databind("all");
                break;
            case "new":
                sqlwhere = "new";
                databind("new");
                break;
            case "tj":
                sqlwhere = "tj";
                databind("tj");
                break;
            case "tjt":
                sqlwhere = "tjt";
                databind("tjt");
                break;
            case "hot":
                sqlwhere = "hot";
                databind("hot");
                break;
            case "fping":
                sqlwhere = "fping";
                databind("fping");
                break;
            case "nofp":
                sqlwhere = "nofp";
                databind("nofp");
                break;
        }
        #endregion
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        #region 快捷搜索
        switch (this.sql.SelectedValue.Trim())
        {
            case "allpro":
                databind("high");
                break;
            case "all":
                sqlwhere = "all";
                databind("all");
                break;
            case "new":
                sqlwhere = "new";
                databind("new");
                break;
            case "tj":
                sqlwhere = "tj";
                databind("tj");
                break;
            case "tjt":
                sqlwhere = "tjt";
                databind("tjt");
                break;
            case "hot":
                sqlwhere = "hot";
                databind("hot");
                break;
            case "fping":
                sqlwhere = "fping";
                databind("fping");
                break;
            case "nofp":
                sqlwhere = "nofp";
                databind("nofp");
                break;
        }
        #endregion
    }
    protected void BigProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn;
        conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smdlb;
        SqlDataAdapter sdalb;
        DataSet dslb;
        DataTable tb;
        DataRow dr;
        SqlDataReader sdrlb;
        if (this.BigProduct.SelectedValue.ToString() != "0")
        {
            tb = new DataTable();
            tb.Columns.Add("id", typeof(int));
            tb.Columns.Add("SmallCategory", typeof(string));
            tb.Columns.Add("sort", typeof(int));
            smdlb = new SqlCommand("select * from SmallProductCategory where BigCategoryId=" + this.BigProduct.SelectedValue + " order by Sort", conn);
            sdrlb = smdlb.ExecuteReader();
            while (sdrlb.Read())
            {
                dr = tb.NewRow();
                dr["id"] = sdrlb["id"].ToString();
                dr["SmallCategory"] = sdrlb["SmallCategory"].ToString();
                dr["sort"] = sdrlb["sort"].ToString();
                tb.Rows.Add(dr);
            }
            sdrlb.Close();
            Doadmin.ddldatabind2(smalllb, tb, "SmallCategory", "id");
        }
        else
        {
            tb = new DataTable();
            tb.Columns.Add("id", typeof(int));
            tb.Columns.Add("SmallCategory", typeof(string));
            tb.Columns.Add("sort", typeof(int));
            dr = tb.NewRow();
            dr["id"] = 0;
            dr["SmallCategory"] = "请选择小类产品";
            dr["sort"] = 0;
            tb.Rows.Add(dr);
            Doadmin.ddldatabind2(smalllb, tb, "SmallCategory", "id");
        }



    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        this.sql.SelectedValue = "allpro";
        databind("high");
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        this.sql.SelectedValue = "allpro";
        this.pro.Text = "";
        databind("highuo");
    }
}
