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 sql;

public partial class bookDetails : System.Web.UI.Page
{
    SqlDB sqldb = new SqlDB();
    Operation operate = new Operation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataListDetails();
            DataListReCall();
            getReTitle(); 
            getUserList();
            updatehits();
        }
    }
    /// <summary>
    /// 得到地址栏参数
    /// </summary>
    /// <returns>id</returns>
    private string GetRequest()
    {
        string id;
        if (Request.QueryString["id"]== null)
        {
            id = "1";
            Response.Redirect("err.aspx");
        }
        else
        {
            id = Request.QueryString["id"].ToString();
            
        }
        return id;
    }
    /// <summary>
    /// 更新当前记录的点击次数
    /// </summary>   
    private void updatehits()
    {
        string strSql = "update guestbook set hits=hits+1 where AriticleId="+GetRequest();
        sqldb.ExecuteNonQuery(strSql);
    }
    /// <summary>
    /// 显示留言内容
    /// </summary>
    private void DataListDetails()
    {
        string id= GetRequest();
        string strSql = "select * from guestBook where Ariticleid="+id;
        operate.DataListBind(datalistDetails,strSql);        
    }
    /// <summary>
    /// 绑定回复内容
    /// </summary>
    private void DataListReCall()
    {
        string id = GetRequest();
        string strSql = "select * from guestBookClass where Ariticleid=" + id;
        operate.DataListBind(datalistReCall, strSql);
    }
    /// <summary>
    /// 为回复标题赋初始值
    /// </summary>
    private void getReTitle()
    {
        string id = GetRequest();
        string strSql = "select title from guestBook where Ariticleid=" + id;
        txtTitle.Text = "回复："+sqldb.ExecuteScalar(strSql);
    }
    /// <summary>
    ///将所有管理员添加到下拉列表中，并显示当前管理员基本信息
    /// </summary>
    private void getUserList()
    {
        string strSql = "select * from userInfo";        
        operate.DropDownListBind(ddlUserList, strSql, "userName", "userId");
        //显示当前管理员基本信息
        ControlBind();
    }
    private void ControlBind()
    {
        string id = ddlUserList.SelectedValue.ToString();
        string strSql = "select * from userInfo where userId=" + id;
        SqlDataReader sdr = sqldb.ExecuteReader(strSql);
        lblEmail.Text = sdr["userEmail"].ToString();
        lblPhone.Text = sdr["userPhone"].ToString();
        lblQQ.Text = sdr["QQ"].ToString();
        sdr.Close();
    }
    //选择不同的管理员，重新显示当前管理员基本信息    
    protected void ddlUserList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ControlBind();
    }
    protected void ButTure_Click(object sender, EventArgs e)
    {
        string userName = ddlUserList.SelectedItem.ToString().Trim();
        string pwd =sqldb.CheckString(txtpwd.Text.Trim()); 
        string strSql = "select * from userInfo where userName='"+userName+"' and pwd='"+pwd+"'"; 
        if (sqldb.ExecuteScalar(strSql).Length>0)
        {
            //执行插入操作
            string AriticleId=GetRequest();
            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+"'";
            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=" + GetRequest();
                sqldb.ExecuteNonQuery(strSql);
                lblMessage.Text = "恭喜您，回复成功！";
                DataListDetails();
                DataListReCall();
                //清空个别文本框
                txtContent.Text = "";
            }
            else
            {
                lblMessage.Text = "回复失败，请重试！";
            }
        }
        else
        {
            //不允许插入
            lblMessage.Text = "对不起，回复失败，只有管理员才允许回复！";
            lblMessage.ForeColor = System.Drawing.Color.Sienna;
        }
    }
}
