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_FlwX : System.Web.UI.Page
{ 
    private string toadd = "├";
    private string latxt = "";
    DataTable TB = new DataTable();
    DataTable TB2 = new DataTable();
    ImageButton img;
    Label la;
    Image imgline;
    private  int lcount;
    private int xcount=5;//限制级别
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!Page.IsPostBack)
        {  
            TB.Columns.Add("ID",typeof(int));
            TB.Columns.Add("NAME",typeof(string));
            TB.Columns.Add("NAMET", typeof(string));
            TB.Columns.Add("FID",typeof(int));
            this.DropDownList1.Items.Insert(0, new ListItem("根目录", "0"));
            this.GetArticleCategory("0");

        }
        this.LinkButton1.Click += new EventHandler(LinkButton1_Click);
        TB2.Columns.Add("ID", typeof(int));
        TB2.Columns.Add("NAME", typeof(string));
        TB2.Columns.Add("NAMET", typeof(string));
        TB2.Columns.Add("FID", typeof(int));
        this.GetArticleCategory2("0");
        this.GridView1.DataSource = TB2;
        this.GridView1.DataBind();
            if (Request.QueryString["zk"] == null)
            {
                for (int i = 0; i < TB2.Rows.Count; i++)
                {
                    if (TB2.Rows[i][3].ToString() != "0")
                    {
                        this.GridView1.Rows[i].Visible = false;

                    }
                    else
                    {
                        img = (ImageButton)this.GridView1.Rows[i].FindControl("open1");
                        img.ImageUrl = "~/SYSadmin/image/tree/open.gif";
                        img.PostBackUrl = "FlwX.aspx?zk=open&idd=" + TB2.Rows[i][0];
                    }

                }
            }
            else
            {
                for (int i = 0; i < TB2.Rows.Count; i++)
                {
                    if (TB2.Rows[i][3].ToString() != "0")
                    {
                        this.GridView1.Rows[i].Visible = false;

                    }
                    else
                    {

                        if (TB2.Rows[i][0].ToString() == Request.QueryString["idd"])
                        {
                            img = (ImageButton)this.GridView1.Rows[i].FindControl("open1");
                            img.ImageUrl = "~/SYSadmin/image/tree/folder.gif";
                            img.PostBackUrl = "FlwX.aspx?zk=open&idd=" + TB2.Rows[i][0];
                        }
                        else
                        {
                            img = (ImageButton)this.GridView1.Rows[i].FindControl("open1");
                            img.ImageUrl = "~/SYSadmin/image/tree/open.gif";
                            img.PostBackUrl = "FlwX.aspx?zk=open&idd=" + TB2.Rows[i][0];
                        }
                    }

                }

                zk(Request.QueryString["idd"]);
            }
        switch (Request.QueryString["type"])
        { 
            case "del":
                SqlConnection conn2 = CDatabase.wtoconn();
                conn2.Open();
                SqlCommand smd2 = new SqlCommand("select count(*) from Articlesgroup where Articlesgroup_parent_id=" + Request.QueryString["id"], conn2);
                int count = Convert.ToInt32(smd2.ExecuteScalar());
                if (count > 0)
                {
                    Response.Write("<script>alert('存在子类，不能删除');window.location.href='FlwX.aspx'</script>");
                }
                else
                {
                    Doadmin.Exec("delete from Articlesgroup where Articlesgroup_id=" + Request.QueryString["id"]);
                    Response.Redirect("FlwX.aspx");
                }
                conn2.Close();
                break;
            case "edit":
                if (!IsPostBack)
                {
                    this.Groupname.Text = Request.QueryString["namet"];
                    this.DropDownList1.SelectedValue = Request.QueryString["id"];
                }
                this.LinkButton1.Click+=new EventHandler(eidt);
                break;
        }

    }
    private  void GetArticleCategory(string pid)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        string sql = "select Articlesgroup_id,Groupname,Articlesgroup_parent_id from Articlesgroup where Articlesgroup_parent_id=" + pid + " order by Groupname";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            DataRow dr = TB.NewRow();
            dr[0] = sdr.GetInt32(0);
            dr[1] = toadd + " " + sdr.GetString(1);
            dr[2] = sdr.GetString(1);
            dr[3] = sdr.GetInt32(2);
            TB.Rows.Add(dr);
            this.DropDownList1.Items.Add(new ListItem(toadd + " " + sdr.GetString(1),Convert.ToString(sdr.GetInt32(0))));
            toadd += "─┴";
            this.GetArticleCategory(Convert.ToString(sdr.GetInt32(0)));
            toadd = toadd.Substring(0, toadd.Length - 2);  //开始这里没有处理好，层次关系显示不出来
        }
        sdr.Close();
        conn.Close();
    }


    private void zk(string pid)
    {
            for (int i = 0; i < TB2.Rows.Count; i++)
            {
                if (TB2.Rows[i][3].ToString() == pid)
                {   
                    this.GridView1.Rows[i].Visible = true;
                    img = (ImageButton)this.GridView1.Rows[i].FindControl("open1");
                    la = (Label)this.GridView1.Rows[i].FindControl("Label2");
                    la.Text = latxt;
                    latxt += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    img.ImageUrl = "~/SYSadmin/image/tree/joinbottom.gif";
                    zk(TB2.Rows[i][0].ToString());
                    latxt = latxt.Substring(0, 0);
                }
            }
    }


    private void GetArticleCategory2(string pid)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        string sql = "select Articlesgroup_id,Groupname,Articlesgroup_parent_id from Articlesgroup where Articlesgroup_parent_id=" + pid + " order by Groupname";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            DataRow dr2 = TB2.NewRow();
            dr2[0] = sdr.GetInt32(0);
            dr2[1] = toadd + " " + sdr.GetString(1);
            dr2[2] = sdr.GetString(1);
            dr2[3] = sdr.GetInt32(2);
            TB2.Rows.Add(dr2);
            toadd += "─┴";
            this.GetArticleCategory2(Convert.ToString(sdr.GetInt32(0)));
            toadd = toadd.Substring(0, toadd.Length - 2);  //开始这里没有处理好，层次关系显示不出来
        }
        sdr.Close();
        conn.Close();
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (lcount < xcount)
        {
            if (Doadmin.Exec("insert into Articlesgroup(Groupname,Articlesgroup_parent_id) values('" + this.Groupname.Text + "'," + this.DropDownList1.SelectedValue.ToString() + ")"))
            {
                Response.Write("<script>alert('添加类成功');window.location.href='FlwX.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('此次操作失败');window.location.href='FlwX.aspx'</script>");
            }
        }
        else
        {          
            Response.Write("<script>alert('"+lcount+"')</script>");
            Response.Write("<script>alert('此次操作失败\\n类别分类超过！');window.location.href='FlwX.aspx'</script>");

        }

    }


    protected void eidt(object sender, EventArgs e)
    {
        if (this.DropDownList1.SelectedValue != Request.QueryString["id"])
        {

            if (Doadmin.Exec("update Articlesgroup set Groupname='" + this.Groupname.Text + "',Articlesgroup_parent_id=" + this.DropDownList1.SelectedValue + " where Articlesgroup_id=" + Request.QueryString["id"]))
            {
                Response.Write("<script>alert('修改成功');window.location.href='FlwX.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('此次操作失败');window.location.href='FlwX.aspx'</script>");
            }
        }
        else
        {
            Doadmin.Exec("update Articlesgroup set Groupname='" + this.Groupname.Text + "' where Articlesgroup_id=" + Request.QueryString["id"]);
            Response.Redirect("FlwX.aspx");
        }
    }
    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[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }
    }
}
