using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace Stat.web.v
{
    /// <summary>
    /// 
    /// </summary>
    public partial class WebsiteList : System.Web.UI.Page
    {
        Model.PageInfo pageModel = new Stat.Model.PageInfo(100);
        Model.SearchInfo searchModel = new Stat.Model.SearchInfo();
        Stat.Security.ISecurity security = Stat.Security.DataFatory.CreateSecurity();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "站点列表";
            GetWebList();
        }
        /// <summary>
        /// 站点列表
        /// </summary>
        private void GetWebList()
        {
            pageModel.PageIndex = Plugin.Common.Rq_Int("page");
            pageModel.PageIndex = pageModel.PageIndex < 1 ? 1 : pageModel.PageIndex;
            searchModel.UserPower = security.GetUserWebsiteList();

            IList<Model.WebSites> list = Stat.BLL.WebsitePageIndex.GetList(ref pageModel, searchModel);
            IList<Model.WebSites> listGv = new List<Model.WebSites>();
            if (list != null && list.Count > 0)
            {
                int k = ((pageModel.PageIndex - 1) * 10);
                for (int i = k; i < (k + 10); i++)
                {
                    if (i < list.Count)
                    {
                        listGv.Add(list[i]);
                    }
                    else
                    {
                        break;
                    }

                }
            }
            this.GvList.DataSource = listGv;
            this.GvList.DataKeyNames = new string[] { "WebId" };
            this.GvList.DataBind();
            this.LtrPageIndex.Text = JPageIndex.GetIndex.Get(pageModel.RecordCount, pageModel.PageIndex, 10, 0, 1, "");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(this.GvList.DataKeys[e.RowIndex][0].ToString());
            if (Stat.BLL.WebSites.Delete(id))
            {
                Plugin.Common.Alert("成功删除！", "?");
            }
            else
            {
                if (Stat.BLL.WebSites.Delete(id))
                {
                    Plugin.Common.Alert("成功删除！", "?");
                }
                else
                {
                    Plugin.Common.Alert("操作失败！请重复一次。");
                }
            }

        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string url = "Website.aspx?wid=" + this.GvList.DataKeys[e.NewEditIndex][0].ToString();
            Response.Redirect(url, true);
        }

        /// <summary>
        /// 其他命令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "GetCode")
            {
                string url = "statCode.aspx?wid=" + e.CommandArgument.ToString();
                Response.Redirect(url, true);
            }
            else if (e.CommandName == "ClearAll")
            {
                if (Stat.BLL.WebSites.ClearAll(int.Parse(e.CommandArgument.ToString())))
                {
                    Plugin.Common.Alert("成功清零！", "?");
                }
                else
                {
                    if (Stat.BLL.WebSites.ClearAll(int.Parse(e.CommandArgument.ToString())))
                    {
                        Plugin.Common.Alert("成功清零！", "?");
                    }
                    else
                    {
                        Plugin.Common.Alert("操作失败！请重复一次。");
                    }
                }
            }
            else if (e.CommandName == "SetContent")
            {
                string url = "WebsiteStructure.aspx?wid=" + e.CommandArgument.ToString();
                Response.Redirect(url, true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                //给行加属性
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#EEF3F9';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';");

            }
        }
    }
}
