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

namespace Stat.web.Manerger
{

    /// <summary>
    /// 
    /// </summary>
    public partial class MMessage : System.Web.UI.Page
    {
        Stat.Cache.IRemoveCache removeCache = Stat.Cache.CacheFatory.CreateRemoveCache();
        Model.Message messageModel = new Stat.Model.Message();
        Model.PageInfo pageModel = new Stat.Model.PageInfo();
        /// <summary>
        /// 
        /// </summary>
        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)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfId.Value = id.ToString();
            if (id > 0)
            {
                IList<Stat.Model.Message> list = Stat.BLL.Message.Get(id);
                if (list.Count > 0)
                {
                    this.txtTitle.Text = list[0].title;
                    this.txtAuthor.Text = list[0].Author;
                    this.txtSource.Text = list[0].Source;
                    this.contentMsg.Text = list[0].content;
                    this.rBtnIsPass.Items.FindByValue(list[0].IsPass.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 = BLL.Message.Get(messageModel, 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.Message model = new Stat.Model.Message();

            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 = this.txtAuthor.Text.Trim();
            model.content = this.contentMsg.Text;
            model.IsPass = Convert.ToInt32(this.rBtnIsPass.SelectedValue);
            model.IsTop = 0;

            if (model.ID > 0)
            {
                if (Stat.BLL.Message.Update(model))
                    Plugin.Common.Alert("修改成功！", "?");
                else
                    Plugin.Common.Alert("修改失败！");
            }
            removeCache.RemoveMessageCache();
        }
        /// <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.Message.Delete(keyid))
                Plugin.Common.Alert("删除成功！", "?");
            else
                Plugin.Common.Alert("操作失败！");
            /*----------------------*/
            removeCache.RemoveMessageCache();
        }
        /// <summary>
        /// 
        /// </summary>
        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 "推荐";
            }
        }
    }
}
