using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.Model;
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Admin.ControlPanel
{
    public partial class WordBlackList : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LB_Path.Text = Config.SiteName + " → 控制面板 →  <a href=\"WordBlackList.aspx\">关键字过滤</a>";


                BindList();

            }
        }
        private void BindList()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);

            s_model.WHERE = "Ban=0";
            s_model.PageSize = 10;

            new TopWinCMS.BLL.WordsManage().BindList(GV_List, PageNavigation1, s_model);

            MyJavascript.RegJS(UpdatePanel1, "attachMouseEvent('" + this.GV_List.ClientID + "');");
        }
        protected void BT_AddBL_Click(object sender, EventArgs e)
        {
            Add(0, TB_Find.Text.Trim(), TB_Replacement.Text.Trim());

            BindList();
        }

        #region 添加或修改
        private void Add(int ID, string Find, string Replacement)
        {
            WordsInfo model = new WordsInfo();
            model.ID = ID;
            model.Find = Find;
            model.Replacement = Replacement;
            model.Ban = 0;
            model.AdminName = AdminLoginInfo.UserName;

            string strErr = null;

            new WordsManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
                return;
            }
            else
            {
                //BindList();
            }
        }
        #endregion

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = (int)GV_List.DataKeys[e.RowIndex].Value;
            string Find = ((TextBox)GV_List.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
            string Replacement =((TextBox)GV_List.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
            Add(id, Find, Replacement);

            GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string ID = GV_List.DataKeys[e.RowIndex].Value.ToString();

            new WordsManage().Delete(ID, UpdatePanel1);


            MsgBox.Alert("删除成功!", UpdatePanel1);

            BindList();
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_List.EditIndex = e.NewEditIndex;
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell myTableCell = e.Row.Cells[4];
                if (myTableCell.Controls.Count > 0)
                {
                    LinkButton btnDel = (LinkButton)myTableCell.Controls[0];

                    //if 
                    //{
                    btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                    //}
                    //else
                    //{
                    //    btnDel.Attributes.Add("onclick", "alert('您没有删除权限？');return false");
                    //}

                }
            }
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            s_model.PageNo = e.NewPageIndex;
            BindList();
        }
    }
}
