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 DefendPROC : 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=\"DefendPROC.aspx\">防垃圾策略</a>";

                BindList();

                LoadConfig();
            }
        }
        private void LoadConfig()
        {
            CB_CheckPost.SelectedIndex = (DefendPROCConfig.CheckPost == true ? 0 : 1);
            CB_CheckCN.SelectedIndex = (DefendPROCConfig.CheckCN == true ? 0 : 1);
            NTB_LinkNum.Text = DefendPROCConfig.LinkNum.ToString();
            NTB_PostSpeed.Text = DefendPROCConfig.PostSpeed.ToString();
        }


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

            s_model.WHERE = "Ban=1";
            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_Save_Click(object sender, EventArgs e)
        {
            try
            {
                DefendPROCConfig.CheckPost = (CB_CheckPost.SelectedIndex == 0 ? true : false);
                DefendPROCConfig.CheckCN = (CB_CheckCN.SelectedIndex == 0 ? true : false);

                DefendPROCConfig.LinkNum = int.Parse(NTB_LinkNum.Text);
                DefendPROCConfig.PostSpeed = int.Parse(NTB_PostSpeed.Text);

                MsgBox.Alert("保存成功！", UpdatePanel1);
            }
            catch(Exception ex)
            {
                MsgBox.Alert("保存错误！" + ex.Message.Replace(@"\", @"\\"), UpdatePanel1);
            }

        }


        protected void BT_AddBL_Click(object sender, EventArgs e)
        {
            Add(0, TB_Find.Text.Trim());
        }


        #region 添加或修改
        private void Add(int ID, string Find)
        {

            WordsInfo model = new WordsInfo();
            model.ID = ID;
            model.Find = Find;
            model.Replacement = "";
            model.Ban = 1;
            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();

            Add(id, Find);

            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[3];
                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();
        }



    }
}