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 IpBlackList : 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=\"IpBlackList.aspx\">IP屏蔽管理</a>";


                BindList();

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

            s_model.WHERE = "1=1";
            s_model.PageSize = 10;

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

            MyJavascript.RegJS(UpdatePanel1, "attachMouseEvent('" + this.GV_List.ClientID + "');");
        }
        protected void BT_AddBL_Click(object sender, EventArgs e)
        {
            IPBlackListInfo model = new IPBlackListInfo();
            model.ID = 0;
            if (RBL_Select.Items[0].Selected == true)
            {
                if (TB_IP.Text.Contains("*"))
                {
                    model.IPStart = TB_IP.Text.Replace("*", "1").Trim();
                    model.IPEnd = TB_IP.Text.Replace("*", "255").Trim();
                }
                else
                {
                    model.IPStart = TB_IP.Text.Trim();
                    model.IPEnd = TB_IP.Text.Trim();
                }
            }
            else
            {
                model.IPStart = TB_IP0.Text.Trim();
                model.IPEnd = TB_IP1.Text.Trim();
            }
            model.AdminName = AdminLoginInfo.UserName;


            string strErr = null;

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

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

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

            IPBlackListInfo model = new IPBlackListInfo();
            model.ID = id;
            model.IPStart = IP0;
            model.IPEnd = IP1;
            model.AdminName = AdminLoginInfo.UserName;

            string strErr = null;

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

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
                return;
            }
            else
            {
                GV_List.EditIndex = -1;
                BindList();
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

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

            new IPBlackListManage().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();

        }
    }
}