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 sql;
public partial class Admin_bookDetailsManger : System.Web.UI.Page
{
    SqlDB sqldb = new SqlDB();
    Operation operate = new Operation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Admin"] == null)
            {
                Response.Redirect("index.aspx");
            }
            P2.Visible = false;
            P1.Visible = false;
            GridViewBind();
        }
    }
    /// <summary>
    /// 绑定GridView显示列表
    /// </summary>
    private void GridViewBind()
    {
        string strSql = "select * from guestBookClass";
        operate.GridViewBind(GridView1, strSql);
    }
    /// <summary>
    /// 准备执行插入命令
    /// </summary>
    //protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "Insert")
    //    {
    //        P2.Visible = true;
    //        P1.Visible = false;   
    //    }
    //}
    /// <summary>
    /// 执行翻页命令
    /// </summary>
    protected void GridView1_OnPageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBind();
    }
    /// <summary>
    /// 执行删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string strSql = "delete from guestbookClass where Id=" + id;
        string AriticleId = sqldb.ExecuteScalar("select AriticleId from guestbookClass where id=" + id);
        if(sqldb.ExecuteNonQueryCheck(strSql)>0)
        {
            strSql = "update guestbook set recallhits=recallhits-1 where AriticleId=" + AriticleId;
            if(sqldb.ExecuteNonQueryCheck(strSql)>0)
            {
                GridViewBind();
                sql.Operation.Show_Msg("恭喜您，回复删除成功！");
            }
        }
        else
        {
            sql.Operation.Show_Msg("回复删除失败，请重新删除！");                
        }       
    }
    /// <summary>
    /// 准备执行编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        P1.Visible = true;
        P2.Visible = false;
        string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
        ViewState["id"] = id;
        string strSql = "select * from GuestbookClass where Id=" + id;
        DataSet ds = sqldb.GetDS(strSql);
        txtTitleEdit.Text = ds.Tables[0].Rows[0]["Title"].ToString();
        txtContentEdit.Text = ds.Tables[0].Rows[0]["content"].ToString();
        ViewState["addId"] = ds.Tables[0].Rows[0]["AriticleId"].ToString(); 
        ds.Clear();

    }
    /// <summary>
    /// 取消添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancelAdd_Click(object sender, EventArgs e)
    {
        P2.Visible = false;
    }
    /// <summary>
    /// 取消编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCanelEdit_Click(object sender, EventArgs e)
    {
        P1.Visible = false;
    }
    /// <summary>
    /// 执行添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //执行插入操作
        string userName = Session["Admin"].ToString();
        string AriticleId = ViewState["addId"].ToString();
        string title = sqldb.CheckString(txtTitle.Text);
        string content = sqldb.CheckString(txtContent.Text);
        string Ip = Request.UserHostAddress.ToString();
        string values = "'" + AriticleId + "','" + userName + "','" + title + "','" + content + "','" + Ip + "','" + DateTime.Now + "'";
        string strSql = "insert into guestbookclass (Ariticleid,username,title,content,ip,datetime) values(" + values + ") ";
        if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
        {
            strSql = "update guestbook set recallhits=recallhits+1,lastuser='" + userName + "',lasttime='" + DateTime.Now + "' where AriticleId=" + AriticleId;
            sqldb.ExecuteNonQuery(strSql);
            GridViewBind();
            sql.Operation.Show_Msg("恭喜您，回复成功");
            //清空个别文本框
            txtContent.Text = "";
            P2.Visible = false;
        }
        else
        {
            sql.Operation.Show_Msg("回复失败，请重试！"); 
        }       
    }
    /// <summary>
    /// 执行编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        //执行插入操作
        string userName = Session["Admin"].ToString();
        string AriticleId = ViewState["addId"].ToString();
        string title = sqldb.CheckString(txtTitleEdit.Text);
        string content = sqldb.CheckString(txtContentEdit.Text);
        string Ip = Request.UserHostAddress.ToString();
        //string values = "'" + AriticleId + "','" + userName + "','" + title + "','" + content + "','" + Ip + "','" + DateTime.Now + "'";
        //string strSql = "insert into guestbookclass (Ariticleid,username,title,content,ip,datetime) values(" + values + ") ";
        string strSql = "update guestbookClass set title='" + title + "',content='" + content + "',datetime='" + DateTime.Now + "'where id=" + ViewState["id"].ToString();
        if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
        {
            strSql = "update guestbook set lasttime='" + DateTime.Now + "' where AriticleId=" + AriticleId;
           if(sqldb.ExecuteNonQueryCheck(strSql)>0)
           {
                GridViewBind();
                sql.Operation.Show_Msg("恭喜您，修改回复成功");
                //清空个别文本框
                txtContentEdit.Text = "";
                P1.Visible = false;
           }
        }
        else
        {
            sql.Operation.Show_Msg("修改回复失败，请重试！");
        } 
    }
    protected void lbtnAdd_Command(object sender, CommandEventArgs e)
    {
        P2.Visible = true;
        P1.Visible = false;
        ViewState["addId"]= e.CommandArgument.ToString();
        string strSql = "select title from guestBook where Ariticleid=" + e.CommandArgument.ToString();
        txtTitle.Text = "回复：" + sqldb.ExecuteScalar(strSql);
    }
}
