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

namespace Stat.web.Manerger
{
    
    /// <summary>
    /// 
    /// </summary>
    public partial class MNotify : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        public string js_Ke = "";
        Stat.Cache.IRemoveCache removeCache = Stat.Cache.CacheFatory.CreateRemoveCache();
        Model.Notify noticeModel = new Stat.Model.Notify();
        Model.PageInfo pageModel = new Stat.Model.PageInfo(15);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Plugin.Common.Rq("ac") == "Add")
            {
   
                if (!IsPostBack)
                    editBox(0);
            }
            else
            {
                GetList();//列表 
            }
        }
        /// <summary>
        /// 添加/修改
        /// </summary>
        /// <param name="id"></param>
        private void editBox(int id)
        {
            js_Ke = "<script src=\"JS/Ke_Editor.js\" type=\"text/javascript\"></script>";
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfId.Value = id.ToString();
            if (id > 0)
            {
                IList<Stat.Model.Notify> list = Stat.BLL.Notify.Get(id);
                if (list.Count > 0)
                {
                    this.txtTitle.Text = list[0].title;
                    this.txtSource.Text = list[0].Source;
                    this.txtAutro.Text = list[0].Author;
                    this.content1.Value = list[0].content;
                    this.rBtnIsPass.Items.FindByValue(list[0].IsPass.ToString()).Selected = true;
                    this.rBtnIsTop.Items.FindByValue(list[0].IsTop.ToString()).Selected = true;
                }
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        private void GetList()
        {
            pageModel.PageIndex = Plugin.Common.Rq_Int("page");
            pageModel.PageIndex = pageModel.PageIndex < 1 ? 1 : pageModel.PageIndex;
            this.gvWebList.DataSource = Stat.BLL.Notify.Get(noticeModel, ref pageModel);
            this.gvWebList.DataKeyNames = new string[] { "id" };
            this.gvWebList.DataBind();
            this.ltrPageIndex.Text = JPageIndex.GetIndex.Get(pageModel.RecordCount, pageModel.PageIndex, pageModel.PageSize, "", 1, 0, 1, 8);

        }


        /// <summary>
        /// 保存
        /// </summary>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            Stat.Model.Notify model = new Stat.Model.Notify();

            model.ID = Convert.ToInt32("0" + this.hdfId.Value);
            model.title = Plugin.Common.SafeRequest(this.txtTitle.Text);
            model.Source = Plugin.Common.SafeRequest(this.txtSource.Text);
            model.Author = Plugin.Common.SafeRequest(this.txtAutro.Text);
            model.content = this.content1.Value;
            model.IsPass = Convert.ToInt32(this.rBtnIsPass.SelectedValue);
            model.IsTop = Convert.ToInt32(this.rBtnIsTop.SelectedValue);


            removeCache.RemoveNotifyCache();

            if (model.ID > 0)
            {
                if (Stat.BLL.Notify.Update(model))
                    Plugin.Common.Alert("修改成功！", "?");
                else
                    Plugin.Common.Alert("修改失败！");
            }
            else
            {
                if (Stat.BLL.Notify.Insert(model))
                    Plugin.Common.Alert("添加成功！", "?");
                else
                    Plugin.Common.Alert("添加失败！");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWebList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                //给行加属性
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#66FF66';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWebList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int keyid = int.Parse(this.gvWebList.DataKeys[e.RowIndex][0].ToString());
            if (Stat.BLL.Notify.Delete(keyid))
                Plugin.Common.Alert("删除成功！", "?");
            else
                Plugin.Common.Alert("操作失败！");

            removeCache.RemoveNotifyCache();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvWebList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            int keyid = int.Parse(this.gvWebList.DataKeys[e.NewEditIndex][0].ToString());
            editBox(keyid);
        }

        /// <summary>
        /// 格式化样式
        /// </summary>
        protected string IsPass(object o)
        {
            if (o.ToString() == "0")
            {
                return "<span style=\"color:#FF0000\">草稿中</span>";
            }
            else
            {
                return "已发布";
            }
        }
        /// <summary>
        /// 格式化样式
        /// </summary>
        protected string IsTop(object o)
        {
            if (o.ToString() == "0")
            {
                return "-";
            }
            else
            {
                return "推荐";
            }
        }
    }
}
