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

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Navigate : System.Web.UI.Page
    {
        Stat.Cache.IRemoveCache removeCache = Stat.Cache.CacheFatory.CreateRemoveCache();
        /// <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)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfId.Value = id.ToString();
            if (id > 0)
            {
                IList<Stat.Model.Navigate> list = Stat.BLL.Navigate.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.Text = 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()
        {
            this.gvWebList.DataSource = Stat.BLL.Navigate.Get();
            this.gvWebList.DataKeyNames = new string[] { "id" };
            this.gvWebList.DataBind();

        }


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

            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 = "";
            model.content = this.content1.Text;
            model.IsPass = Convert.ToInt32(this.rBtnIsPass.SelectedValue);
            model.IsTop = Convert.ToInt32(this.rBtnIsTop.SelectedValue);


            removeCache.RemoveNavigateCache();

            if (model.ID > 0)
            {
                if (Stat.BLL.Navigate.Update(model))
                    Plugin.Common.Alert("操作成功！","?");
                else
                    Plugin.Common.Alert("操作失败！");
            }
            else
            {
                if (Stat.BLL.Navigate.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.Navigate.Delete(keyid))
                Plugin.Common.Alert("操作成功！", "?");
            else
                Plugin.Common.Alert("操作失败！");

            removeCache.RemoveNavigateCache();
        }
        /// <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 "推荐";
            }
        }

    }
}
