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;
using System.Web.Configuration;
public partial class Bloger_LogManage_CategoryManage : System.Web.UI.Page
{
    private DataBaseConnection DBConn;
    private string categoryName;
    private string oldCategoryId;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void SqlDataSource2_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        e.Command.Parameters["@bloger_name"].Value = this.User.Identity.Name;

        e.Command.Parameters["@category_name"].Value = this.TextBox1.Text;
    }
    
    protected void InsertButton_Click(object sender, EventArgs e)
    {
    }

    protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        e.Command.Parameters["@bloger_name"].Value = this.User.Identity.Name;
    }

    // 删除标签
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        
    }

    // 插入新标签
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        categoryName = this.TextBox1.Text;

        string connStr = WebConfigurationManager.ConnectionStrings["DesertEagleConnectionString"].ToString();
        this.DBConn = new DataBaseConnection();
        this.DBConn.Open(connStr);

        if (this.DBConn.CheckData("DE_category" , "category_name" , categoryName))
        {
            Response.Write("<script>alert('此标签已存在')</script>");
        }
        else if (categoryName == "")
        {
            Response.Write("<script>alert('标签名不能为空')</script>");
        }
        else
        {
            this.SqlDataSource2.Insert();
        }

        this.DBConn.Close();

        this.TextBox1.Text = "";
    }

    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        
    }

    // 删除2 标签
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {


    }
    protected void SqlDataSource2_Deleting(object sender, SqlDataSourceCommandEventArgs e)
    {
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int index = e.NewSelectedIndex;    //被删除标签所在行
        oldCategoryId = ((Label)this.GridView1.Rows[index].Cells[0].FindControl("Label1")).Text;
        //得到被删除标签的id

        string userName = this.User.Identity.Name; //用户名

        // 连接字符串connStr
        string connStr = WebConfigurationManager.ConnectionStrings["DesertEagleConnectionString"].ToString();

        SqlConnection sqlConn = new SqlConnection(connStr);
        if (sqlConn.State == ConnectionState.Closed)
        {
            sqlConn.Open();
        }
        string sqlCmdStr = "SELECT * FROM DE_category WHERE bloger_name = '" + userName + "' AND category_name = '默认'";
        SqlDataAdapter SqlAdapter = new SqlDataAdapter(sqlCmdStr, sqlConn);
        DataSet Ds = new DataSet();
        SqlAdapter.Fill(Ds);

        String newCategoryId = null;
        foreach (DataRow row in Ds.Tables[0].Rows)
        {
            newCategoryId = row["category_id"].ToString();
            break;
        }
        
        // updata
        sqlCmdStr = "UPDATE DE_logs SET category_id = " + newCategoryId + " WHERE category_id = " + oldCategoryId;
        SqlCommand sqlCom = new SqlCommand(sqlCmdStr, sqlConn);
        sqlCom.ExecuteNonQuery();

        sqlCmdStr = "DELETE FROM DE_category WHERE category_id = " + oldCategoryId; ;
        SqlCommand sqlCom2 = new SqlCommand(sqlCmdStr, sqlConn);
        sqlCom2.ExecuteNonQuery();

        this.GridView1.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
       // gridview的更新按钮单击时激发
        for (int i = 0; i < this.GridView1.Controls[0].Controls.Count; i++)
        {
            if (this.GridView1.Controls[0].Controls[i].FindControl("TextBox1") != null)
            {
                categoryName = ((TextBox)(this.GridView1.Controls[0].Controls[i].FindControl("TextBox1"))).Text;
                break;
            }
        }
        string connStr = WebConfigurationManager.ConnectionStrings["DesertEagleConnectionString"].ToString();
        this.DBConn = new DataBaseConnection();
        this.DBConn.Open(connStr);

        if (this.DBConn.CheckData("DE_category", "category_name", categoryName))
        {
            Response.Write("<script>alert('此标签已存在')</script>");
            for (int i = 0; i < this.GridView1.Controls[0].Controls.Count; i++)
            {
                if (this.GridView1.Controls[0].Controls[i].FindControl("TextBox1") != null)
                {
                    ((TextBox)(this.GridView1.Controls[0].Controls[i].FindControl("TextBox1"))).Text = "";// oldcategoryName;
                    break;
                }
            }
        }

        else
        {
            ;
        } // End of if
        this.DBConn.Close();
    }
    protected void LinkButton1_Load(object sender, EventArgs e)
    {
    }
}