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

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MAdCode : 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 (!IsPostBack)
            {
                if (Plugin.Common.Rq("ac") == "Add")
                    editBox(0);
                else
                    GetList();//列表 
            }
        }
        /// <summary>
        /// 添加/修改
        /// </summary>
        /// <param name="id"></param>
        private void editBox(int id)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfAdId.Value = id.ToString();
            if (id > 0)
            {
                IList<Stat.Model.AD> list = Stat.BLL.AD.Get(id);
                if (list.Count > 0)
                {
                    this.txtName.Text = list[0].Title;
                    this.txtCode.Text = list[0].Code;
                    this.dpdPosition.SelectedIndex = -1;
                    this.dpdPosition.Items.FindByValue(list[0].Posit.ToString()).Selected = true;
                    this.rBtnState.Items.FindByValue(list[0].State.ToString()).Selected = true;
                }
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        private void GetList()
        {
            this.gvWebList.DataSource = Stat.BLL.AD.Get();
            this.gvWebList.DataKeyNames = new string[] { "id" };
            this.gvWebList.DataBind();

        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            Stat.Model.AD model = new Stat.Model.AD();
            model.ID = Convert.ToInt32("0" + this.hdfAdId.Value);
            model.Title = Plugin.Common.SafeRequest(this.txtName.Text);
            model.Code = this.txtCode.Text;
            model.Posit = Convert.ToInt32(this.dpdPosition.SelectedValue);
            model.State = Convert.ToInt32(this.rBtnState.SelectedValue);

            removeCache.RemoveAdCache();

            if (model.ID > 0)
            {
                if (Stat.BLL.AD.Update(model))
                    Plugin.Common.Alert("修改成功！", "?");
                else
                    Plugin.Common.Alert("修改失败！");
            }
            else
            {
                if (Stat.BLL.AD.Insert(model))
                    Plugin.Common.Alert("添加成功！", "?");
                else
                    Plugin.Common.Alert("添加失败！");
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        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='';");
                //给行中控件加属性
                ((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm('确认要删除吗？');");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        protected void gvWebList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            removeCache.RemoveAdCache();
            int keyid = int.Parse(this.gvWebList.DataKeys[e.RowIndex][0].ToString());
            if (Stat.BLL.AD.Delete(keyid))
                Plugin.Common.Alert("删除成功！", "?");
            else
                Plugin.Common.Alert("操作失败！");
        }
        /// <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 IsState(object o)
        {
            if (o.ToString() == "0")
            {
                return "<span style=\"color:#FF0000\">禁用</span>";
            }
            else
            {
                return "正常";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        protected string IsPosit(object o)
        {
            if (o.ToString() == "0")
            {
                return "统计底部";
            }
            else if (o.ToString() == "1")
            {
                return "“当前位置”右侧";
            }
            else if (o.ToString() == "2")
            {
                return "站点列表 中间";
            }
            else if (o.ToString() == "3")
            {
                return "统计顶部";
            }
            else if (o.ToString() == "4")
            {
                return "站点列表 (通栏)";
            }
            else
            {
                return "其他";
            }
        }
    }
}
