using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TopWinCMS.Model;
using TopWinCMS.BLL;
using TopWinCMS.Common;
namespace TopWinCMS.WebUI.Member
{
    public partial class Favorite : TopWinCMS.BLL.PageFactory.Pages.ClientPublicPage
    {
        SQLModel s_model = new SQLModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Page.Title = Config.SiteName + "→ 会员中心 → 网络收藏夹";

                BindList();

            }
        }
        private void BindList()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);

            s_model.WHERE = " MemberID=" + MemberLoginInfo.MemberID;
            s_model.PageSize = PageNavigation1.PageSize;

            new TopWinCMS.BLL.FavoriteManage().BindList(GV_List, PageNavigation1, s_model);

        }

        #region 添加或修改
        private void Add(int ID, string Title, string URL)
        {
            FavoriteInfo model = new FavoriteInfo();
            model.ID = ID;
            model.Title = Title;
            model.URL = URL;
            model.MemberID = int.Parse(MemberLoginInfo.MemberID);

            string strErr = null;

            new FavoriteManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr);
                return;
            }
 
        }
        #endregion

        protected void BT_Add_Click(object sender, EventArgs e)
        {
            Add(0, TB_Title.Text.Trim(), TB_URL.Text.Trim());
            
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell myTableCell = e.Row.Cells[4];
                if (myTableCell.Controls.Count > 0)
                {
                    LinkButton btnDel = (LinkButton)myTableCell.Controls[0];

                    //if 
                    //{
                    btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                    //}
                    //else
                    //{
                    //    btnDel.Attributes.Add("onclick", "alert('您没有删除权限？');return false");
                    //}

                }
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = GV_List.DataKeys[e.RowIndex].Value.ToString();

            new FavoriteManage().Delete(ID, UpdatePanel1);


            MsgBox.Alert("删除成功!", UpdatePanel1);

            BindList();
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = (int)GV_List.DataKeys[e.RowIndex].Value;
            string Title = ((TextBox)GV_List.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
            string URL = ((TextBox)GV_List.Rows[e.RowIndex].Cells[1].Controls[1]).Text.Trim();
            Add(id, Title, URL);

            GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_List.EditIndex = -1;
            BindList();
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_List.EditIndex = e.NewEditIndex;
            BindList();
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            s_model.PageNo = e.NewPageIndex;
            BindList();
        }
 
    }
}
