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

namespace Stat.web.v
{
    /// <summary>
    /// 
    /// </summary>
    public partial class WebsiteStructure : System.Web.UI.Page
    {
        Model.WebsiteStructure stuctureModel = new Stat.Model.WebsiteStructure();
        Model.PageInfo pageModel = new Stat.Model.PageInfo();
        Stat.Cache.IRemoveCache removeCache = Stat.Cache.CacheFatory.CreateRemoveCache();


        /// <summary>
        /// 
        /// </summary>
        protected int webid = Plugin.Common.Rq_Int("wid");
        /// <summary>
        /// 
        /// </summary>
        protected string websiteName;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (webid == 0) { Response.Write("操作错误！请不要手动修改URL参数。"); Response.End(); }
            IList<Stat.Model.WebSites> list =Stat.Cache.CacheFatory.CreateWebSite().Get(webid);
            if (list != null && list.Count > 0){websiteName = list[0].WebName;}
            else { Response.Write("操作错误！请不要手动修改URL参数。"); Response.End(); }
     

            this.Title = websiteName + "的内容分类规则列表";

            if (!IsPostBack)
            {
                GetList();
            }
        }

        private void GetList()
        {
            stuctureModel.Webid = Plugin.Common.Rq_Int("wid");
            pageModel.PageIndex = Plugin.Common.Rq_Int("page");
            pageModel.PageIndex = pageModel.PageIndex < 1 ? 1 : pageModel.PageIndex;

            this.GvList.DataSource = BLL.WebsiteStructure.GetList(ref pageModel, stuctureModel);
            this.GvList.DataKeyNames = new string[] { "id" };
            this.GvList.DataBind();

            string urlParameter = "wid=" + stuctureModel.Webid;
            this.LtrPageIndex.Text = JPageIndex.GetIndex.Get(pageModel.RecordCount, pageModel.PageIndex, pageModel.PageSize, 0, 1, urlParameter);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >=0)
            {
                if (e.Row.Cells[3].Text == "1")
                    e.Row.Cells[3].Text = "<font color=#0000CC>栏目</font>";
                else
                    e.Row.Cells[3].Text = "内容";

                if (e.Row.Cells[4].Text == "1")
                    e.Row.Cells[4].Text = "启用";
                else
                    e.Row.Cells[4].Text = "<font color=#FF0000>禁用</font>";
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Model.WebsiteStructure model=new Stat.Model.WebsiteStructure();
            model.id=int.Parse(this.GvList.DataKeys[e.RowIndex][0].ToString());
            model.Data_Action="Delete";
            if (BLL.WebsiteStructure.WebsiteStructureInsertUpdateDelete(model) == 1)
            {
                removeCache.RemoveWebsiteCache(webid);
                Plugin.Common.Alert("操作成功！", this.Request.Url.ToString());
            }
            else
            {
                Plugin.Common.Alert("操作失败");
                Plugin.Common.HistoryGo(-1);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GvList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string url = "WebsiteRole.aspx?ruleid=" + this.GvList.DataKeys[e.NewEditIndex][0].ToString();
            Response.Redirect(url, true);
        }
    }
}
