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

namespace Stat.web.Manerger
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MFrendLick : 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")
                editBox(0);
            else
                GetFrLinckList();//列表 
        }
        /// <summary>
        /// 添加/修改
        /// </summary>
        /// <param name="id"></param>
        private void editBox(int id)
        {
            this.pnWebList.Visible = false;
            this.pnEditWeb.Visible = true;
            this.hdfFrLinkId.Value = id.ToString();
            if (id > 0)
            {
                IList<Stat.Model.FriendLink> list = Stat.BLL.FriendLink.Get(id);
                if (list.Count > 0)
                {
                    this.pnBtnUpLoad.Visible = false;
                    this.pnSignImg.Visible = true;
                    this.txtWebName.Text = list[0].LinkName;
                    this.txtWebUrl.Text = list[0].LinkURL;
                    this.txtImgPath.Text = list[0].LinkImgPath;
                    this.txtInfo.Text = list[0].LinkInfo;
                    this.txtSort.Text = list[0].OrderNum.ToString();
                    this.rBtnShowStype.Items.FindByValue(list[0].Style.ToString()).Selected = true;
                    this.rBtnState.Items.FindByValue(list[0].State.ToString()).Selected = true;
                }
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        private void GetFrLinckList()
        {
            this.gvWebList.DataSource = Stat.BLL.FriendLink.GetcType(0); //0 ：友情链接
            this.gvWebList.DataKeyNames = new string[] { "id" };
            this.gvWebList.DataBind();

        }
        /// <summary>
        /// 保存数据
        /// </summary>
        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            Stat.Model.FriendLink model = new Stat.Model.FriendLink();
            model.id = Convert.ToInt32("0" + this.hdfFrLinkId.Value);
            model.LinkName = Plugin.Common.SafeRequest(this.txtWebName.Text);
            model.LinkURL = Plugin.Common.SafeRequest(this.txtWebUrl.Text);
            model.LinkImgPath = Plugin.Common.SafeRequest(this.txtImgPath.Text);
            model.LinkInfo = Plugin.Common.SafeRequest(this.txtInfo.Text);
            model.State = Convert.ToInt32(this.rBtnState.SelectedValue);
            model.Style = Convert.ToInt32(this.rBtnShowStype.SelectedValue);
            model.cType = 0; //0：友情链接
            model.Position = 0; //给个默认值
            model.OrderNum = Plugin.Common.IsNumeric(this.txtSort.Text) ? Convert.ToInt32(this.txtSort.Text) : 0;
            removeCache.RemoveFrendLinkCache();
            if (model.id > 0)
            {
                if (Stat.BLL.FriendLink.Update(model))
                    Plugin.Common.Alert("修改成功！", "?");
                else
                    Plugin.Common.Alert("修改失败！");
            }
            else
            {
                if (Stat.BLL.FriendLink.Insert(model))
                    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 void gvWebList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            removeCache.RemoveFrendLinkCache();
            int keyid = int.Parse(this.gvWebList.DataKeys[e.RowIndex][0].ToString());
            if (Stat.BLL.FriendLink.Delete(keyid))
                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[6].Controls[0])).Attributes.Add("onclick", "return confirm('确认要删除吗？');");
            }
        }
        /// <summary>
        /// 格式化样式
        /// </summary>
        protected string IsState(object o)
        {
            if (o.ToString() == "0")
            {
                return "<font color=#FF0000>未审</font>";
            }
            else
            {
                return "正常";
            }
        }
        /// <summary>
        /// 格式化连接样式
        /// </summary>
        protected string ForStyle(object p, object s)
        {
            if (s == null || s.ToString() == "0")
            {
                return "文字连接";
            }
            else
            {
                return "<img name=\"\" src=\"" + p + "\" width=\"88\" height=\"31\" alt=\"\" />";
            }
        }


        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpFile1_Click(object sender, EventArgs e)
        {
            this.btnUpFileImg.Enabled = false;
            string res =Plugin.UpFile.FileUpAndReturnGetNewFileName(this.Page, this.fupd, "ver");
            this.pnBtnUpLoad.Visible = false;
            if (res != null)
            {
                this.pnSignImg.Visible = true;
                this.txtImgPath.Text = "/Upfile/" + res;
            }
            else
            {
                this.pnSignImg.Visible = false;
                this.pnBtnUpLoad.Visible = true;
            }
            this.btnUpFileImg.Enabled = true;
        }
        /// <summary>
        /// 重新上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rebtn1_Click(object sender, EventArgs e)
        {
            this.pnSignImg.Visible = false;
            this.pnBtnUpLoad.Visible = true;
        }
    }
}
