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_NewsManger : 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();
        }
    }
    private void GridViewBind()
    {
        string strSql = "select * from news";
        operate.GridViewBind(GridView1, strSql);
    }
    protected void btnAddNews_Click(object sender, EventArgs e)
    {
        string strSql = "select * from news where newsTitle='" + txtNewsTitle.Text + "'";
        if (sqldb.ExecuteScalar(strSql).Length > 0)
        {
            //该管理员已存在、不能重复添加
            sql.Operation.Show_Msg("该标题已存在、不能重复添加");            
        }
        else
        {
            //可以添加
            string title= sqldb.CheckString(txtNewsTitle.Text.Trim());
            string titleCut;
            if (title.Length > 13)
            {
                titleCut = title.Substring(0, 13) + "...";
            }
            else
            {
                titleCut = title;
            }
            string Value = "'" +title + "','" +titleCut + "','" + sqldb.CheckString(txtNewsDetails.Text)+"','"+DateTime.Now+"'";
            strSql = "insert into news(newsTitle,newsTitleCut,newsDetails,newsTime) values(" + Value + ")";
            if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
            {
                txtNewsTitle.Text = "";
                txtNewsDetails.Text = "";
                P2.Visible = false;
                GridViewBind();
                sql.Operation.Show_Msg("新闻添加成功！");                
            }
            else
            {
                sql.Operation.Show_Msg("新闻添加失败！");
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            P2.Visible = true;
            P1.Visible = false;
        }
    }
    protected void GridView1_OnPageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string strSql = "delete from news where newsId=" + id;
        if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
        {
            Response.Write("<script language='javaScript'>alert('新闻删除成功！');</script>");
            GridViewBind();
        }
        else
        {
            Response.Write("<script language='javaScript'>alert('新闻删除失败，请重新删除！');</script>");           
        }
    }
    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 news where newsId=" + id;
        DataSet ds = sqldb.GetDS(strSql);
        txtEditTitle.Text = ds.Tables[0].Rows[0]["newsTitle"].ToString();
        txtEditDetails.Text = ds.Tables[0].Rows[0]["newsDetails"].ToString();        
        ds.Clear();
       
    }
    protected void btnCancelAdd_Click(object sender, EventArgs e)
    {
        P2.Visible = false;
    }
    protected void btnCanelEdit_Click(object sender, EventArgs e)
    {
        P1.Visible = false;
    }
    protected void btnEditNews_Click(object sender, EventArgs e)
    {
        string title = sqldb.CheckString(txtEditTitle.Text.Trim());
        string titleCut;
        if (title.Length > 13)
        {
            titleCut = title.Substring(0, 13) + "...";
        }
        else
        {
            titleCut = title;
        }
        string strSql = "update news set newsTitle='" + title + "',newsTitleCut='" + titleCut + "',newsDetails='" + sqldb.CheckString(txtEditDetails.Text) + "',newsTime='" + DateTime.Now + "' where newsId=" + ViewState["id"].ToString();
        if (sqldb.ExecuteNonQueryCheck(strSql) > 0)
        {
            sql.Operation.Show_Msg("新闻更新成功！"); 
            P1.Visible = false;
            GridViewBind();
        }
        else
        {
            sql.Operation.Show_Msg("新闻更新失败，请重新操作！");           
        }
    }
}
